Skip to content

Commit aeffe94

Browse files
authored
Refactor file locations (#23)
This PR organizes source files in a more clear manner.
1 parent 24ea1d5 commit aeffe94

14 files changed

+148
-186
lines changed

Sources/BinaryParseKit/BuiltInExtensions.swift

Lines changed: 0 additions & 29 deletions
This file was deleted.

Sources/BinaryParseKit/CustomExtensions.swift

Lines changed: 0 additions & 143 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//
2+
// Data+SizedParsable.swift
3+
// BinaryParseKit
4+
//
5+
// Created by Larry Zeng on 11/28/25.
6+
//
7+
import Foundation
8+
9+
extension Data: SizedParsable {}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// FloatingPointExtensions.swift
3+
// BinaryParseKit
4+
//
5+
// Created by Larry Zeng on 7/16/25.
6+
//
7+
import BinaryParsing
8+
9+
/// A protocol for types that can be initialized from a bit pattern.
10+
///
11+
/// This protocol enables binary floating-point types to be parsed from
12+
/// their underlying bit representation, allowing for precise control
13+
/// over how floating-point values are interpreted from binary data.
14+
public protocol ExpressibleByBitPattern {
15+
/// The underlying integer type that represents the bit pattern.
16+
associatedtype BitPattern: FixedWidthInteger & BitwiseCopyable
17+
18+
/// Initializes a value from its bit pattern representation.
19+
///
20+
/// - Parameter bitPattern: The bit pattern to interpret as this type
21+
init(bitPattern: BitPattern)
22+
23+
var bitPattern: BitPattern { get }
24+
}
25+
26+
extension Float16: ExpressibleByBitPattern {}
27+
extension Float: ExpressibleByBitPattern {}
28+
extension Double: ExpressibleByBitPattern {}
29+
30+
/// Provides endian-aware parsing for binary floating-point types.
31+
///
32+
/// This extension enables floating-point types (Float, Double, Float16) to be parsed
33+
/// from binary data with explicit endianness control by converting through their
34+
/// underlying bit pattern representation.
35+
public extension BinaryFloatingPoint where Self: BitwiseCopyable & ExpressibleByBitPattern {
36+
/// Initializes a floating-point value by parsing binary data with the specified endianness.
37+
///
38+
/// - Parameters:
39+
/// - input: A mutable parser span containing the binary data to parse
40+
/// - endianness: The byte order to use when parsing the underlying bit pattern
41+
/// - Throws: `ParsingError` if parsing fails
42+
init(parsing input: inout BinaryParsing.ParserSpan, endianness: BinaryParsing.Endianness) throws(ParsingError) {
43+
let byteCount = MemoryLayout<Self>.size
44+
let bitPattern = try BitPattern(parsing: &input, endianness: endianness, byteCount: byteCount)
45+
self = Self(bitPattern: bitPattern)
46+
}
47+
}
48+
49+
extension Float: EndianParsable {}
50+
extension Float16: EndianParsable {}
51+
extension Double: EndianParsable {}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// IntegerExtensions.swift
3+
// BinaryParseKit
4+
//
5+
// Created by Larry Zeng on 7/16/25.
6+
//
7+
import BinaryParsing
8+
9+
extension UInt8: @retroactive ExpressibleByParsing {}
10+
extension UInt8: EndianSizedParsable {}
11+
extension UInt16: EndianSizedParsable, EndianParsable {}
12+
extension UInt32: EndianSizedParsable, EndianParsable {}
13+
extension UInt: EndianSizedParsable {}
14+
extension UInt64: EndianSizedParsable, EndianParsable {}
15+
16+
extension Int8: EndianSizedParsable {}
17+
extension Int16: EndianSizedParsable, EndianParsable {}
18+
extension Int32: EndianSizedParsable, EndianParsable {}
19+
extension Int: EndianSizedParsable {}
20+
extension Int64: EndianSizedParsable, EndianParsable {}
21+
22+
extension UInt8: EndianParsable {
23+
public init(
24+
parsing input: inout BinaryParsing.ParserSpan,
25+
endianness: BinaryParsing.Endianness,
26+
) throws(ParsingError) {
27+
try self.init(parsing: &input, endianness: endianness, byteCount: MemoryLayout<Self>.size)
28+
}
29+
}
30+
31+
extension UInt: EndianParsable {
32+
public init(
33+
parsing input: inout BinaryParsing.ParserSpan,
34+
endianness: BinaryParsing.Endianness,
35+
) throws(ParsingError) {
36+
try self.init(parsing: &input, endianness: endianness, byteCount: MemoryLayout<Self>.size)
37+
}
38+
}
39+
40+
extension Int8: EndianParsable {
41+
public init(
42+
parsing input: inout BinaryParsing.ParserSpan,
43+
endianness: BinaryParsing.Endianness,
44+
) throws(ParsingError) {
45+
try self.init(parsing: &input, endianness: endianness, byteCount: MemoryLayout<Self>.size)
46+
}
47+
}
48+
49+
extension Int: EndianParsable {
50+
public init(
51+
parsing input: inout BinaryParsing.ParserSpan,
52+
endianness: BinaryParsing.Endianness,
53+
) throws(ParsingError) {
54+
try self.init(parsing: &input, endianness: endianness, byteCount: MemoryLayout<Self>.size)
55+
}
56+
}

Sources/BinaryParseKit/MatchableProtocols.swift renamed to Sources/BinaryParseKit/Extensions/Matchable+.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
//
2-
// MatchableProtocols.swift
2+
// Matchable+.swift
33
// BinaryParseKit
44
//
5-
// Created by Larry Zeng on 11/11/25.
5+
// Created by Larry Zeng on 11/28/25.
66
//
77

8-
/// A protocol for types that can provide a sequence of bytes for matching purposes.
9-
public protocol Matchable {
10-
func bytesToMatch() -> [UInt8]
8+
public extension Matchable where Self: RawRepresentable, Self.RawValue == UInt8 {
9+
func bytesToMatch() -> [UInt8] {
10+
[rawValue]
11+
}
1112
}
1213

1314
/// Default implementation of `bytesToMatch()` for ``Matchable`` where it's also a `RawRepresentable` and its `RawValue`
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// PrinterError.swift
3+
// BinaryParseKit
4+
//
5+
// Created by Larry Zeng on 11/28/25.
6+
//
7+
8+
public enum PrinterError: Swift.Error {
9+
/// Indicates that the construction of printer intel failed, with the underlying error provided.
10+
case intelConstructionFailed(underlying: any Error)
11+
/// Indicates that the provided type does not conform to ``Printable``.
12+
case notPrintable(type: Any.Type)
13+
/// Indicates that error is thrown by underling printer during printing process.
14+
case printingError(underlying: any Error)
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//
2+
// Matchable.swift
3+
// BinaryParseKit
4+
//
5+
// Created by Larry Zeng on 11/11/25.
6+
//
7+
8+
/// A protocol for types that can provide a sequence of bytes for matching purposes.
9+
public protocol Matchable {
10+
func bytesToMatch() -> [UInt8]
11+
}

0 commit comments

Comments
 (0)