Skip to content

Commit cf08d29

Browse files
committed
Implement the Serializer.
1 parent e6e0c68 commit cf08d29

File tree

15 files changed

+7806
-56
lines changed

15 files changed

+7806
-56
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftNIO open source project
4+
//
5+
// Copyright (c) 2020 Apple Inc. and the SwiftNIO project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
let asciiSpace = UInt8(ascii: " ")
16+
let asciiTab = UInt8(ascii: "\t")
17+
let asciiOpenParenthesis = UInt8(ascii: "(")
18+
let asciiCloseParenthesis = UInt8(ascii: ")")
19+
let asciiDash = UInt8(ascii: "-")
20+
let asciiUnderscore = UInt8(ascii: "_")
21+
let asciiZero = UInt8(ascii: "0")
22+
let asciiOne = UInt8(ascii: "1")
23+
let asciiNine = UInt8(ascii: "9")
24+
let asciiDigits = asciiZero...asciiNine
25+
let asciiDquote = UInt8(ascii: "\"")
26+
let asciiColon = UInt8(ascii: ":")
27+
let asciiSemicolon = UInt8(ascii: ";")
28+
let asciiBackslash = UInt8(ascii: "\\")
29+
let asciiQuestionMark = UInt8(ascii: "?")
30+
let asciiExclamationMark = UInt8(ascii: "!")
31+
let asciiOctothorpe = UInt8(ascii: "#")
32+
let asciiDollar = UInt8(ascii: "$")
33+
let asciiPercent = UInt8(ascii: "%")
34+
let asciiAmpersand = UInt8(ascii: "&")
35+
let asciiSquote = UInt8(ascii: "'")
36+
let asciiCaret = UInt8(ascii: "^")
37+
let asciiBacktick = UInt8(ascii: "`")
38+
let asciiPipe = UInt8(ascii: "|")
39+
let asciiTilde = UInt8(ascii: "~")
40+
let asciiAsterisk = UInt8(ascii: "*")
41+
let asciiEqual = UInt8(ascii: "=")
42+
let asciiPlus = UInt8(ascii: "+")
43+
let asciiSlash = UInt8(ascii: "/")
44+
let asciiPeriod = UInt8(ascii: ".")
45+
let asciiComma = UInt8(ascii: ",")
46+
let asciiCapitalA = UInt8(ascii: "A")
47+
let asciiCapitalZ = UInt8(ascii: "Z")
48+
let asciiLowerA = UInt8(ascii: "a")
49+
let asciiLowerZ = UInt8(ascii: "z")
50+
let asciiCapitals = asciiCapitalA...asciiCapitalZ
51+
let asciiLowercases = asciiLowerA...asciiLowerZ

