this repo has no description
at main 47 lines 1.6 kB view raw
1// 2// EncodingContainer.swift 3// URLQueryItemCoder 4// 5// Created by Kyle Hughes on 1/15/23. 6// 7 8import Foundation 9 10internal enum EncodingContainer { 11 case keyed(Keyed) 12 case lowLevelEncoder(LowLevelEncoder) 13 case singleValue(SingleValue) 14 case unkeyed(Unkeyed) 15 16 // MARK: Internal Static Interface 17 18 internal static func encodeByDeferringToType<Value>( 19 _ value: Value, 20 at codingPath: [any CodingKey], 21 using configuration: EncodingStrategies 22 ) throws -> Self where Value: Encodable { 23 let lowLevelEncoder = LowLevelEncoder(codingPath: codingPath, configuration: configuration) 24 25 try value.encode(to: lowLevelEncoder) 26 27 guard let container = lowLevelEncoder.container else { 28 preconditionFailure("Encodable type \(Value.self) was not encoded by low level encoder.") 29 } 30 31 return container 32 } 33 34 internal static func encodeWithSpecialTreatment<Value>( 35 _ value: Value, 36 at codingPath: [any CodingKey], 37 using configuration: EncodingStrategies 38 ) throws -> Self where Value: Encodable { 39 if let data = value as? Data { 40 return try configuration.dataStrategy.encode(data, at: codingPath, using: configuration) 41 } else if let date = value as? Date { 42 return try configuration.dateStrategy.encode(date, at: codingPath, using: configuration) 43 } else { 44 return try .encodeByDeferringToType(value, at: codingPath, using: configuration) 45 } 46 } 47}