14
14
15
15
import Foundation
16
16
17
- public struct Base64EncodedData : Sendable , Codable , Hashable {
18
- var data : ArraySlice < UInt8 >
17
+ /// Provides a route to encode or decode base64-encoded data
18
+ ///
19
+ /// This type holds raw, unencoded, data as a slice of bytes. It can be used to encode that
20
+ /// data to a provided `Encoder` as base64-encoded data or to decode from base64 encoding when
21
+ /// initialized from a decoder.
22
+ ///
23
+ /// There is a convenience initializer to create an instance backed by provided data in the form
24
+ /// of a slice of bytes:
25
+ /// ```swift
26
+ /// let bytes: ArraySlice<UInt8> = ...
27
+ /// let base64EncodedData = Base64EncodedData(data: bytes)
28
+ /// ```
29
+ ///
30
+ /// To decode base64-encoded data it is possible to call the initializer directly, providing a decoder:
31
+ /// ```swift
32
+ /// let base64EncodedData = Base64EncodedData(from: decoder)
33
+ ///```
34
+ ///
35
+ /// However more commonly the decoding initializer would be called by a decoder e.g.
36
+ /// ```swift
37
+ /// let encodedData: Data = ...
38
+ /// let decoded = try JSONDecoder().decode(Base64EncodedData.self, from: encodedData)
39
+ ///```
40
+ ///
41
+ /// Once an instance is holding data, it may be base64 encoded to a provided encoder:
42
+ /// ```swift
43
+ /// let bytes: ArraySlice<UInt8> = ...
44
+ /// let base64EncodedData = Base64EncodedData(data: bytes)
45
+ /// base64EncodedData.encode(to: encoder)
46
+ /// ```
47
+ ///
48
+ /// However more commonly it would be called by an encoder e.g.
49
+ /// ```swift
50
+ /// let bytes: ArraySlice<UInt8> = ...
51
+ /// let encodedData = JSONEncoder().encode(encodedBytes)
52
+ /// ```
53
+ public struct Base64EncodedData : Sendable , Hashable {
54
+ /// data to be encoded
55
+ public var data : ArraySlice < UInt8 >
19
56
57
+ /// Initializes an instance of ``Base64EncodedData`` wrapping the provided slice of bytes.
58
+ /// - Parameter data: The underlying bytes to wrap.
20
59
public init ( data: ArraySlice < UInt8 > ) {
21
60
self . data = data
22
61
}
62
+ }
23
63
64
+ extension Base64EncodedData : Codable {
65
+ /// Creates a new instance by decoding from the given decoder.
66
+ ///
67
+ /// This initializer throws an error if reading from the decoder fails, or
68
+ /// if the data read is corrupted or otherwise invalid.
69
+ ///
70
+ /// - Parameter decoder: The decoder to read data from.
24
71
public init ( from decoder: any Decoder ) throws {
25
72
let container = try decoder. singleValueContainer ( )
26
73
let base64EncodedString = try container. decode ( String . self)
@@ -34,6 +81,16 @@ public struct Base64EncodedData: Sendable, Codable, Hashable {
34
81
self . init ( data: ArraySlice ( data) )
35
82
}
36
83
84
+
85
+ /// Encodes this value into the given encoder.
86
+ ///
87
+ /// If the value fails to encode anything, `encoder` will encode an empty
88
+ /// keyed container in its place.
89
+ ///
90
+ /// This function throws an error if any values are invalid for the given
91
+ /// encoder's format.
92
+ ///
93
+ /// - Parameter encoder: The encoder to write data to.
37
94
public func encode( to encoder: any Encoder ) throws {
38
95
var container = encoder. singleValueContainer ( )
39
96
0 commit comments