From 7b61a03e31aca3d11b414d49313453e21d6a88f1 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Mon, 28 Feb 2022 12:51:29 -0500 Subject: [PATCH] use options for test_func --- src/egui_app.rs | 4 ++-- src/misc.rs | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/egui_app.rs b/src/egui_app.rs index e7cdc4e..219703c 100644 --- a/src/egui_app.rs +++ b/src/egui_app.rs @@ -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)); } diff --git a/src/misc.rs b/src/misc.rs index 1705d42..3ac979c 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -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 { // 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