forked from SwiftyLab/MetaCodable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase64Coder.swift
More file actions
64 lines (60 loc) · 2.23 KB
/
Base64Coder.swift
File metadata and controls
64 lines (60 loc) · 2.23 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
import Foundation
import MetaCodable
/// An `HelperCoder` that helps decoding/encoding
/// base64 data.
///
/// This type can be used to decode/encode base64 data
/// string and convert to `Data` type.
public struct Base64Coder: HelperCoder {
/// The options to use when decoding data.
private let decodeOptions: Data.Base64DecodingOptions
/// The options to use when encoding data.
private let encodeOptions: Data.Base64EncodingOptions
/// Creates a new instance of `HelperCoder` that decodes/encodes
/// base64 data.
///
/// - Parameters:
/// - decodeOptions: The options to use when decoding data.
/// - encodeOptions: The options to use when encoding data.
public init(
decodeOptions: Data.Base64DecodingOptions = [],
encodeOptions: Data.Base64EncodingOptions = []
) {
self.decodeOptions = decodeOptions
self.encodeOptions = encodeOptions
}
/// Decodes base64 data with provided decoding options
/// from the given `decoder`.
///
/// The data is decoded from a base64 string representation.
///
/// - Parameter decoder: The decoder to read data from.
/// - Returns: The data decoded.
///
/// - Throws: `DecodingError.typeMismatch` if the decoded string
/// isn't a valid base64 representation.
public func decode(from decoder: Decoder) throws -> Data {
let base64Str = try String(from: decoder)
guard let data = Data(base64Encoded: base64Str, options: decodeOptions)
else {
let errDesc = "Invalid base64 string \"\(base64Str)\""
throw DecodingError.typeMismatch(
Data.self,
.init(codingPath: decoder.codingPath, debugDescription: errDesc)
)
}
return data
}
/// Encodes base64 data with provided encoding options
/// to the given `decoder`.
///
/// The data is encoded as a base64 string representation.
///
/// - Parameters:
/// - value: The data to encode.
/// - encoder: The encoder to write data to.
public func encode(_ value: Data, to encoder: Encoder) throws {
let base64Str = value.base64EncodedString(options: encodeOptions)
try base64Str.encode(to: encoder)
}
}