|
| 1 | +import MetaCodable |
| 2 | + |
| 3 | +/// An ``/MetaCodable/HelperCoder`` that performs default decoding/encoding. |
| 4 | +/// |
| 5 | +/// This type doesn't provide any customization and used only to opt out of |
| 6 | +/// decoding/encoding customizations. |
| 7 | +@_documentation(visibility:internal) |
| 8 | +public struct DefaultSequenceElementCoding<Coded: Codable>: HelperCoder { |
| 9 | + /// Decodes value from the given `decoder`. |
| 10 | + /// |
| 11 | + /// Decodes the data using type's `Decodable` implementation. |
| 12 | + /// |
| 13 | + /// - Parameter decoder: The decoder to read data from. |
| 14 | + /// - Returns: The decoded value. |
| 15 | + /// - Throws: If decoding fails due to corrupted or invalid data. |
| 16 | + @inlinable |
| 17 | + public func decode(from decoder: Decoder) throws -> Coded { |
| 18 | + return try Coded(from: decoder) |
| 19 | + } |
| 20 | + |
| 21 | + /// Decodes optional value from the given `decoder`. |
| 22 | + /// |
| 23 | + /// Decodes the data using optional type's `Decodable` implementation. |
| 24 | + /// |
| 25 | + /// - Parameter decoder: The decoder to read data from. |
| 26 | + /// - Returns: The optional decoded value. |
| 27 | + /// - Throws: If decoding fails due to corrupted or invalid data. |
| 28 | + @inlinable |
| 29 | + public func decodeIfPresent(from decoder: Decoder) throws -> Coded? { |
| 30 | + return try Coded?(from: decoder) |
| 31 | + } |
| 32 | + |
| 33 | + /// Decodes value of the from the given `container` and specified `key`. |
| 34 | + /// |
| 35 | + /// Uses container's default `decode(_:forKey:)` implementation |
| 36 | + /// to get value from the `container` at the specified `key`. |
| 37 | + /// |
| 38 | + /// - Parameters: |
| 39 | + /// - container: The container to read data from. |
| 40 | + /// - key: The key for the value decoded. |
| 41 | + /// |
| 42 | + /// - Returns: The decoded value. |
| 43 | + /// - Throws: If decoding fails due to corrupted or invalid data. |
| 44 | + @inlinable |
| 45 | + public func decode<DecodingContainer: KeyedDecodingContainerProtocol>( |
| 46 | + from container: DecodingContainer, |
| 47 | + forKey key: DecodingContainer.Key |
| 48 | + ) throws -> Coded { |
| 49 | + return try container.decode(Coded.self, forKey: key) |
| 50 | + } |
| 51 | + |
| 52 | + /// Decodes optional value of the from the given `container` and |
| 53 | + /// specified `key`. |
| 54 | + /// |
| 55 | + /// Uses container's default `decodeIfPresent(_:forKey:)` implementation |
| 56 | + /// to get value from the `container` at the specified `key`. |
| 57 | + /// |
| 58 | + /// - Parameters: |
| 59 | + /// - container: The container to read data from. |
| 60 | + /// - key: The key for the value decoded. |
| 61 | + /// |
| 62 | + /// - Returns: The optional decoded value. |
| 63 | + /// - Throws: If decoding fails due to corrupted or invalid data. |
| 64 | + @inlinable |
| 65 | + public func decodeIfPresent<DecodingContainer>( |
| 66 | + from container: DecodingContainer, forKey key: DecodingContainer.Key |
| 67 | + ) throws -> Coded? where DecodingContainer: KeyedDecodingContainerProtocol { |
| 68 | + return try container.decodeIfPresent(Coded.self, forKey: key) |
| 69 | + } |
| 70 | + |
| 71 | + /// Encodes given value to the provided `encoder`. |
| 72 | + /// |
| 73 | + /// Decodes the value using type's `Encodable` implementation. |
| 74 | + /// |
| 75 | + /// - Parameters: |
| 76 | + /// - value: The value to encode. |
| 77 | + /// - encoder: The encoder to write data to. |
| 78 | + /// |
| 79 | + /// - Throws: If any values are invalid for the given encoder’s format. |
| 80 | + @inlinable |
| 81 | + public func encode(_ value: Coded, to encoder: Encoder) throws { |
| 82 | + try value.encode(to: encoder) |
| 83 | + } |
| 84 | + |
| 85 | + /// Encodes given optional value to the provided `encoder`. |
| 86 | + /// |
| 87 | + /// Decodes the optional value using optional type's `Encodable` |
| 88 | + /// implementation. |
| 89 | + /// |
| 90 | + /// - Parameters: |
| 91 | + /// - value: The value to encode. |
| 92 | + /// - encoder: The encoder to write data to. |
| 93 | + /// |
| 94 | + /// - Throws: If any values are invalid for the given encoder’s format. |
| 95 | + @inlinable |
| 96 | + public func encodeIfPresent(_ value: Coded?, to encoder: Encoder) throws { |
| 97 | + try value.encode(to: encoder) |
| 98 | + } |
| 99 | + |
| 100 | + /// Encodes given value of to the provided `container` at the specified |
| 101 | + /// `key`. |
| 102 | + /// |
| 103 | + /// Uses container's default `encode(_:forKey:)` implementation |
| 104 | + /// to write value to the `container` at the specified `key`. |
| 105 | + /// |
| 106 | + /// - Parameters: |
| 107 | + /// - value: The value to encode. |
| 108 | + /// - container: The container to write data to. |
| 109 | + /// - key: The key to write data at. |
| 110 | + /// |
| 111 | + /// - Throws: If any values are invalid for the given encoder’s format. |
| 112 | + @inlinable |
| 113 | + public func encode<EncodingContainer: KeyedEncodingContainerProtocol>( |
| 114 | + _ value: Coded, to container: inout EncodingContainer, |
| 115 | + atKey key: EncodingContainer.Key |
| 116 | + ) throws { |
| 117 | + try container.encode(value, forKey: key) |
| 118 | + } |
| 119 | + |
| 120 | + /// Encodes given optional value of to the provided `container` |
| 121 | + /// at the specified `key`. |
| 122 | + /// |
| 123 | + /// Uses container's default `encodeIfPresent(_:forKey:)` implementation |
| 124 | + /// to write optional value to the `container` at the specified `key`. |
| 125 | + /// |
| 126 | + /// - Parameters: |
| 127 | + /// - value: The value to encode. |
| 128 | + /// - container: The container to write data to. |
| 129 | + /// - key: The key to write data at. |
| 130 | + /// |
| 131 | + /// - Throws: If any values are invalid for the given encoder’s format. |
| 132 | + @inlinable |
| 133 | + public func encodeIfPresent<EncodingContainer>( |
| 134 | + _ value: Coded?, to container: inout EncodingContainer, |
| 135 | + atKey key: EncodingContainer.Key |
| 136 | + ) throws where EncodingContainer: KeyedEncodingContainerProtocol { |
| 137 | + try container.encodeIfPresent(value, forKey: key) |
| 138 | + } |
| 139 | +} |
0 commit comments