@@ -56,11 +56,7 @@ final class URIValueFromNodeDecoder {
56
56
/// - data: The data to parse.
57
57
/// - rootKey: The key of the root object.
58
58
/// - configuration: The configuration of the decoder.
59
- init (
60
- data: Substring ,
61
- rootKey: URIParsedKeyComponent ,
62
- configuration: URICoderConfiguration
63
- ) {
59
+ init ( data: Substring , rootKey: URIParsedKeyComponent , configuration: URICoderConfiguration ) {
64
60
self . rootKey = rootKey
65
61
self . dateTranscoder = configuration. dateTranscoder
66
62
self . configuration = configuration
@@ -144,6 +140,7 @@ extension URIValueFromNodeDecoder {
144
140
/// Use the root as a primitive value.
145
141
/// - Parameter work: The closure in which to use the value.
146
142
/// - Returns: Any value returned from the closure.
143
+ /// - Throws: When parsing the root fails.
147
144
private func withParsedRootAsPrimitive< R> ( _ work: ( URIDecodedPrimitive ? ) throws -> R ) throws -> R {
148
145
let value : URIDecodedPrimitive ?
149
146
if let cached = cache. primitive {
@@ -165,6 +162,7 @@ extension URIValueFromNodeDecoder {
165
162
/// Use the root as an array.
166
163
/// - Parameter work: The closure in which to use the value.
167
164
/// - Returns: Any value returned from the closure.
165
+ /// - Throws: When parsing the root fails.
168
166
private func withParsedRootAsArray< R> ( _ work: ( URIDecodedArray ) throws -> R ) throws -> R {
169
167
let value : URIDecodedArray
170
168
if let cached = cache. array {
@@ -186,6 +184,7 @@ extension URIValueFromNodeDecoder {
186
184
/// Use the root as a dictionary.
187
185
/// - Parameter work: The closure in which to use the value.
188
186
/// - Returns: Any value returned from the closure.
187
+ /// - Throws: When parsing the root fails.
189
188
private func withParsedRootAsDictionary< R> ( _ work: ( URIDecodedDictionary ) throws -> R ) throws -> R {
190
189
let value : URIDecodedDictionary
191
190
if let cached = cache. dictionary {
@@ -234,6 +233,7 @@ extension URIValueFromNodeDecoder {
234
233
/// - key: The key for which to return the value.
235
234
/// - dictionary: The dictionary in which to find the value.
236
235
/// - Returns: The value in the dictionary, or nil if not found.
236
+ /// - Throws: When multiple values are found for the key.
237
237
func primitiveValue( forKey key: String , in dictionary: URIDecodedDictionary ) throws -> URIParsedValue ? {
238
238
let values = dictionary [ key [ ... ] , default: [ ] ]
239
239
if values. isEmpty { return nil }
@@ -246,6 +246,7 @@ extension URIValueFromNodeDecoder {
246
246
/// Use the current top of the stack as a primitive value.
247
247
/// - Parameter work: The closure in which to use the value.
248
248
/// - Returns: Any value returned from the closure.
249
+ /// - Throws: When parsing the root fails.
249
250
private func withCurrentPrimitiveElement< R> ( _ work: ( URIDecodedPrimitive ? ) throws -> R ) throws -> R {
250
251
if !codingStack. isEmpty {
251
252
// Nesting is involved.
@@ -285,6 +286,7 @@ extension URIValueFromNodeDecoder {
285
286
/// Use the current top of the stack as an array.
286
287
/// - Parameter work: The closure in which to use the value.
287
288
/// - Returns: Any value returned from the closure.
289
+ /// - Throws: When parsing the root fails.
288
290
private func withCurrentArrayElements< R> ( _ work: ( URIDecodedArray ) throws -> R ) throws -> R {
289
291
if let nestedArrayParentKey = codingStack. first {
290
292
// Top level is dictionary, first level nesting is array.
@@ -301,6 +303,7 @@ extension URIValueFromNodeDecoder {
301
303
/// Use the current top of the stack as a dictionary.
302
304
/// - Parameter work: The closure in which to use the value.
303
305
/// - Returns: Any value returned from the closure.
306
+ /// - Throws: When parsing the root fails or if there is unsupported extra nesting of containers.
304
307
private func withCurrentDictionaryElements< R> ( _ work: ( URIDecodedDictionary ) throws -> R ) throws -> R {
305
308
if !codingStack. isEmpty {
306
309
try throwMismatch ( " Nesting a dictionary inside another container is not supported. " )
@@ -314,22 +317,26 @@ extension URIValueFromNodeDecoder {
314
317
315
318
/// Returns the current top-of-stack as a primitive value.
316
319
/// - Returns: The primitive value, or nil if not found.
320
+ /// - Throws: When parsing the root fails.
317
321
func currentElementAsSingleValue( ) throws -> URIParsedValue ? { try withCurrentPrimitiveElement { $0 } }
318
322
319
323
/// Returns the count of elements in the current top-of-stack array.
320
324
/// - Returns: The number of elements.
325
+ /// - Throws: When parsing the root fails.
321
326
func countOfCurrentArray( ) throws -> Int { try withCurrentArrayElements { $0. count } }
322
327
323
328
/// Returns an element from the current top-of-stack array.
324
329
/// - Parameter index: The position in the array to return.
325
330
/// - Returns: The primitive value from the array.
331
+ /// - Throws: When parsing the root fails.
326
332
func nestedElementInCurrentArray( atIndex index: Int ) throws -> URIParsedValue {
327
333
try withCurrentArrayElements { $0 [ index] }
328
334
}
329
335
330
336
/// Returns an element from the current top-of-stack dictionary.
331
337
/// - Parameter key: The key to find a value for.
332
338
/// - Returns: The value for the key, or nil if not found.
339
+ /// - Throws: When parsing the root fails.
333
340
func nestedElementInCurrentDictionary( forKey key: String ) throws -> URIParsedValue ? {
334
341
try withCurrentDictionaryElements { dictionary in try primitiveValue ( forKey: key, in: dictionary) }
335
342
}
@@ -338,12 +345,14 @@ extension URIValueFromNodeDecoder {
338
345
/// contains a value for the provided key.
339
346
/// - Parameter key: The key for which to look for a value.
340
347
/// - Returns: `true` if a value was found, `false` otherwise.
348
+ /// - Throws: When parsing the root fails.
341
349
func containsElementInCurrentDictionary( forKey key: String ) throws -> Bool {
342
350
try withCurrentDictionaryElements { dictionary in dictionary [ key [ ... ] ] != nil }
343
351
}
344
352
345
353
/// Returns a list of keys found in the current top-of-stack dictionary.
346
354
/// - Returns: A list of keys from the dictionary.
355
+ /// - Throws: When parsing the root fails.
347
356
func elementKeysInCurrentDictionary( ) throws -> [ String ] {
348
357
try withCurrentDictionaryElements { dictionary in dictionary. keys. map ( String . init) }
349
358
}
0 commit comments