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    /// A syntax error — the parser encountered a token it did not expect.
40    UnexpectedToken {
41        /// What the parser expected at this position.
42        expected: String,
43        /// What was actually found.
44        found: String,
45    },
46}
47
48/// Specific kinds of errors that can occur when parsing a `.ron` data file.
49#[derive(Debug, Clone, PartialEq, Eq)]
50pub enum RonErrorKind {
51    /// A syntax error — the parser encountered a token it did not expect.
52    UnexpectedToken {
53        /// What the parser expected at this position.
54        expected: String,
55        /// What was actually found.
56        found: String,
57    },
58    /// A string literal is missing its closing quote.
59    UnterminatedString,
60    /// The same field name appears more than once within a struct.
61    DuplicateField {
62        /// The duplicated field name.
63        field_name: String,
64    },
65    /// A numeric literal could not be parsed as a valid number.
66    InvalidNumber {
67        /// The text that failed to parse.
68        text: String,
69    },
70}
71
72/// Specific kinds of validation errors produced when RON data does not match a schema.
73#[derive(Debug, Clone, PartialEq, Eq)]
74pub enum ErrorKind {
75    /// A required field defined in the schema is absent from the data.
76    MissingField {
77        /// The name of the missing field.
78        field_name: String,
79    },
80    /// The data contains a field not defined in the schema.
81    UnknownField {
82        /// The name of the unrecognized field.
83        field_name: String,
84    },
85    /// A value exists but has the wrong type.
86    TypeMismatch {
87        /// The type expected by the schema.
88        expected: String,
89        /// A description of what was actually found.
90        found: String,
91    },
92    /// A bare identifier does not match any variant of the expected enum.
93    InvalidEnumVariant {
94        /// The enum the identifier was validated against.
95        enum_name: String,
96        /// The identifier that was found.
97        variant: String,
98        /// All valid variants for this enum.
99        valid: Vec<String>,
100    },
101    /// The value inside `Some(...)` has the wrong type.
102    InvalidOptionValue {
103        /// The type expected by the schema.
104        expected: String,
105        /// A description of what was actually found.
106        found: String,
107    },
108    /// A list element has the wrong type.
109    InvalidListElement {
110        /// The 0-based index of the offending element.
111        index: usize,
112        /// The type expected by the schema.
113        expected: String,
114        /// A description of what was actually found.
115        found: String,
116    },
117    /// Expected a struct `(...)` but found a non-struct value.
118    ExpectedStruct {
119        /// A description of what was actually found.
120        found: String,
121    },
122    /// Expected a list `[...]` but found a non-list value.
123    ExpectedList {
124        /// A description of what was actually found.
125        found: String,
126    },
127    /// Expected `Some(...)` or `None` but found something else.
128    ExpectedOption {
129        /// A description of what was actually found.
130        found: String,
131    },
132}
133
134/// An error produced when parsing a `.ronschema` file fails.
135#[derive(Debug, Clone, PartialEq, Eq)]
136pub struct SchemaParseError {
137    /// Source location where the error occurred.
138    pub span: Span,
139    /// What went wrong.
140    pub kind: SchemaErrorKind,
141}
142
143/// An error produced when parsing a `.ron` data file fails.
144#[derive(Debug, Clone, PartialEq, Eq)]
145pub struct RonParseError {
146    /// Source location where the error occurred.
147    pub span: Span,
148    /// What went wrong.
149    pub kind: RonErrorKind,
150}
151
152/// An error produced when RON data does not conform to a schema.
153#[derive(Debug, Clone, PartialEq, Eq)]
154pub struct ValidationError {
155    /// Dot/bracket field path to the problematic value (e.g., `"cost.generic"`, `"card_types[0]"`).
156    pub path: String,
157    /// Source location of the problematic value in the data file.
158    pub span: Span,
159    /// What went wrong.
160    pub kind: ErrorKind,
161}