ron_schema/error.rs
1/*************************
2 * Author: Bradley Hunter
3 */
4
5use crate::span::Span;
6
7/// Specific kinds of errors that can occur when parsing a `.ronschema` file.
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum SchemaErrorKind {
10 /// A type name was not recognized (e.g., `"Intgeer"` instead of `"Integer"`).
11 UnknownType {
12 /// The unrecognized type name.
13 name: String,
14 /// A suggested correction, if one is close enough.
15 suggestion: Option<String>,
16 },
17 /// A type references an enum that is never defined in the schema.
18 UnresolvedEnum {
19 /// The referenced enum name.
20 name: String,
21 },
22 /// The same enum name is defined more than once.
23 DuplicateEnum {
24 /// The duplicated enum name.
25 name: String,
26 },
27 /// The same variant appears more than once within an enum definition.
28 DuplicateVariant {
29 /// The enum containing the duplicate.
30 enum_name: String,
31 /// The duplicated variant name.
32 variant: String,
33 },
34 /// The same field name appears more than once within a struct definition.
35 DuplicateField {
36 /// The duplicated field name.
37 field_name: String,
38 },
39 /// The same type alias name is defined more than once.
40 DuplicateAlias {
41 /// The duplicated alias name.
42 name: String,
43 },
44 /// A type alias references itself, directly or indirectly.
45 RecursiveAlias {
46 /// The alias name involved in the cycle.
47 name: String,
48 },
49 /// A `PascalCase` name could not be resolved to an enum or type alias.
50 UnresolvedType {
51 /// The unresolved type name.
52 name: String,
53 },
54 /// A syntax error — the parser encountered a token it did not expect.
55 UnexpectedToken {
56 /// What the parser expected at this position.
57 expected: String,
58 /// What was actually found.
59 found: String,
60 },
61}
62
63/// Specific kinds of errors that can occur when parsing a `.ron` data file.
64#[derive(Debug, Clone, PartialEq, Eq)]
65pub enum RonErrorKind {
66 /// A syntax error — the parser encountered a token it did not expect.
67 UnexpectedToken {
68 /// What the parser expected at this position.
69 expected: String,
70 /// What was actually found.
71 found: String,
72 },
73 /// A string literal is missing its closing quote.
74 UnterminatedString,
75 /// The same field name appears more than once within a struct.
76 DuplicateField {
77 /// The duplicated field name.
78 field_name: String,
79 },
80 /// A numeric literal could not be parsed as a valid number.
81 InvalidNumber {
82 /// The text that failed to parse.
83 text: String,
84 },
85}
86
87/// Specific kinds of validation errors produced when RON data does not match a schema.
88#[derive(Debug, Clone, PartialEq, Eq)]
89pub enum ErrorKind {
90 /// A required field defined in the schema is absent from the data.
91 MissingField {
92 /// The name of the missing field.
93 field_name: String,
94 },
95 /// The data contains a field not defined in the schema.
96 UnknownField {
97 /// The name of the unrecognized field.
98 field_name: String,
99 },
100 /// A value exists but has the wrong type.
101 TypeMismatch {
102 /// The type expected by the schema.
103 expected: String,
104 /// A description of what was actually found.
105 found: String,
106 },
107 /// A bare identifier does not match any variant of the expected enum.
108 InvalidEnumVariant {
109 /// The enum the identifier was validated against.
110 enum_name: String,
111 /// The identifier that was found.
112 variant: String,
113 /// All valid variants for this enum.
114 valid: Vec<String>,
115 },
116 /// The value inside `Some(...)` has the wrong type.
117 InvalidOptionValue {
118 /// The type expected by the schema.
119 expected: String,
120 /// A description of what was actually found.
121 found: String,
122 },
123 /// A list element has the wrong type.
124 InvalidListElement {
125 /// The 0-based index of the offending element.
126 index: usize,
127 /// The type expected by the schema.
128 expected: String,
129 /// A description of what was actually found.
130 found: String,
131 },
132 /// Expected a struct `(...)` but found a non-struct value.
133 ExpectedStruct {
134 /// A description of what was actually found.
135 found: String,
136 },
137 /// Expected a list `[...]` but found a non-list value.
138 ExpectedList {
139 /// A description of what was actually found.
140 found: String,
141 },
142 /// Expected `Some(...)` or `None` but found something else.
143 ExpectedOption {
144 /// A description of what was actually found.
145 found: String,
146 },
147}
148
149/// An error produced when parsing a `.ronschema` file fails.
150#[derive(Debug, Clone, PartialEq, Eq)]
151pub struct SchemaParseError {
152 /// Source location where the error occurred.
153 pub span: Span,
154 /// What went wrong.
155 pub kind: SchemaErrorKind,
156}
157
158/// An error produced when parsing a `.ron` data file fails.
159#[derive(Debug, Clone, PartialEq, Eq)]
160pub struct RonParseError {
161 /// Source location where the error occurred.
162 pub span: Span,
163 /// What went wrong.
164 pub kind: RonErrorKind,
165}
166
167/// An error produced when RON data does not conform to a schema.
168#[derive(Debug, Clone, PartialEq, Eq)]
169pub struct ValidationError {
170 /// Dot/bracket field path to the problematic value (e.g., `"cost.generic"`, `"card_types[0]"`).
171 pub path: String,
172 /// Source location of the problematic value in the data file.
173 pub span: Span,
174 /// What went wrong.
175 pub kind: ErrorKind,
176}