From 8307b06c56934aa74985a0fee1271f1bc299b83f Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Sat, 2 May 2020 21:50:35 -0400 Subject: [PATCH] bug: fix bug with multiple tokens --- src/app/query.rs | 36 +++++++++++++++-- src/app/states.rs | 20 +++++++--- src/canvas/canvas_colours.rs | 2 + src/canvas/widgets/process_table.rs | 61 +++++++++++++++++++---------- src/constants.rs | 2 - src/utils/error.rs | 4 +- 6 files changed, 89 insertions(+), 36 deletions(-) diff --git a/src/app/query.rs b/src/app/query.rs index 991e5b71..7a890921 100644 --- a/src/app/query.rs +++ b/src/app/query.rs @@ -39,9 +39,25 @@ pub trait ProcessQuery { impl ProcessQuery for ProcWidgetState { fn parse_query(&self) -> Result { fn process_string_to_filter(query: &mut VecDeque) -> Result { - Ok(Query { - query: process_and(query)?, - }) + let mut lhs: And = process_and(query)?; + + while query.front().is_some() { + let rhs = Some(Box::new(process_or(query)?)); + + lhs = And { + lhs: Or { + lhs: Prefix { + and: Some(Box::from(lhs)), + compare_prefix: None, + regex_prefix: None, + }, + rhs: None, + }, + rhs, + }; + } + + Ok(Query { query: lhs }) } fn process_and(query: &mut VecDeque) -> Result { @@ -113,6 +129,10 @@ impl ProcessQuery for ProcWidgetState { fn process_prefix(query: &mut VecDeque) -> Result { if let Some(queue_top) = query.pop_front() { if queue_top == "(" { + if query.front().is_none() { + return Err(QueryError("Missing closing parentheses".into())); + } + // Get content within bracket; and check if paren is complete let and = process_and(query)?; if let Some(close_paren) = query.pop_front() { @@ -190,6 +210,8 @@ impl ProcessQuery for ProcWidgetState { condition = Some(QueryComparison::Equal); if let Some(queue_next) = query.pop_front() { value = queue_next.parse::().ok(); + } else { + return Err(QueryError("Missing value".into())); } } else if content == ">" || content == "<" { // We also have to check if the next string is an "="... @@ -202,6 +224,8 @@ impl ProcessQuery for ProcWidgetState { }); if let Some(queue_next_next) = query.pop_front() { value = queue_next_next.parse::().ok(); + } else { + return Err(QueryError("Missing value".into())); } } else { condition = Some(if content == ">" { @@ -211,6 +235,8 @@ impl ProcessQuery for ProcWidgetState { }); value = queue_next.parse::().ok(); } + } else { + return Err(QueryError("Missing value".into())); } } @@ -286,11 +312,13 @@ impl ProcessQuery for ProcWidgetState { } } } + } else { + return Err(QueryError("Missing argument for search prefix".into())); } } } - Err(QueryError("Failed to parse comparator.".into())) + Err(QueryError("Invalid search".into())) } let mut split_query = VecDeque::new(); diff --git a/src/app/states.rs b/src/app/states.rs index 3d45a572..bcd4da40 100644 --- a/src/app/states.rs +++ b/src/app/states.rs @@ -74,6 +74,7 @@ pub struct AppSearchState { pub char_cursor_position: usize, /// The query pub query: Option, + pub error_message: Option, } impl Default for AppSearchState { @@ -88,6 +89,7 @@ impl Default for AppSearchState { cursor_bar: 0, char_cursor_position: 0, query: None, + error_message: None, } } } @@ -198,15 +200,21 @@ impl ProcWidgetState { .current_search_query .is_empty() { - self.process_search_state.search_state.is_invalid_search = false; self.process_search_state.search_state.is_blank_search = true; - } else if let Ok(parsed_query) = self.parse_query() { - self.process_search_state.search_state.query = Some(parsed_query); - self.process_search_state.search_state.is_blank_search = false; self.process_search_state.search_state.is_invalid_search = false; + self.process_search_state.search_state.error_message = None; } else { - self.process_search_state.search_state.is_blank_search = false; - self.process_search_state.search_state.is_invalid_search = true; + let parsed_query = self.parse_query(); + if let Ok(parsed_query) = parsed_query { + self.process_search_state.search_state.query = Some(parsed_query); + self.process_search_state.search_state.is_blank_search = false; + self.process_search_state.search_state.is_invalid_search = false; + self.process_search_state.search_state.error_message = None; + } else if let Err(err) = parsed_query { + self.process_search_state.search_state.is_blank_search = false; + self.process_search_state.search_state.is_invalid_search = true; + self.process_search_state.search_state.error_message = Some(err.to_string()); + } } self.scroll_state.previous_scroll_position = 0; self.scroll_state.current_scroll_position = 0; diff --git a/src/canvas/canvas_colours.rs b/src/canvas/canvas_colours.rs index 1e1fe236..6be85866 100644 --- a/src/canvas/canvas_colours.rs +++ b/src/canvas/canvas_colours.rs @@ -26,6 +26,7 @@ pub struct CanvasColours { pub graph_style: Style, // Full, Medium, Low pub battery_bar_styles: Vec