fast-sfv 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +71 -0
- data/README.md +97 -0
- data/lib/fast/sfv/errors.rb +44 -0
- data/lib/fast/sfv/parser.rb +641 -0
- data/lib/fast/sfv/serializer.rb +581 -0
- data/lib/fast/sfv/types.rb +134 -0
- data/lib/fast/sfv/version.rb +7 -0
- data/lib/fast/sfv.rb +49 -0
- metadata +55 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: b35fbd244725d9dc429d66ef52b492aad28c04d2bcce9ab02439e096a6dcba59
|
|
4
|
+
data.tar.gz: b9d5a03341a53112fb7c49a6d6c05670e13ec87cf3d9a024b517b43f279906cd
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: a59ad053d17076648d11c441b356d681dbbbc9acf57ac58c52344704acf26295a643fbb65634c4364044f1bd6f362a43aedff186f2eef06de56a5e96736a08e2
|
|
7
|
+
data.tar.gz: 5a9da7c669f0e3a3faf09ccba81e2ec0d5788b511075862a0d83b615c2e7eba9e5bd2142a73c4a72470525db3ecc51e26cfc16f2c16077dda99a183b60b0ae42
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.1 (2026-11-25)
|
|
4
|
+
|
|
5
|
+
- Docs: re-authored from scratch — the README front door and the
|
|
6
|
+
diataxis tree under `docs/` (tutorial, four how-to guides, two
|
|
7
|
+
reference pages, three explanations). Every ruby example executed
|
|
8
|
+
as printed, every RFC 9651 section citation checked against the RFC
|
|
9
|
+
text, and the performance page carries both runtime modes side by
|
|
10
|
+
side: the plain interpreter and YJIT (the compiling runtime, the
|
|
11
|
+
common production configuration).
|
|
12
|
+
- Internals: an idiomaticity pass over the parse and emit paths —
|
|
13
|
+
binary-string literals (`"".b`), `String#ascii_only?` as the ASCII
|
|
14
|
+
pre-scan's hot path (the offset walk kept for the error it
|
|
15
|
+
reports), `case` member dispatch, and the long methods decomposed
|
|
16
|
+
into named phases (`emit_decimal`'s scan, rounding and wire-digit
|
|
17
|
+
emission; `parse_number`'s limits; the display-string walks).
|
|
18
|
+
Behavior is byte-identical: every error message and offset, every
|
|
19
|
+
parse and serialize allocation budget unchanged. Parse throughput
|
|
20
|
+
improved with the `ascii_only?` scan (interpreter `parse_item`
|
|
21
|
+
256k → 343k i/s on the benchmark samples; YJIT 1.67M → 2.07M).
|
|
22
|
+
- The repo's comments no longer reference internal
|
|
23
|
+
development-workspace documents.
|
|
24
|
+
|
|
25
|
+
## 0.1.0 (2026-11-25)
|
|
26
|
+
|
|
27
|
+
- Tokens: token values switched from `Symbol` to frozen
|
|
28
|
+
`Fast::SFV::Token < String` — the class is the wire-type tag, the
|
|
29
|
+
content is String semantics (`==`, `eql?`, `hash` and `to_s`
|
|
30
|
+
inherited; `inspect` overridden to name the class), and construction
|
|
31
|
+
validates the full token grammar (RFC 9651 §3.3.4) at the boundary
|
|
32
|
+
(`ArgumentError` for invalid content, `TypeError` for a non-String,
|
|
33
|
+
no length cap). The serializer dispatches on the class: a `Token`
|
|
34
|
+
emits bare, a plain `String` quoted, an ASCII-8BIT `String` still a
|
|
35
|
+
byte sequence, and a `Symbol` — wrong-typed input after the switch —
|
|
36
|
+
raises positioned `SerializeError`. Parse and serialize allocation
|
|
37
|
+
budgets re-measured and re-frozen for the Token path (a token value
|
|
38
|
+
costs its content slice plus the `Token`; no intern cache — measured
|
|
39
|
+
+1 object per value worst-case, not needed). A deliberate, documented
|
|
40
|
+
divergence from starry, which returns `Symbol` tokens.
|
|
41
|
+
- Serializer: `Fast::SFV.serialize_item`/`serialize_list`/
|
|
42
|
+
`serialize_dict` and `to_s` on the value objects — the strict RFC
|
|
43
|
+
9651 §4.1 emit (all eight bare item types, parameters, inner lists,
|
|
44
|
+
dictionaries, the Boolean-true short forms, the empty-container field
|
|
45
|
+
omission), append-only byte emission with the Ruby type as the
|
|
46
|
+
wire-type tag (no coercion, no content inference), the positioned
|
|
47
|
+
`Fast::SFV::SerializeError`, and the vendored httpwg corpus's
|
|
48
|
+
serialize side (553 serialisation-tests records plus 734 parse-side
|
|
49
|
+
round trips) refereeing every emit edge, with the decimal digits
|
|
50
|
+
rounded as written and ties to even. The emit allocation budgets (the
|
|
51
|
+
output String only) join the parse budgets, and the benchmark bundle
|
|
52
|
+
(`gems.rb` + `benchmark/sfv.rb`) compares against starry, the RFC
|
|
53
|
+
8941 reference implementation, on the common surface — a reference
|
|
54
|
+
comparison, never a runtime dependency.
|
|
55
|
+
- Parser: `Fast::SFV.parse_item`/`parse_list`/`parse_dict` with the
|
|
56
|
+
`valid_item?`/`valid_list?`/`valid_dict?` mirrors — the strict RFC 9651
|
|
57
|
+
§4.2 grammar (all eight bare item types, parameters, inner lists,
|
|
58
|
+
dictionaries, the `", "` field-line combining), the frozen value
|
|
59
|
+
objects (`Item`, `InnerList`, `Date`, `DisplayString`), the positioned
|
|
60
|
+
`Fast::SFV::ParseError`, and the vendored httpwg conformance corpus
|
|
61
|
+
(1607 records) refereeing every grammar edge.
|
|
62
|
+
- Scaffold: `Fast::SFV` module and version, the monorepo test harness
|
|
63
|
+
(`config/sus.rb` routing `gems/*/lib` + `gems/*/test`), the gemspec,
|
|
64
|
+
and the packaging machinery (CI build loop, release dispatch map,
|
|
65
|
+
`script/verify-packaged.rb`).
|
|
66
|
+
- Docs: the diátaxis docs tree under `docs/` (tutorial, four how-to
|
|
67
|
+
guides, two reference pages, three explanations), the README
|
|
68
|
+
quickstart with the documentation index, the docs-examples spec
|
|
69
|
+
(`test/fast/sfv/docs.rb`) that executes every fenced example and
|
|
70
|
+
checks every documented output, and the completed gemspec metadata
|
|
71
|
+
(`documentation_uri` pointing at the docs tree).
|
data/README.md
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# fast-sfv
|
|
2
|
+
|
|
3
|
+
fast-sfv is the RFC 9651 Structured Field Values implementation for Ruby: strict parse, canonical emit, frozen value objects, zero runtime dependencies.
|
|
4
|
+
|
|
5
|
+
## What it is
|
|
6
|
+
|
|
7
|
+
Implements RFC 9651 Structured Field Values — Items, Lists, Dictionaries with parameters — the syntax layer the rest of the fast-http-fields family builds on. Every other field gem parses and emits through Structured Field Values.
|
|
8
|
+
|
|
9
|
+
Three parse entry points (`parse_item`/`parse_list`/`parse_dict`) with total `valid_*?` mirrors; three serialize entry points (`serialize_item`/`serialize_list`/`serialize_dict`).
|
|
10
|
+
|
|
11
|
+
Pure Ruby, hand-rolled byte-index scanning, deep-frozen results, no runtime dependencies.
|
|
12
|
+
|
|
13
|
+
MIT license. Source at `https://github.com/jetpks/fast-http-fields`.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
gem install fast-sfv
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
In a Gemfile:
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
gem "fast-sfv"
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Require path is `fast/sfv`:
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
require "fast/sfv"
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Requires Ruby >= 3.4 and RubyGems >= 3.5.3. No other runtime requirement.
|
|
34
|
+
|
|
35
|
+
## Quickstart
|
|
36
|
+
|
|
37
|
+
Parse a dictionary, read it, and emit it back:
|
|
38
|
+
|
|
39
|
+
```ruby
|
|
40
|
+
require "fast/sfv"
|
|
41
|
+
|
|
42
|
+
dict = Fast::SFV.parse_dict("a=1, b;foo=bar, c=(1 2)")
|
|
43
|
+
dict.keys # => ["a", "b", "c"]
|
|
44
|
+
dict["a"].value # => 1
|
|
45
|
+
dict["c"].items.map(&:value) # => [1, 2]
|
|
46
|
+
|
|
47
|
+
Fast::SFV.serialize_dict(dict) # => "a=1, b;foo=bar, c=(1 2)"
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Malformed content raises with a byte offset:
|
|
51
|
+
|
|
52
|
+
```ruby
|
|
53
|
+
require "fast/sfv"
|
|
54
|
+
|
|
55
|
+
begin
|
|
56
|
+
Fast::SFV.parse_item("foo bar")
|
|
57
|
+
rescue Fast::SFV::ParseError => e
|
|
58
|
+
e.offset # => 4
|
|
59
|
+
end
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Next: the [tutorial](docs/tutorials/structured-fields.md) for the guided path.
|
|
63
|
+
|
|
64
|
+
## Strict by design
|
|
65
|
+
|
|
66
|
+
- Field content that is not a Structured Field Value of the asked shape raises `Fast::SFV::ParseError` carrying the byte offset where the parse stopped.
|
|
67
|
+
- An input that is neither a String nor an Array of field lines raises `TypeError` — a caller bug, not field content.
|
|
68
|
+
- Any non-serializable value raises `Fast::SFV::SerializeError` with the byte offset into the output built so far; `TypeError` never appears on the emit path.
|
|
69
|
+
- `valid_item?`, `valid_list?`, `valid_dict?` answer the same question with a boolean and never raise.
|
|
70
|
+
|
|
71
|
+
See [Diagnose a rejected field value](docs/how-to-guides/diagnose-failures.md) and [Why parsing and serialization are strict](docs/explanation/strictness.md).
|
|
72
|
+
|
|
73
|
+
## Documentation
|
|
74
|
+
|
|
75
|
+
Current release 0.1.0. Changelog at [CHANGELOG.md](CHANGELOG.md). The docs tree is also linked from the gemspec metadata `documentation_uri`.
|
|
76
|
+
|
|
77
|
+
### Tutorial
|
|
78
|
+
|
|
79
|
+
- [Your first Structured Field Values](docs/tutorials/structured-fields.md) — Parse an Item, a List, a Dictionary; meet the eight bare item types; emit a value back; meet the errors.
|
|
80
|
+
|
|
81
|
+
### How-to guides
|
|
82
|
+
|
|
83
|
+
- [Parse a field value](docs/how-to-guides/parse-field-values.md) — Choose the entry point, pass one line or many, walk the result, handle the failure.
|
|
84
|
+
- [Serialize a field value](docs/how-to-guides/serialize-field-values.md) — Serialize what you parsed, build values by hand, pick wire types, use short forms, respect the limits.
|
|
85
|
+
- [Validate a field value without exceptions](docs/how-to-guides/validate-field-values.md) — Answer "is this a Structured Field Value?" with `valid_item?`/`valid_list?`/`valid_dict?` and no exceptions.
|
|
86
|
+
- [Diagnose a rejected field value](docs/how-to-guides/diagnose-failures.md) — Read a `ParseError`/`SerializeError` byte offset and turn it into a diagnosis.
|
|
87
|
+
|
|
88
|
+
### Reference
|
|
89
|
+
|
|
90
|
+
- [API reference](docs/reference/api.md) — The complete public surface: every method, class, error, input/output shape, with signatures and return types.
|
|
91
|
+
- [Conformance and the httpwg corpus](docs/reference/conformance.md) — RFC 9651 conformance scope, the grammar coverage map, and the vendored httpwg corpus (provenance, semantics, counts).
|
|
92
|
+
|
|
93
|
+
### Explanation
|
|
94
|
+
|
|
95
|
+
- [Why parsing and serialization are strict](docs/explanation/strictness.md) — Why both directions are strict, and why the failures split into `TypeError`, `ParseError`, and `SerializeError`.
|
|
96
|
+
- [Why the Ruby type is the wire-type tag](docs/explanation/wire-type-tag.md) — Why the Ruby type of a value is its wire-type tag — the `Token < String` design, the ASCII-8BIT byte-sequence tag, and the starry divergence.
|
|
97
|
+
- [Performance and allocation budgets](docs/explanation/performance.md) — How the parser and serializer stay allocation-lean; the benchmark table and allocation budgets, with the measurement methodology and caveats.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Fast
|
|
4
|
+
module SFV
|
|
5
|
+
# The family root: every Fast::SFV failure descends from Error, so a
|
|
6
|
+
# caller can guard the whole surface with one rescue.
|
|
7
|
+
class Error < StandardError; end
|
|
8
|
+
|
|
9
|
+
# The field value is not a Structured Field Value at all: the strict
|
|
10
|
+
# RFC 9651 §4.2 parse could not proceed (§1.1 forbids tolerance, so
|
|
11
|
+
# there is no lenient reading to fall back to). #offset is the byte
|
|
12
|
+
# position in the combined input where the parse stopped, an Integer
|
|
13
|
+
# in 0..input.bytesize; #message carries the reason. A wrong input
|
|
14
|
+
# type is a caller bug, not field content: it raises plain TypeError
|
|
15
|
+
# and never this class.
|
|
16
|
+
class ParseError < Error
|
|
17
|
+
attr_reader :offset
|
|
18
|
+
|
|
19
|
+
def initialize(message, offset)
|
|
20
|
+
super(message)
|
|
21
|
+
@offset = offset
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# The value tree is not serializable as a Structured Field Value: the
|
|
26
|
+
# strict RFC 9651 §4.1 emit could not proceed (a sender never puts
|
|
27
|
+
# garbage on the wire — §1.2 demands behavior indistinguishable from
|
|
28
|
+
# the algorithms, so there is no lenient reading to fall back to).
|
|
29
|
+
# #offset is the byte position in the output built so far, an Integer
|
|
30
|
+
# in 0..output.bytesize; #message carries the reason. The whole emit
|
|
31
|
+
# path raises this class and never TypeError: on emit the wrongness
|
|
32
|
+
# is in the value tree being sent — wrong types, wrong shapes,
|
|
33
|
+
# out-of-range values, invalid key or string content — which is field
|
|
34
|
+
# content, not a caller protocol bug.
|
|
35
|
+
class SerializeError < Error
|
|
36
|
+
attr_reader :offset
|
|
37
|
+
|
|
38
|
+
def initialize(message, offset)
|
|
39
|
+
super(message)
|
|
40
|
+
@offset = offset
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|