2023-08-03 22:13:38 -07:00
|
|
|
use crate::formattedtext::FormattedText;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum DaisyError {
|
|
|
|
|
|
|
|
// Parser errors
|
|
|
|
//MissingCloseParen,
|
|
|
|
ExtraCloseParen,
|
|
|
|
EmptyGroup,
|
|
|
|
Syntax,
|
|
|
|
BadNumber,
|
2023-08-04 12:22:23 -07:00
|
|
|
BadVariable,
|
|
|
|
BadFunction,
|
2023-08-05 09:29:59 -07:00
|
|
|
BadTuple,
|
2023-08-03 22:13:38 -07:00
|
|
|
|
|
|
|
// Evaluation errors
|
|
|
|
BadMath,
|
|
|
|
TooBig,
|
|
|
|
ZeroDivision,
|
|
|
|
IncompatibleUnit,
|
|
|
|
IncompatibleUnits(String, String),
|
|
|
|
Undefined(String),
|
|
|
|
EvaluationError,
|
|
|
|
BadArguments(String, usize, usize)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DaisyError {
|
|
|
|
pub fn text(&self) -> FormattedText {
|
|
|
|
match self {
|
|
|
|
//DaisyError::MissingCloseParen => {
|
|
|
|
// String::from("Missing close parenthesis")
|
|
|
|
//},
|
|
|
|
DaisyError::ExtraCloseParen => {
|
|
|
|
return FormattedText::new(
|
|
|
|
"[e]Syntax Error:[n] Extra close parenthesis".to_string()
|
|
|
|
);
|
|
|
|
},
|
|
|
|
DaisyError::EmptyGroup => {
|
|
|
|
return FormattedText::new(
|
|
|
|
"[e]Syntax Error:[n] Groups can't be empty".to_string()
|
|
|
|
);
|
|
|
|
},
|
|
|
|
DaisyError::Syntax => {
|
|
|
|
return FormattedText::new(
|
|
|
|
"[e]Syntax Error[n]".to_string()
|
|
|
|
);
|
|
|
|
},
|
|
|
|
DaisyError::BadNumber => {
|
|
|
|
return FormattedText::new(
|
|
|
|
"[e]Syntax Error:[n] Invalid number".to_string()
|
|
|
|
);
|
2023-08-04 12:22:23 -07:00
|
|
|
},
|
|
|
|
DaisyError::BadVariable => {
|
|
|
|
return FormattedText::new(
|
|
|
|
"[e]Syntax Error:[n] Bad variable name".to_string()
|
|
|
|
);
|
|
|
|
},
|
|
|
|
DaisyError::BadFunction => {
|
|
|
|
return FormattedText::new(
|
|
|
|
"[e]Syntax Error:[n] Bad function name".to_string()
|
|
|
|
);
|
2023-08-05 09:29:59 -07:00
|
|
|
},
|
|
|
|
DaisyError::BadTuple => {
|
|
|
|
return FormattedText::new(
|
|
|
|
"[e]Syntax Error:[n] Bad tuple syntax".to_string()
|
|
|
|
);
|
|
|
|
},
|
2023-08-03 22:13:38 -07:00
|
|
|
|
2023-08-04 12:22:23 -07:00
|
|
|
|
2023-08-03 22:13:38 -07:00
|
|
|
DaisyError::BadMath => {
|
|
|
|
return FormattedText::new(
|
|
|
|
"[e]Evaluation Error:[n] Failed to evaluate expression".to_string()
|
|
|
|
);
|
|
|
|
},
|
|
|
|
DaisyError::TooBig => {
|
|
|
|
return FormattedText::new(
|
|
|
|
"[e]Evaluation Error:[n] Number too big".to_string()
|
|
|
|
);
|
|
|
|
},
|
|
|
|
DaisyError::ZeroDivision => {
|
|
|
|
return FormattedText::new(
|
|
|
|
"[e]Evaluation Error:[n] Division by zero".to_string()
|
|
|
|
);
|
|
|
|
},
|
|
|
|
DaisyError::IncompatibleUnit => {
|
|
|
|
return FormattedText::new(
|
|
|
|
"[e]Evaluation Error:[n] Incompatible unit".to_string()
|
|
|
|
);
|
|
|
|
},
|
|
|
|
DaisyError::IncompatibleUnits(a, b) => {
|
|
|
|
return FormattedText::new(format!(
|
|
|
|
"[e]Evaluation Error:[n] Incompatible units ([c]{a}[n] and [c]{b}[n])"
|
|
|
|
));
|
|
|
|
},
|
|
|
|
DaisyError::Undefined(s) => {
|
|
|
|
return FormattedText::new(format!(
|
|
|
|
"[e]Evaluation Error:[n] [c]{s}[n] is not defined"
|
|
|
|
));
|
|
|
|
},
|
|
|
|
DaisyError::EvaluationError => {
|
|
|
|
return FormattedText::new(
|
|
|
|
"[e]Evaluation Error:[n] Could not evaluate".to_string()
|
|
|
|
);
|
|
|
|
},
|
|
|
|
DaisyError::BadArguments(s, want, got) => {
|
|
|
|
return FormattedText::new(format!(
|
2023-08-03 22:15:11 -07:00
|
|
|
"[e]Evaluation Error:[n] [c]{s}[n] takes {want} argument{}, but it got {got}",
|
2023-08-03 22:13:38 -07:00
|
|
|
if *want == 1 {""} else {"s"},
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|