Skip to content

Commit d863f80

Browse files
authored
Remove MatchableRawRepresentable (#12)
This PR removes `MatchableRawRepresentable` in favor of `Matchable`
1 parent dcf9990 commit d863f80

File tree

8 files changed

+42
-24
lines changed

8 files changed

+42
-24
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ repos:
66
- id: trailing-whitespace
77
exclude: ".*\\.(html|plist)$"
88
- id: end-of-file-fixer
9-
exclude: ".*\\.(xcstrings|html|plist)$"
9+
exclude: ".*\\.(xcstrings|html|plist|json)$"
1010
- repo: local
1111
hooks:
1212
- id: swiftformat

.vscode/launch.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"configurations": [
3+
{
4+
"type": "swift",
5+
"request": "launch",
6+
"args": [],
7+
"cwd": "${workspaceFolder:BinaryParseKit}",
8+
"name": "Debug BinaryParseKitClient",
9+
"target": "BinaryParseKitClient",
10+
"configuration": "debug",
11+
"preLaunchTask": "swift: Build Debug BinaryParseKitClient"
12+
},
13+
{
14+
"type": "swift",
15+
"request": "launch",
16+
"args": [],
17+
"cwd": "${workspaceFolder:BinaryParseKit}",
18+
"name": "Release BinaryParseKitClient",
19+
"target": "BinaryParseKitClient",
20+
"configuration": "release",
21+
"preLaunchTask": "swift: Build Release BinaryParseKitClient"
22+
}
23+
]
24+
}

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,11 @@ let cmd = try Command(parsing: Data([0x01, 0x12, 0x34]))
231231

232232
**Raw Representable Enums**
233233

234-
For enums with raw values, conform to `MatchableRawRepresentable` (or `Matchable`):
234+
For enums with raw values, conform to `Matchable`:
235235

236236
```swift
237237
@ParseEnum
238-
enum StatusCode: UInt8, MatchableRawRepresentable {
238+
enum StatusCode: UInt8, Matchable {
239239
@match
240240
case success = 0x00
241241

@@ -284,7 +284,7 @@ BinaryParseKit defines four parsing protocols:
284284
Most built-in types already conform to these protocols. For custom types, implement the appropriate protocol(s).
285285

286286
In addition, as mentioned in the previous enum parsing section,
287-
`Matchable` and `MatchableRawRepresentable` is introduced to allow each case to provide bytes for matching in the process.
287+
`Matchable` is introduced to allow each case to provide bytes for matching in the process.
288288

289289
## Contributing
290290

Sources/BinaryParseKit/BinaryParseKit.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -309,16 +309,15 @@ public macro ParseEnum() = #externalMacro(
309309

310310
/// Defines a match case using the enum's raw value without consuming bytes from the buffer.
311311
///
312-
/// Use this macro for `MatchableRawRepresentable` enums where each case's raw value
312+
/// Use this macro for ``Matchable`` enums where each case's raw value
313313
/// serves as the match pattern. The matched bytes are NOT consumed from the buffer.
314314
///
315-
/// - Note: This declaration can only be used when the enum conforms to ``Matchable`` (or ``MatchableRawRepresentable``)
316-
/// protocol.
315+
/// - Note: This declaration can only be used when the enum conforms to ``Matchable`` protocol.
317316
///
318317
/// Example:
319318
/// ```swift
320319
/// @ParseEnum
321-
/// enum StatusCode: UInt8, MatchableRawRepresentable {
320+
/// enum StatusCode: UInt8, Matchable {
322321
/// @match
323322
/// case success = 0x00 // Matches byte 0x00 without advancing the pointer
324323
///
@@ -391,17 +390,16 @@ public macro match(bytes: [UInt8]) = #externalMacro(
391390

