simplify error handling code
This commit is contained in:
parent
061f5cfbd4
commit
6217f0aff4
@ -86,20 +86,17 @@ impl Default for FunctionEntry {
|
|||||||
|
|
||||||
impl FunctionEntry {
|
impl FunctionEntry {
|
||||||
/// Create autocomplete ui and handle user input
|
/// Create autocomplete ui and handle user input
|
||||||
pub fn auto_complete(&mut self, ui: &mut egui::Ui, i: i32) -> (bool, Option<String>) {
|
pub fn auto_complete(&mut self, ui: &mut egui::Ui, i: i32) {
|
||||||
let mut output_string: String = self.raw_func_str.clone();
|
let mut output_string: String = self.raw_func_str.clone();
|
||||||
self.autocomplete.ui(ui, &mut output_string, i);
|
self.autocomplete.ui(ui, &mut output_string, i);
|
||||||
|
|
||||||
let changed = output_string != self.raw_func_str;
|
if output_string != self.raw_func_str {
|
||||||
if changed {
|
|
||||||
self.update_string(&output_string);
|
self.update_string(&output_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
(changed, self.get_test_result())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get function's cached test result
|
/// Get function's cached test result
|
||||||
pub fn get_test_result(&self) -> Option<String> { self.test_result.clone() }
|
pub fn get_test_result(&self) -> &Option<String> { &self.test_result }
|
||||||
|
|
||||||
/// Update function string and test it
|
/// Update function string and test it
|
||||||
fn update_string(&mut self, raw_func_str: &str) {
|
fn update_string(&mut self, raw_func_str: &str) {
|
||||||
|
|||||||
@ -286,13 +286,6 @@ pub struct MathApp {
|
|||||||
/// Stores vector of functions
|
/// Stores vector of functions
|
||||||
functions: Vec<FunctionEntry>,
|
functions: Vec<FunctionEntry>,
|
||||||
|
|
||||||
/// Stores last error from parsing functions (used to display the same error
|
|
||||||
/// when side panel is minimized)
|
|
||||||
func_errors: Vec<Option<String>>,
|
|
||||||
|
|
||||||
/// Stores whether or not an error is stored in `self.func_errors`
|
|
||||||
exists_error: bool,
|
|
||||||
|
|
||||||
/// Contains the list of Areas calculated (the vector of f64) and time it
|
/// Contains the list of Areas calculated (the vector of f64) and time it
|
||||||
/// took for the last frame (the Duration). Stored in a Tuple.
|
/// took for the last frame (the Duration). Stored in a Tuple.
|
||||||
last_info: (Vec<Option<f64>>, Duration),
|
last_info: (Vec<Option<f64>>, Duration),
|
||||||
@ -311,8 +304,6 @@ impl Default for MathApp {
|
|||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
functions: vec![DEFAULT_FUNCTION_ENTRY.clone()],
|
functions: vec![DEFAULT_FUNCTION_ENTRY.clone()],
|
||||||
func_errors: vec![None],
|
|
||||||
exists_error: false,
|
|
||||||
last_info: (vec![None], Duration::ZERO),
|
last_info: (vec![None], Duration::ZERO),
|
||||||
dark_mode: true,
|
dark_mode: true,
|
||||||
opened: Opened::default(),
|
opened: Opened::default(),
|
||||||
@ -442,7 +433,6 @@ impl MathApp {
|
|||||||
|
|
||||||
let functions_len = self.functions.len();
|
let functions_len = self.functions.len();
|
||||||
let mut remove_i: Option<usize> = None;
|
let mut remove_i: Option<usize> = None;
|
||||||
self.exists_error = false;
|
|
||||||
for (i, function) in self.functions.iter_mut().enumerate() {
|
for (i, function) in self.functions.iter_mut().enumerate() {
|
||||||
// Entry for a function
|
// Entry for a function
|
||||||
ui.horizontal(|ui| {
|
ui.horizontal(|ui| {
|
||||||
@ -479,21 +469,13 @@ impl MathApp {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Contains the function string in a text box that the user can edit
|
// Contains the function string in a text box that the user can edit
|
||||||
let (changed, error) = function.auto_complete(ui, i as i32);
|
function.auto_complete(ui, i as i32)
|
||||||
|
|
||||||
if let Some(error_string) = error {
|
|
||||||
self.exists_error = true;
|
|
||||||
if changed {
|
|
||||||
self.func_errors[i] = Some(error_string);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove function if the user requests it
|
// Remove function if the user requests it
|
||||||
if let Some(remove_i_unwrap) = remove_i {
|
if let Some(remove_i_unwrap) = remove_i {
|
||||||
self.functions.remove(remove_i_unwrap);
|
self.functions.remove(remove_i_unwrap);
|
||||||
self.func_errors.remove(remove_i_unwrap);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hyperlink to project's github
|
// Hyperlink to project's github
|
||||||
@ -557,7 +539,6 @@ impl epi::App for MathApp {
|
|||||||
.clicked()
|
.clicked()
|
||||||
{
|
{
|
||||||
self.functions.push(DEFAULT_FUNCTION_ENTRY.clone());
|
self.functions.push(DEFAULT_FUNCTION_ENTRY.clone());
|
||||||
self.func_errors.push(None);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Toggles opening the Help window
|
// Toggles opening the Help window
|
||||||
@ -665,15 +646,19 @@ impl epi::App for MathApp {
|
|||||||
// parsing)
|
// parsing)
|
||||||
CentralPanel::default().show(ctx, |ui| {
|
CentralPanel::default().show(ctx, |ui| {
|
||||||
// Display an error if it exists
|
// Display an error if it exists
|
||||||
if self.exists_error {
|
let errors_formatted: Vec<String> = self
|
||||||
ui.centered_and_justified(|ui| {
|
.functions
|
||||||
self.func_errors
|
|
||||||
.iter()
|
.iter()
|
||||||
|
.map(|func| func.get_test_result())
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.filter(|(_, error)| error.is_some())
|
.filter(|(_, error)| error.is_some())
|
||||||
.map(|(i, error)| (i, error.as_ref().unwrap()))
|
.map(|(i, error)| format!("(Function #{}) {}\n", i, error.as_ref().unwrap()))
|
||||||
.for_each(|(i, error)| {
|
.collect();
|
||||||
ui.heading(format!("(Function #{}) {}\n", i, error));
|
|
||||||
|
if errors_formatted.len() > 0 {
|
||||||
|
ui.centered_and_justified(|ui| {
|
||||||
|
errors_formatted.iter().for_each(|string| {
|
||||||
|
ui.heading(string);
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user