@@ -22,6 +22,17 @@ struct ByteTreeUserInfoKey: Hashable {
2222 }
2323}
2424
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+
2536/// A type that can be deserialized from ByteTree into a scalar value that
2637/// doesn't have any child nodes
2738protocol ByteTreeScalarDecodable {
@@ -32,7 +43,8 @@ protocol ByteTreeScalarDecodable {
3243 /// - size: The length of the serialized data in bytes
3344 /// - Returns: The deserialized value
3445 static func read( from pointer: UnsafeRawPointer , size: Int ,
35- userInfo: UnsafePointer < [ ByteTreeUserInfoKey : Any ] > ) -> Self
46+ userInfo: UnsafePointer < [ ByteTreeUserInfoKey : Any ] >
47+ ) throws -> Self
3648}
3749
3850/// A type that can be deserialized from ByteTree into an object with child
@@ -50,7 +62,8 @@ protocol ByteTreeObjectDecodable {
5062 /// - Returns: The deserialized object
5163 static func read( from reader: UnsafeMutablePointer < ByteTreeObjectReader > ,
5264 numFields: Int ,
53- userInfo: UnsafePointer < [ ByteTreeUserInfoKey : Any ] > ) -> Self
65+ userInfo: UnsafePointer < [ ByteTreeUserInfoKey : Any ] >
66+ ) throws -> Self
5467}
5568
5669// MARK: - Reader objects
@@ -92,9 +105,9 @@ struct ByteTreeObjectReader {
92105 /// - Returns: The decoded field
93106 mutating func readField< FieldType: ByteTreeScalarDecodable > (
94107 _ objectType: FieldType . Type , index: Int
95- ) -> FieldType {
108+ ) throws -> FieldType {
96109 advanceAndValidateIndex ( index)
97- return reader. pointee. read ( objectType)
110+ return try reader. pointee. read ( objectType)
98111 }
99112
100113 /// Read the field at the given index as the specified type. All indicies must
@@ -107,9 +120,9 @@ struct ByteTreeObjectReader {
107120 /// - Returns: The decoded field
108121 mutating func readField< FieldType: ByteTreeObjectDecodable > (
109122 _ objectType: FieldType . Type , index: Int
110- ) -> FieldType {
123+ ) throws -> FieldType {
111124 advanceAndValidateIndex ( index)
112- return reader. pointee. read ( objectType)
125+ return try reader. pointee. read ( objectType)
113126 }
114127
115128 /// Read and immediately discard the field at the specified index. This
@@ -180,7 +193,7 @@ struct ByteTreeReader {
180193 ) throws -> T {
181194 var reader = ByteTreeReader ( pointer: pointer, userInfo: userInfo)
182195 try reader. readAndValidateProtocolVersion ( protocolVersionValidation)
183- return reader. read ( rootObjectType)
196+ return try reader. read ( rootObjectType)
184197 }
185198
186199 /// Deserialize an object tree from the ByteTree data at the given memory
@@ -275,15 +288,15 @@ struct ByteTreeReader {
275288 /// - Returns: The deserialized object
276289 fileprivate mutating func read< T: ByteTreeObjectDecodable > (
277290 _ objectType: T . Type
278- ) -> T {
291+ ) throws -> T {
279292 let numFields = readObjectLength ( )
280293 var objectReader = ByteTreeObjectReader ( reader: & self ,
281294 numFields: numFields)
282295 defer {
283296 objectReader. finalize ( )
284297 }
285- return T . read ( from: & objectReader, numFields: numFields,
286- userInfo: userInfo)
298+ return try T . read ( from: & objectReader, numFields: numFields,
299+ userInfo: userInfo)
287300 }
288301
289302 /// Read the next field in the tree as a scalar of the specified type.
@@ -292,12 +305,12 @@ struct ByteTreeReader {
292305 /// - Returns: The deserialized scalar
293306 fileprivate mutating func read< T: ByteTreeScalarDecodable > (
294307 _ scalarType: T . Type
295- ) -> T {
308+ ) throws -> T {
296309 let fieldSize = readScalarLength ( )
297310 defer {
298311 pointer = pointer. advanced ( by: fieldSize)
299312 }
300- return T . read ( from: pointer, size: fieldSize, userInfo: userInfo)
313+ return try T . read ( from: pointer, size: fieldSize, userInfo: userInfo)
301314 }
302315
303316 /// Discard the next scalar field, advancing the pointer to the next field
@@ -347,12 +360,12 @@ extension Optional: ByteTreeObjectDecodable
347360 static func read( from reader: UnsafeMutablePointer < ByteTreeObjectReader > ,
348361 numFields: Int ,
349362 userInfo: UnsafePointer < [ ByteTreeUserInfoKey : Any ] >
350- ) -> Optional < Wrapped > {
363+ ) throws -> Optional < Wrapped > {
351364 if numFields == 0 {
352365 return nil
353366 } else {
354- return Wrapped . read ( from: reader, numFields: numFields,
355- userInfo: userInfo)
367+ return try Wrapped . read ( from: reader, numFields: numFields,
368+ userInfo: userInfo)
356369 }
357370 }
358371}
@@ -363,9 +376,9 @@ extension Array: ByteTreeObjectDecodable
363376 static func read( from reader: UnsafeMutablePointer < ByteTreeObjectReader > ,
364377 numFields: Int ,
365378 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)
369382 }
370383 }
371384}
0 commit comments