Skip to content

Commit 9586523

Browse files
committed
Soundness
1 parent f696b93 commit 9586523

11 files changed

+50
-51
lines changed

Sources/RawStructuredFieldValues/ComponentTypes.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,10 @@ public struct InnerList: Hashable, SHSendable {
192192
}
193193
}
194194

195-
extension String {
195+
public extension String {
196196
/// Whether this string is a valid structured headers token, or whether it would
197197
/// need to be stored in a structured headers string.
198-
public var structuredHeadersIsValidToken: Bool {
198+
var structuredHeadersIsValidToken: Bool {
199199
let view = self.utf8
200200

201201
switch view.first {

Sources/RawStructuredFieldValues/Errors.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,22 @@ public struct StructuredHeaderError: Error, SHSendable {
4141
}
4242
}
4343

44-
extension StructuredHeaderError {
45-
public static let invalidTrailingBytes = StructuredHeaderError(.invalidTrailingBytes)
46-
public static let invalidInnerList = StructuredHeaderError(.invalidInnerList)
47-
public static let invalidItem = StructuredHeaderError(.invalidItem)
48-
public static let invalidKey = StructuredHeaderError(.invalidKey)
49-
public static let invalidIntegerOrDecimal = StructuredHeaderError(.invalidIntegerOrDecimal)
50-
public static let invalidString = StructuredHeaderError(.invalidString)
51-
public static let invalidByteSequence = StructuredHeaderError(.invalidByteSequence)
52-
public static let invalidBoolean = StructuredHeaderError(.invalidBoolean)
53-
public static let invalidToken = StructuredHeaderError(.invalidToken)
54-
public static let invalidList = StructuredHeaderError(.invalidList)
55-
public static let invalidDictionary = StructuredHeaderError(.invalidDictionary)
56-
public static let missingKey = StructuredHeaderError(.missingKey)
57-
public static let invalidTypeForItem = StructuredHeaderError(.invalidTypeForItem)
58-
public static let integerOutOfRange = StructuredHeaderError(.integerOutOfRange)
59-
public static let indexOutOfRange = StructuredHeaderError(.indexOutOfRange)
44+
public extension StructuredHeaderError {
45+
static let invalidTrailingBytes = StructuredHeaderError(.invalidTrailingBytes)
46+
static let invalidInnerList = StructuredHeaderError(.invalidInnerList)
47+
static let invalidItem = StructuredHeaderError(.invalidItem)
48+
static let invalidKey = StructuredHeaderError(.invalidKey)
49+
static let invalidIntegerOrDecimal = StructuredHeaderError(.invalidIntegerOrDecimal)
50+
static let invalidString = StructuredHeaderError(.invalidString)
51+
static let invalidByteSequence = StructuredHeaderError(.invalidByteSequence)
52+
static let invalidBoolean = StructuredHeaderError(.invalidBoolean)
53+
static let invalidToken = StructuredHeaderError(.invalidToken)
54+
static let invalidList = StructuredHeaderError(.invalidList)
55+
static let invalidDictionary = StructuredHeaderError(.invalidDictionary)
56+
static let missingKey = StructuredHeaderError(.missingKey)
57+
static let invalidTypeForItem = StructuredHeaderError(.invalidTypeForItem)
58+
static let integerOutOfRange = StructuredHeaderError(.integerOutOfRange)
59+
static let indexOutOfRange = StructuredHeaderError(.indexOutOfRange)
6060
}
6161

6262
extension StructuredHeaderError: Hashable {}

Sources/RawStructuredFieldValues/FieldParser.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ public struct StructuredFieldValueParser<BaseData: RandomAccessCollection> where
2525
}
2626
}
2727

28-
extension StructuredFieldValueParser: SHSendable where BaseData: SHSendable, BaseData.SubSequence: SHSendable {
29-
30-
}
28+
extension StructuredFieldValueParser: SHSendable where BaseData: SHSendable, BaseData.SubSequence: SHSendable {}
3129

3230
extension StructuredFieldValueParser {
3331
// Helper typealiases to avoid the explosion of generic parameters
@@ -519,10 +517,10 @@ extension RandomAccessCollection where Element == UInt8, SubSequence == Self {
519517
}
520518
}
521519

522-
extension String {
520+
private extension String {
523521
// This is the slow path, so we never inline this.
524522
@inline(never)
525-
fileprivate static func decodingEscapes<Bytes: RandomAccessCollection>(_ bytes: Bytes, escapes: Int) -> String where Bytes.Element == UInt8 {
523+
static func decodingEscapes<Bytes: RandomAccessCollection>(_ bytes: Bytes, escapes: Int) -> String where Bytes.Element == UInt8 {
526524
// We assume the string is previously validated, so the escapes are easily removed. See the doc comment for
527525
// `StrippingStringEscapesCollection` for more details on what we're doing here.
528526
let unescapedBytes = StrippingStringEscapesCollection(bytes, escapes: escapes)

Sources/RawStructuredFieldValues/FieldSerializer.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ public struct StructuredFieldValueSerializer: SHSendable {
2323
}
2424
}
2525

26-
extension StructuredFieldValueSerializer {
26+
public extension StructuredFieldValueSerializer {
2727
/// Writes a structured dictionary header field value.
2828
///
2929
/// - parameters:
3030
/// - root: The dictionary object.
3131
/// - throws: If the dictionary could not be serialized.
3232
/// - returns: The bytes of the serialized header field value.
33-
public mutating func writeDictionaryFieldValue(_ root: OrderedMap<String, ItemOrInnerList>) throws -> [UInt8] {
33+
mutating func writeDictionaryFieldValue(_ root: OrderedMap<String, ItemOrInnerList>) throws -> [UInt8] {
3434
guard root.count > 0 else {
3535
return []
3636
}
@@ -48,7 +48,7 @@ extension StructuredFieldValueSerializer {
4848
/// - root: The list object.
4949
/// - throws: If the list could not be serialized.
5050
/// - returns: The bytes of the serialized header field value.
51-
public mutating func writeListFieldValue(_ list: [ItemOrInnerList]) throws -> [UInt8] {
51+
mutating func writeListFieldValue(_ list: [ItemOrInnerList]) throws -> [UInt8] {
5252
guard list.count > 0 else {
5353
return []
5454
}
@@ -66,7 +66,7 @@ extension StructuredFieldValueSerializer {
6666
/// - root: The item.
6767
/// - throws: If the item could not be serialized.
6868
/// - returns: The bytes of the serialized header field value.
69-
public mutating func writeItemFieldValue(_ item: Item) throws -> [UInt8] {
69+
mutating func writeItemFieldValue(_ item: Item) throws -> [UInt8] {
7070
defer {
7171
self.data.removeAll(keepingCapacity: true)
7272
}

Sources/RawStructuredFieldValues/OrderedMap.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public struct OrderedMap<Key, Value>: SHSendable where Key: Hashable {
3535
/// Warning! Unlike a regular dictionary, we do not promise this will be O(1)!
3636
public subscript(key: Key) -> Value? {
3737
get {
38-
self.backing.first(where: { $0.key == key }).map { $0.value }
38+
self.backing.first(where: { $0.key == key }).map(\.value)
3939
}
4040
set {
4141
if let existing = self.backing.firstIndex(where: { $0.key == key }) {
@@ -53,15 +53,15 @@ public struct OrderedMap<Key, Value>: SHSendable where Key: Hashable {
5353

5454
// MARK: - Helper struct for storing elements
5555

56-
extension OrderedMap {
56+
private extension OrderedMap {
5757
// This struct takes some explaining.
5858
//
5959
// We don't want to maintain too much code here. In particular, we'd like to have straightforward equatable and hashable
6060
// implementations. However, tuples aren't equatable or hashable. So we need to actually store something that is: a nominal
6161
// type. That's this!
6262
//
6363
// This existence of this struct is a pure implementation detail and not exposed to the user of the type.
64-
fileprivate struct Entry {
64+
struct Entry {
6565
var key: Key
6666
var value: Value
6767
}

Sources/RawStructuredFieldValues/PseudoDecimal.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ extension PseudoDecimal: ExpressibleByFloatLiteral {
182182
}
183183
}
184184

185-
extension String {
186-
public init(_ decimal: PseudoDecimal) {
185+
public extension String {
186+
init(_ decimal: PseudoDecimal) {
187187
var local = decimal
188188
local.canonicalise()
189189

@@ -216,8 +216,8 @@ extension String {
216216
}
217217

218218
// Swift Numerics would let us do this for all the floating point types.
219-
extension Double {
220-
public init(_ pseudoDecimal: PseudoDecimal) {
219+
public extension Double {
220+
init(_ pseudoDecimal: PseudoDecimal) {
221221
self = Double(pseudoDecimal.mantissa) * pow(10, Double(pseudoDecimal.exponent))
222222
}
223223
}

Sources/StructuredFieldValues/Decoder/StructuredFieldValueDecoder.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public struct StructuredFieldValueDecoder: SHSendable {
2323
public init() {}
2424
}
2525

26-
extension StructuredFieldValueDecoder {
26+
public extension StructuredFieldValueDecoder {
2727
/// A strategy that should be used to map coding keys to wire format keys.
28-
public struct KeyDecodingStrategy: Hashable, SHSendable {
28+
struct KeyDecodingStrategy: Hashable, SHSendable {
2929
fileprivate enum Base: Hashable {
3030
case lowercase
3131
}
@@ -184,9 +184,9 @@ extension _StructuredFieldDecoder: Decoder {
184184
// We have single value containers for items and bareItems.
185185
switch self.currentElement! {
186186
case .item(let item):
187-
return BareItemDecoder(item.bareItem, codingPath: self._codingStack.map { $0.key })
187+
return BareItemDecoder(item.bareItem, codingPath: self._codingStack.map(\.key))
188188
case .bareItem(let bareItem):
189-
return BareItemDecoder(bareItem, codingPath: self._codingStack.map { $0.key })
189+
return BareItemDecoder(bareItem, codingPath: self._codingStack.map(\.key))
190190
case .dictionary, .list, .innerList, .bareInnerList, .parameters:
191191
throw StructuredHeaderError.invalidTypeForItem
192192
}
@@ -217,9 +217,9 @@ extension _StructuredFieldDecoder: Decoder {
217217
}
218218
}
219219

220-
extension _StructuredFieldDecoder {
220+
private extension _StructuredFieldDecoder {
221221
/// The basic elements that make up a Structured Header
222-
fileprivate enum Element {
222+
enum Element {
223223
case dictionary(OrderedMap<String, ItemOrInnerList>)
224224
case list([ItemOrInnerList])
225225
case item(Item)
@@ -314,6 +314,6 @@ extension _StructuredFieldDecoder {
314314

315315
/// The element at the current head of the coding stack.
316316
private var currentElement: Element? {
317-
self._codingStack.last.map { $0.element }
317+
self._codingStack.last.map(\.element)
318318
}
319319
}

Sources/StructuredFieldValues/Encoder/StructuredFieldKeyedEncodingContainer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ extension StructuredFieldKeyedEncodingContainer: KeyedEncodingContainerProtocol
9494
}
9595

9696
mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer<NestedKey> where NestedKey: CodingKey {
97-
return self.encoder.container(keyedBy: keyType)
97+
self.encoder.container(keyedBy: keyType)
9898
}
9999

100100
mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {

Sources/StructuredFieldValues/Encoder/StructuredFieldUnkeyedEncodingContainer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ extension StructuredFieldUnkeyedEncodingContainer: UnkeyedEncodingContainer {
9898
}
9999

100100
mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type) -> KeyedEncodingContainer<NestedKey> where NestedKey: CodingKey {
101-
return self.encoder.container(keyedBy: keyType)
101+
self.encoder.container(keyedBy: keyType)
102102
}
103103

104104
mutating func nestedUnkeyedContainer() -> UnkeyedEncodingContainer {

Sources/StructuredFieldValues/Encoder/StructuredFieldValueEncoder.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public struct StructuredFieldValueEncoder: SHSendable {
2222
public init() {}
2323
}
2424

25-
extension StructuredFieldValueEncoder {
25+
public extension StructuredFieldValueEncoder {
2626
/// A strategy that should be used to map coding keys to wire format keys.
27-
public struct KeyEncodingStrategy: Hashable, SHSendable {
27+
struct KeyEncodingStrategy: Hashable, SHSendable {
2828
fileprivate enum Base: Hashable {
2929
case lowercase
3030
}
@@ -197,7 +197,7 @@ extension _StructuredFieldEncoder: Encoder {
197197
}
198198

199199
func container<Key>(keyedBy type: Key.Type) -> KeyedEncodingContainer<Key> where Key: CodingKey {
200-
return KeyedEncodingContainer(StructuredFieldKeyedEncodingContainer(encoder: self))
200+
KeyedEncodingContainer(StructuredFieldKeyedEncodingContainer(encoder: self))
201201
}
202202

203203
func unkeyedContainer() -> UnkeyedEncodingContainer {
@@ -924,8 +924,8 @@ extension _StructuredFieldEncoder {
924924
}
925925
}
926926

927-
extension Item {
928-
fileprivate init(_ partialItem: _StructuredFieldEncoder.NodeType.PartialItem) {
927+
private extension Item {
928+
init(_ partialItem: _StructuredFieldEncoder.NodeType.PartialItem) {
929929
self.init(bareItem: partialItem.bareItem!, parameters: partialItem.parameters)
930930
}
931931
}

Tests/StructuredFieldValuesTests/StructuredFieldParserTests.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ final class StructuredFieldParserTests: XCTestCase {
9696

9797
private func _validateInnerList(_ innerList: InnerList, against schema: JSONSchema, fixtureName: String) throws {
9898
guard case .array(let arrayElements) = schema,
99-
arrayElements.count == 2,
100-
case .some(.array(let expectedItems)) = arrayElements.first,
101-
let expectedParameters = arrayElements.last else {
99+
arrayElements.count == 2,
100+
case .some(.array(let expectedItems)) = arrayElements.first,
101+
let expectedParameters = arrayElements.last
102+
else {
102103
XCTFail("\(fixtureName): Unexpected inner list: got \(innerList), expected \(schema)")
103104
return
104105
}

0 commit comments

Comments
 (0)