Skip to content

Commit 5758c7e

Browse files
committed
Working on CodingKey
1 parent a894a26 commit 5758c7e

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

Sources/CodingKey.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ public extension TLVCodingKey where Self: CaseIterable, Self: RawRepresentable,
5555

5656
internal extension TLVTypeCode {
5757

58-
init? <K: CodingKey> (codingKey: K) {
58+
enum CodingKeyError: Error {
59+
60+
case missingIntegerValue
61+
case invalidIntegerValue(Int)
62+
}
63+
64+
init<K: CodingKey> (codingKey: K) throws {
5965

6066
if let tlvCodingKey = codingKey as? TLVCodingKey {
6167

@@ -65,17 +71,18 @@ internal extension TLVTypeCode {
6571

6672
guard intValue <= Int(UInt8.max),
6773
intValue >= Int(UInt8.min)
68-
else { return nil }
74+
else { throw CodingKeyError.invalidIntegerValue(intValue) }
6975

7076
self.init(rawValue: UInt8(intValue))
7177

72-
} else if MemoryLayout<K>.size == MemoryLayout<UInt8>.size {
78+
} else if MemoryLayout<K>.size == MemoryLayout<UInt8>.size,
79+
Mirror(reflecting: codingKey).displayStyle == .enum {
7380

7481
self.init(rawValue: unsafeBitCast(codingKey, to: UInt8.self))
7582

7683
} else {
7784

78-
return nil
85+
throw CodingKeyError.missingIntegerValue
7986
}
8087
}
8188
}

0 commit comments

Comments
 (0)