1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use crate::Location;
use std::path::{Path, PathBuf};
use thiserror::Error;
/// This crates error enum
#[derive(Error, Debug)]
pub enum GenoError {
/// I/O error
#[error("{error} on '{file_path}'")]
Io {
/// Path of the file that caused the error
file_path: PathBuf,
/// The I/O error that occurred
error: std::io::Error,
},
/// Parsing error
#[error("unable to parse '{content}' ({file_path}:{location})")]
Parse {
/// Content that caused the parse failure
content: String,
/// File path of the schema
file_path: PathBuf,
/// [Location] of the parse error
location: Location,
},
/// Number out of range error
#[error("value out of range '{content}' ({file_path}:{location})")]
NumberRange {
/// The content that caused the error
content: String,
/// File path of the schema
file_path: PathBuf,
/// [Location] of the parse error
location: Location,
},
/// Duplicate type error
#[error("duplicate type definition '{type_name}' ({file_path}:{location})")]
DuplicateType {
/// The type that was duplicated
type_name: String,
/// File path of the schema
file_path: PathBuf,
/// [Location] of the parse error
location: Location,
},
/// Undefined type error
#[error("undefined type '{name}' ({file_path}:{location})")]
UndefinedType {
/// The name of the undefined type
name: String,
/// File path of the schema
file_path: PathBuf,
/// [Location] of the parse error
location: Location,
},
/// Duplicate field error
#[error("duplicate field '{name}' in struct '{struct_name}' ({file_path}:{location})")]
DuplicateField {
/// The name of the struct that has the duplicate field
struct_name: String,
/// The name of the duplicate field
name: String,
/// File path of the schema
file_path: PathBuf,
/// [Location] of the parse error
location: Location,
},
/// Duplicate enum variant name
#[error("duplicate variant name '{name}' in enum '{enum_name}' ({file_path}:{location})")]
DuplicateVariant {
/// The name of the enum that has the duplicate variant
enum_name: String,
/// The name of the duplicate variant
name: String,
/// File path of the schema
file_path: PathBuf,
/// [Location] of the parse error
location: Location,
},
/// Duplicate enum value
#[error("duplicate variant value '{value}' in enum '{enum_name}' ({file_path}:{location})")]
DuplicateVariantValue {
/// The name of the enum that has the duplicate value
enum_name: String,
/// The value that was duplicated
value: String,
/// File path of the schema
file_path: PathBuf,
/// [Location] of the parse error
location: Location,
},
/// Enumeration has no variants
#[error("enum '{name}' has no variants ({file_path}:{location})")]
EmptyEnum {
/// The name of the empty enum
name: String,
/// File path of the schema
file_path: PathBuf,
/// [Location] of the parse error
location: Location,
},
/// Metadata format is not valid
#[error("metadata format {value} invalid ({file_path}:{location})")]
InvalidMetadataFormat {
/// The value that was invalid
value: String,
/// File path of the schema
file_path: PathBuf,
/// [Location] of the parse error
location: Location,
},
/// Metadata format is missing
#[error("metadata format missing ({file_path}:{location})")]
MissingMetadataFormat {
/// File path of the schema
file_path: PathBuf,
/// [Location] of the parse error
location: Location,
},
/// Must start with an uppercase letter
#[error("identifier {name} must be Pascal case ({file_path}:{location})")]
MustBePascalCase {
/// The name of the identifier
name: String,
/// File path of the schema
file_path: PathBuf,
/// [Location] of the parse error
location: Location,
},
/// Must start with a lowercase letter
#[error("identifier {name} must be camel case ({file_path}:{location})")]
MustBeCamelCase {
/// The name of the identifier
name: String,
/// File path of the schema
file_path: PathBuf,
/// [Location] of the parse error
location: Location,
},
}
macro_rules! define_error_new {
($func:ident, $error:ident) => {
/// Creates a new [`$error`] error with the given file path and location
pub fn $func(file_path: &Path, location: &Location) -> Self {
Self::$error {
file_path: file_path.to_path_buf(),
location: location.clone(),
}
}
};
($func:ident, $error:ident, $name:ident) => {
/// Creates a new [`$error`] error with the given name, file path and location
pub fn $func($name: &str, file_path: &Path, location: &Location) -> Self {
Self::$error {
$name: $name.to_string(),
file_path: file_path.to_path_buf(),
location: location.clone(),
}
}
};
($func:ident, $error:ident, $parent_name:ident, $name:ident) => {
/// Creates a new [`$error`] error with the given parent name, name, file path and location
pub fn $func(
$parent_name: &str,
$name: &str,
file_path: &Path,
location: &Location,
) -> Self {
Self::$error {
$parent_name: $parent_name.to_string(),
$name: $name.to_string(),
file_path: file_path.to_path_buf(),
location: location.clone(),
}
}
};
}
impl GenoError {
define_error_new!(new_number_range, NumberRange, content);
define_error_new!(new_duplicate_type, DuplicateType, type_name);
define_error_new!(new_duplicate_field, DuplicateField, struct_name, name);
define_error_new!(new_duplicate_variant, DuplicateVariant, enum_name, name);
define_error_new!(new_undefined_type, UndefinedType, name);
define_error_new!(
new_duplicate_variant_value,
DuplicateVariantValue,
enum_name,
value
);
define_error_new!(new_empty_enum, EmptyEnum, name);
define_error_new!(new_invalid_metadata_format, InvalidMetadataFormat, value);
define_error_new!(new_missing_metadata_format, MissingMetadataFormat);
define_error_new!(new_must_be_pascal_case, MustBePascalCase, name);
define_error_new!(new_must_be_camel_case, MustBeCamelCase, name);
}