@@ -30,10 +30,10 @@ extension ShadowDecoder {
3030 self . _focus = . file
3131 case 1 :
3232 let key = decoder. codingPath [ 0 ]
33- let r = try key. intValue ?! DecodingError . _invalidKey ( forRow : key, codingPath: decoder. codingPath)
33+ let r = try key. intValue ?> CSVDecoder . Error . _invalidRowKey ( forKey : key, codingPath: decoder. codingPath)
3434 self . _focus = . row( r)
3535 default :
36- throw DecodingError . _invalidContainerRequest ( codingPath: decoder. codingPath)
36+ throw CSVDecoder . Error . _invalidContainerRequest ( forKey : decoder . codingPath . last! , codingPath: decoder. codingPath)
3737 }
3838 self . _decoder = decoder
3939 }
@@ -78,32 +78,32 @@ extension ShadowDecoder.KeyedContainer {
7878 func nestedContainer< NestedKey> ( keyedBy type: NestedKey . Type , forKey key: Key ) throws -> KeyedDecodingContainer < NestedKey > where NestedKey : CodingKey {
7979 switch self . _focus {
8080 case . file:
81- guard let rowIndex = key. intValue else { throw DecodingError . _invalidKey ( forRow : key, codingPath: self . codingPath + [ key] ) }
81+ guard let rowIndex = key. intValue else { throw CSVDecoder . Error . _invalidRowKey ( forKey : key, codingPath: self . codingPath + [ key] ) }
8282 var codingPath = self . _decoder. codingPath; codingPath. append ( IndexKey ( rowIndex) )
8383 let decoder = ShadowDecoder ( source: self . _decoder. source, codingPath: codingPath)
8484 return KeyedDecodingContainer ( ShadowDecoder . KeyedContainer< NestedKey> ( unsafeDecoder: decoder, rowIndex: rowIndex) )
85- case . row: throw DecodingError . _invalidContainerRequest ( codingPath: self . codingPath)
85+ case . row: throw CSVDecoder . Error . _invalidContainerRequest ( forKey : key , codingPath: self . codingPath)
8686 }
8787 }
8888
8989 func nestedUnkeyedContainer( forKey key: Key ) throws -> UnkeyedDecodingContainer {
9090 switch self . _focus {
9191 case . file:
92- guard let rowIndex = key. intValue else { throw DecodingError . _invalidKey ( forRow : key, codingPath: self . codingPath + [ key] ) }
92+ guard let rowIndex = key. intValue else { throw CSVDecoder . Error . _invalidRowKey ( forKey : key, codingPath: self . codingPath + [ key] ) }
9393 var codingPath = self . _decoder. codingPath; codingPath. append ( IndexKey ( rowIndex) )
9494 let decoder = ShadowDecoder ( source: self . _decoder. source, codingPath: codingPath)
9595 return ShadowDecoder . UnkeyedContainer ( unsafeDecoder: decoder, rowIndex: rowIndex)
96- case . row: throw DecodingError . _invalidContainerRequest ( codingPath: self . codingPath)
96+ case . row: throw CSVDecoder . Error . _invalidContainerRequest ( forKey : key , codingPath: self . codingPath)
9797 }
9898 }
9999
100100 func superDecoder( forKey key: Key ) throws -> Decoder {
101101 switch self . _focus {
102102 case . file:
103- guard let rowIndex = key. intValue else { throw DecodingError . _invalidKey ( forRow : key, codingPath: self . codingPath + [ key] ) }
103+ guard let rowIndex = key. intValue else { throw CSVDecoder . Error . _invalidRowKey ( forKey : key, codingPath: self . codingPath + [ key] ) }
104104 var codingPath = self . _decoder. codingPath; codingPath. append ( IndexKey ( rowIndex) )
105105 return ShadowDecoder ( source: self . _decoder. source, codingPath: codingPath)
106- case . row: throw DecodingError . _invalidContainerRequest ( codingPath: self . codingPath)
106+ case . row: throw CSVDecoder . Error . _invalidContainerRequest ( forKey : key , codingPath: self . codingPath)
107107 }
108108 }
109109
@@ -112,13 +112,11 @@ extension ShadowDecoder.KeyedContainer {
112112 case . file:
113113 var codingPath = self . _decoder. codingPath; codingPath. append ( IndexKey ( 0 ) )
114114 return ShadowDecoder ( source: self . _decoder. source, codingPath: codingPath)
115- case . row: throw DecodingError . _invalidContainerRequest ( codingPath: self . codingPath)
115+ case . row: throw CSVDecoder . Error . _invalidContainerRequest ( forKey : NameKey ( index : 0 , name : " super " ) , codingPath: self . codingPath)
116116 }
117117 }
118118}
119119
120- // MARK: -
121-
122120extension ShadowDecoder . KeyedContainer {
123121 func decode( _ type: String . Type , forKey key: Key ) throws -> String {
124122 try self . _fieldContainer ( forKey: key) . decode ( String . self)
@@ -281,9 +279,9 @@ private extension ShadowDecoder.KeyedContainer {
281279 case . row( let rowIndex) :
282280 index = ( rowIndex, try self . _decoder. source. fieldIndex ( forKey: key, codingPath: self . codingPath) )
283281 case . file:
284- guard let rowIndex = key. intValue else { throw DecodingError . _invalidKey ( forRow : key, codingPath: codingPath) }
282+ guard let rowIndex = key. intValue else { throw CSVDecoder . Error . _invalidRowKey ( forKey : key, codingPath: codingPath) }
285283 // Values are only allowed to be decoded directly from a nested container in "file level" if the CSV rows have a single column.
286- guard self . _decoder. source. numExpectedFields == 1 else { throw DecodingError . _invalidNestedRequired ( codingPath: self . codingPath) }
284+ guard self . _decoder. source. numExpectedFields == 1 else { throw CSVDecoder . Error . _invalidNestedRequired ( codingPath: self . codingPath) }
287285 index = ( rowIndex, 0 )
288286 codingPath. append ( IndexKey ( index. field) )
289287 }
@@ -293,26 +291,29 @@ private extension ShadowDecoder.KeyedContainer {
293291 }
294292}
295293
296- fileprivate extension DecodingError {
294+ fileprivate extension CSVDecoder . Error {
297295 /// Error raised when a coding key representing a row within the CSV file cannot be transformed into an integer value.
298- /// - parameter codingPath: The whole coding path, including the invalid row key.
299- static func _invalidKey( forRow key: CodingKey , codingPath: [ CodingKey ] ) -> DecodingError {
300- DecodingError . keyNotFound ( key, . init(
301- codingPath: codingPath,
302- debugDescription: " The coding key identifying a CSV row couldn't be transformed into an integer value. " ) )
296+ /// - parameter codingPath: The full decoding chain.
297+ static func _invalidRowKey( forKey key: CodingKey , codingPath: [ CodingKey ] ) -> CSVError < CSVDecoder > {
298+ . init( . invalidPath,
299+ reason: " The coding key identifying a CSV row couldn't be transformed into an integer value. " ,
300+ help: " The provided coding key identifying a CSV row must implement `intValue`. " ,
301+ userInfo: [ " Coding path " : codingPath, " Key " : key] )
303302 }
304303 /// Error raised when a keyed container is requested on an invalid coding path.
305- /// - parameter codingPath: The full chain of containers which generated this error .
306- static func _invalidContainerRequest( codingPath: [ CodingKey ] ) -> DecodingError {
307- DecodingError . dataCorrupted (
308- Context ( codingPath : codingPath ,
309- debugDescription : " CSV doesn't support more than two nested decoding container. " )
310- )
304+ /// - parameter codingPath: The full decoding chain .
305+ static func _invalidContainerRequest( forKey key : CodingKey , codingPath: [ CodingKey ] ) -> CSVError < CSVDecoder > {
306+ . init ( . invalidPath ,
307+ reason : " A CSV doesn't support more than two nested decoding container. " ,
308+ help : " Don't ask for a nested container on the targeted key for this coding path. " ,
309+ userInfo : [ " Coding path " : codingPath , " Key " : key ] )
311310 }
312311 /// Error raised when a value is decoded, but a container was expected by the decoder.
313- static func _invalidNestedRequired( codingPath: [ CodingKey ] ) -> DecodingError {
314- DecodingError . dataCorrupted ( . init(
315- codingPath: codingPath,
316- debugDescription: " A nested container is needed to decode CSV row values " ) )
312+ /// - parameter codingPath: The full decoding chain.
313+ static func _invalidNestedRequired( codingPath: [ CodingKey ] ) -> CSVError < CSVDecoder > {
314+ . init( . invalidPath,
315+ reason: " A nested container is needed to decode CSV row values " ,
316+ help: " Request a nested container instead of trying to decode a value directly. " ,
317+ userInfo: [ " Coding path " : codingPath] )
317318 }
318319}
0 commit comments