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.
@@ -0,0 +1,134 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fast
4
+ module SFV
5
+ # A parsed Item (RFC 9651 §3.3): one bare item plus its parameters.
6
+ # #value is one of the eight bare types — Integer, Float, a frozen
7
+ # UTF-8 String, a Token, a frozen ASCII-8BIT String, true, false, a
8
+ # Date or a DisplayString; #params is the ordered, frozen, String-keyed
9
+ # Hash the parser filled (empty and shared when there were none). The
10
+ # instance is frozen at construction: nothing reachable from a parse
11
+ # result can be mutated.
12
+ class Item
13
+ attr_reader :value, :params
14
+
15
+ def initialize(value, params)
16
+ @value = value
17
+ @params = params
18
+ freeze
19
+ end
20
+
21
+ # The canonical wire form (RFC 9651 §4.1.3) — exactly what
22
+ # Fast::SFV.serialize_item answers: the bare item, then its
23
+ # parameters. Strict: a hand-built non-serializable value raises
24
+ # SerializeError; a parse result never does, since every corpus
25
+ # value round-trips. Computed on demand, so construction stays
26
+ # as cheap as the parse path's frozen budgets require.
27
+ def to_s
28
+ Serializer.item_string(self)
29
+ end
30
+ end
31
+
32
+ # A parsed Inner List (RFC 9651 §3.1.1): a frozen Array of Items plus
33
+ # the parameters that belong to the list itself — the member Items
34
+ # carry their own.
35
+ class InnerList
36
+ attr_reader :items, :params
37
+
38
+ def initialize(items, params)
39
+ @items = items
40
+ @params = params
41
+ freeze
42
+ end
43
+
44
+ # The canonical wire form (RFC 9651 §4.1.1.1) — "(" and ")" around
45
+ # the items joined by single SPs, the list's own parameters after
46
+ # the close; "()" for the empty list. The same strictness as
47
+ # Item#to_s.
48
+ def to_s
49
+ Serializer.inner_list_string(self)
50
+ end
51
+ end
52
+
53
+ # A parsed Date (RFC 9651 §3.3.7): the Integer delta-seconds from
54
+ # 1970-01-01T00:00:00Z, which rides in #value — Integer cannot be
55
+ # subclassed, so the wrapper is the type tag.
56
+ class Date
57
+ attr_reader :value
58
+
59
+ def initialize(value)
60
+ @value = value
61
+ freeze
62
+ end
63
+
64
+ # The canonical wire form (RFC 9651 §4.1.10): "@" + the integer.
65
+ def to_s
66
+ Serializer.date_string(self)
67
+ end
68
+ end
69
+
70
+ # A parsed Display String (RFC 9651 §3.3.8): a frozen UTF-8 String that
71
+ # may carry non-ASCII content, percent-decoded by the parser.
72
+ class DisplayString
73
+ attr_reader :value
74
+
75
+ def initialize(value)
76
+ @value = value
77
+ freeze
78
+ end
79
+
80
+ # The canonical wire form (RFC 9651 §4.1.11): `"` delimited, "%",
81
+ # DQUOTE and the control/high bytes as "%" + two lowercase hex
82
+ # digits, the ASCII bytes literal.
83
+ def to_s
84
+ Serializer.display_string_string(self)
85
+ end
86
+ end
87
+
88
+ # A Token (RFC 9651 §3.3.4): a frozen String subclass whose class is
89
+ # the wire-type tag. The content is String semantics, inherited
90
+ # untouched — #==, #eql?, #hash and #to_s behave exactly as a plain
91
+ # String's, so a Token compares equal to the String that spells it
92
+ # in both directions and serves as a Hash key by content — while
93
+ # Token-ness is what the parse and emit paths dispatch on: a quoted
94
+ # sf-string is a plain String, a token is a Token, and the content
95
+ # never decides which. Construction is the boundary: the content
96
+ # must spell a whole token — ALPHA or "*" first, then tchar, ":" or
97
+ # "/" (§4.2.6's own byte classes) — or ArgumentError; a non-String
98
+ # raises TypeError, Ruby's own conversion error. No length cap
99
+ # (§3.3.4 requires at least 512 characters) and no intern cache: a
100
+ # repeated value builds its own Token, measured as one object beside
101
+ # the content slice it is built from.
102
+ class Token < String
103
+ # Validates the token grammar on the content bytes — the parse
104
+ # path's own classes, read lazily because types.rb loads before
105
+ # parser.rb — then freezes: the only Token is a whole, valid,
106
+ # immutable one.
107
+ def initialize(content)
108
+ super(content)
109
+ size = bytesize
110
+ raise ArgumentError, "a token must not be empty" if size.zero?
111
+ unless Parser::TOKEN_FIRST[getbyte(0)]
112
+ raise ArgumentError, "a token must start with ALPHA or '*' (got #{inspect})"
113
+ end
114
+
115
+ index = 1
116
+ while index < size
117
+ unless Parser::TOKEN_BODY[getbyte(index)]
118
+ raise ArgumentError, "a token may only carry tchar, ':' and '/' (got #{inspect})"
119
+ end
120
+
121
+ index += 1
122
+ end
123
+ freeze
124
+ end
125
+
126
+ # The default String-subclass form is indistinguishable from a
127
+ # plain String's inspect: the class name and the content together
128
+ # say what the value is.
129
+ def inspect
130
+ "#<#{self.class.name} #{super}>"
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fast
4
+ module SFV
5
+ VERSION = "0.1.1"
6
+ end
7
+ end
data/lib/fast/sfv.rb ADDED
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "sfv/version"
4
+ require_relative "sfv/errors"
5
+ require_relative "sfv/types"
6
+ require_relative "sfv/parser"
7
+ require_relative "sfv/serializer"
8
+
9
+ module Fast
10
+ # RFC 9651 Structured Field Values — the syntax substrate of the
11
+ # fast-http-fields family: every other field gem parses and emits
12
+ # through SFV.
13
+ #
14
+ # The parse side landed first. The three typed entry points —
15
+ # parse_item, parse_list, parse_dict — are strict (RFC 9651 §1.1
16
+ # forbids tolerance): field content that is not a Structured Field
17
+ # Value of the asked shape raises Fast::SFV::ParseError, carrying the
18
+ # byte offset where the parse stopped; an input that is neither a
19
+ # String nor an Array of field lines raises TypeError instead, because
20
+ # a wrong type is a caller bug, not field content. The valid_item?,
21
+ # valid_list? and valid_dict? mirrors answer the same question with a
22
+ # boolean and never raise.
23
+ #
24
+ # A String input is one field value; an Array input is the field's
25
+ # lines, combined with ", " (§4.2's combining rule). Results are
26
+ # deep-frozen: the Items and InnerLists, the list Array, the ordered
27
+ # dictionary and parameter Hashes with their String keys, and every
28
+ # String the parse produced. Tokens arrive as Fast::SFV::Token
29
+ # values — frozen String subclasses whose class is the wire-type tag
30
+ # — byte sequences as frozen ASCII-8BIT Strings, dates and display
31
+ # strings wrapped in Fast::SFV::Date and Fast::SFV::DisplayString.
32
+ #
33
+ # The emit side takes those shapes back to the wire:
34
+ # serialize_item, serialize_list and serialize_dict produce the
35
+ # canonical §4.1 form as a fresh US-ASCII String the caller owns, or
36
+ # nil for an empty List or Dictionary (§4.1 step 1: omit the field
37
+ # entirely). The Ruby type of a value is its wire-type tag — an
38
+ # Integer is an integer, a Float a decimal, a Fast::SFV::Token a
39
+ # token, a String an sf-string unless it is tagged ASCII-8BIT and so a
40
+ # byte sequence; the content never decides, and the Token class is
41
+ # read before String in the dispatch because a Token is a String.
42
+ # Strict: any non-serializable input raises Fast::SFV::SerializeError
43
+ # with the byte offset into the output built so far — there is no
44
+ # coercion, so TypeError never appears on the emit path. Item#to_s,
45
+ # InnerList#to_s, Date#to_s and DisplayString#to_s are the same emit,
46
+ # and serialize_item answers exactly item.to_s.
47
+ module SFV
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fast-sfv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Eric Jacobs
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: |
13
+ Fast::SFV implements RFC 9651 Structured Field Values — Items, Lists,
14
+ and Dictionaries with parameters — the syntax layer every other
15
+ fast-http-fields gem builds on. Strict parse, canonical emit, frozen
16
+ value objects, byte-index scanning, zero runtime dependencies.
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - CHANGELOG.md
22
+ - README.md
23
+ - lib/fast/sfv.rb
24
+ - lib/fast/sfv/errors.rb
25
+ - lib/fast/sfv/parser.rb
26
+ - lib/fast/sfv/serializer.rb
27
+ - lib/fast/sfv/types.rb
28
+ - lib/fast/sfv/version.rb
29
+ homepage: https://github.com/jetpks/fast-http-fields
30
+ licenses:
31
+ - MIT
32
+ metadata:
33
+ homepage_uri: https://github.com/jetpks/fast-http-fields
34
+ source_code_uri: https://github.com/jetpks/fast-http-fields
35
+ changelog_uri: https://github.com/jetpks/fast-http-fields/blob/main/gems/sfv/CHANGELOG.md
36
+ documentation_uri: https://github.com/jetpks/fast-http-fields/tree/main/gems/sfv/docs
37
+ rubygems_mfa_required: 'true'
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '3.4'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 3.5.3
51
+ requirements: []
52
+ rubygems_version: 4.0.20
53
+ specification_version: 4
54
+ summary: 'RFC 9651 Structured Field Values for Ruby: strict parse, canonical emit.'
55
+ test_files: []