use options for test_func

This commit is contained in:
Simon Gardling 2022-02-28 12:51:29 -05:00
parent 5c84a0b4d2
commit 7b61a03e31
2 changed files with 11 additions and 11 deletions

View File

@ -178,9 +178,9 @@ impl epi::App for MathApp {
if !self.func_strs[i].is_empty() {
let proc_func_str = add_asterisks(self.func_strs[i].clone());
let func_test_output = test_func(proc_func_str.clone());
if !func_test_output.is_empty() {
if func_test_output.is_some() {
parse_error += &format!("(Function #{}) ", i);
parse_error += &func_test_output;
parse_error += &func_test_output.expect("");
} else {
function.update(proc_func_str, integral, Some(self.integral_min_x), Some(self.integral_max_x), Some(self.integral_num));
}

View File

@ -112,33 +112,33 @@ pub fn add_asterisks(function_in: String) -> String {
}
// Tests function to make sure it's able to be parsed. Returns the string of the Error produced, or an empty string if it runs successfully.
pub fn test_func(function_string: String) -> String {
pub fn test_func(function_string: String) -> Option<String> {
// Factorials do not work, and it would be really difficult to make them work
if function_string.contains('!') {
return "Factorials are unsupported".to_string();
return Some("Factorials are unsupported".to_string());
}
let new_func_str: String = add_asterisks(function_string);
let expr_result = new_func_str.parse();
let expr_error = match &expr_result {
Ok(_) => "".to_string(),
Err(error) => format!("{}", error),
Ok(_) => None,
Err(error) => Some(format!("{}", error)),
};
if !expr_error.is_empty() {
if expr_error.is_some() {
return expr_error;
}
let expr: Expr = expr_result.unwrap();
let func_result = expr.bind("x");
let func_error = match &func_result {
Ok(_) => "".to_string(),
Err(error) => format!("{}", error),
Ok(_) => None,
Err(error) => Some(format!("{}", error)),
};
if !func_error.is_empty() {
if func_error.is_some() {
return func_error;
}
"".to_string()
None
}
// Rounds f64 to specific number of digits