@@ -22,6 +22,17 @@ struct ByteTreeUserInfoKey: Hashable {
22
22
}
23
23
}
24
24
25
+ public enum ByteTreeDecodingError : Error , CustomStringConvertible {
26
+ case invalidEnumRawValue( type: String , value: Int )
27
+
28
+ public var description : String {
29
+ switch self {
30
+ case . invalidEnumRawValue( let type, let value) :
31
+ return " Invalid raw value \" \( value) \" for \( type) "
32
+ }
33
+ }
34
+ }
35
+
25
36
/// A type that can be deserialized from ByteTree into a scalar value that
26
37
/// doesn't have any child nodes
27
38
protocol ByteTreeScalarDecodable {
@@ -32,7 +43,8 @@ protocol ByteTreeScalarDecodable {
32
43
/// - size: The length of the serialized data in bytes
33
44
/// - Returns: The deserialized value
34
45
static func read( from pointer: UnsafeRawPointer , size: Int ,
35
- userInfo: UnsafePointer < [ ByteTreeUserInfoKey : Any ] > ) -> Self
46
+ userInfo: UnsafePointer < [ ByteTreeUserInfoKey : Any ] >
47
+ ) throws -> Self
36
48
}
37
49
38
50
/// A type that can be deserialized from ByteTree into an object with child
@@ -50,7 +62,8 @@ protocol ByteTreeObjectDecodable {
50
62
/// - Returns: The deserialized object
51
63
static func read( from reader: UnsafeMutablePointer < ByteTreeObjectReader > ,
52
64
numFields: Int ,
53
- userInfo: UnsafePointer < [ ByteTreeUserInfoKey : Any ] > ) -> Self
65
+ userInfo: UnsafePointer < [ ByteTreeUserInfoKey : Any ] >
66
+ ) throws -> Self
54
67
}
55
68
56
69
// MARK: - Reader objects
@@ -92,9 +105,9 @@ struct ByteTreeObjectReader {
92
105
/// - Returns: The decoded field
93
106
mutating func readField< FieldType: ByteTreeScalarDecodable > (
94
107
_ objectType: FieldType . Type , index: Int
95
- ) -> FieldType {
108
+ ) throws -> FieldType {
96
109
advanceAndValidateIndex ( index)
97
- return reader. pointee. read ( objectType)
110
+ return try reader. pointee. read ( objectType)
98
111
}
99
112
100
113
/// Read the field at the given index as the specified type. All indicies must
@@ -107,9 +120,9 @@ struct ByteTreeObjectReader {
107
120
/// - Returns: The decoded field
108
121
mutating func readField< FieldType: ByteTreeObjectDecodable > (
109
122
_ objectType: FieldType . Type , index: Int
110
- ) -> FieldType {
123
+ ) throws -> FieldType {
111
124
advanceAndValidateIndex ( index)
112
- return reader. pointee. read ( objectType)
125
+ return try reader. pointee. read ( objectType)
113
126
}
114
127
115
128
/// Read and immediately discard the field at the specified index. This
@@ -180,7 +193,7 @@ struct ByteTreeReader {
180
193
) throws -> T {
181
194
var reader = ByteTreeReader ( pointer: pointer, userInfo: userInfo)
182
195
try reader. readAndValidateProtocolVersion ( protocolVersionValidation)
183
- return reader. read ( rootObjectType)
196
+ return try reader. read ( rootObjectType)
184
197
}
185
198
186
199
/// Deserialize an object tree from the ByteTree data at the given memory
@@ -275,15 +288,15 @@ struct ByteTreeReader {
275
288
/// - Returns: The deserialized object
276
289
fileprivate mutating func read< T: ByteTreeObjectDecodable > (
277
290
_ objectType: T . Type
278
- ) -> T {
291
+ ) throws -> T {
279
292
let numFields = readObjectLength ( )
280
293
var objectReader = ByteTreeObjectReader ( reader: & self ,
281
294
numFields: numFields)
282
295
defer {
283
296
objectReader. finalize ( )
284
297
}
285
- return T . read ( from: & objectReader, numFields: numFields,
286
- userInfo: userInfo)
298
+ return try T . read ( from: & objectReader, numFields: numFields,
299
+ userInfo: userInfo)
287
300
}
288
301
289
302
/// Read the next field in the tree as a scalar of the specified type.
@@ -292,12 +305,12 @@ struct ByteTreeReader {
292
305
/// - Returns: The deserialized scalar
293
306
fileprivate mutating func read< T: ByteTreeScalarDecodable > (
294
307
_ scalarType: T . Type
295
- ) -> T {
308
+ ) throws -> T {
296
309
let fieldSize = readScalarLength ( )
297
310
defer {
298
311
pointer = pointer. advanced ( by: fieldSize)
299
312
}
300
- return T . read ( from: pointer, size: fieldSize, userInfo: userInfo)
313
+ return try T . read ( from: pointer, size: fieldSize, userInfo: userInfo)
301
314
}
302
315
303
316
/// Discard the next scalar field, advancing the pointer to the next field
@@ -347,12 +360,12 @@ extension Optional: ByteTreeObjectDecodable
347
360
static func read( from reader: UnsafeMutablePointer < ByteTreeObjectReader > ,
348
361
numFields: Int ,
349
362
userInfo: UnsafePointer < [ ByteTreeUserInfoKey : Any ] >
350
- ) -> Optional < Wrapped > {
363
+ ) throws -> Optional < Wrapped > {
351
364
if numFields == 0 {
352
365
return nil
353
366
} else {
354
- return Wrapped . read ( from: reader, numFields: numFields,
355
- userInfo: userInfo)
367
+ return try Wrapped . read ( from: reader, numFields: numFields,
368
+ userInfo: userInfo)
356
369
}
357
370
}
358
371
}
@@ -363,9 +376,9 @@ extension Array: ByteTreeObjectDecodable
363
376
static func read( from reader: UnsafeMutablePointer < ByteTreeObjectReader > ,
364
377
numFields: Int ,
365
378
userInfo: UnsafePointer < [ ByteTreeUserInfoKey : Any ] >
366
- ) -> Array < Element > {
367
- return ( 0 ..< numFields) . map {
368
- return reader. pointee. readField ( Element . self, index: $0)
379
+ ) throws -> Array < Element > {
380
+ return try ( 0 ..< numFields) . map {
381
+ return try reader. pointee. readField ( Element . self, index: $0)
369
382
}
370
383
}
371
384
}
0 commit comments