diff options
author | ClementTsang <cjhtsang@uwaterloo.ca> | 2020-05-21 22:06:51 -0400 |
---|---|---|
committer | ClementTsang <cjhtsang@uwaterloo.ca> | 2020-05-21 22:06:51 -0400 |
commit | cef81d1b8db3e75aa768c4103776fa8c513c18cc (patch) | |
tree | 073abc04579e43fb9668baed280f78b02478cbbd | |
parent | c7edad91ed82ea24ed031c13430e3a531e5d0284 (diff) |
refactor: use fold to avoid clones in search for implied AND
-rw-r--r-- | src/app/query.rs | 44 |
1 files changed, 21 insertions, 23 deletions
diff --git a/src/app/query.rs b/src/app/query.rs index 6a57975c..6b3d904d 100644 --- a/src/app/query.rs +++ b/src/app/query.rs @@ -184,7 +184,7 @@ impl ProcessQuery for ProcWidgetState { } // Now convert this back to a OR... - let mut returned_or = Or { + let initial_or = Or { lhs: And { lhs: Prefix { or: Some(Box::new(list_of_ors.pop_front().unwrap())), @@ -195,22 +195,20 @@ impl ProcessQuery for ProcWidgetState { }, rhs: None, }; - list_of_ors.into_iter().for_each(|rhs| { - returned_or = Or { - lhs: And { - lhs: Prefix { - or: Some(Box::new(returned_or.clone())), - compare_prefix: None, - regex_prefix: None, - }, - rhs: Some(Box::new(Prefix { - or: Some(Box::new(rhs)), - compare_prefix: None, - regex_prefix: None, - })), + let returned_or = list_of_ors.into_iter().fold(initial_or, |lhs, rhs| Or { + lhs: And { + lhs: Prefix { + or: Some(Box::new(lhs)), + compare_prefix: None, + regex_prefix: None, }, - rhs: None, - }; + rhs: Some(Box::new(Prefix { + or: Some(Box::new(rhs)), + compare_prefix: None, + regex_prefix: None, + })), + }, + rhs: None, }); if let Some(close_paren) = query.pop_front() { @@ -471,7 +469,7 @@ impl Debug for Query { } } -#[derive(Default, Clone)] +#[derive(Default)] pub struct Or { pub lhs: And, pub rhs: Option<Box<And>>, @@ -516,7 +514,7 @@ impl Debug for Or { } } -#[derive(Default, Clone)] +#[derive(Default)] pub struct And { pub lhs: Prefix, pub rhs: Option<Box<Prefix>>, @@ -561,7 +559,7 @@ impl Debug for And { } } -#[derive(Debug, Clone)] +#[derive(Debug)] pub enum PrefixType { Pid, Cpu, @@ -594,7 +592,7 @@ impl std::str::FromStr for PrefixType { } } -#[derive(Default, Clone)] +#[derive(Default)] pub struct Prefix { pub or: Option<Box<Or>>, pub regex_prefix: Option<(PrefixType, StringQuery)>, @@ -724,7 +722,7 @@ impl Debug for Prefix { } } -#[derive(Debug, Clone)] +#[derive(Debug)] pub enum QueryComparison { Equal, Less, @@ -733,13 +731,13 @@ pub enum QueryComparison { GreaterOrEqual, } -#[derive(Debug, Clone)] +#[derive(Debug)] pub enum StringQuery { Value(String), Regex(regex::Regex), } -#[derive(Debug, Clone)] +#[derive(Debug)] pub struct NumericalQuery { pub condition: QueryComparison, pub value: f64, |