Skip to content

Commit 885ef64

Browse files
committed
Rename a number of types and modules.
Motivation: In general we should try to be specific about what we're referring to. "Structured headers" is not really an accurate description of what this module works with: "Structured Header Field Values", or "Structured Field Value" is usually better. To that end this change adjusts our naming to be closer to what we want. It also changes our module names to prioritise the use of the Codable-based one. Modifications: - Renamed CodableStructuredHeaders to StructuredFieldValues. - Renamed StructuredHeaders to RawStructuredFieldValues. - Renamed a number of types from Field to FieldValue. - Renamed a number of types from Header to Field or FieldValue. - Renamed some functions along the above lines. Result: Better and more consistent terminology will be used.
1 parent f881e6c commit 885ef64

31 files changed

+313
-313
lines changed

Package.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,30 @@ let package = Package(
1818
name: "swift-http-structured-headers",
1919
products: [
2020
.library(
21-
name: "StructuredHeaders",
22-
targets: ["StructuredHeaders"]
21+
name: "StructuredFieldValues",
22+
targets: ["StructuredFieldValues"]
2323
),
2424
.library(
25-
name: "CodableStructuredHeaders",
26-
targets: ["CodableStructuredHeaders"]
25+
name: "RawStructuredFieldValues",
26+
targets: ["RawStructuredFieldValues"]
2727
),
2828
],
2929
targets: [
3030
.target(
31-
name: "StructuredHeaders",
31+
name: "RawStructuredFieldValues",
3232
dependencies: []
3333
),
3434
.target(
35-
name: "CodableStructuredHeaders",
36-
dependencies: ["StructuredHeaders"]
35+
name: "StructuredFieldValues",
36+
dependencies: ["RawStructuredFieldValues"]
3737
),
3838
.target(
3939
name: "sh-parser",
40-
dependencies: ["StructuredHeaders"]
40+
dependencies: ["RawStructuredFieldValues"]
4141
),
4242
.testTarget(
43-
name: "StructuredHeadersTests",
44-
dependencies: ["StructuredHeaders", "CodableStructuredHeaders"]
43+
name: "StructuredFieldValuesTests",
44+
dependencies: ["RawStructuredFieldValues", "StructuredFieldValues"]
4545
),
4646
]
4747
)

README.md

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
# swift-http-structured-headers
22

3-
A Swift implementation of the HTTP Structured Header Field specification.
3+
A Swift implementation of the HTTP Structured Header Field Value specification.
44

5-
Provides parsing and serialization facilities for structured header fields, as well as implementations of `Encoder` and `Decoder` to allow using `Codable` data types as the payloads of HTTP structured header fields.
5+
Provides parsing and serialization facilities for structured header field values, as well as implementations of `Encoder` and `Decoder` to allow using `Codable` data types as the payloads of HTTP structured header fields.
66

7-
## About Structured Header Fields
7+
## About Structured Header Field Values
88

