lib/preserves/src/error.zig
daab053ee43316e1809a84551d573ddd1e5bf3d2
1 //! The error set of the package's text parser, one tag per kind of bad input. A caller that reports
2 //! a bad document needs to know which kind of input the parser stopped at. `parse` alone returns
3 //! it, and `text.decode` and the JSON decoder return their own error sets.
4 /// The errors `parse` returns: one tag per kind of malformed text, plus running out of nesting
5 /// depth or memory. Code that calls `parse` switches on these tags, for a report of the input the
6 /// parser rejected. The set lists every error `parse` can return, so a switch over it is
7 /// exhaustive. On any error `parse` frees everything it allocated. `json.ParseError` names the same
8 /// set, and no JSON function returns it.
9 pub const ParseError = error{
10 /// Input that ends before a value starts: empty or blank text, text that ends right after `#`
11 /// or `#x`, or a record, dictionary or annotation cut off before its next value.
12 UnexpectedEnd,
13 /// A character such as `>`, `:` or `;` at the start of a value, or a dictionary key followed by
14 /// something other than `:`. `#xd` followed by something other than `"`.
15 UnexpectedChar,
16 /// A string or quoted symbol that reaches the end of the input before its closing quote.
17 UnterminatedString,
18 /// A record that reaches the end of the input before its closing `>`.
19 UnterminatedRecord,
20 /// A sequence that reaches the end of the input before its closing `]`.
21 UnterminatedSequence,
22 /// A dictionary that reaches the end of the input before its closing `}`.
23 UnterminatedDictionary,
24 /// A set, written `#{`, that reaches the end of the input before its closing `}`.
25 UnterminatedSet,
26 /// Input that ends right after a backslash in a string, a quoted symbol or a `#"` byte string.
27 UnterminatedEscape,
28 /// A byte string written `#"`, `#x"` or `#[` that reaches the end of the input before its
29 /// closing `"` or `]`.
30 UnterminatedByteString,
31 /// A double written `#xd"` that reaches the end of the input before its closing `"`.
32 UnterminatedHexDouble,
33 /// In a string or quoted symbol, a backslash followed by a character other than `"`, `\`, `/`,
34 /// `'`, `n`, `r`, `t`, `b`, `f` or `u`. In a `#"` byte string, a backslash followed by a
35 /// character other than `x`, `\`, `"`, `n`, `r` or `t`. The text writer writes `\'` inside
36 /// quoted symbols, so `parse` accepts that output.
37 UnknownEscape,
38 /// A string, quoted symbol, or bare symbol has invalid UTF-8.
39 InvalidUtf8,
40 /// A `#` followed by a character other than `t`, `f`, `{`, `:`, `"`, `x`, `[`, space or tab.
41 /// `#x` followed by a character other than `"` or `d`.
42 UnknownHashForm,
43 /// A double written `#xd"…"` whose digits, ignoring whitespace, are not exactly 16 hexadecimal
44 /// digits.
45 InvalidHexDouble,
46 /// Anything other than whitespace, commas and comments after the first complete value.
47 TrailingContent,
48 /// In a `#"` byte string, `\x` not followed by two hexadecimal digits. A `#x"…"` byte string
49 /// with an odd number of digits or a character other than a hexadecimal digit. Also a `#[…]`
50 /// byte string with invalid base64.
51 InvalidHexEscape,
52 /// In a string or quoted symbol, `\u` not followed by four hexadecimal digits, or naming a lone
53 /// surrogate, which UTF-8 leaves unencoded.
54 InvalidUnicodeEscape,
55 /// A set, written `#{…}`, that holds two equal elements.
56 DuplicateSetElement,
57 /// A dictionary that holds two equal keys.
58 DuplicateDictionaryKey,
59 /// Values nested more than 256 levels deep. Each record, sequence, dictionary, set, annotation
60 /// and `#:` embedded value adds a level. `text.decode` returns the same tag at the same depth.
61 NestingLimitExceeded,
62 /// An allocation that fails while `parse` builds the value.
63 OutOfMemory,
64 };
65
66 test "ParseError set is reachable" {
67 const std = @import("std");
68 var err: ?ParseError = null;
69 err = ParseError.UnexpectedEnd;
70 try std.testing.expect(err.? == ParseError.UnexpectedEnd);
71 }