Skip to content

Commit f2ae7bb

Browse files
committed
Implement feedback
1 parent 061d2a0 commit f2ae7bb

File tree

5 files changed

+33
-29
lines changed

5 files changed

+33
-29
lines changed

Sources/StructuredFieldValues/Decoder/BareItemDecoder.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ extension BareItemDecoder: SingleValueDecodingContainer {
146146
throw StructuredHeaderError.invalidTypeForItem
147147
}
148148

149-
return DisplayString(string)
149+
return DisplayString(rawValue: string)
150150
}
151151

152152
func decodeNil() -> Bool {

Sources/StructuredFieldValues/DisplayString.swift

+5-9
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,11 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
/// A type that represents the Display String Structured Type.
16-
public struct DisplayString: Codable, Equatable {
17-
/// The value of this Display String.
18-
public private(set) var description: String
16+
public struct DisplayString: RawRepresentable, Codable, Equatable, Hashable {
17+
public typealias RawValue = String
18+
public var rawValue: String
1919

20-
/// Initializes a new Display String.
21-
///
22-
/// - parameters:
23-
/// - description: The value of this Display String.
24-
public init(_ description: String) {
25-
self.description = description
20+
public init(rawValue: String) {
21+
self.rawValue = rawValue
2622
}
2723
}

Sources/StructuredFieldValues/Encoder/StructuredFieldValueEncoder.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ extension _StructuredFieldEncoder: SingleValueEncodingContainer {
318318
}
319319

320320
func encode(_ data: DisplayString) throws {
321-
try self.currentStackEntry.storage.insertBareItem(.displayString(data.description))
321+
try self.currentStackEntry.storage.insertBareItem(.displayString(data.rawValue))
322322
}
323323

324324
func encode<T>(_ value: T) throws where T: Encodable {
@@ -488,7 +488,7 @@ extension _StructuredFieldEncoder {
488488
}
489489

490490
func append(_ value: DisplayString) throws {
491-
try self.currentStackEntry.storage.appendBareItem(.displayString(value.description))
491+
try self.currentStackEntry.storage.appendBareItem(.displayString(value.rawValue))
492492
}
493493

494494
func append<T>(_ value: T) throws where T: Encodable {
@@ -673,7 +673,7 @@ extension _StructuredFieldEncoder {
673673

674674
func encode(_ value: DisplayString, forKey key: String) throws {
675675
let key = self.sanitizeKey(key)
676-
let displayString = value.description
676+
let displayString = value.rawValue
677677
try self.currentStackEntry.storage.insertBareItem(.displayString(displayString), atKey: key)
678678
}
679679

Tests/StructuredFieldValuesTests/StructuredFieldDecoderTests.swift

+12-12
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ final class StructuredFieldDecoderTests: XCTestCase {
614614

615615
func testDecodingDisplayStringAsTopLevelData() throws {
616616
XCTAssertEqual(
617-
ItemField(DisplayString("füü")),
617+
ItemField(DisplayString(rawValue: "füü")),
618618
try StructuredFieldValueDecoder().decode(from: Array("%\"f%c3%bc%c3%bc\"".utf8))
619619
)
620620
}
@@ -628,7 +628,7 @@ final class StructuredFieldDecoderTests: XCTestCase {
628628

629629
XCTAssertEqual(
630630
Item(
631-
item: DisplayString("füü"),
631+
item: DisplayString(rawValue: "füü"),
632632
parameters: [:]
633633
),
634634
try StructuredFieldValueDecoder().decode(
@@ -638,7 +638,7 @@ final class StructuredFieldDecoderTests: XCTestCase {
638638
)
639639

640640
XCTAssertEqual(
641-
Item(item: DisplayString("füü"), parameters: ["q": 0.8]),
641+
Item(item: DisplayString(rawValue: "füü"), parameters: ["q": 0.8]),
642642
try StructuredFieldValueDecoder().decode(
643643
Item.self,
644644
from: Array("%\"f%c3%bc%c3%bc\";q=0.8".utf8)
@@ -654,7 +654,7 @@ final class StructuredFieldDecoderTests: XCTestCase {
654654
}
655655

656656
XCTAssertEqual(
657-
Item(item: 1, parameters: ["q": DisplayString("füü")]),
657+
Item(item: 1, parameters: ["q": DisplayString(rawValue: "füü")]),
658658
try StructuredFieldValueDecoder().decode(
659659
Item.self,
660660
from: Array("1;q=%\"f%c3%bc%c3%bc\"".utf8)
@@ -666,8 +666,8 @@ final class StructuredFieldDecoderTests: XCTestCase {
666666
XCTAssertEqual(
667667
List(
668668
[
669-
DisplayString("füü"),
670-
DisplayString("foo \"bar\" \\ baz"),
669+
DisplayString(rawValue: "füü"),
670+
DisplayString(rawValue: "foo \"bar\" \\ baz"),
671671
]
672672
),
673673
try StructuredFieldValueDecoder().decode(
@@ -681,8 +681,8 @@ final class StructuredFieldDecoderTests: XCTestCase {
681681
List(
682682
Array(
683683
repeating: [
684-
DisplayString("füü"),
685-
DisplayString("foo \"bar\" \\ baz"),
684+
DisplayString(rawValue: "füü"),
685+
DisplayString(rawValue: "foo \"bar\" \\ baz"),
686686
],
687687
count: 2
688688
)
@@ -708,8 +708,8 @@ final class StructuredFieldDecoderTests: XCTestCase {
708708
Array(
709709
repeating: ListField(
710710
items: [
711-
DisplayString("füü"),
712-
DisplayString("foo \"bar\" \\ baz"),
711+
DisplayString(rawValue: "füü"),
712+
DisplayString(rawValue: "foo \"bar\" \\ baz"),
713713
],
714714
parameters: ["foo": true]
715715
),
@@ -736,8 +736,8 @@ final class StructuredFieldDecoderTests: XCTestCase {
736736

737737
XCTAssertEqual(
738738
DictionaryField(
739-
bin: DisplayString("füü"),
740-
box: DisplayString("foo \"bar\" \\ baz")
739+
bin: DisplayString(rawValue: "füü"),
740+
box: DisplayString(rawValue: "foo \"bar\" \\ baz")
741741
),
742742
try StructuredFieldValueDecoder().decode(
743743
from: Array("bin=%\"f%c3%bc%c3%bc\", box=%\"foo %22bar%22 \\ baz\"".utf8)

Tests/StructuredFieldValuesTests/StructuredFieldEncoderTests.swift

+12-4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ final class StructuredFieldEncoderTests: XCTestCase {
5757
// Display String
5858
XCTAssertEqual(
5959
Array("%\"f%c3%bc%c3%bc\"".utf8),
60-
try encoder.encode(ItemField(DisplayString("füü")))
60+
try encoder.encode(ItemField(DisplayString(rawValue: "füü")))
6161
)
6262
}
6363

@@ -136,11 +136,15 @@ final class StructuredFieldEncoderTests: XCTestCase {
136136
// Display String
137137
XCTAssertEqual(
138138
Array("%\"f%c3%bc%c3%bc\";x".utf8),
139-
try encoder.encode(KeyedItem(item: DisplayString("füü"), parameters: ["x": true]))
139+
try encoder.encode(
140+
KeyedItem(item: DisplayString(rawValue: "füü"), parameters: ["x": true])
141+
)
140142
)
141143
XCTAssertEqual(
142144
Array("%\"foo %22bar%22 \\ baz\"".utf8),
143-
try encoder.encode(KeyedItem(item: DisplayString("foo \"bar\" \\ baz"), parameters: [:]))
145+
try encoder.encode(
146+
KeyedItem(item: DisplayString(rawValue: "foo \"bar\" \\ baz"), parameters: [:])
147+
)
144148
)
145149
}
146150

@@ -252,7 +256,11 @@ final class StructuredFieldEncoderTests: XCTestCase {
252256
// Display String
253257
XCTAssertEqual(
254258
Array("%\"f%c3%bc%c3%bc\", %\"foo %22bar%22 \\ baz\"".utf8),
255-
try encoder.encode(List([DisplayString("füü"), DisplayString("foo \"bar\" \\ baz")]))
259+
try encoder.encode(
260+
List(
261+
[DisplayString(rawValue: "füü"), DisplayString(rawValue: "foo \"bar\" \\ baz")]
262+
)
263+
)
256264
)
257265
}
258266

0 commit comments

Comments
 (0)