Skip to main content

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 default value does not match the field's declared type.
60    InvalidDefault {
61        /// The field name.
62        field_name: String,
63        /// The type expected by the schema.
64        expected: String,
65        /// A description of the default value.
66        found: String,
67    },
68    /// A syntax error — the parser encountered a token it did not expect.
69    UnexpectedToken {
70        /// What the parser expected at this position.
71        expected: String,
72        /// What was actually found.
73        found: String,
74    },
75}
76
77/// Specific kinds of errors that can occur when parsing a `.ron` data file.
78#[derive(Debug, Clone, PartialEq, Eq)]
79pub enum RonErrorKind {
80    /// A syntax error — the parser encountered a token it did not expect.
81    UnexpectedToken {
82        /// What the parser expected at this position.
83        expected: String,
84        /// What was actually found.
85        found: String,
86    },
87    /// A string literal is missing its closing quote.
88    UnterminatedString,
89    /// The same field name appears more than once within a struct.
90    DuplicateField {
91        /// The duplicated field name.
92        field_name: String,
93    },
94    /// A numeric literal could not be parsed as a valid number.
95    InvalidNumber {
96        /// The text that failed to parse.
97        text: String,
98    },
99}
100
101/// Specific kinds of validation errors produced when RON data does not match a schema.
102#[derive(Debug, Clone, PartialEq, Eq)]
103pub enum ErrorKind {
104    /// A required field defined in the schema is absent from the data.
105    MissingField {
106        /// The name of the missing field.
107        field_name: String,
108    },
109    /// The data contains a field not defined in the schema.
110    UnknownField {
111        /// The name of the unrecognized field.
112        field_name: String,
113    },
114    /// A value exists but has the wrong type.
115    TypeMismatch {
116        /// The type expected by the schema.
117        expected: String,
118        /// A description of what was actually found.
119        found: String,
120    },
121    /// A bare identifier does not match any variant of the expected enum.
122    InvalidEnumVariant {
123        /// The enum the identifier was validated against.
124        enum_name: String,
125        /// The identifier that was found.
126        variant: String,
127        /// All valid variants for this enum.
128        valid: Vec<String>,
129    },
130    /// The value inside `Some(...)` has the wrong type.
131    InvalidOptionValue {
132        /// The type expected by the schema.
133        expected: String,
134        /// A description of what was actually found.
135        found: String,
136    },
137    /// A list element has the wrong type.
138    InvalidListElement {
139        /// The 0-based index of the offending element.
140        index: usize,
141        /// The type expected by the schema.
142        expected: String,
143        /// A description of what was actually found.
144        found: String,
145    },
146    /// Expected a struct `(...)` but found a non-struct value.
147    ExpectedStruct {
148        /// A description of what was actually found.
149        found: String,
150    },
151    /// Expected a list `[...]` but found a non-list value.
152    ExpectedList {
153        /// A description of what was actually found.
154        found: String,
155    },
156    /// Expected `Some(...)` or `None` but found something else.
157    ExpectedOption {
158        /// A description of what was actually found.
159        found: String,
160    },
161    /// Expected a map `{ ... }` but found a non-map value.
162    ExpectedMap {
163        /// A description of what was actually found.
164        found: String,
165    },
166    /// A map key has the wrong type.
167    InvalidMapKey {
168        /// A string representation of the key.
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    /// A map value has the wrong type.
176    InvalidMapValue {
177        /// A string representation of the key this value belongs to.
178        key: String,
179        /// The type expected by the schema.
180        expected: String,
181        /// A description of what was actually found.
182        found: String,
183    },
184    /// An enum variant's associated data does not match the expected type.
185    InvalidVariantData {
186        /// The enum name.
187        enum_name: String,
188        /// The variant name.
189        variant: String,
190        /// The type expected by the schema.
191        expected: String,
192        /// A description of what was actually found.
193        found: String,
194    },
195    /// Expected a tuple `(...)` but found a non-tuple value.
196    ExpectedTuple {
197        /// A description of what was actually found.
198        found: String,
199    },
200    /// A tuple has the wrong number of elements.
201    TupleLengthMismatch {
202        /// The number of elements expected by the schema.
203        expected: usize,
204        /// The number of elements found in the data.
205        found: usize,
206    },
207    /// A tuple element has the wrong type.
208    InvalidTupleElement {
209        /// The 0-based index of the offending element.
210        index: usize,
211        /// The type expected by the schema.
212        expected: String,
213        /// A description of what was actually found.
214        found: String,
215    },
216}
217
218/// An error produced when parsing a `.ronschema` file fails.
219#[derive(Debug, Clone, PartialEq, Eq)]
220pub struct SchemaParseError {
221    /// Source location where the error occurred.
222    pub span: Span,
223    /// What went wrong.
224    pub kind: SchemaErrorKind,
225}
226
227/// An error produced when parsing a `.ron` data file fails.
228#[derive(Debug, Clone, PartialEq, Eq)]
229pub struct RonParseError {
230    /// Source location where the error occurred.
231    pub span: Span,
232    /// What went wrong.
233    pub kind: RonErrorKind,
234}
235
236/// An error produced when RON data does not conform to a schema.
237#[derive(Debug, Clone, PartialEq, Eq)]
238pub struct ValidationError {
239    /// Dot/bracket field path to the problematic value (e.g., `"cost.generic"`, `"card_types[0]"`).
240    pub path: String,
241    /// Source location of the problematic value in the data file.
242    pub span: Span,
243    /// What went wrong.
244    pub kind: ErrorKind,
245}
246
247/// Specific kinds of warnings produced during validation.
248#[derive(Debug, Clone, PartialEq, Eq)]
249pub enum WarningKind {
250    /// Fields in the data appear in a different order than declared in the schema.
251    FieldOrderMismatch {
252        /// The field that appears out of order.
253        field_name: String,
254        /// The field that should have appeared before it according to the schema.
255        expected_after: String,
256    },
257}
258
259/// A warning produced during validation — informational, does not indicate invalid data.
260#[derive(Debug, Clone, PartialEq, Eq)]
261pub struct Warning {
262    /// Dot/bracket field path to the relevant value.
263    pub path: String,
264    /// Source location of the relevant value in the data file.
265    pub span: Span,
266    /// What the warning is about.
267    pub kind: WarningKind,
268}
269
270/// The result of validating RON data against a schema.
271#[derive(Debug, Clone, PartialEq)]
272pub struct ValidationResult {
273    /// All validation errors found. Empty means the data is valid.
274    pub errors: Vec<ValidationError>,
275    /// All warnings found. Warnings do not indicate invalid data.
276    pub warnings: Vec<Warning>,
277}