From b9b32972afda261020dd207b4ea2b44b7b697b83 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Sun, 21 Jan 2024 17:02:00 +0100 Subject: add multiple functions to canvas --- src/math/expression_function.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'src/math/expression_function.rs') diff --git a/src/math/expression_function.rs b/src/math/expression_function.rs index 44f83d1..b9a480f 100644 --- a/src/math/expression_function.rs +++ b/src/math/expression_function.rs @@ -17,19 +17,23 @@ pub struct ExpressionFunction { } impl ExpressionFunction { - pub fn from_string(str: String, operations: &Vec) -> Self { + pub fn from_string(str: String, operations: &Vec) -> Result { let str = str.replace(' ', ""); - let (lhs, expr) = str.split_once('=').unwrap(); - let (name, a) = lhs.split_once('(').unwrap(); - let args: Vec = a[0..a.len() - 1] - .split(',') - .map(|s| s.to_string()) - .collect(); - Self { - expr: Expression::from_string(expr, operations), - name: name.to_string(), - args, + if let Some((lhs, expr)) = str.split_once('=') { + if let Some((name, a)) = lhs.split_once('(') { + let args: Vec = a[0..a.len() - 1] + .split(',') + .map(|s| s.to_string()) + .collect(); + + return Ok(Self { + expr: Expression::from_string(expr, operations), + name: name.to_string(), + args, + }) + } } + Err(()) } pub fn name(&self) -> &str { -- cgit v1.2.3-70-g09d2