improve parsing (very janky)
This commit is contained in:
56
src/lib.rs
56
src/lib.rs
@@ -8,7 +8,7 @@ use std::panic;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use web_sys::HtmlCanvasElement;
|
||||
mod misc;
|
||||
use crate::misc::{Cache, ChartOutput, DrawResult};
|
||||
use crate::misc::{Cache, ChartOutput, DrawResult, add_asterisks, test_func_basic};
|
||||
|
||||
#[global_allocator]
|
||||
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
|
||||
@@ -57,27 +57,21 @@ impl ChartManager {
|
||||
// Used in order to hook into `panic!()` to log in the browser's console
|
||||
pub fn init_panic_hook() { panic::set_hook(Box::new(console_error_panic_hook::hook)); }
|
||||
|
||||
fn get_func(&self) -> impl Fn(f64) -> f64 {
|
||||
|
||||
let expr: Expr = self.func_str.parse().unwrap();
|
||||
let func = expr.bind("x").unwrap();
|
||||
func
|
||||
}
|
||||
|
||||
pub fn test_func(function_string: String) -> String {
|
||||
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 new_func_str: String = add_asterisks(function_string.clone());
|
||||
let run1_result = test_func_basic(new_func_str);
|
||||
if run1_result == "" {
|
||||
return run1_result;
|
||||
}
|
||||
|
||||
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()
|
||||
return test_func_basic(function_string);
|
||||
}
|
||||
|
||||
// Recommends a possible solution to an error from method `test_func`
|
||||
@@ -87,12 +81,6 @@ impl ChartManager {
|
||||
}.to_string()
|
||||
}
|
||||
|
||||
fn get_func(&self) -> impl Fn(f64) -> f64 {
|
||||
let expr: Expr = self.func_str.parse().unwrap();
|
||||
let func = expr.bind("x").unwrap();
|
||||
func
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn draw(
|
||||
&mut self, element: HtmlCanvasElement, dark_mode: bool
|
||||
@@ -190,15 +178,27 @@ impl ChartManager {
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn update(
|
||||
&mut self, canvas: HtmlCanvasElement, func_str: &str, min_x: f32, max_x: f32, min_y: f32,
|
||||
&mut self, canvas: HtmlCanvasElement, func_str_new: String, min_x: f32, max_x: f32, min_y: f32,
|
||||
max_y: f32, num_interval: usize, resolution: i32, dark_mode: bool
|
||||
) -> Result<ChartOutput, JsValue> {
|
||||
let underlying_update = (*func_str != self.func_str)
|
||||
let func_str: String;
|
||||
let new_func_str: String = add_asterisks(func_str_new.clone());
|
||||
if test_func_basic(add_asterisks(func_str_new.clone())) == "" {
|
||||
func_str = new_func_str;
|
||||
} else if test_func_basic(func_str_new.clone()) == "" {
|
||||
func_str = func_str_new;
|
||||
} else {
|
||||
panic!("function is invalid");
|
||||
}
|
||||
|
||||
|
||||
let underlying_update = (func_str != self.func_str)
|
||||
| (min_x != self.min_x)
|
||||
| (max_x != self.max_x)
|
||||
| (min_y != self.min_y)
|
||||
| (max_y != self.max_y);
|
||||
|
||||
|
||||
if 0 > resolution {
|
||||
panic!("resolution cannot be less than 0");
|
||||
}
|
||||
@@ -221,7 +221,7 @@ impl ChartManager {
|
||||
self.front_cache.invalidate();
|
||||
}
|
||||
|
||||
self.func_str = func_str.to_string();
|
||||
self.func_str = func_str;
|
||||
self.min_x = min_x;
|
||||
self.max_x = max_x;
|
||||
self.min_y = min_y;
|
||||
|
||||
Reference in New Issue
Block a user