1#![deny(missing_debug_implementations)]
2use crate::builder::ValueBound;
5use crate::eval::EvalError;
6use crate::value::{DataValue, DataValueMap};
7use crate::GeneratorType;
8use std::borrow::Borrow;
9
10#[derive(Debug)]
12pub struct SchemaError {
13 kind: SchemaErrorKind,
14 info: SchemaErrorInfo,
15}
16
17impl std::fmt::Display for SchemaError {
18 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19 match self.kind {
20 SchemaErrorKind::ParseError => write!(f, "Parse error: {}", self.info),
21 SchemaErrorKind::BuildError => write!(f, "Build error: {}", self.info),
22 SchemaErrorKind::GenerateError => write!(f, "Generate error: {}", self.info),
23 SchemaErrorKind::OutputError => write!(f, "Output error: {}", self.info),
24 }
25 }
26}
27
28impl std::error::Error for SchemaError {}
29
30impl SchemaError {
31 pub fn is_kind_of(&self, kind: SchemaErrorKind) -> bool {
33 self.kind == kind
34 }
35
36 pub fn get_kind(&self) -> SchemaErrorKind {
38 self.kind
39 }
40
41 pub fn get_error_info(&self) -> &dyn std::error::Error {
43 self.info.0.borrow()
44 }
45}
46
47#[derive(Debug, Eq, PartialEq, Copy, Clone)]
49pub enum SchemaErrorKind {
50 ParseError,
52 BuildError,
54 GenerateError,
56 OutputError,
58}
59
60#[derive(Debug)]
62struct SchemaErrorInfo(Box<dyn std::error::Error>);
63
64impl std::fmt::Display for SchemaErrorInfo {
65 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66 write!(f, "{}", self.0)
67 }
68}
69
70impl From<BuildError> for SchemaError {
71 fn from(e: BuildError) -> Self {
72 e.into_sbrd_gen_error(SchemaErrorKind::BuildError)
73 }
74}
75
76pub trait IntoSbrdError: 'static + std::error::Error + Sized {
80 fn into_sbrd_gen_error(self, kind: SchemaErrorKind) -> SchemaError {
84 SchemaError {
85 kind,
86 info: SchemaErrorInfo(Box::new(self)),
87 }
88 }
89}
90
91impl<E> IntoSbrdError for E where E: 'static + std::error::Error + Sized {}
92
93pub type SchemaResult<T> = std::result::Result<T, SchemaError>;
97
98#[derive(Debug)]
100pub enum BuildError {
101 SpecifiedKeyNotUnique(Vec<String>),
106
107 NotExistSpecifiedKey(String, Vec<String>),
113
114 AlreadyExistKey(String),
119
120 InvalidType(GeneratorType),
125
126 InvalidValue(String),
131
132 NotExistValueOf(String),
137
138 FailParseValue(String, String, String),
145
146 RangeEmpty(ValueBound<DataValue>),
151
152 EmptySelectableChildren,
154
155 EmptySelectValues,
157
158 EmptySelectable,
160
161 NotExistDefaultCase,
163
164 AllWeightsZero,
166
167 FileError(std::io::Error, std::path::PathBuf),
173
174 FailBuildDistribution(String, String),
180}
181
182impl std::fmt::Display for BuildError {
183 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
184 match self {
185 BuildError::SpecifiedKeyNotUnique(keys) => {
186 write!(f, "Not Unique Specified Keys: {:?}", keys)
187 }
188 BuildError::NotExistSpecifiedKey(key, keys) => {
189 write!(f, "Not Exist Key \"{}\" in {:?}", key, keys)
190 }
191 BuildError::AlreadyExistKey(k) => write!(f, "Already Exist Key: {}", k),
192 BuildError::InvalidType(t) => write!(f, "Invalid Type: {}", t),
193 BuildError::InvalidValue(s) => write!(f, "Invalid Value: {}", s),
194 BuildError::NotExistValueOf(s) => write!(f, "Not Exist Value for {}", s),
195 BuildError::FailParseValue(s, t, e) => {
196 write!(f, "Fail Parse {} as {} with error: {}", s, t, e)
197 }
198 BuildError::RangeEmpty(range) => write!(f, "Empty Range: {}", range),
199 BuildError::EmptySelectableChildren => write!(f, "Selectable children is empty"),
200 BuildError::EmptySelectValues => write!(f, "Selectable values is empty"),
201 BuildError::EmptySelectable => {
202 write!(f, "Selectable children or values is empty")
203 }
204 BuildError::NotExistDefaultCase => write!(f, "Not Exist default case condition"),
205 BuildError::AllWeightsZero => write!(f, "All weights are zero"),
206 BuildError::FileError(fe, path) => write!(
207 f,
208 "File Error: {} at filepath `{}`",
209 fe,
210 path.as_path().display()
211 ),
212 BuildError::FailBuildDistribution(dn, e) => {
213 write!(f, "Fail build {} distribution with error: {}", dn, e)
214 }
215 }
216 }
217}
218
219impl std::error::Error for BuildError {}
220
221#[derive(Debug, PartialEq)]
223pub enum GenerateError {
224 FailEval(EvalError, String, DataValueMap<String>),
231
232 FailGenerate(String),
237
238 NotExistGeneratedKey(String, DataValueMap<String>),
244}
245
246impl std::fmt::Display for GenerateError {
247 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
248 match self {
249 GenerateError::FailEval(e, script, context) => {
250 write!(
251 f,
252 "Fail Evaluate Script \"{}\" in context {:?} with error: {}",
253 script, context, e
254 )
255 }
256 GenerateError::FailGenerate(s) => {
257 write!(f, "Fail Generate valid data. Because {}", s)
258 }
259 GenerateError::NotExistGeneratedKey(key, values) => {
260 write!(f, "Not exist key \"{}\" in {:?}", key, values)
261 }
262 }
263 }
264}
265
266impl std::error::Error for GenerateError {}