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 map key type is not valid (must be `String`, `Integer`, or an enum type).
55 InvalidMapKeyType {
56 /// A description of the invalid key type.
57 found: String,
58 },
59 /// A syntax error — the parser encountered a token it did not expect.
60 UnexpectedToken {
61 /// What the parser expected at this position.
62 expected: String,
63 /// What was actually found.
64 found: String,
65 },
66}
67
68/// Specific kinds of errors that can occur when parsing a `.ron` data file.
69#[derive(Debug, Clone, PartialEq, Eq)]
70pub enum RonErrorKind {
71 /// A syntax error — the parser encountered a token it did not expect.
72 UnexpectedToken {
73 /// What the parser expected at this position.
74 expected: String,
75 /// What was actually found.
76 found: String,
77 },
78 /// A string literal is missing its closing quote.
79 UnterminatedString,
80 /// The same field name appears more than once within a struct.
81 DuplicateField {
82 /// The duplicated field name.
83 field_name: String,
84 },
85 /// A numeric literal could not be parsed as a valid number.
86 InvalidNumber {
87 /// The text that failed to parse.
88 text: String,
89 },
90}
91
92/// Specific kinds of validation errors produced when RON data does not match a schema.
93#[derive(Debug, Clone, PartialEq, Eq)]
94pub enum ErrorKind {
95 /// A required field defined in the schema is absent from the data.
96 MissingField {
97 /// The name of the missing field.
98 field_name: String,
99 },
100 /// The data contains a field not defined in the schema.
101 UnknownField {
102 /// The name of the unrecognized field.
103 field_name: String,
104 },
105 /// A value exists but has the wrong type.
106 TypeMismatch {
107 /// The type expected by the schema.
108 expected: String,
109 /// A description of what was actually found.
110 found: String,
111 },
112 /// A bare identifier does not match any variant of the expected enum.
113 InvalidEnumVariant {
114 /// The enum the identifier was validated against.
115 enum_name: String,
116 /// The identifier that was found.
117 variant: String,
118 /// All valid variants for this enum.
119 valid: Vec<String>,
120 },
121 /// The value inside `Some(...)` has the wrong type.
122 InvalidOptionValue {
123 /// The type expected by the schema.
124 expected: String,
125 /// A description of what was actually found.
126 found: String,
127 },
128 /// A list element has the wrong type.
129 InvalidListElement {
130 /// The 0-based index of the offending element.
131 index: usize,
132 /// The type expected by the schema.
133 expected: String,
134 /// A description of what was actually found.
135 found: String,
136 },
137 /// Expected a struct `(...)` but found a non-struct value.
138 ExpectedStruct {
139 /// A description of what was actually found.
140 found: String,
141 },
142 /// Expected a list `[...]` but found a non-list value.
143 ExpectedList {
144 /// A description of what was actually found.
145 found: String,
146 },
147 /// Expected `Some(...)` or `None` but found something else.
148 ExpectedOption {
149 /// A description of what was actually found.
150 found: String,
151 },
152 /// Expected a map `{ ... }` but found a non-map value.
153 ExpectedMap {
154 /// A description of what was actually found.
155 found: String,
156 },
157 /// A map key has the wrong type.
158 InvalidMapKey {
159 /// A string representation of the key.
160 key: String,
161 /// The type expected by the schema.
162 expected: String,
163 /// A description of what was actually found.
164 found: String,
165 },
166 /// A map value has the wrong type.
167 InvalidMapValue {
168 /// A string representation of the key this value belongs to.
169 key: String,
170 /// The type expected by the schema.
171 expected: String,
172 /// A description of what was actually found.
173 found: String,
174 },
175 /// Expected a tuple `(...)` but found a non-tuple value.
176 ExpectedTuple {
177 /// A description of what was actually found.
178 found: String,
179 },
180 /// A tuple has the wrong number of elements.
181 TupleLengthMismatch {
182 /// The number of elements expected by the schema.
183 expected: usize,
184 /// The number of elements found in the data.
185 found: usize,
186 },
187 /// A tuple element has the wrong type.
188 InvalidTupleElement {
189 /// The 0-based index of the offending element.
190 index: usize,
191 /// The type expected by the schema.
192 expected: String,
193 /// A description of what was actually found.
194 found: String,
195 },
196}
197
198/// An error produced when parsing a `.ronschema` file fails.
199#[derive(Debug, Clone, PartialEq, Eq)]
200pub struct SchemaParseError {
201 /// Source location where the error occurred.
202 pub span: Span,
203 /// What went wrong.
204 pub kind: SchemaErrorKind,
205}
206
207/// An error produced when parsing a `.ron` data file fails.
208#[derive(Debug, Clone, PartialEq, Eq)]
209pub struct RonParseError {
210 /// Source location where the error occurred.
211 pub span: Span,
212 /// What went wrong.
213 pub kind: RonErrorKind,
214}
215
216/// An error produced when RON data does not conform to a schema.
217#[derive(Debug, Clone, PartialEq, Eq)]
218pub struct ValidationError {
219 /// Dot/bracket field path to the problematic value (e.g., `"cost.generic"`, `"card_types[0]"`).
220 pub path: String,
221 /// Source location of the problematic value in the data file.
222 pub span: Span,
223 /// What went wrong.
224 pub kind: ErrorKind,
225}