rustfmt
This commit is contained in:
parent
95b57f711c
commit
0154ff3933
25
src/lib.rs
25
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, add_asterisks};
|
||||
use crate::misc::{add_asterisks, Cache, ChartOutput, DrawResult};
|
||||
|
||||
#[global_allocator]
|
||||
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
|
||||
@ -74,7 +74,7 @@ impl ChartManager {
|
||||
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 {
|
||||
@ -84,13 +84,13 @@ impl ChartManager {
|
||||
if !func_error.is_empty() {
|
||||
return func_error;
|
||||
}
|
||||
|
||||
|
||||
"".to_string()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn draw(
|
||||
&mut self, element: HtmlCanvasElement, dark_mode: bool
|
||||
&mut self, element: HtmlCanvasElement, dark_mode: bool,
|
||||
) -> DrawResult<(impl Fn((i32, i32)) -> Option<(f32, f32)>, f32)> {
|
||||
let func = self.get_func();
|
||||
|
||||
@ -112,7 +112,12 @@ impl ChartManager {
|
||||
.build_cartesian_2d(self.min_x..self.max_x, self.min_y..self.max_y)?;
|
||||
|
||||
if dark_mode {
|
||||
chart.configure_mesh().x_labels(3).y_labels(3).light_line_style(&RGBColor(254, 254, 254)).draw()?;
|
||||
chart
|
||||
.configure_mesh()
|
||||
.x_labels(3)
|
||||
.y_labels(3)
|
||||
.light_line_style(&RGBColor(254, 254, 254))
|
||||
.draw()?;
|
||||
} else {
|
||||
chart.configure_mesh().x_labels(3).y_labels(3).draw()?;
|
||||
}
|
||||
@ -185,19 +190,17 @@ impl ChartManager {
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn update(
|
||||
&mut self, canvas: HtmlCanvasElement, func_str_new: String, min_x: f32, max_x: f32, min_y: f32,
|
||||
max_y: f32, num_interval: usize, resolution: usize, dark_mode: bool
|
||||
&mut self, canvas: HtmlCanvasElement, func_str_new: String, min_x: f32, max_x: f32,
|
||||
min_y: f32, max_y: f32, num_interval: usize, resolution: usize, dark_mode: bool,
|
||||
) -> Result<ChartOutput, JsValue> {
|
||||
let func_str: String = add_asterisks(func_str_new);
|
||||
|
||||
|
||||
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 underlying_update | (self.resolution != resolution) {
|
||||
self.back_cache.invalidate();
|
||||
}
|
||||
@ -214,7 +217,9 @@ impl ChartManager {
|
||||
self.num_interval = num_interval;
|
||||
self.resolution = resolution;
|
||||
|
||||
let draw_output = self.draw(canvas, dark_mode).map_err(|err| err.to_string())?;
|
||||
let draw_output = self
|
||||
.draw(canvas, dark_mode)
|
||||
.map_err(|err| err.to_string())?;
|
||||
let map_coord = draw_output.0;
|
||||
|
||||
let chart_output = ChartOutput {
|
||||
|
||||
31
src/misc.rs
31
src/misc.rs
@ -2,12 +2,14 @@ use wasm_bindgen::prelude::*;
|
||||
|
||||
pub type DrawResult<T> = Result<T, Box<dyn std::error::Error>>;
|
||||
|
||||
// EXTREMELY Janky function that tries to put asterisks in the proper places to be parsed. This is so cursed. But it works, and I hopefully won't ever have to touch it again.
|
||||
// EXTREMELY Janky function that tries to put asterisks in the proper places to be parsed. This is so cursed. But it works, and I hopefully won't ever have to touch it again.
|
||||
// One limitation though, variables with multiple characters like `pi` cannot be multiplied (like `pipipipi` won't result in `pi*pi*pi*pi`). But that's such a niche use case (and that same thing could be done by using exponents) that it doesn't really matter.
|
||||
pub fn add_asterisks(function_in: String) -> String {
|
||||
let function = function_in.replace("log10(", "log("); // replace log10 with log
|
||||
let valid_variables: Vec<char> = "xe".chars().collect();
|
||||
let letters: Vec<char>= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".chars().collect();
|
||||
let letters: Vec<char> = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
.chars()
|
||||
.collect();
|
||||
let numbers: Vec<char> = "0123456789".chars().collect();
|
||||
let function_chars: Vec<char> = function.chars().collect();
|
||||
let func_chars_len = function_chars.len();
|
||||
@ -16,27 +18,27 @@ pub fn add_asterisks(function_in: String) -> String {
|
||||
for c in function_chars.clone() {
|
||||
let mut add_asterisk: bool = false;
|
||||
let prev_chars_len = prev_chars.len();
|
||||
let curr_i: usize = func_chars_len-prev_chars_len;
|
||||
let curr_i: usize = func_chars_len - prev_chars_len;
|
||||
|
||||
let prev_prev_char = if prev_chars_len >= 2 {
|
||||
match prev_chars.get(prev_chars_len-2) {
|
||||
match prev_chars.get(prev_chars_len - 2) {
|
||||
Some(x) => *x,
|
||||
None => panic!()
|
||||
None => panic!(),
|
||||
}
|
||||
} else {
|
||||
' '
|
||||
};
|
||||
|
||||
let prev_char = if prev_chars_len >= 1 {
|
||||
match prev_chars.get(prev_chars_len-1) {
|
||||
match prev_chars.get(prev_chars_len - 1) {
|
||||
Some(x) => *x,
|
||||
None => panic!()
|
||||
None => panic!(),
|
||||
}
|
||||
} else {
|
||||
' '
|
||||
};
|
||||
|
||||
let for_char = match function_chars.get(curr_i) {
|
||||
let for_char = match function_chars.get(curr_i) {
|
||||
Some(x) => *x,
|
||||
None => ' ',
|
||||
};
|
||||
@ -44,13 +46,16 @@ pub fn add_asterisks(function_in: String) -> String {
|
||||
let prev_pi = (prev_prev_char == 'p') && (prev_char == 'i');
|
||||
let for_pi = (for_char == 'i') && (c == 'p');
|
||||
|
||||
|
||||
if prev_char == ')' {
|
||||
if (c == '(') | numbers.contains(&c) | letters.contains(&c) {
|
||||
add_asterisk = true;
|
||||
}
|
||||
} else if c == '(' {
|
||||
if (valid_variables.contains(&prev_char) | (prev_char == ')') | numbers.contains(&prev_char)) && !letters.contains(&prev_prev_char) {
|
||||
if (valid_variables.contains(&prev_char)
|
||||
| (prev_char == ')')
|
||||
| numbers.contains(&prev_char))
|
||||
&& !letters.contains(&prev_prev_char)
|
||||
{
|
||||
add_asterisk = true;
|
||||
} else if prev_pi {
|
||||
add_asterisk = true;
|
||||
@ -62,7 +67,9 @@ pub fn add_asterisks(function_in: String) -> String {
|
||||
} else if letters.contains(&c) {
|
||||
if numbers.contains(&prev_char) {
|
||||
add_asterisk = true;
|
||||
} else if (valid_variables.contains(&prev_char) && valid_variables.contains(&c)) | prev_pi {
|
||||
} else if (valid_variables.contains(&prev_char) && valid_variables.contains(&c))
|
||||
| prev_pi
|
||||
{
|
||||
add_asterisk = true;
|
||||
}
|
||||
} else if numbers.contains(&c) {
|
||||
@ -184,4 +191,4 @@ fn asterisk_test() {
|
||||
assert_eq!(&add_asterisks("2e".to_string()), "2*e");
|
||||
assert_eq!(&add_asterisks("2log10(x)".to_string()), "2*log(x)");
|
||||
assert_eq!(&add_asterisks("2log(x)".to_string()), "2*log(x)");
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user