Skip to content

Commit

Permalink
Add LosslessStringConvertible conformance to BluetoothAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Nov 24, 2024
1 parent dfca731 commit 1a384d8
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Sources/Bluetooth/Address.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ extension BluetoothAddress: RawRepresentable {
}

/// Initialize a Bluetooth Address from its big endian string representation (e.g. `00:1A:7D:DA:71:13`).
internal init?<S: StringProtocol>(_ rawValue: S) {
internal static func parse<S: StringProtocol>(_ rawValue: S) -> Self? {

// verify string length
let characters = rawValue.utf8
Expand Down Expand Up @@ -116,7 +116,7 @@ extension BluetoothAddress: RawRepresentable {
}
}

self.init(bigEndian: BluetoothAddress(bytes: bytes))
return BluetoothAddress(bigEndian: BluetoothAddress(bytes: bytes))
}

/// Convert a Bluetooth Address to its big endian string representation (e.g. `00:1A:7D:DA:71:13`).
Expand All @@ -138,6 +138,18 @@ extension BluetoothAddress: CustomStringConvertible {
public var description: String { rawValue }
}

// MARK: - LosslessStringConvertible

extension BluetoothAddress: LosslessStringConvertible {

public init?(_ string: String) {
guard let value = BluetoothAddress.parse(string) else {
return nil
}
self = value
}
}

// MARK: - Data

extension BluetoothAddress: DataConvertible {
Expand Down

0 comments on commit 1a384d8

Please sign in to comment.