From 0610aa66a49fea212bff4c21fffb110f2e4471ba Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Sun, 10 Nov 2024 20:15:34 -0500 Subject: [PATCH] #161 Add `DataConvertible` conformance to `GATTNumberOfDigitals` --- .../BluetoothGATT/GATTNumberOfDigitals.swift | 45 +++++++++---------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/Sources/BluetoothGATT/GATTNumberOfDigitals.swift b/Sources/BluetoothGATT/GATTNumberOfDigitals.swift index a5430522a..15b1aa3b5 100644 --- a/Sources/BluetoothGATT/GATTNumberOfDigitals.swift +++ b/Sources/BluetoothGATT/GATTNumberOfDigitals.swift @@ -6,59 +6,56 @@ // Copyright © 2018 PureSwift. All rights reserved. // -import Foundation +import Bluetooth -// MARK: - Number of Digitals /// GATT Number of Digitals Descriptor /// /// The Characteristic Number of Digitals descriptor is used for defining the number of digitals in a characteristic. @frozen -public struct GATTNumberOfDigitals: GATTDescriptor, RawRepresentable, Equatable, Hashable { - - public static let uuid: BluetoothUUID = .numberOfDigitals - - public static let length = 1 +public struct GATTNumberOfDigitals: GATTDescriptor, RawRepresentable, Equatable, Hashable, Sendable { + public static var uuid: BluetoothUUID { .numberOfDigitals } + public var rawValue: UInt8 public init(rawValue: UInt8) { - self.rawValue = rawValue } +} + +// MARK: - DataConvertible + +extension GATTNumberOfDigitals: DataConvertible { - public init?(data: Data) { - - guard data.count == type(of: self).length + public static var length: Int { 1 } + + public init?(data: Data) { + guard data.count == Self.length else { return nil } - - rawValue = data[0] + self.init(rawValue: data[0]) } - public var data: Data { - - return Data([rawValue]) + public func append(to data: inout Data) where Data : DataContainer { + data += rawValue } - public var descriptor: GATTAttribute.Descriptor { - - return GATTAttribute.Descriptor(uuid: type(of: self).uuid, - value: data, - permissions: [.read]) - } + public var dataLength: Int { Self.length } } +// MARK: - CustomStringConvertible + extension GATTNumberOfDigitals: CustomStringConvertible { public var description: String { - return rawValue.description } } +// MARK: - ExpressibleByIntegerLiteral + extension GATTNumberOfDigitals: ExpressibleByIntegerLiteral { public init(integerLiteral value: UInt8) { - self.init(rawValue: value) } }