Skip to content

Commit 1b1f3ea

Browse files
committed
Added Codable conformance and unit tests
1 parent 32f5eae commit 1b1f3ea

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

Sources/SwiftASCII/ASCIICharacter.swift

+32
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,38 @@ extension ASCIICharacter: Equatable {
186186

187187
}
188188

189+
extension ASCIICharacter: Codable {
190+
191+
enum CodingKeys: String, CodingKey {
192+
193+
case asciiValue
194+
195+
}
196+
197+
public init(from decoder: Decoder) throws {
198+
199+
let container = try decoder.container(keyedBy: CodingKeys.self)
200+
let asciiValue = try container.decode(UInt8.self, forKey: .asciiValue)
201+
guard let newInstance = Self(asciiValue) else {
202+
throw DecodingError.dataCorrupted(
203+
.init(codingPath: container.codingPath,
204+
debugDescription: "Value was not valid ASCII character number.")
205+
)
206+
}
207+
self = newInstance
208+
209+
}
210+
211+
public func encode(to encoder: Encoder) throws {
212+
213+
var container = encoder.container(keyedBy: CodingKeys.self)
214+
215+
try container.encode(asciiValue, forKey: .asciiValue)
216+
217+
}
218+
219+
}
220+
189221
extension ASCIICharacter {
190222

191223
public static func + (lhs: ASCIICharacter, rhs: ASCIICharacter) -> ASCIIString {

Sources/SwiftASCII/ASCIIString.swift

+25
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,31 @@ extension ASCIIString: Equatable {
147147

148148
}
149149

150+
extension ASCIIString: Codable {
151+
152+
public init(from decoder: Decoder) throws {
153+
154+
let container = try decoder.singleValueContainer()
155+
let stringValue = try container.decode(String.self)
156+
guard let newInstance = Self(exactly: stringValue) else {
157+
throw DecodingError.dataCorrupted(
158+
.init(codingPath: container.codingPath,
159+
debugDescription: "Encoded string is not a valid ASCII string.")
160+
)
161+
}
162+
self = newInstance
163+
164+
}
165+
166+
public func encode(to encoder: Encoder) throws {
167+
168+
var container = encoder.singleValueContainer()
169+
try container.encode(stringValue)
170+
171+
}
172+
173+
}
174+
150175
extension ASCIIString {
151176

152177
public static func + (lhs: ASCIIString, rhs: ASCIIString) -> ASCIIString {

Tests/SwiftASCIITests/ASCIICharacter Tests.swift

+14
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,20 @@ class ASCIICharacterTests: XCTestCase {
129129

130130
}
131131

132+
func testCodable() throws {
133+
134+
let encoder = JSONEncoder()
135+
let decoder = JSONDecoder()
136+
137+
let str = ASCIICharacter("A")
138+
139+
let encoded = try encoder.encode(str)
140+
let decoded = try decoder.decode(ASCIICharacter.self, from: encoded)
141+
142+
XCTAssertEqual(str, decoded)
143+
144+
}
145+
132146
func testStaticInits() {
133147

134148
let str: ASCIICharacter = .lossy("😃")

Tests/SwiftASCIITests/ASCIIString Tests.swift

+14
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,20 @@ class ASCIIStringTests: XCTestCase {
9696

9797
}
9898

99+
func testCodable() throws {
100+
101+
let encoder = JSONEncoder()
102+
let decoder = JSONDecoder()
103+
104+
let str = ASCIIString("A string")
105+
106+
let encoded = try encoder.encode(str)
107+
let decoded = try decoder.decode(ASCIIString.self, from: encoded)
108+
109+
XCTAssertEqual(str, decoded)
110+
111+
}
112+
99113
func testStaticInits() {
100114

101115
let str: ASCIIString = .lossy("Emöji 😃")

0 commit comments

Comments
 (0)