From 2025528682988dda0244744e3136dde75a5e3627 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Wed, 6 Apr 2022 10:24:32 -0400 Subject: [PATCH] cleanup --- build.sh | 4 ++-- src/function.rs | 52 +++++++++++++++++++++---------------------------- src/widgets.rs | 4 ++-- 3 files changed, 26 insertions(+), 34 deletions(-) diff --git a/build.sh b/build.sh index 747f2fa..582086c 100755 --- a/build.sh +++ b/build.sh @@ -13,13 +13,13 @@ wasm_opt() { } if test "$1" == "" || test "$1" == "release"; then - wasm-pack build --target web --release --no-typescript + RUSTFLAGS=--cfg=web_sys_unstable_apis wasm-pack build --target web --release --no-typescript echo "Binary size (pre-wasm_opt): $(du -sb pkg/ytbn_graphing_software_bg.wasm)" wasm_opt #apply wasm optimizations echo "Binary size (pre-strip): $(du -sb pkg/ytbn_graphing_software_bg.wasm)" llvm-strip --strip-all pkg/ytbn_graphing_software_bg.wasm elif test "$1" == "debug"; then - wasm-pack build --target web --debug --no-typescript + RUSTFLAGS=--cfg=web_sys_unstable_apis wasm-pack build --target web --debug --no-typescript else echo "ERROR: build.sh, argument invalid" exit 1 diff --git a/src/function.rs b/src/function.rs index 3f2180f..e1d8183 100644 --- a/src/function.rs +++ b/src/function.rs @@ -36,12 +36,10 @@ lazy_static::lazy_static! { /// derivatives, etc etc #[derive(Clone)] pub struct FunctionEntry { - /// The `BackingFunction` instance that is used to generate `f(x)`, `f'(x)`, - /// and `f''(x)` + /// The `BackingFunction` instance that is used to generate `f(x)`, `f'(x)`, and `f''(x)` function: BackingFunction, - /// Stores a function string (that hasn't been processed via - /// `process_func_str`) to display to the user + /// Stores a function string (that hasn't been processed via `process_func_str`) to display to the user raw_func_str: String, /// Minimum and Maximum values of what do display @@ -51,8 +49,7 @@ pub struct FunctionEntry { /// If calculating/displayingintegrals are enabled pub integral: bool, - /// If displaying derivatives are enabled (note, they are still calculated - /// for other purposes) + /// If displaying derivatives are enabled (note, they are still calculated for other purposes) pub derivative: bool, back_data: Option>, @@ -88,8 +85,7 @@ impl Default for FunctionEntry { } impl FunctionEntry { - pub fn get_func_raw(&self) -> String { self.raw_func_str.to_string() } - + /// Create autocomplete ui and handle user input pub fn auto_complete(&mut self, ui: &mut egui::Ui, i: i32) -> (bool, bool, Option) { let (output_string, in_focus) = self.autocomplete.ui(ui, self.raw_func_str.clone(), i); @@ -101,8 +97,10 @@ impl FunctionEntry { (in_focus, changed, self.get_test_result()) } + /// Get function's cached test result pub fn get_test_result(&self) -> Option { self.test_result.clone() } + /// Update function string and test it fn update_string(&mut self, raw_func_str: &str) { let processed_func = process_func_str(raw_func_str); let output = crate::parsing::test_func(&processed_func); @@ -118,6 +116,7 @@ impl FunctionEntry { self.invalidate_whole(); } + /// Get function that can be used to calculate integral based on Riemann Sum type fn get_sum_func(&self, sum: Riemann) -> FunctionHelper { match sum { Riemann::Left => { @@ -132,8 +131,7 @@ impl FunctionEntry { } } - /// Creates and does the math for creating all the rectangles under the - /// graph + /// Creates and does the math for creating all the rectangles under the graph fn integral_rectangles( &self, integral_min_x: &f64, integral_max_x: &f64, sum: &Riemann, integral_num: &usize, ) -> (Vec<(f64, f64)>, f64) { @@ -249,7 +247,9 @@ impl FunctionEntry { } }) .collect(); + debug_assert_eq!(back_data.len(), settings.plot_width + 1); + self.back_data = Some(back_data); if derivative_required { @@ -322,8 +322,8 @@ impl FunctionEntry { } } - /// Displays the function's output on PlotUI `plot_ui` with settings - /// `settings`. Returns an `Option` of the calculated integral + /// Displays the function's output on PlotUI `plot_ui` with settings `settings`. + /// Returns an `Option` of the calculated integral. pub fn display( &self, plot_ui: &mut PlotUi, settings: &AppSettings, main_plot_color: Color32, ) -> Option { @@ -341,7 +341,7 @@ impl FunctionEntry { .clone() .to_line() .color(main_plot_color) - .name(self.get_func_raw()), + .name(&self.raw_func_str), ); } @@ -420,21 +420,6 @@ impl FunctionEntry { /// Invalidate Derivative data pub fn invalidate_derivative(&mut self) { self.derivative_data = None; } - /// Depreciated, but still used for tests - #[cfg(test)] - pub fn update( - &mut self, raw_func_str: &str, integral: bool, derivative: bool, - ) -> Option { - self.derivative = derivative; - self.integral = integral; - if raw_func_str != self.get_func_raw() { - self.update_string(raw_func_str); - self.get_test_result() - } else { - None - } - } - /// Runs asserts to make sure everything is the expected value #[cfg(test)] pub fn tests( @@ -468,7 +453,12 @@ impl FunctionEntry { } { - self.update("x^3", false, false); + self.update_string("x^3"); + assert_eq!(&self.raw_func_str, "x^3"); + + self.integral = false; + self.derivative = false; + assert!(!self.integral); assert!(!self.derivative); @@ -541,7 +531,9 @@ mod tests { let settings = app_settings_constructor(sum, -1.0, 1.0, 10, 10); let mut function = FunctionEntry::default(); - function.update("x^2", true, true); + function.update_string("x^2"); + function.integral = true; + function.derivative = true; function.tests( settings, diff --git a/src/widgets.rs b/src/widgets.rs index 41adfea..7aafafa 100644 --- a/src/widgets.rs +++ b/src/widgets.rs @@ -114,8 +114,8 @@ impl AutoComplete { if clicked | apply_key { new_string += hints[self.i]; - // don't need this here as it simply won't be display next - // frame in `math_app.rs` ui.memory().close_popup(); + // don't need this here as it simply won't be display next frame in `math_app.rs` + // ui.memory().close_popup(); true } else {