daisy/src/evaluate/mod.rs

52 lines
1.0 KiB
Rust
Raw Normal View History

2023-06-11 13:53:45 -07:00
mod operator;
mod function;
mod evaluate;
2023-06-11 14:24:43 -07:00
2023-06-11 13:53:45 -07:00
pub use self::evaluate::evaluate;
2023-06-13 20:15:20 -07:00
#[derive(Debug)]
2023-06-11 13:53:45 -07:00
pub enum EvalError {
BadMath,
TooBig,
ZeroDivision,
2023-06-14 20:18:28 -07:00
IncompatibleUnit,
2023-08-01 09:43:47 -07:00
IncompatibleUnits(String, String),
2023-08-03 14:04:12 -07:00
Undefined(String),
EvaluationError,
2023-08-03 14:39:53 -07:00
BadArguments(String, usize, usize)
2023-07-31 16:05:48 -07:00
}
impl ToString for EvalError {
fn to_string(&self) -> String {
match self {
EvalError::BadMath => {
String::from("Failed to evaluate expression")
},
EvalError::TooBig => {
String::from("Number too big")
},
EvalError::ZeroDivision => {
String::from("Division by zero")
},
EvalError::IncompatibleUnit => {
2023-08-01 09:43:47 -07:00
String::from("Incompatible unit")
},
EvalError::IncompatibleUnits(a, b) => {
2023-08-03 14:04:12 -07:00
format!("Incompatible units ({a} and {b})")
2023-07-31 16:43:30 -07:00
},
EvalError::Undefined(s) => {
2023-08-03 14:04:12 -07:00
format!("{s} is undefined")
},
EvalError::EvaluationError => {
String::from("Could not evaluate")
2023-08-03 14:39:53 -07:00
},
EvalError::BadArguments(s, want, got) => {
format!("{s} takes {want} argument{}, got {got}",
if *want == 1 {""} else {"s"},
)
2023-07-31 16:05:48 -07:00
}
}
}
}