-
Notifications
You must be signed in to change notification settings - Fork 22
/
ComponentTypes.swift
234 lines (189 loc) · 6.48 KB
/
ComponentTypes.swift
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2020 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
// This file contains common, currency, component types.
//
// These types are used by the parser, the serializer, and by users of the direct encoding/decoding APIs.
// They are not used by those using the Codable interface.
// MARK: - ItemOrInnerList
/// `ItemOrInnerList` represents the values in a structured header dictionary, or the
/// entries in a structured header list.
public enum ItemOrInnerList: Sendable {
case item(Item)
case innerList(InnerList)
}
extension ItemOrInnerList: Hashable {}
// MARK: - BareItem
/// `BareItem` is a representation of the base data types at the bottom of a structured
/// header field. These types are not parameterised: they are raw data.
public enum BareItem: Sendable {
/// A boolean item.
case bool(Bool)
/// An integer item.
case integer(Int)
/// A decimal item.
case decimal(PseudoDecimal)
/// A string item.
case string(String)
/// A byte sequence. This case must contain base64-encoded data, as
/// `StructuredHeaders` does not do base64 encoding or decoding.
case undecodedByteSequence(String)
/// A token item.
case token(String)
}
extension BareItem: ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = .bool(value)
}
}
extension BareItem: ExpressibleByIntegerLiteral {
public init(integerLiteral value: Int) {
self = .integer(value)
}
}
extension BareItem: ExpressibleByFloatLiteral {
public init(floatLiteral value: Float64) {
self = .decimal(.init(floatLiteral: value))
}
}
extension BareItem: ExpressibleByStringLiteral {
public init(stringLiteral value: StringLiteralType) {
if value.structuredHeadersIsValidToken {
self = .token(value)
} else {
self = .string(value)
}
}
}
extension BareItem: Hashable {}
// MARK: - Item
/// `Item` represents a structured header field item: a combination of a `bareItem`
/// and some parameters.
public struct Item: Sendable {
/// The `BareItem` that this `Item` contains.
public var bareItem: BareItem
/// The parameters associated with `bareItem`
public var parameters: OrderedMap<String, BareItem>
public init(bareItem: BareItem, parameters: OrderedMap<String, BareItem>) {
self.bareItem = bareItem
self.parameters = parameters
}
}
extension Item: Hashable {}
// MARK: - BareInnerList
/// A `BareInnerList` represents the items contained within an ``InnerList``, without
/// the associated parameters.
public struct BareInnerList: Hashable, Sendable {
private var items: [Item]
public init() {
self.items = []
}
public mutating func append(_ item: Item) {
self.items.append(item)
}
}
extension BareInnerList: ExpressibleByArrayLiteral {
public init(arrayLiteral elements: Item...) {
self.items = elements
}
}
// TODO: RangeReplaceableCollection I guess
extension BareInnerList: RandomAccessCollection, MutableCollection {
public struct Index {
fileprivate var baseIndex: Array<Item>.Index
init(_ baseIndex: Array<Item>.Index) {
self.baseIndex = baseIndex
}
}
public var count: Int {
self.items.count
}
public var startIndex: Index {
Index(self.items.startIndex)
}
public var endIndex: Index {
Index(self.items.endIndex)
}
public func index(after i: Index) -> Index {
Index(self.items.index(after: i.baseIndex))
}
public func index(before i: Index) -> Index {
Index(self.items.index(before: i.baseIndex))
}
public func index(_ i: Index, offsetBy offset: Int) -> Index {
Index(self.items.index(i.baseIndex, offsetBy: offset))
}
public subscript(index: Index) -> Item {
get {
self.items[index.baseIndex]
}
set {
self.items[index.baseIndex] = newValue
}
}
}
extension BareInnerList.Index: Hashable {}
extension BareInnerList.Index: Comparable {
public static func < (lhs: Self, rhs: Self) -> Bool {
lhs.baseIndex < rhs.baseIndex
}
}
// MARK: - InnerList
/// An `InnerList` is a list of items, with some associated parameters.
public struct InnerList: Hashable, Sendable {
/// The items contained within this inner list.
public var bareInnerList: BareInnerList
/// The parameters associated with the `bareInnerList`.
public var parameters: OrderedMap<String, BareItem>
public init(bareInnerList: BareInnerList, parameters: OrderedMap<String, BareItem>) {
self.bareInnerList = bareInnerList
self.parameters = parameters
}
}
extension String {
/// Whether this string is a valid structured headers token, or whether it would
/// need to be stored in a structured headers string.
public var structuredHeadersIsValidToken: Bool {
let view = self.utf8
switch view.first {
case .some(asciiCapitals), .some(asciiLowercases), .some(asciiAsterisk):
// Good
()
default:
return false
}
for byte in view {
switch byte {
// Valid token characters are RFC 7230 tchar, colon, and slash.
// tchar is:
//
// tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
// / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
// / DIGIT / ALPHA
//
// The following insane case statement covers this. Tokens suck.
case asciiExclamationMark, asciiOctothorpe, asciiDollar, asciiPercent,
asciiAmpersand, asciiSquote, asciiAsterisk, asciiPlus, asciiDash,
asciiPeriod, asciiCaret, asciiUnderscore, asciiBacktick, asciiPipe,
asciiTilde, asciiDigits, asciiCapitals, asciiLowercases,
asciiColon, asciiSlash:
// Good
()
default:
// Bad token
return false
}
}
return true
}
}