From 2c61adda557fdb6ada4ff5cfe618aab5b9d53388 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Wed, 16 Feb 2022 13:45:23 -0500 Subject: [PATCH] even better error handling --- src/lib.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e9323b2..1a25bd3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,18 +58,32 @@ impl ChartManager { pub fn init_panic_hook() { panic::set_hook(Box::new(console_error_panic_hook::hook)); } pub fn test_func(function_string: String) -> String { - let expr: Expr = function_string.parse().unwrap(); - let func_result = expr.bind("x"); - match func_result { + let expr_result = function_string.parse(); + let expr_error = match &expr_result { Ok(_) => "".to_string(), Err(error) => format!("{}", error), + }; + if !expr_error.is_empty() { + 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), + }; + if !func_error.is_empty() { + return func_error; + } + + "".to_string() } fn get_func(&self) -> impl Fn(f64) -> f64 { let expr: Expr = self.func_str.parse().unwrap(); let func = expr.bind("x").unwrap(); - return func; + func } #[inline]