Sources/StructuredHeaders/ComponentTypes.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ extension ItemOrInnerList: Hashable { }
2929
public enum BareItem<BaseData: RandomAccessCollection> where BaseData.Element == UInt8, BaseData.SubSequence == BaseData, BaseData: Hashable {
3030
case bool(Bool)
3131
case integer(Int)
32-
case decimal(Float64) // Not great, can we do better?
32+
case decimal(PseudoDecimal)
3333
case string(String)
3434
case undecodedByteSequence(BaseData)
3535
case token(BaseData)
@@ -49,7 +49,7 @@ extension BareItem: ExpressibleByIntegerLiteral {
4949

5050
extension BareItem: ExpressibleByFloatLiteral {
5151
public init(floatLiteral value: Float64) {
52-
self = .decimal(value)
52+
self = .decimal(.init(floatLiteral: value))
5353
}
5454
}
5555

@@ -65,6 +65,11 @@ extension BareItem: Hashable { }
6565
public struct Item<BaseData: RandomAccessCollection> where BaseData.Element == UInt8, BaseData.SubSequence == BaseData, BaseData: Hashable {
6666
public var bareItem: BareItem<BaseData>
6767
public var parameters: OrderedMap<BaseData, BareItem<BaseData>>
68+
69+
public init(bareItem: BareItem<BaseData>, parameters: OrderedMap<BaseData, BareItem<BaseData>>) {
70+
self.bareItem = bareItem
71+
self.parameters = parameters
72+
}
6873
}
6974

7075
extension Item: Hashable { }
@@ -144,4 +149,9 @@ extension BareInnerList.Index: Comparable {
144149
public struct InnerList<BaseData: RandomAccessCollection>: Hashable where BaseData.Element == UInt8, BaseData.SubSequence == BaseData, BaseData: Hashable {
145150
public var bareInnerList: BareInnerList<BaseData>
146151
public var parameters: OrderedMap<BaseData, BareItem<BaseData>>
152+
153+
public init(bareInnerList: BareInnerList<BaseData>, parameters: OrderedMap<BaseData, BareItem<BaseData>>) {
154+
self.bareInnerList = bareInnerList
155+
self.parameters = parameters
156+
}
147157
}

Sources/StructuredHeaders/Decoder/BareItemDecoder.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ extension BareItemDecoder: SingleValueDecodingContainer {
142142
throw StructuredHeaderError.invalidTypeForItem
143143
}
144144

145-
return T(decimal)
145+
// Going via Double is a bit sad. Swift Numerics would help here.
146+
return T(Double(decimal))
146147
}
147148

148149
private func _decodeFixedWidthInteger<T: FixedWidthInteger>(_ type: T.Type) throws -> T {

Sources/StructuredHeaders/Errors.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
// MARK:- StructuredHeaderError
1516
public struct StructuredHeaderError: Error {
1617
private enum _BaseError: Hashable {
1718
case invalidTrailingBytes

Sources/StructuredHeaders/FieldParser.swift

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,6 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
fileprivate let asciiSpace = UInt8(ascii: " ")
16-
fileprivate let asciiTab = UInt8(ascii: "\t")
17-
fileprivate let asciiOpenParenthesis = UInt8(ascii: "(")
18-
fileprivate let asciiCloseParenthesis = UInt8(ascii: ")")
19-
fileprivate let asciiDash = UInt8(ascii: "-")
20-
fileprivate let asciiUnderscore = UInt8(ascii: "_")
21-
fileprivate let asciiZero = UInt8(ascii: "0")
22-
fileprivate let asciiOne = UInt8(ascii: "1")
23-
fileprivate let asciiNine = UInt8(ascii: "9")
24-
fileprivate let asciiDigits = asciiZero...asciiNine
25-
fileprivate let asciiDquote = UInt8(ascii: "\"")
26-
fileprivate let asciiColon = UInt8(ascii: ":")
27-
fileprivate let asciiSemicolon = UInt8(ascii: ";")
28-
fileprivate let asciiBackslash = UInt8(ascii: "\\")
29-
fileprivate let asciiQuestionMark = UInt8(ascii: "?")
30-
fileprivate let asciiExclamationMark = UInt8(ascii: "!")
31-
fileprivate let asciiOctothorpe = UInt8(ascii: "#")
32-
fileprivate let asciiDollar = UInt8(ascii: "$")
33-
fileprivate let asciiPercent = UInt8(ascii: "%")
34-
fileprivate let asciiAmpersand = UInt8(ascii: "&")
35-
fileprivate let asciiSquote = UInt8(ascii: "'")
36-
fileprivate let asciiCaret = UInt8(ascii: "^")
37-
fileprivate let asciiBacktick = UInt8(ascii: "`")
38-
fileprivate let asciiPipe = UInt8(ascii: "|")
39-
fileprivate let asciiTilde = UInt8(ascii: "~")
40-
fileprivate let asciiAsterisk = UInt8(ascii: "*")
41-
fileprivate let asciiEqual = UInt8(ascii: "=")
42-
fileprivate let asciiPlus = UInt8(ascii: "+")
43-
fileprivate let asciiSlash = UInt8(ascii: "/")
44-
fileprivate let asciiPeriod = UInt8(ascii: ".")
45-
fileprivate let asciiComma = UInt8(ascii: ",")
46-
fileprivate let asciiCapitalA = UInt8(ascii: "A")
47-
fileprivate let asciiCapitalZ = UInt8(ascii: "Z")
48-
fileprivate let asciiLowerA = UInt8(ascii: "a")
49-
fileprivate let asciiLowerZ = UInt8(ascii: "z")
50-
fileprivate let asciiCapitals = asciiCapitalA...asciiCapitalZ
51-
fileprivate let asciiLowercases = asciiLowerA...asciiLowerZ
52-
5315
/// A FieldParser is the basic parsing object for structured header fields represented as lists.
5416
public struct StructuredFieldParser<BaseData: RandomAccessCollection> where BaseData.Element == UInt8, BaseData.SubSequence: Hashable {
5517
// Right now I'm on the fence about whether this should be generic. It's convenient,
@@ -322,8 +284,9 @@ extension StructuredFieldParser {
322284
}
323285

324286
// Same notes here as above
325-
let baseFloat = Float64(String(decoding: integerBytes, as: UTF8.self))!
326-
return .decimal(baseFloat * Float64(sign))
287+
var decimal = PseudoDecimal(bytes: integerBytes)
288+
decimal.mantissa *= Int64(sign)
289+
return .decimal(decimal)
327290
}
328291
}
329292

0 commit comments

Comments
 (0)