fix error handling
This commit is contained in:
parent
06d5a32d09
commit
0cbf4b5dc7
@ -207,6 +207,10 @@ pub struct FunctionEntry {
|
|||||||
roots_data: Option<Vec<Value>>,
|
roots_data: Option<Vec<Value>>,
|
||||||
|
|
||||||
autocomplete: AutoComplete,
|
autocomplete: AutoComplete,
|
||||||
|
|
||||||
|
invalid: bool,
|
||||||
|
|
||||||
|
test_result: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for FunctionEntry {
|
impl Default for FunctionEntry {
|
||||||
@ -225,6 +229,8 @@ impl Default for FunctionEntry {
|
|||||||
extrema_data: None,
|
extrema_data: None,
|
||||||
roots_data: None,
|
roots_data: None,
|
||||||
autocomplete: AutoComplete::default(),
|
autocomplete: AutoComplete::default(),
|
||||||
|
invalid: true,
|
||||||
|
test_result: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -235,6 +241,7 @@ impl FunctionEntry {
|
|||||||
pub fn auto_complete(&mut self, ui: &mut egui::Ui, string: &mut String) -> bool {
|
pub fn auto_complete(&mut self, ui: &mut egui::Ui, string: &mut String) -> bool {
|
||||||
self.autocomplete.ui(ui, string)
|
self.autocomplete.ui(ui, string)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update function settings
|
/// Update function settings
|
||||||
pub fn update(
|
pub fn update(
|
||||||
&mut self, raw_func_str: &str, integral: bool, derivative: bool,
|
&mut self, raw_func_str: &str, integral: bool, derivative: bool,
|
||||||
@ -244,13 +251,20 @@ impl FunctionEntry {
|
|||||||
let output = crate::parsing::test_func(&processed_func);
|
let output = crate::parsing::test_func(&processed_func);
|
||||||
self.raw_func_str = raw_func_str.to_string();
|
self.raw_func_str = raw_func_str.to_string();
|
||||||
if output.is_some() {
|
if output.is_some() {
|
||||||
|
self.test_result = output.clone();
|
||||||
|
self.invalid = true;
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
self.invalid = false;
|
||||||
|
|
||||||
self.function = BackingFunction::new(&processed_func);
|
self.function = BackingFunction::new(&processed_func);
|
||||||
self.invalidate_whole();
|
self.invalidate_whole();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if self.invalid {
|
||||||
|
return self.test_result.clone();
|
||||||
|
}
|
||||||
|
|
||||||
self.derivative = derivative;
|
self.derivative = derivative;
|
||||||
self.integral = integral;
|
self.integral = integral;
|
||||||
return None;
|
return None;
|
||||||
@ -345,6 +359,10 @@ impl FunctionEntry {
|
|||||||
pub fn calculate(
|
pub fn calculate(
|
||||||
&mut self, min_x: &f64, max_x: &f64, width_changed: bool, settings: &AppSettings,
|
&mut self, min_x: &f64, max_x: &f64, width_changed: bool, settings: &AppSettings,
|
||||||
) {
|
) {
|
||||||
|
if self.invalid {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let resolution: f64 = settings.plot_width as f64 / (max_x.abs() + min_x.abs());
|
let resolution: f64 = settings.plot_width as f64 / (max_x.abs() + min_x.abs());
|
||||||
let resolution_iter = resolution_helper(&settings.plot_width + 1, min_x, &resolution);
|
let resolution_iter = resolution_helper(&settings.plot_width + 1, min_x, &resolution);
|
||||||
|
|
||||||
@ -459,6 +477,10 @@ impl FunctionEntry {
|
|||||||
/// Displays the function's output on PlotUI `plot_ui` with settings
|
/// Displays the function's output on PlotUI `plot_ui` with settings
|
||||||
/// `settings`. Returns an `Option<f64>` of the calculated integral
|
/// `settings`. Returns an `Option<f64>` of the calculated integral
|
||||||
pub fn display(&self, plot_ui: &mut PlotUi, settings: &AppSettings) -> Option<f64> {
|
pub fn display(&self, plot_ui: &mut PlotUi, settings: &AppSettings) -> Option<f64> {
|
||||||
|
if self.invalid {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
let derivative_str = self.function.get_derivative_str();
|
let derivative_str = self.function.get_derivative_str();
|
||||||
let step = (settings.integral_min_x - settings.integral_max_x).abs()
|
let step = (settings.integral_min_x - settings.integral_max_x).abs()
|
||||||
/ (settings.integral_num as f64);
|
/ (settings.integral_num as f64);
|
||||||
|
|||||||
@ -506,9 +506,15 @@ impl MathApp {
|
|||||||
integral_enabled.bitxor_assign(integral_toggle);
|
integral_enabled.bitxor_assign(integral_toggle);
|
||||||
derivative_enabled.bitxor_assign(derivative_toggle);
|
derivative_enabled.bitxor_assign(derivative_toggle);
|
||||||
|
|
||||||
self.func_errors[i] = function
|
let update_result = function
|
||||||
.update(&self.func_strs[i], integral_enabled, derivative_enabled)
|
.update(&self.func_strs[i], integral_enabled, derivative_enabled)
|
||||||
.map(|error| (i, error));
|
.map(|error| (i, error));
|
||||||
|
|
||||||
|
if update_result.is_some() {
|
||||||
|
self.exists_error = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.func_errors[i] = update_result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove function if the user requests it
|
// Remove function if the user requests it
|
||||||
@ -709,7 +715,6 @@ impl epi::App for MathApp {
|
|||||||
|
|
||||||
dyn_mut_iter(&mut self.functions)
|
dyn_mut_iter(&mut self.functions)
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.filter(|(i, _)| !self.func_strs[*i].is_empty())
|
|
||||||
.for_each(|(_, function)| {
|
.for_each(|(_, function)| {
|
||||||
function.calculate(
|
function.calculate(
|
||||||
&minx_bounds,
|
&minx_bounds,
|
||||||
@ -723,7 +728,6 @@ impl epi::App for MathApp {
|
|||||||
.functions
|
.functions
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.filter(|(i, _)| !self.func_strs[*i].is_empty())
|
|
||||||
.map(|(_, function)| function.display(plot_ui, &self.settings))
|
.map(|(_, function)| function.display(plot_ui, &self.settings))
|
||||||
.collect();
|
.collect();
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user