Skip to main content

xylo_lang/
error.rs

1#[cfg(feature = "no-std")]
2use alloc::{
3    format,
4    string::{String, ToString},
5};
6
7#[derive(Debug)]
8pub enum Error {
9    ParseError,
10    NotDigit(String),
11    InvalidList,
12    InvalidRange,
13    InvalidRoot,
14    MissingSeed,
15    UnknownFunction(String),
16    InvalidArgument(String),
17    InvalidDefinition(String),
18    InvalidCondition,
19    InvalidMatch,
20    MatchNotFound,
21    NotIterable,
22    NegativeNumber,
23    OutOfBounds,
24    NotFound,
25    MaxDepthReached,
26    PngError(png::EncodingError),
27    #[cfg(feature = "std")]
28    FileError(std::io::Error),
29}
30
31impl ToString for Error {
32    fn to_string(&self) -> String {
33        match self {
34            Error::ParseError => "Could not parse file.".into(),
35            Error::NotDigit(name) => format!("Value passed to `{}` was not a digit.", name),
36            Error::InvalidList => "Type mismatch in list.".into(),
37            Error::InvalidRange => "Invalid range.".into(),
38            Error::InvalidRoot => "The `root` function must return a shape.".into(),
39            Error::MissingSeed => "Seed required for rng.".into(),
40            Error::UnknownFunction(name) => format!("Unknown function `{}`.", name),
41            Error::InvalidArgument(name) => {
42                format!("Invalid argument passed to `{}` function.", name)
43            }
44            Error::InvalidDefinition(name) => {
45                format!("Incorrect parameters in `{}` function.", name)
46            }
47            Error::InvalidCondition => "If condition must reduce to a boolean.".into(),
48            Error::InvalidMatch => "Incorrect type comparison in match statement.".into(),
49            Error::MatchNotFound => "Not all possibilities covered in match statement".into(),
50            Error::NotIterable => "Value is not iterable.".into(),
51            Error::NegativeNumber => "Number cannot be negative.".into(),
52            Error::OutOfBounds => "Index out of bounds.".into(),
53            Error::NotFound => "Value not found.".into(),
54            Error::MaxDepthReached => "Max call stack depth reached.".into(),
55            Error::PngError(e) => e.to_string(),
56            #[cfg(feature = "std")]
57            Error::FileError(e) => e.to_string(),
58        }
59    }
60}
61
62pub type Result<T> = core::result::Result<T, Error>;