You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR allows macros to generate `Printable` conformance for `struct`
and `enum`. Any `Printable` can be printed by a `Printer`.
For instance
```swift
@ParseStruct
struct Header {
...
}
let header = Header(parsing: [...])
let bytes: [UInt8] = header.printParsed(printer: .byteArray)
let hexString: String = header.printParsed(printer: .hexString())
let data: Data = header.printParsed(printer: .data)
```
Copy file name to clipboardExpand all lines: README.md
+8-5Lines changed: 8 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,6 +15,7 @@ A declarative Swift package for parsing binary data using macros, built on top o
15
15
-**Custom byte counts**: Parse types with specific byte lengths
16
16
-**Remaining data parsing**: Parse all remaining bytes in a buffer
17
17
-**Enum parsing**: Parse enums with pattern matching and associated values
18
+
-**Parser is printer**: Serialize parsed structures back into binary data
18
19
19
20
## Requirements
20
21
@@ -303,12 +304,14 @@ If you encounter any issues, have feature requests, or want to suggest improveme
303
304
304
305
### Future Directions
305
306
306
-
Some areas we're considering for future development:
307
+
Roadmap:
307
308
308
-
-**More convenient APIs** - Shorter syntax and better autocomplete support, such as merging `ParseStruct` and `ParseEnum`
309
-
-**Advanced validation** - Runtime validation of parsing constraints, such as require minimal byte size checking in front instead of at each parsing
310
-
-**Performance optimizations** - Further optimization of generated parsing code, such as linear time enum matching for constant bytes provided (`O( max(n, m) )` instead of `O(n * m)`, where `n` is the number of cases and `m` is the max number of bytes provided for each case)
311
-
-**Porting to prio iOS/macOS 26** - because `Span` is introduced only in iOS/macOS 26, port of using `withUnsafePointer` can be provided to prior versions of OSes for better compatibility
309
+
-[x] Parsers as printer
310
+
-[ ] Porting to prior iOS 18/macOS 15: Because `Span` is introduced only in iOS 18/macOS 15, port of using `withUnsafePointer` can be provided to prior versions of OSes for better compatibility
311
+
-[ ] Bitmask support
312
+
-[ ] Advanced validation: Runtime validation of parsing constraints, such as require minimal byte size checking in front instead of at each parsing
313
+
-[ ] Performance optimizations: Further optimization of generated parsing code, such as linear time enum matching for constant bytes provided (`O( max(n, m) )` instead of `O(n * m)`, where `n` is the number of cases and `m` is the max number of bytes provided for each case)
314
+
-[ ] API improvements: Shorter syntax and better autocomplete support, such as merging `ParseStruct` and `ParseEnum`
312
315
313
316
Your feedback on these directions and other ideas is highly appreciated!
This articles describes how to implement a custom printer by conforming to the `Printer` protocol.
4
+
5
+
## Printer Protocol
6
+
7
+
The ``Printer`` protocols requires implementing a single method ``Printer/print(_:)-(PrinterIntel)`` that takes in a ``PrinterIntel`` and outputs ``Printer/PrinterOutput``.
8
+
9
+
``PrinterIntel`` provides context information about the parsed object and is in a recursive structure. There are the following cases:
10
+
11
+
-``PrinterIntel/builtIn(_:)`` with ``PrinterIntel/BuiltInPrinterIntel``: for built-in types like `UInt8`, `Int32`, or user defined types that are not marked with `@ParseStruct` or `@ParseEnum`.
12
+
-``PrinterIntel/skip(_:)`` with ``PrinterIntel/SkipPrinterIntel``: for skipped bytes.
13
+
-``PrinterIntel/struct(_:)`` with ``PrinterIntel/StructPrinterIntel``: for types marked with `@ParseStruct`.
14
+
-``PrinterIntel/enum(_:)`` with ``PrinterIntel/EnumCasePrinterIntel``: for types marked with `@ParseEnum`.
0 commit comments