392391
/// Matches and consumes bytes from the buffer using the enum's raw value.
393392
///
394-
/// Use this macro for `MatchableRawRepresentable` enums where each case's raw value
393+
/// Use this macro for ``Matchable`` enums where each case's raw value
395394
/// serves as the match pattern. The matched bytes ARE consumed from the buffer,
396395
/// making this suitable for cases with associated values that need to parse subsequent data.
397396
///
398-
/// - Note: This declaration can only be used when the enum conforms to ``Matchable`` (or ``MatchableRawRepresentable``)
399-
/// protocol.
397+
/// - Note: This declaration can only be used when the enum conforms to ``Matchable`` protocol.
400398
///
401399
/// Example:
402400
/// ```swift
403401
/// @ParseEnum
404-
/// enum Command: UInt8, MatchableRawRepresentable {
402+
/// enum Command: UInt8, Matchable {
405403
/// @matchAndTake
406404
/// @parse(endianness: .big)
407405
/// case setValue(UInt16) = 0x01 // Matches and consumes 0x01, then parses UInt16

Sources/BinaryParseKit/BuiltInExtensions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extension Int64: EndianSizedParsable, EndianParsable {}
2222

2323
extension Data: SizedParsable {}
2424

25-
public extension MatchableRawRepresentable where Self.RawValue == UInt8 {
25+
public extension Matchable where Self: RawRepresentable, Self.RawValue == UInt8 {
2626
func bytesToMatch() -> [UInt8] {
2727
[rawValue]
2828
}

Sources/BinaryParseKit/Documentation.docc/BinaryParseKit.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ BinaryParseKit provides a convenient and type-safe way to parse binary data in S
5151
### Matchable Protocols
5252

5353
- ``Matchable``
54-
- ``MatchableRawRepresentable``
5554

5655
### Printable
5756

Sources/BinaryParseKit/MatchableProtocols.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@ public protocol Matchable {
1010
func bytesToMatch() -> [UInt8]
1111
}
1212

13-
/// A protocol for types that conform to `RawRepresentable` and `Matchable`.
14-
/// It provides a default implementation for `RawRepresentable` whose `RawValue` conforms to `Matchable`.
15-
public protocol MatchableRawRepresentable: RawRepresentable, Matchable {}
16-
17-
/// Default implementation of `bytesToMatch()` for `MatchableRawRepresentable` where `RawValue` conforms to `Matchable`.
18-
public extension MatchableRawRepresentable where Self.RawValue: Matchable {
13+
/// Default implementation of `bytesToMatch()` for ``Matchable`` where it's also a `RawRepresentable` and its `RawValue`
14+
/// conforms to `Matchable`.
15+
public extension Matchable where Self: RawRepresentable, Self.RawValue: Matchable {
1916
/// Returns `rawValue`'s `bytesToMatch()`.
2017
func bytesToMatch() -> [UInt8] {
2118
rawValue.bytesToMatch()

Tests/BinaryParseKitTests/TestEnumParsing.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ struct EnumParsingTest {
5454
// MARK: - Raw Representable Enum Tests
5555

5656
@ParseEnum
57-
enum RawRepresentableUInt8: UInt8, MatchableRawRepresentable {
57+
enum RawRepresentableUInt8: UInt8, Matchable {
5858
@match
5959
case first = 0x10
6060

@@ -85,15 +85,15 @@ struct EnumParsingTest {
8585
}
8686

8787
@ParseEnum
88-
enum RawRepresentableUInt16: UInt16, MatchableRawRepresentable {
88+
enum RawRepresentableUInt16: UInt16, Matchable {
8989
@match
9090
case value1 = 0x0102
9191
@match
9292
case value2 = 0x0304
9393
}
9494

9595
@Test
96-
func `raw representable MatchableRawRepresentable enum`() throws {
96+
func `raw representable Matchable enum`() throws {
9797
let value1 = try RawRepresentableUInt16(parsing: Data([0x01, 0x02]))
9898
#expect(value1 == .value1)
9999

@@ -448,7 +448,7 @@ struct EnumParsingTest {
448448
}
449449
}
450450

451-
extension MatchableRawRepresentable where Self.RawValue == UInt16 {
451+
extension Matchable where Self: RawRepresentable, Self.RawValue == UInt16 {
452452
func bytesToMatch() -> [UInt8] {
453453
[
454454
UInt8((rawValue & 0xFF00) >> 8),

0 commit comments

Comments
 (0)