Swiftpack.co - spacenation/swift-binary-decoder as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by spacenation.
spacenation/swift-binary-decoder 2.0.0-beta.21
:rocket: Binary Decoder
⭐️ 2
🕓 2 days ago
.package(url: "https://github.com/spacenation/swift-binary-decoder.git", from: "2.0.0-beta.21")

ci

BinaryCoder

SPM package to decode and encode types from binary data.

Binary Decoder

try? BinaryDecoder.decode(Message<DeliveryFlags>.self, from: [0b0000_1111, 0b1111_1111, 0b0000_0011])

Binary Encoder

try? BinaryEncoder.encode(Message(source: 15, data: Data([0b1111_1111]), flags: DeliveryFlags(isDelivered: true, isRead: true)))

Example

private struct Message<Flags: Equatable & BinaryCodable>: Equatable, BinaryCodable {
    let source: UInt8
    let data: Data
    let flags: Flags
    
    init(source: UInt8, data: Data, flags: Flags) {
        self.source = source
        self.data = data
        self.flags = flags
    }
    
    init(from decoder: BinaryDecoder) throws {
        source = try decoder.decode()
        data = try decoder.decode(size: .byte)
        try decoder.skip(size: 6)
        flags = try decoder.decode()
    }
    
    func encode(to encoder: BinaryEncoder) throws {
        encoder.encode(source)
        encoder.encode(data, size: .byte)
        encoder.encodeEmpty(size: 6)
        try encoder.encode(flags)
    }
}

Code Contributions

Feel free to contribute via fork/pull request to master branch. If you want to request a feature or report a bug please start a new issue.

Coffee Contributions

If you find this project useful please consider becoming my GitHub sponsor.

GitHub

link
Stars: 2
Last commit: 2 days ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

SequenceA for NonEmptyList
2 days ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics