From 948c1206e9e72ffbcb893a400b25502251f2dec0 Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Thu, 21 May 2020 16:55:51 -0400 Subject: [PATCH] chore: wrote better debugging printing for queries This should make reading completed queries a bit easier for debugging --- src/app/query.rs | 46 +++++++++++++++++++++++++++++++++++++++++----- src/app/states.rs | 2 +- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/app/query.rs b/src/app/query.rs index 307256d5..05f74aef 100644 --- a/src/app/query.rs +++ b/src/app/query.rs @@ -77,6 +77,7 @@ impl ProcessQuery for ProcWidgetState { let mut rhs: Option> = None; while let Some(queue_top) = query.front() { + debug!("OR QT: {:?}", queue_top); if OR_LIST.contains(&queue_top.to_lowercase().as_str()) { query.pop_front(); rhs = Some(Box::new(process_and(query)?)); @@ -110,6 +111,7 @@ impl ProcessQuery for ProcWidgetState { let mut rhs: Option> = None; while let Some(queue_top) = query.front() { + debug!("AND QT: {:?}", queue_top); if queue_top == ")" { break; } else if AND_LIST.contains(&queue_top.to_lowercase().as_str()) { @@ -138,7 +140,7 @@ impl ProcessQuery for ProcWidgetState { fn process_prefix(query: &mut VecDeque, inside_quotations: bool) -> Result { if let Some(queue_top) = query.pop_front() { - // debug!("QT: {:?}", queue_top); + debug!("Prefix QT: {:?}", queue_top); if !inside_quotations && queue_top == "(" { if query.front().is_none() { return Err(QueryError("Missing closing parentheses".into())); @@ -407,7 +409,6 @@ impl ProcessQuery for ProcWidgetState { } } -#[derive(Debug)] pub struct Query { /// Remember, AND > OR, but AND must come after OR when we parse. pub query: And, @@ -430,7 +431,12 @@ impl Query { } } -#[derive(Debug)] +impl std::fmt::Debug for Query { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_fmt(format_args!("{:?}", self.query)) + } +} + pub struct Or { pub lhs: And, pub rhs: Option>, @@ -466,7 +472,15 @@ impl Or { } } -#[derive(Debug)] +impl std::fmt::Debug for Or { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match &self.rhs { + Some(rhs) => f.write_fmt(format_args!("({:?} OR {:?})", self.lhs, rhs)), + None => f.write_fmt(format_args!("{:?}", self.lhs)), + } + } +} + pub struct And { pub lhs: Prefix, pub rhs: Option>, @@ -502,6 +516,15 @@ impl And { } } +impl std::fmt::Debug for And { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match &self.rhs { + Some(rhs) => f.write_fmt(format_args!("({:?} AND {:?})", self.lhs, rhs)), + None => f.write_fmt(format_args!("{:?}", self.lhs)), + } + } +} + #[derive(Debug)] pub enum PrefixType { Pid, @@ -535,7 +558,6 @@ impl std::str::FromStr for PrefixType { } } -#[derive(Debug)] pub struct Prefix { pub or: Option>, pub regex_prefix: Option<(PrefixType, StringQuery)>, @@ -650,6 +672,20 @@ impl Prefix { } } +impl std::fmt::Debug for Prefix { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + if let Some(or) = &self.or { + f.write_fmt(format_args!("{:?}", or)) + } else if let Some(regex_prefix) = &self.regex_prefix { + f.write_fmt(format_args!("{:?}", regex_prefix)) + } else if let Some(compare_prefix) = &self.compare_prefix { + f.write_fmt(format_args!("{:?}", compare_prefix)) + } else { + f.write_fmt(format_args!("")) + } + } +} + #[derive(Debug)] pub enum QueryComparison { Equal, diff --git a/src/app/states.rs b/src/app/states.rs index b5e97ae4..47cb5657 100644 --- a/src/app/states.rs +++ b/src/app/states.rs @@ -205,7 +205,7 @@ impl ProcWidgetState { self.process_search_state.search_state.error_message = None; } else { let parsed_query = self.parse_query(); - // debug!("PQ: {:#?}", parsed_query); + debug!("Parsed query: {:?}", parsed_query); if let Ok(parsed_query) = parsed_query { self.process_search_state.search_state.query = Some(parsed_query);