Skip to content

Commit

Permalink
#161 Add DataConvertible conformance to BluetoothUUID
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Nov 7, 2024
1 parent 74230b2 commit 107fc89
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions Sources/Bluetooth/BluetoothUUID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ extension BluetoothUUID: RawRepresentable {

// MARK: - Data

public extension BluetoothUUID {
extension BluetoothUUID: DataConvertible {

init?<Data: DataContainer>(data: Data) {
public init?<Data: DataContainer>(data: Data) {

guard let length = Length(rawValue: data.count)
else { return nil }
Expand All @@ -120,6 +120,41 @@ public extension BluetoothUUID {
self = .bit128(value)
}
}

public var dataLength: Int {
length.rawValue
}

public func append<Data>(to data: inout Data) where Data : DataContainer {
switch self {
case let .bit16(value):
data += value
case let .bit32(value):
data += value
case let .bit128(value):
data += value
}
}
}

internal extension BluetoothUUID {

/// Number of bytes to represent Bluetooth UUID.
enum Length: Int {

case bit16 = 2
case bit32 = 4
case bit128 = 16
}

private var length: Length {

switch self {
case .bit16: return .bit16
case .bit32: return .bit32
case .bit128: return .bit128
}
}
}

// MARK: - Codable
Expand Down

0 comments on commit 107fc89

Please sign in to comment.