diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-01-21 17:02:00 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-01-21 17:02:00 +0100 |
| commit | b9b32972afda261020dd207b4ea2b44b7b697b83 (patch) | |
| tree | dba426aabaa569952e2aa0dda0f5e1e836bae55f /src/math/expression_function.rs | |
| parent | 53a48a778af34bd25963881539e6f66f47dc5580 (diff) | |
add multiple functions to canvas
Diffstat (limited to 'src/math/expression_function.rs')
| -rw-r--r-- | src/math/expression_function.rs | 26 |
1 files changed, 15 insertions, 11 deletions
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<Operation>) -> Self { + pub fn from_string(str: String, operations: &Vec<Operation>) -> Result<Self, ()> { let str = str.replace(' ', ""); - let (lhs, expr) = str.split_once('=').unwrap(); - let (name, a) = lhs.split_once('(').unwrap(); - let args: Vec<String> = 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<String> = 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 { |