9-
HTTP Structured Header fields are a HTTP extension recorded in [RFC 8941](https://www.rfc-editor.org/rfc/rfc8941.html) . They provide a set of data types and algorithms for handling HTTP header field values in a consistent way, allowing a single parser and serializer to handle a wide range of header field values.
9+
HTTP Structured Header Field Values are a HTTP extension recorded in [RFC 8941](https://www.rfc-editor.org/rfc/rfc8941.html). They provide a set of data types and algorithms for handling HTTP header field values in a consistent way, allowing a single parser and serializer to handle a wide range of header field values.
1010

11-
## Swift HTTP Structured Header Fields
11+
## Swift HTTP Structured Header Field Values
1212

13-
This package provides a parser and serializer that implement RFC 8941. They are entirely complete, able to handle all valid HTTP structured header fields. This package also provides `Encoder` and `Decoder` objects for working with Codable in Swift. This allows rapid prototyping and experimentation with HTTP structured header fields, as well as interaction with the wider Swift Codable community.
13+
This package provides a parser and serializer that implement RFC 8941. They are entirely complete, able to handle all valid HTTP structured header field values. This package also provides `Encoder` and `Decoder` objects for working with Codable in Swift. This allows rapid prototyping and experimentation with HTTP structured header field values, as well as interaction with the wider Swift Codable community.
1414

15-
This package provides two top-level modules: `StructuredHeaders` and `CodableStructuredHeaders`.
15+
This package provides two top-level modules: `StructuredFieldValues` and `RawStructuredFieldValues`.
1616

17-
The base module, `StructuredHeaders`, provides a low-level implementation of a serializer and parser. Both of these have been written to avoid using Foundation, making them suitable for a range of use-cases where Foundation is not available. They rely entirely on the Swift standard library and are implemented as generically as possible. One of the limitations due to the absence of Foundation is that this interface is not capable of performing Base64 encoding or decoding: users are free to bring whatever encoder and decoder they choose to use.
17+
The base module, `RawStructuredFieldValues`, provides a low-level implementation of a serializer and parser. Both of these have been written to avoid using Foundation, making them suitable for a range of use-cases where Foundation is not available. They rely entirely on the Swift standard library and are implemented as generically as possible. One of the limitations due to the absence of Foundation is that this interface is not capable of performing Base64 encoding or decoding: users are free to bring whatever encoder and decoder they choose to use.
1818

1919
This API is low-level, exposing the raw parse tree as the format for the serializer and parser. This allows high-performance and high-flexibility parsing and serialization, at the cost of being verbose and complex. Users are required to understand the structured header format and to operate the slightly awkward types, but maximal fidelity is retained and the performance overhead is low.
2020

21-
The upper-level module, `CodableStructuredHeaders`, brings along the `Encoder` and `Decoder` and also adds a dependency on Foundation. This Foundation dependency is necessary to correctly handle the base64 formatting, as well as to provide a good natural container for binary data: `Data`. This interface is substantially friendlier and easier to work with, using Swift's `Codable` support to provide a great user experience.
21+
The upper-level module, `StructuredFieldValues`, brings along the `Encoder` and `Decoder` and also adds a dependency on Foundation. This Foundation dependency is necessary to correctly handle the base64 formatting, as well as to provide a good natural container for binary data: `Data`. This interface is substantially friendlier and easier to work with, using Swift's `Codable` support to provide a great user experience.
2222

23-
In most cases users should prefer to use `CodableStructuredHeaders` unless they know they need the performance advantages of `StructuredHeaders`. The experience will be much better.
23+
In most cases users should prefer to use `StructuredFieldValues` unless they know they need the performance advantages of `RawStructuredFieldValues`. The experience will be much better.
2424

25-
## Working with Structured Header Fields
25+
## Working with Structured Header Field Values
2626

27-
`swift-http-structured-headers` has a simply, easy-to-use high-level API for working with structured header fields. To begin with, let's consider the [HTTP Client Hints specification](https://www.rfc-editor.org/rfc/rfc8942.html). This defines the following new header field:
27+
`swift-http-structured-headers` has a simple, easy-to-use high-level API for working with structured header field values. To begin with, let's consider the [HTTP Client Hints specification](https://www.rfc-editor.org/rfc/rfc8942.html). This defines the following new header field:
2828

2929
> The Accept-CH response header field indicates server support for the hints indicated in its value. Servers wishing to receive user agent information through Client Hints SHOULD add Accept-CH response header to their responses as early as possible.
3030
>
@@ -41,66 +41,66 @@ In most cases users should prefer to use `CodableStructuredHeaders` unless they
4141
```swift
4242
let field = Array("Sec-CH-Example, Sec-CH-Example-2".utf8)
4343

44-
struct AcceptCH: StructuredHeaderField {
45-
static let structuredFiedType: StructuredHeaderFieldType = .list
44+
struct AcceptCH: StructuredFieldValue {
45+
static let structuredFieldType: StructuredFieldType = .list
4646

4747
var items: [String]
4848
}
4949

5050
// Decoding
51-
let decoder = StructuredFieldDecoder()
51+
let decoder = StructuredFieldValueDecoder()
5252
let parsed = try decoder.decode(AcceptCH.self, from: field)
5353

5454
// Encoding
55-
let encoder = StructuredFieldEncoder()
55+
let encoder = StructuredFieldValueEncoder()
5656
let serialized = try encoder.encode(AcceptCH(items: ["Sec-CH-Example", "Sec-CH-Example-2"]))
5757
```
5858

59-
However, structured header fields can be substantially more complex. Structured header fields can make use of 4 containers and 6 base item types. The containers are:
59+
However, structured header field values can be substantially more complex. Structured header fields can make use of 4 containers and 6 base item types. The containers are:
6060

61-
1. Dictionaries. These are top-level elements and associate token keys with values. The values may be items, or may be inner lists, and each value may also have parameters associated with them. `CodableStructuredHeaders` can model dictionaries as either Swift objects (where the property names are dictionary keys).
62-
2. Lists. These are top-level elements, providing a sequence of items or inner lists. Each item or inner list may have parameters associated with them. `CodableStructuredHeaders` models these as Swift objects with one key, `items`, that must be a collection of entries.
63-
3. Inner Lists. These are lists that may be sub-entries of a dictionary or a list. The list entries are items, which may have parameters associated with them: additionally, an inner list may have parameters associated with itself as well. `CodableStructuredHeaders` models these as either Swift `Array`s _or_, if it's important to extract parameters, as a two-field Swift `struct` where one field is called `items` and contains an `Array`, and other field is called `parameters` and contains a dictionary.
64-
4. Parameters. Parameters associated token keys with items without parameters. These are used to store metadata about objects within a field. `CodableStructuredHeaders` models these as either Swift objects (where the property names are the parameter keys) or as Swift dictionaries.
61+
1. Dictionaries. These are top-level elements and associate token keys with values. The values may be items, or may be inner lists, and each value may also have parameters associated with them. `StructuredFieldValues` can model dictionaries as either Swift objects (where the property names are dictionary keys).
62+
2. Lists. These are top-level elements, providing a sequence of items or inner lists. Each item or inner list may have parameters associated with them. `StructuredFieldValues` models these as Swift objects with one key, `items`, that must be a collection of entries.
63+
3. Inner Lists. These are lists that may be sub-entries of a dictionary or a list. The list entries are items, which may have parameters associated with them: additionally, an inner list may have parameters associated with itself as well. `StructuredFieldValues` models these as either Swift `Array`s _or_, if it's important to extract parameters, as a two-field Swift `struct` where one field is called `items` and contains an `Array`, and other field is called `parameters` and contains a dictionary.
64+
4. Parameters. Parameters associated token keys with items without parameters. These are used to store metadata about objects within a field. `StructuredFieldValues` models these as either Swift objects (where the property names are the parameter keys) or as Swift dictionaries.
6565

6666
The base types are:
6767

68-
1. Booleans. `CodableStructuredHeaders` models these as Swift's `Bool` type.
69-
2. Integers. `CodableStructuredHeaders` models these as any fixed-width integer type.
70-
3. Decimals. `CodableStructuredHeaders` models these as any floating-point type, or as Foundation's `Decimal`.
71-
4. Tokens. `CodableStructuredHeaders` models these as Swift's `String` type, where the range of characters is restricted.
72-
5. Strings. `CodableStructuredHeaders` models these as Swift's `String` type.
73-
6. Binary data. `CodableStructuredHeaders` models this as Foundation's `Data` type.
68+
1. Booleans. `StructuredFieldValues` models these as Swift's `Bool` type.
69+
2. Integers. `StructuredFieldValues` models these as any fixed-width integer type.
70+
3. Decimals. `StructuredFieldValues` models these as any floating-point type, or as Foundation's `Decimal`.
71+
4. Tokens. `StructuredFieldValues` models these as Swift's `String` type, where the range of characters is restricted.
72+
5. Strings. `StructuredFieldValues` models these as Swift's `String` type.
73+
6. Binary data. `StructuredFieldValues` models this as Foundation's `Data` type.
7474

75-
For any Structured Header Field Item, the item may either be represented directly by the appropriate type, or by a Swift struct with two properties: `item` and `parameters`. This latter mode is how parameters on a given item may be captured.
75+
For any Structured Header Field Value Item, the item may either be represented directly by the appropriate type, or by a Swift struct with two properties: `item` and `parameters`. This latter mode is how parameters on a given item may be captured.
7676

77-
The top-level structured header field must identify what kind of header field it corresponds to: `.item`, `.list`, or `.dictionary`. This is inherent in the type of the field and will be specified in the relevant field specification.
77+
The top-level Structured Header Field Value must identify what kind of header field it corresponds to: `.item`, `.list`, or `.dictionary`. This is inherent in the type of the field and will be specified in the relevant field specification.
7878

7979
## Lower Levels
8080

81-
In some cases the Codable interface will not be either performant enough or powerful enough for the intended use-case. In cases like this, users can use the types in the `StructuredHeaders` module instead.
81+
In some cases the Codable interface will not be either performant enough or powerful enough for the intended use-case. In cases like this, users can use the types in the `RawStructuredFieldValues` module instead.
8282

83-
There are two core types: `StructuredFieldParser` and `StructuredFieldSerializer`. Rather than work with high-level Swift objects, these two objects either produce or accept a Swift representation of the data tree for a given structured header field.
83+
There are two core types: `StructuredFieldValueParser` and `StructuredFieldValueSerializer`. Rather than work with high-level Swift objects, these two objects either produce or accept a Swift representation of the data tree for a given structured header field.
8484

8585
This exposes the maximum amount of information about the header field. It allows users to handle situations where Codable cannot necessarily provide the relevant information, such in cases where dictionary ordering is semantic, or where it's necessary to control whether fields are tokens or strings more closely.
8686

87-
These APIs also have lower overhead than the `CodableStructuredHeaders` APIs.
87+
These APIs also have lower overhead than the `StructuredFieldValues` APIs.
8888

89-
The cost is that the APIs are substantially more verbose. Consider the above header field, `Accept-CH`. To parse or serialize this in `StructuredHeaders` would look like this:
89+
The cost is that the APIs are substantially more verbose. Consider the above header field, `Accept-CH`. To parse or serialize this in `RawStructuredFieldValues` would look like this:
9090

9191
```swift
9292
let field = Array("Sec-CH-Example, Sec-CH-Example-2".utf8)
93-
var parser = StructuredFieldParser(field)
94-
let parsed = parser.parseListField()
93+
var parser = StructuredFieldValueParser(field)
94+
let parsed = parser.parseListFieldValue()
9595

9696
print(parsed)
9797
// [
9898
// .item(Item(bareItem: .token("Sec-CH-Example"), parameters: [])),
9999
// .item(Item(bareItem: .token("Sec-CH-Example-2"), parameters: [])),
100100
// ]
101101

102-
var serializer = StructuredFieldSerializer()
103-
let serialized = serializer.writeListHeader(parsed)
102+
var serializer = StructuredFieldValueSerializer()
103+
let serialized = serializer.writeListFieldValue(parsed)
104104
```
105105

106106
Notice the substantially more verbose types involved in this operation. These types are highly generic, giving the opportunity for parsing and serializing that greatly reduces the runtime overhead. They also make it easier to distinguish between tokens and strings, and to observe the order of objects in dictionaries or parameters, which can be lost at the Codable level.

Sources/StructuredHeaders/FieldParser.swift renamed to Sources/RawStructuredFieldValues/FieldParser.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the SwiftNIO open source project
44
//
5-
// Copyright (c) 2020 Apple Inc. and the SwiftNIO project authors
5+
// Copyright (c) 2020-2021 Apple Inc. and the SwiftNIO project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -12,8 +12,8 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
/// A `StructuredFieldParser` is the basic parsing object for structured header fields.
16-
public struct StructuredFieldParser<BaseData: RandomAccessCollection> where BaseData.Element == UInt8 {
15+
/// A `StructuredFieldValueParser` is the basic parsing object for structured header field values.
16+
public struct StructuredFieldValueParser<BaseData: RandomAccessCollection> where BaseData.Element == UInt8 {
1717
// Right now I'm on the fence about whether this should be generic. It's convenient,
1818
// and makes it really easy for us to express the parsing over a wide range of data types.
1919
// But it risks code size in a really nasty way! We should validate that we don't pay too
@@ -25,22 +25,22 @@ public struct StructuredFieldParser<BaseData: RandomAccessCollection> where Base
2525
}
2626
}
2727

28-
extension StructuredFieldParser {
28+
extension StructuredFieldValueParser {
2929
// Helper typealiases to avoid the explosion of generic parameters
30-
public typealias BareItem = StructuredHeaders.BareItem
31-
public typealias Item = StructuredHeaders.Item
32-
public typealias BareInnerList = StructuredHeaders.BareInnerList
33-
public typealias InnerList = StructuredHeaders.InnerList
34-
public typealias ItemOrInnerList = StructuredHeaders.ItemOrInnerList
30+
public typealias BareItem = RawStructuredFieldValues.BareItem
31+
public typealias Item = RawStructuredFieldValues.Item
32+
public typealias BareInnerList = RawStructuredFieldValues.BareInnerList
33+
public typealias InnerList = RawStructuredFieldValues.InnerList
34+
public typealias ItemOrInnerList = RawStructuredFieldValues.ItemOrInnerList
3535
public typealias Key = String
3636

37-
/// Parse the HTTP structured field as a list.
37+
/// Parse the HTTP structured field value as a list.
3838
///
3939
/// This is a straightforward implementation of the parser in the spec.
4040
///
41-
/// - throws: If the field could not be parsed.
41+
/// - throws: If the field value could not be parsed.
4242
/// - returns: An array of items or inner lists.
43-
public mutating func parseListField() throws -> [ItemOrInnerList] {
43+
public mutating func parseListFieldValue() throws -> [ItemOrInnerList] {
4444
// Step one, strip leading spaces.
4545
self.underlyingData.stripLeadingSpaces()
4646

@@ -58,11 +58,11 @@ extension StructuredFieldParser {
5858
return members
5959
}
6060

61-
/// Parse the HTTP structured header field as a dictionary.
61+
/// Parse the HTTP structured header field value as a dictionary.
6262
///
63-
/// - throws: If the field could not be parsed.
63+
/// - throws: If the field value could not be parsed.
6464
/// - returns: An `OrderedMap` corresponding to the entries in the dictionary.
65-
public mutating func parseDictionaryField() throws -> OrderedMap<Key, ItemOrInnerList> {
65+
public mutating func parseDictionaryFieldValue() throws -> OrderedMap<Key, ItemOrInnerList> {
6666
// Step one, strip leading spaces.
6767
self.underlyingData.stripLeadingSpaces()
6868

@@ -80,11 +80,11 @@ extension StructuredFieldParser {
8080
return map
8181
}
8282

83-
/// Parse the HTTP structured header field as an item.
83+
/// Parse the HTTP structured header field value as an item.
8484
///
85-
/// - throws: If the field could not be parsed.
85+
/// - throws: If the field value could not be parsed.
8686
/// - returns: The `Item` in the field.
87-
public mutating func parseItemField() throws -> Item {
87+
public mutating func parseItemFieldValue() throws -> Item {
8888
// Step one, strip leading spaces.
8989
self.underlyingData.stripLeadingSpaces()
9090

0 commit comments

Comments
 (0)