Skip to content

Commit d59e2fa

Browse files
committed
Update README
1 parent eccae30 commit d59e2fa

1 file changed

Lines changed: 89 additions & 7 deletions

File tree

‎README.md‎

Lines changed: 89 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,96 @@
11
# TLVCoding
22

3-
[![Build Status](https://travis-ci.org/PureSwift/TLVCoding.svg?branch=master)](https://travis-ci.org/PureSwift/TLVCoding)
3+
[![Swift](https://github.com/PureSwift/TLVCoding/actions/workflows/swift.yml/badge.svg)](https://github.com/PureSwift/TLVCoding/actions/workflows/swift.yml)
44

5-
TLV8 (Type-Length-Value) Coder library
5+
TLV8 (Type-Length-Value) Coder library, compatible with [Embedded Swift](https://github.com/swiftlang/swift/tree/main/docs/EmbeddedSwift).
66

7-
# See Also
7+
Types are encoded and decoded explicitly via `TLVCodable` — either with conformances generated by the `@TLVCodable` macro, or written by hand. The library does not depend on `Codable` runtime machinery or Foundation, so it can be used on bare-metal targets.
88

9+
## Usage
10+
11+
### Macro-generated conformance
12+
13+
```swift
14+
import TLVCoding
15+
16+
@TLVCodable
17+
struct ProvisioningState: Equatable {
18+
19+
var state: State
20+
21+
var result: Result
22+
23+
enum CodingKeys: UInt8, TLVCodingKey {
24+
case state = 0x01
25+
case result = 0x02
26+
}
27+
}
28+
29+
let value = ProvisioningState(state: .provisioning, result: .success)
30+
let data = value.tlvData // Data([0x01, 0x01, 0x01, 0x02, 0x01, 0x01])
31+
let decoded = ProvisioningState(tlvData: data)
32+
```
33+
34+
If no `CodingKeys` enum is declared, one is generated with sequential type codes in property declaration order.
35+
36+
### Hand-written conformance
37+
38+
The same conformance can be written manually (e.g. for Embedded Swift or custom binary layouts):
39+
40+
```swift
41+
extension ProvisioningState: TLVCodable {
42+
43+
init?(tlvData: Data) {
44+
guard let container = TLVContainer(data: tlvData),
45+
let state = container.decode(State.self, forKey: CodingKeys.state),
46+
let result = container.decode(Result.self, forKey: CodingKeys.result)
47+
else { return nil }
48+
self.state = state
49+
self.result = result
50+
}
51+
52+
var tlvData: Data {
53+
var container = TLVContainer()
54+
container.encode(state, forKey: CodingKeys.state)
55+
container.encode(result, forKey: CodingKeys.result)
56+
return container.data
57+
}
58+
}
59+
```
60+
61+
Types with a custom binary layout can skip `TLVContainer` entirely and produce their payload directly:
62+
63+
```swift
64+
extension Version: TLVCodable {
65+
66+
init?(tlvData: Data) {
67+
guard tlvData.count == 3 else { return nil }
68+
self.major = tlvData[0]
69+
self.minor = tlvData[1]
70+
self.patch = tlvData[2]
71+
}
72+
73+
var tlvData: Data {
74+
Data([major, minor, patch])
75+
}
76+
}
77+
```
78+
79+
### Embedded Swift
80+
81+
Macros are disabled under Embedded Swift (write conformances by hand) and the swift-syntax dependency can be skipped entirely with the `SWIFTPM_ENABLE_MACROS` environment variable:
82+
83+
```bash
84+
SWIFTPM_ENABLE_MACROS=0 swift build --target TLVCoding \
85+
--triple armv7em-none-none-eabi \
86+
-Xswiftc -enable-experimental-feature -Xswiftc Embedded \
87+
-Xswiftc -wmo
88+
```
89+
90+
On platforms without Foundation, a minimal `Data` type is provided by the library.
91+
92+
## See Also
93+
94+
- [CoreModel](https://github.com/PureSwift/CoreModel) - Swift ORM with the same explicit coding and macro design
995
- [BluetoothLinux](https://github.com/PureSwift/BluetoothLinux) - Pure Swift Linux Bluetooth Stack
1096
- [GATT](https://github.com/PureSwift/GATT) - Bluetooth Generic Attribute Profile (GATT) for Swift
11-
- [SwiftFoundation](https://github.com/PureSwift/SwiftFoundation) - Cross-Platform, Protocol-Oriented Programming base library to complement the Swift Standard Library.
12-
- [Cacao](https://github.com/PureSwift/Cacao) - Pure Swift Cross-platform UIKit
13-
- [Silica](https://github.com/PureSwift/Silica) - Pure Swift CoreGraphics (Quartz2D) implementation
14-
- [Predicate](https://github.com/PureSwift/Predicate) - Pure Swift Predicate implementation

0 commit comments

Comments
 (0)