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,641 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fast
4
+ module SFV
5
+ # The RFC 9651 §4.2 parse path: one instance per parse, walking the
6
+ # combined field value by byte index — first-byte dispatch into the
7
+ # eight bare-item grammars, character-class tables for the
8
+ # continuation sets, no pattern machinery anywhere. The six entry
9
+ # points at the bottom of this file construct one Parser and read the
10
+ # result off it.
11
+ class Parser
12
+ # Builds a 256-entry byte class: true for every byte of +spec+, so a
13
+ # table lookup answers "is this byte in the class" in one step.
14
+ def self.byte_class(spec)
15
+ table = Array.new(256, false)
16
+ spec.each_byte { |byte| table[byte] = true }
17
+ table.freeze
18
+ end
19
+
20
+ # Builds a 256-entry byte-value table: the index of every byte of
21
+ # +spec+, for the base64 decode walk (the alphabet was validated
22
+ # before any lookup, so the zero filler is never read).
23
+ def self.byte_values(spec)
24
+ table = Array.new(256, 0)
25
+ spec.each_byte.with_index { |byte, index| table[byte] = index }
26
+ table.freeze
27
+ end
28
+
29
+ UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
30
+ LOWER = "abcdefghijklmnopqrstuvwxyz"
31
+ DIGITS = "0123456789"
32
+
33
+ # A key's first byte: lcalpha or "*" (§4.2.3.3 step 1).
34
+ KEY_FIRST = byte_class("#{LOWER}*")
35
+ # A key's continuation: lcalpha, DIGIT, "_", "-", "." or "*" (§4.2.3.3).
36
+ KEY_BODY = byte_class("#{LOWER}#{DIGITS}_-.*")
37
+ # A token's first byte: ALPHA or "*" (§4.2.6 step 1).
38
+ TOKEN_FIRST = byte_class("#{UPPER}#{LOWER}*")
39
+ # A token's continuation: tchar (RFC 9110) plus ":" and "/", the two
40
+ # bytes §4.2.6 adds to the HTTP token set.
41
+ TOKEN_BODY = byte_class("#{UPPER}#{LOWER}#{DIGITS}!#$%&'*+-.^_`|~:/")
42
+ # One DIGIT of an integer or decimal (§4.2.4).
43
+ DIGIT = byte_class(DIGITS)
44
+ # The base64 alphabet including its pad byte (§4.2.7 step 6).
45
+ B64 = byte_class("#{UPPER}#{LOWER}#{DIGITS}+/=")
46
+ # The base64 alphabet's decode values.
47
+ B64_VALUE = byte_values("#{UPPER}#{LOWER}#{DIGITS}+/")
48
+ # A display string percent-escape digit: 0-9 or a-f (§4.2.10).
49
+ HEX = byte_class("#{DIGITS}abcdef")
50
+ # The bytes that may follow an inner list item: SP or ")"
51
+ # (§4.2.1.2 step 3.5).
52
+ INNER_BOUNDARY = byte_class(" )")
53
+ # The two bytes a string escape may carry: DQUOTE and backslash
54
+ # (§4.2.5 step 4.3).
55
+ STRING_ESCAPE = byte_class("\"\\")
56
+ # Optional whitespace, SP or tab: the byte the List and Dictionary
57
+ # separators discard (§4.2.1 step 2.2, §4.2.2 step 2.6).
58
+ OWS = byte_class(" \t")
59
+
60
+ SP = 0x20
61
+ COMMA = 0x2C
62
+ SEMICOLON = 0x3B
63
+ COLON = 0x3A
64
+ LPAREN = 0x28
65
+ RPAREN = 0x29
66
+ DQUOTE = 0x22
67
+ BACKSLASH = 0x5C
68
+ HYPHEN = 0x2D
69
+ PERIOD = 0x2E
70
+ EQUALS = 0x3D
71
+ PERCENT = 0x25
72
+ QUESTION = 0x3F
73
+ AT_SIGN = 0x40
74
+
75
+ # Items and inner lists without parameters share this one Hash —
76
+ # nothing can mutate a frozen Hash, so every #params is frozen and
77
+ # empty either way, and a bare item costs no parameter allocation.
78
+ EMPTY_PARAMS = {}.freeze
79
+ # The byte sequence "::" — shared for the same reason.
80
+ EMPTY_BYTES = "".b.freeze
81
+
82
+ # The input is one String field value, or the field's lines as an
83
+ # Array, combined with ", " (§4.2's combining rule — a Rack join
84
+ # conforms). Anything else is a caller bug: TypeError, not a
85
+ # Fast::SFV::Error.
86
+ def initialize(input)
87
+ @input = field_value(input)
88
+ @pos = 0
89
+ @end = @input.bytesize
90
+ end
91
+
92
+ # §4.2 steps 1-8 with field_type "item": one Item, or a ParseError.
93
+ def item
94
+ check_ascii
95
+ skip_sp
96
+ member = item_member
97
+ skip_sp
98
+ check_end
99
+ member
100
+ end
101
+
102
+ # §4.2 steps 1-8 with field_type "list": a frozen Array of Items and
103
+ # InnerLists. Empty input is the empty list (§4.2.1 step 3).
104
+ def list
105
+ check_ascii
106
+ skip_sp
107
+ members = list_members
108
+ skip_sp
109
+ check_end
110
+ members.freeze
111
+ end
112
+
113
+ # §4.2 steps 1-8 with field_type "dictionary": a frozen, ordered
114
+ # Hash of String keys to Items and InnerLists. Empty input is the
115
+ # empty dictionary (§4.2.2 step 3).
116
+ def dictionary
117
+ check_ascii
118
+ skip_sp
119
+ members = dictionary_members
120
+ skip_sp
121
+ check_end
122
+ members.freeze
123
+ end
124
+
125
+ private
126
+
127
+ # The field value the scanner walks: a String as given, or the lines
128
+ # joined with ", " (each line a String, a subclass included).
129
+ def field_value(input)
130
+ case input
131
+ when String
132
+ input
133
+ when Array
134
+ input.each do |line|
135
+ next if line.is_a?(String)
136
+
137
+ got = defined?(line.class) ? line.class : "a classless object"
138
+ raise TypeError, "an Array of String field lines is required (got #{got})"
139
+ end
140
+ input.join(", ")
141
+ else
142
+ got = defined?(input.class) ? input.class : "a classless object"
143
+ raise TypeError, "a String or an Array of String field lines is required (got #{got})"
144
+ end
145
+ end
146
+
147
+ # §4.2 step 1: the whole field value must be ASCII — a byte above
148
+ # 0x7F fails the parse before any grammar runs. ascii_only? is the
149
+ # one-scan hot path; the walk runs only on failure, to find the
150
+ # offset the error reports.
151
+ def check_ascii
152
+ return if @input.ascii_only?
153
+
154
+ index = 0
155
+ index += 1 while @input.getbyte(index) <= 0x7F
156
+ @pos = index
157
+ fail_parse("the field value must be ASCII")
158
+ end
159
+
160
+ # §4.2 steps 6 and 7: trailing SP is fine, anything else fails.
161
+ def check_end
162
+ fail_parse("unexpected content after the value") unless @pos >= @end
163
+ end
164
+
165
+ # Discards leading SP (§4.2 steps 2 and 6, the *SP of §4.2.3.2 and
166
+ # the 1*SP of §4.2.1.2).
167
+ def skip_sp
168
+ @pos += 1 while @pos < @end && @input.getbyte(@pos) == SP
169
+ end
170
+
171
+ # Discards leading OWS, SP or tab, around the list and dictionary
172
+ # commas.
173
+ def skip_ows
174
+ @pos += 1 while @pos < @end && OWS[@input.getbyte(@pos)]
175
+ end
176
+
177
+ # The byte at @pos, NUL when the input is exhausted: NUL is in no
178
+ # grammar class, so every table lookup and delimiter comparison
179
+ # fails cleanly at end of input instead of raising.
180
+ def byte
181
+ @pos < @end ? @input.getbyte(@pos) : 0
182
+ end
183
+
184
+ # §4.2.1 Parsing a List: members separated by OWS "," OWS; a trailing
185
+ # comma fails (step 2.6), and a member that cannot parse fails the
186
+ # whole list, so an empty member never slips through.
187
+ def list_members
188
+ members = []
189
+ while @pos < @end
190
+ members << item_or_inner_list
191
+ skip_ows
192
+ return members if @pos >= @end
193
+
194
+ separator = @input.getbyte(@pos)
195
+ @pos += 1
196
+ fail_parse("list members are separated by ','") unless separator == COMMA
197
+ skip_ows
198
+ fail_parse("a list may not end with ','") if @pos >= @end
199
+ end
200
+ members
201
+ end
202
+
203
+ # §4.2.2 Parsing a Dictionary: key, then "=" and a member, or the
204
+ # valueless short form (Boolean true with parameters); members
205
+ # separated by OWS "," OWS; a trailing comma fails. A duplicate key
206
+ # overwrites the value at its first position — exactly what a Ruby
207
+ # Hash assignment does.
208
+ def dictionary_members
209
+ dictionary = {}
210
+ while @pos < @end
211
+ key = parse_key
212
+ if byte == EQUALS
213
+ @pos += 1
214
+ dictionary[key] = item_or_inner_list
215
+ else
216
+ dictionary[key] = Item.new(true, parse_parameters)
217
+ end
218
+ skip_ows
219
+ return dictionary if @pos >= @end
220
+
221
+ separator = @input.getbyte(@pos)
222
+ @pos += 1
223
+ fail_parse("dictionary members are separated by ','") unless separator == COMMA
224
+ skip_ows
225
+ fail_parse("a dictionary may not end with ','") if @pos >= @end
226
+ end
227
+ dictionary
228
+ end
229
+
230
+ # §4.2.1.1: "(" opens an inner list; anything else is a single item.
231
+ def item_or_inner_list
232
+ if byte == LPAREN
233
+ inner_list
234
+ else
235
+ item_member
236
+ end
237
+ end
238
+
239
+ # §4.2.1.2 Parsing an Inner List: items separated by 1*SP only, so a
240
+ # tab inside fails; after every item the next byte must be SP or ")";
241
+ # ")" closes the list and carries its parameters. The dispatch has
242
+ # already checked the "(".
243
+ def inner_list
244
+ @pos += 1
245
+ items = []
246
+ while @pos < @end
247
+ skip_sp
248
+ if byte == RPAREN
249
+ @pos += 1
250
+ return InnerList.new(items.freeze, parse_parameters)
251
+ end
252
+
253
+ items << item_member
254
+ boundary = byte
255
+ fail_parse("an inner list item must be followed by SP or ')'") unless INNER_BOUNDARY[boundary]
256
+ end
257
+ fail_parse("an inner list must be closed with ')'")
258
+ end
259
+
260
+ # §4.2.3 Parsing an Item: a bare item, then its parameters.
261
+ def item_member
262
+ Item.new(parse_bare_item, parse_parameters)
263
+ end
264
+
265
+ # §4.2.3.1 Parsing a Bare Item: dispatch on the first byte, in the
266
+ # RFC's own order. An exhausted input lands on NUL, which is in no
267
+ # class, so an empty item fails here.
268
+ def parse_bare_item
269
+ first = byte
270
+ if first == HYPHEN || DIGIT[first]
271
+ parse_number
272
+ elsif first == DQUOTE
273
+ parse_string
274
+ elsif TOKEN_FIRST[first]
275
+ parse_token
276
+ elsif first == COLON
277
+ parse_byte_sequence
278
+ elsif first == QUESTION
279
+ parse_boolean
280
+ elsif first == AT_SIGN
281
+ parse_date
282
+ elsif first == PERCENT
283
+ parse_display_string
284
+ else
285
+ fail_parse("an item must start with a bare item byte")
286
+ end
287
+ end
288
+
289
+ # §4.2.3.2 Parsing Parameters: ";" then *SP then a key, then "=" and
290
+ # a bare item, or the valueless form Boolean true. A duplicate key
291
+ # overwrites the value at its first position; items with no
292
+ # parameters share the one frozen empty Hash.
293
+ def parse_parameters
294
+ params = nil
295
+ while byte == SEMICOLON
296
+ @pos += 1
297
+ skip_sp
298
+ key = parse_key
299
+ value = true
300
+ if byte == EQUALS
301
+ @pos += 1
302
+ value = parse_bare_item
303
+ end
304
+ (params ||= {})[key] = value
305
+ end
306
+ (params || EMPTY_PARAMS).freeze
307
+ end
308
+
309
+ # §4.2.3.3 Parsing a Key: lcalpha or "*" first, then lcalpha, DIGIT,
310
+ # "_", "-", "." or "*". The first advance is covered by the
311
+ # KEY_FIRST check.
312
+ def parse_key
313
+ fail_parse("a key must start with a lowercase letter or '*'") unless KEY_FIRST[byte]
314
+
315
+ start = @pos
316
+ @pos += 1
317
+ @pos += 1 while KEY_BODY[byte]
318
+ @input.byteslice(start, @pos - start).force_encoding(Encoding::UTF_8).freeze
319
+ end
320
+
321
+ # §4.2.4 Parsing an Integer or Decimal: an optional "-", then DIGITs
322
+ # with at most one "." — 12 integer digits and 3 fraction digits when
323
+ # decimal, 15 digits when integer, 16 characters in all. The scan
324
+ # walks the digits; #number_magnitude applies the limits and converts.
325
+ def parse_number
326
+ negative = byte == HYPHEN
327
+ @pos += 1 if negative
328
+ fail_parse("a number must start with a digit") unless DIGIT[byte]
329
+
330
+ start = @pos
331
+ period = nil
332
+ while @pos < @end
333
+ current = @input.getbyte(@pos)
334
+ if DIGIT[current]
335
+ @pos += 1
336
+ elsif period.nil? && current == PERIOD
337
+ fail_parse("a decimal carries at most 12 integer digits") if @pos - start > 12
338
+
339
+ period = @pos
340
+ @pos += 1
341
+ else
342
+ break
343
+ end
344
+ end
345
+
346
+ magnitude = number_magnitude(start, period)
347
+ negative ? -magnitude : magnitude
348
+ end
349
+
350
+ # The magnitude the scanned digits spell, limits applied: a Float
351
+ # when a period was walked (to_f on the whole literal), the
352
+ # in-place Integer otherwise.
353
+ def number_magnitude(start, period)
354
+ if period
355
+ fail_parse("a decimal is at most 16 characters") if @pos - start > 16
356
+ fail_parse("a decimal must end with a fraction digit") if @pos - period == 1
357
+ fail_parse("a decimal carries at most 3 fraction digits") if @pos - period > 4
358
+
359
+ @input.byteslice(start, @pos - start).to_f
360
+ else
361
+ fail_parse("an integer is at most 15 digits") if @pos - start > 15
362
+
363
+ integer_from(start)
364
+ end
365
+ end
366
+
367
+ # The integer the DIGIT bytes from +start+ to @pos spell, accumulated
368
+ # in place: no intermediate String, and a 15-digit value stays inside
369
+ # the immediate Integers.
370
+ def integer_from(start)
371
+ value = 0
372
+ while start < @pos
373
+ value = value * 10 + (@input.getbyte(start) - 0x30)
374
+ start += 1
375
+ end
376
+ value
377
+ end
378
+
379
+ # §4.2.5 Parsing a String: DQUOTE-delimited; a backslash escapes only
380
+ # DQUOTE and backslash; every other byte must be SP or VCHAR. The
381
+ # content is one contiguous byte range until the first escape, so the
382
+ # common case is a single slice; from the first escape on, every
383
+ # later byte joins the buffer.
384
+ def parse_string
385
+ @pos += 1
386
+ start = @pos
387
+ content = nil
388
+ while @pos < @end
389
+ current = @input.getbyte(@pos)
390
+ if current == BACKSLASH
391
+ content ||= @input.byteslice(start, @pos - start)
392
+ @pos += 1
393
+ fail_parse("a string escape must be followed by a character") if @pos >= @end
394
+
395
+ escaped = @input.getbyte(@pos)
396
+ fail_parse("only '\"' and '\\' may be escaped in a string") unless STRING_ESCAPE[escaped]
397
+
398
+ content << escaped
399
+ @pos += 1
400
+ elsif current == DQUOTE
401
+ text = content || @input.byteslice(start, @pos - start)
402
+ @pos += 1
403
+ return text.force_encoding(Encoding::UTF_8).freeze
404
+ elsif current < SP || current > 0x7E
405
+ fail_parse("a string may only carry SP and printable ASCII")
406
+ else
407
+ content << current if content
408
+ @pos += 1
409
+ end
410
+ end
411
+ fail_parse("a string must be closed with '\"'")
412
+ end
413
+
414
+ # §4.2.6 Parsing a Token: ALPHA or "*" first, then tchar, ":" or "/" —
415
+ # a Token on the Ruby side: the class is the wire-type tag, and the
416
+ # constructor re-validates the walked bytes at the boundary.
417
+ def parse_token
418
+ start = @pos
419
+ @pos += 1
420
+ @pos += 1 while TOKEN_BODY[byte]
421
+ Token.new(@input.byteslice(start, @pos - start))
422
+ end
423
+
424
+ # §4.2.7 Parsing a Byte Sequence: ":" ... ":" over the base64
425
+ # alphabet, "=" only as the trailing pad. The decode synthesizes
426
+ # padding, so unpadded, over-padded and dirty-padded values all
427
+ # decode; a character outside the alphabet, a misplaced pad or a
428
+ # lone trailing character fails.
429
+ def parse_byte_sequence
430
+ @pos += 1
431
+ start = @pos
432
+ close = start
433
+ close += 1 while close < @end && @input.getbyte(close) != COLON
434
+ fail_parse("a byte sequence must be closed with ':'") if close >= @end
435
+
436
+ # The data characters run to the first "=", and everything after it
437
+ # must be that trailing "=" run and nothing else.
438
+ data_end = start
439
+ data_end += 1 while data_end < close && @input.getbyte(data_end) != EQUALS
440
+ validate_base64(start, data_end, close)
441
+
442
+ @pos = close + 1
443
+ decode_bytes(start, data_end)
444
+ end
445
+
446
+ # The characters in start...data_end must be base64, the rest of
447
+ # the run up to +close+ the trailing "=" pad and nothing else.
448
+ def validate_base64(start, data_end, close)
449
+ index = start
450
+ while index < data_end
451
+ fail_parse("a byte sequence may only carry base64 characters") unless B64[@input.getbyte(index)]
452
+
453
+ index += 1
454
+ end
455
+ while index < close
456
+ fail_parse("base64 '=' may only pad the end of the content") unless @input.getbyte(index) == EQUALS
457
+
458
+ index += 1
459
+ end
460
+ end
461
+
462
+ # §4.2.7 step 7: the base64 decode of the data characters with the
463
+ # padding synthesized — four characters to three bytes, a trailing
464
+ # pair to one, a trailing triple to two.
465
+ def decode_bytes(start, data_end)
466
+ length = data_end - start
467
+ return EMPTY_BYTES if length.zero?
468
+
469
+ bytes = "".b
470
+ index = start
471
+ remaining = length
472
+ while remaining >= 4
473
+ value = group_value(index, 4)
474
+ bytes << (value >> 16) << ((value >> 8) & 0xFF) << (value & 0xFF)
475
+ index += 4
476
+ remaining -= 4
477
+ end
478
+
479
+ case remaining
480
+ when 2
481
+ bytes << (group_value(index, 2) >> 16)
482
+ when 3
483
+ value = group_value(index, 3)
484
+ bytes << (value >> 16) << ((value >> 8) & 0xFF)
485
+ when 1
486
+ fail_parse("a byte sequence may not end with a single base64 character")
487
+ end
488
+ bytes.freeze
489
+ end
490
+
491
+ # The 24-bit value of the +count+ base64 characters at +index+ —
492
+ # four characters, or the trailing pair or triple with the
493
+ # missing low places zero.
494
+ def group_value(index, count)
495
+ value = B64_VALUE[@input.getbyte(index)] << 18
496
+ value |= B64_VALUE[@input.getbyte(index + 1)] << 12 if count > 1
497
+ value |= B64_VALUE[@input.getbyte(index + 2)] << 6 if count > 2
498
+ value |= B64_VALUE[@input.getbyte(index + 3)] if count > 3
499
+ value
500
+ end
501
+
502
+ # §4.2.8 Parsing a Boolean: exactly "?1" or "?0". The dispatch has
503
+ # already consumed the "?".
504
+ def parse_boolean
505
+ @pos += 1
506
+ fail_parse("a boolean is '?' followed by '1' or '0'") if @pos >= @end
507
+
508
+ value = @input.getbyte(@pos)
509
+ @pos += 1
510
+ return true if value == 0x31
511
+ return false if value == 0x30
512
+
513
+ fail_parse("a boolean is '?' followed by '1' or '0'")
514
+ end
515
+
516
+ # §4.2.9 Parsing a Date: "@" then an Integer — a Decimal fails. The
517
+ # dispatch has already consumed the "@".
518
+ def parse_date
519
+ @pos += 1
520
+ value = parse_number
521
+ fail_parse("a date is '@' followed by an integer, not a decimal") if value.is_a?(Float)
522
+
523
+ Date.new(value)
524
+ end
525
+
526
+ # §4.2.10 Parsing a Display String: `%"` ... `"`, literal bytes SP or
527
+ # VCHAR, "%" always exactly two lowercase hex digits; the collected
528
+ # bytes must decode as UTF-8 at the closing quote. The dispatch has
529
+ # already consumed the "%".
530
+ def parse_display_string
531
+ @pos += 1
532
+ fail_parse("a display string must open with '%\"'") unless byte == DQUOTE
533
+
534
+ @pos += 1
535
+ bytes = display_string_bytes
536
+ text = bytes.force_encoding(Encoding::UTF_8)
537
+ fail_parse("a display string must be valid UTF-8") unless text.valid_encoding?
538
+
539
+ DisplayString.new(text.freeze)
540
+ end
541
+
542
+ # The raw bytes between the quotes, percent-escapes decoded — the
543
+ # closing DQUOTE consumed; every other failure raises on the walk.
544
+ def display_string_bytes
545
+ bytes = "".b
546
+ while @pos < @end
547
+ current = @input.getbyte(@pos)
548
+ fail_parse("a display string may only carry SP and printable ASCII") if current < SP || current > 0x7E
549
+
550
+ if current == PERCENT
551
+ @pos += 1
552
+ fail_parse("a display string escape needs two hex digits") if @pos + 1 >= @end
553
+
554
+ high = @input.getbyte(@pos)
555
+ low = @input.getbyte(@pos + 1)
556
+ fail_parse("a display string escape needs lowercase hex digits") unless HEX[high] && HEX[low]
557
+
558
+ bytes << (hex_value(high) * 16 + hex_value(low))
559
+ @pos += 2
560
+ elsif current == DQUOTE
561
+ @pos += 1
562
+ return bytes
563
+ else
564
+ bytes << current
565
+ @pos += 1
566
+ end
567
+ end
568
+ fail_parse("a display string must be closed with '\"'")
569
+ end
570
+
571
+ # The value of one lowercase hex digit, already validated by HEX.
572
+ def hex_value(byte)
573
+ byte <= 0x39 ? byte - 0x30 : byte - 0x61 + 10
574
+ end
575
+
576
+ # Raises the positioned ParseError: the reason, and the byte offset
577
+ # into the combined input where the parse stopped.
578
+ def fail_parse(reason)
579
+ raise ParseError.new("#{reason} at offset #{@pos}", @pos)
580
+ end
581
+ end
582
+
583
+ private_constant :Parser
584
+
585
+ module_function
586
+
587
+ # Parses one RFC 9651 Item (§4.2, field_type "item") — a bare item
588
+ # with parameters, as a frozen Fast::SFV::Item.
589
+ #
590
+ # Strict: malformed field content raises ParseError; a non-String,
591
+ # non-Array input raises TypeError.
592
+ def parse_item(input)
593
+ Parser.new(input).item
594
+ end
595
+
596
+ # Parses an RFC 9651 List (§4.2, field_type "list") — a frozen Array
597
+ # of Items and InnerLists. Empty input parses to the empty list.
598
+ #
599
+ # Strict: malformed field content raises ParseError; a non-String,
600
+ # non-Array input raises TypeError.
601
+ def parse_list(input)
602
+ Parser.new(input).list
603
+ end
604
+
605
+ # Parses an RFC 9651 Dictionary (§4.2, field_type "dictionary") — a
606
+ # frozen, ordered Hash of String keys to Items and InnerLists. Empty
607
+ # input parses to the empty dictionary.
608
+ #
609
+ # Strict: malformed field content raises ParseError; a non-String,
610
+ # non-Array input raises TypeError.
611
+ def parse_dict(input)
612
+ Parser.new(input).dictionary
613
+ end
614
+
615
+ # The total predicate mirrors: the strict parse with a boolean answer
616
+ # that never raises — false for malformed content and for a wrong
617
+ # input type alike (a caller bug is not field content either).
618
+ def valid_item?(input)
619
+ parse_item(input)
620
+ true
621
+ rescue TypeError, ParseError
622
+ false
623
+ end
624
+
625
+ # See valid_item?.
626
+ def valid_list?(input)
627
+ parse_list(input)
628
+ true
629
+ rescue TypeError, ParseError
630
+ false
631
+ end
632
+
633
+ # See valid_item?.
634
+ def valid_dict?(input)
635
+ parse_dict(input)
636
+ true
637
+ rescue TypeError, ParseError
638
+ false
639
+ end
640
+ end
641
+ end