#![deny(missing_docs)]
#![warn(rustdoc::private_intra_doc_links)]
#![warn(rustdoc::missing_crate_level_docs)]
#![warn(rustdoc::private_doc_tests)]
#![deny(missing_debug_implementations)]
mod calculator_float;
pub use calculator_float::CalculatorFloat;
mod calculator;
pub use calculator::Calculator;
mod calculator_complex;
pub use calculator_complex::CalculatorComplex;
use thiserror::Error;
#[derive(Error, Debug, PartialEq)]
pub enum CalculatorError {
#[error("Input cannot be converted to CalculatorFloat")]
NotConvertable,
#[error("Symbolic value {val:?} can not be converted to float")]
FloatSymbolicNotConvertable {
val: String,
},
#[error("Symbolic value {val:?} can not be converted to complex")]
ComplexSymbolicNotConvertable {
val: CalculatorComplex,
},
#[error("Imaginary part of CalculatorComplex {val:?} not zero")]
ComplexCanNotBeConvertedToFloat {
val: CalculatorComplex,
},
#[error("Parsing error: {msg:?}")]
ParsingError {
msg: &'static str,
},
#[error("Function {fct:?} not implemented.")]
NotImplementedError {
fct: &'static str,
},
#[error("Function {fct:?} not found.")]
FunctionNotFound {
fct: String,
},
#[error("Variable {name:?} not set.")]
VariableNotSet {
name: String,
},
#[error("Parsing error: Unexpected end of expression")]
UnexpectedEndOfExpression,
#[error("Division by zero error")]
DivisionByZero,
#[error("Parsing Expression did not return value as expected.")]
NoValueReturnedParsing,
#[error("Not enough function arguments.")]
NotEnoughFunctionArguments,
#[error("Trying to assign variable {variable_name} in side-effect free parsing. Set variable in Calculator with .set_variable, replace with number in str or use parse_str_assign to resolve error.")]
ForbiddenAssign {
variable_name: String,
},
#[error("CalculatorFloat::Str is not a valid expression that can be parsed: Variable assignment to {variable_name}")]
NotParsableAssign {
variable_name: String,
},
#[error("CalculatorFloat::Str is not a valid expression that can be parsed: Urecognized elements in expression")]
NotParsableUnrecognized,
#[error("CalculatorFloat::Str is not a valid expression that can be parsed: Assign operator `=` found in expression")]
NotParsableSingleAssign,
}
#[cfg(test)]
mod tests {
use super::CalculatorComplex;
use super::CalculatorError;
#[test]
fn test_debug() {
let not_conv = CalculatorError::NotConvertable;
assert_eq!(format!("{not_conv:?}"), "NotConvertable");
let float_sym = CalculatorError::FloatSymbolicNotConvertable {
val: String::from("2x"),
};
assert_eq!(
format!("{float_sym:?}"),
"FloatSymbolicNotConvertable { val: \"2x\" }"
);
let complex_sym = CalculatorError::ComplexSymbolicNotConvertable {
val: CalculatorComplex::from("2x"),
};
assert_eq!(
format!("{complex_sym:?}"),
"ComplexSymbolicNotConvertable { val: CalculatorComplex { re: Str(\"2x\"), im: Float(0.0) } }"
);
let complex_im_sym = CalculatorError::ComplexSymbolicNotConvertable {
val: CalculatorComplex::new(1, 3),
};
assert_eq!(
format!("{complex_im_sym:?}"),
"ComplexSymbolicNotConvertable { val: CalculatorComplex { re: Float(1.0), im: Float(3.0) } }"
);
let parse = CalculatorError::ParsingError { msg: "test" };
assert_eq!(format!("{parse:?}"), "ParsingError { msg: \"test\" }");
let not_impl = CalculatorError::NotImplementedError { fct: "Test" };
assert_eq!(
format!("{not_impl:?}"),
"NotImplementedError { fct: \"Test\" }"
);
let func_not_found = CalculatorError::FunctionNotFound {
fct: String::from("Test"),
};
assert_eq!(
format!("{func_not_found:?}"),
"FunctionNotFound { fct: \"Test\" }"
);
let var_not_set = CalculatorError::VariableNotSet {
name: String::from("Test"),
};
assert_eq!(
format!("{var_not_set:?}"),
"VariableNotSet { name: \"Test\" }"
);
let end_of_exp = CalculatorError::UnexpectedEndOfExpression;
assert_eq!(format!("{end_of_exp:?}"), "UnexpectedEndOfExpression");
let div_zero = CalculatorError::DivisionByZero;
assert_eq!(format!("{div_zero:?}"), "DivisionByZero");
let parsing_no_val = CalculatorError::NoValueReturnedParsing;
assert_eq!(format!("{parsing_no_val:?}"), "NoValueReturnedParsing");
let func_args = CalculatorError::NotEnoughFunctionArguments;
assert_eq!(format!("{func_args:?}"), "NotEnoughFunctionArguments");
}
}