forked from SwiftyLab/MetaCodable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConditionalCoder.swift
More file actions
78 lines (72 loc) · 2.66 KB
/
ConditionalCoder.swift
File metadata and controls
78 lines (72 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import MetaCodable
/// An `HelperCoder` that helps decoding/encoding
/// with two separate `HelperCoder`s.
///
/// This type can be used to use separate `HelperCoder`s
/// for decoding and encoding.
public struct ConditionalCoder<D: HelperCoder, E: HelperCoder>: HelperCoder
where D.Coded == E.Coded {
/// The `HelperCoder` used for decoding.
@usableFromInline
internal let decoder: D
/// The `HelperCoder` used for encoding.
@usableFromInline
internal let encoder: E
/// Creates a new instance of `HelperCoder` that decodes/encodes
/// conditionally with provided decoder/encoder respectively.
///
/// The provided decoder is used only for decoding
/// and encoder only for encoding.
///
/// - Parameters:
/// - decoder: The `HelperCoder` used for decoding.
/// - encoder: The `HelperCoder` used for encoding.
public init(decoder: D, encoder: E) {
self.decoder = decoder
self.encoder = encoder
}
/// Decodes using the decode specific `HelperCoder`
/// from the given `decoder`.
///
/// - Parameter decoder: The decoder to read data from.
/// - Returns: The decoded value.
/// - Throws: If the underlying `HelperCoder` throws error.
@inlinable
public func decode(from decoder: Decoder) throws -> D.Coded {
return try self.decoder.decode(from: decoder)
}
/// Decodes optional value using the decode specific
/// `HelperCoder` from the given `decoder`.
///
/// - Parameter decoder: The decoder to read data from.
/// - Returns: The decoded optional value.
/// - Throws: If the underlying `HelperCoder` throws error.
@inlinable
public func decodeIfPresent(from decoder: Decoder) throws -> D.Coded? {
return try self.decoder.decodeIfPresent(from: decoder)
}
/// Encodes using the encode specific `HelperCoder`
/// from the given `encoder`.
///
/// - Parameters:
/// - value: The value to encode.
/// - encoder: The encoder to write data to.
///
/// - Throws: If the underlying `HelperCoder` throws error.
@inlinable
public func encode(_ value: E.Coded, to encoder: Encoder) throws {
try self.encoder.encode(value, to: encoder)
}
/// Encodes optional value using the encode specific
/// `HelperCoder` from the given `encoder`.
///
/// - Parameters:
/// - value: The value to encode.
/// - encoder: The encoder to write data to.
///
/// - Throws: If the underlying `HelperCoder` throws error.
@inlinable
public func encodeIfPresent(_ value: E.Coded?, to encoder: Encoder) throws {
try self.encoder.encodeIfPresent(value, to: encoder)
}
}