refactor: use fold to avoid clones in search for implied AND

This commit is contained in:
ClementTsang 2020-05-21 22:06:51 -04:00
parent c7edad91ed
commit cef81d1b8d

View File

@ -184,7 +184,7 @@ impl ProcessQuery for ProcWidgetState {
} }
// Now convert this back to a OR... // Now convert this back to a OR...
let mut returned_or = Or { let initial_or = Or {
lhs: And { lhs: And {
lhs: Prefix { lhs: Prefix {
or: Some(Box::new(list_of_ors.pop_front().unwrap())), or: Some(Box::new(list_of_ors.pop_front().unwrap())),
@ -195,22 +195,20 @@ impl ProcessQuery for ProcWidgetState {
}, },
rhs: None, rhs: None,
}; };
list_of_ors.into_iter().for_each(|rhs| { let returned_or = list_of_ors.into_iter().fold(initial_or, |lhs, rhs| Or {
returned_or = Or { lhs: And {
lhs: And { lhs: Prefix {
lhs: Prefix { or: Some(Box::new(lhs)),
or: Some(Box::new(returned_or.clone())), compare_prefix: None,
compare_prefix: None, regex_prefix: None,
regex_prefix: None,
},
rhs: Some(Box::new(Prefix {
or: Some(Box::new(rhs)),
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() { 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 struct Or {
pub lhs: And, pub lhs: And,
pub rhs: Option<Box<And>>, pub rhs: Option<Box<And>>,
@ -516,7 +514,7 @@ impl Debug for Or {
} }
} }
#[derive(Default, Clone)] #[derive(Default)]
pub struct And { pub struct And {
pub lhs: Prefix, pub lhs: Prefix,
pub rhs: Option<Box<Prefix>>, pub rhs: Option<Box<Prefix>>,
@ -561,7 +559,7 @@ impl Debug for And {
} }
} }
#[derive(Debug, Clone)] #[derive(Debug)]
pub enum PrefixType { pub enum PrefixType {
Pid, Pid,
Cpu, Cpu,
@ -594,7 +592,7 @@ impl std::str::FromStr for PrefixType {
} }
} }
#[derive(Default, Clone)] #[derive(Default)]
pub struct Prefix { pub struct Prefix {
pub or: Option<Box<Or>>, pub or: Option<Box<Or>>,
pub regex_prefix: Option<(PrefixType, StringQuery)>, pub regex_prefix: Option<(PrefixType, StringQuery)>,
@ -724,7 +722,7 @@ impl Debug for Prefix {
} }
} }
#[derive(Debug, Clone)] #[derive(Debug)]
pub enum QueryComparison { pub enum QueryComparison {
Equal, Equal,
Less, Less,
@ -733,13 +731,13 @@ pub enum QueryComparison {
GreaterOrEqual, GreaterOrEqual,
} }
#[derive(Debug, Clone)] #[derive(Debug)]
pub enum StringQuery { pub enum StringQuery {
Value(String), Value(String),
Regex(regex::Regex), Regex(regex::Regex),
} }
#[derive(Debug, Clone)] #[derive(Debug)]
pub struct NumericalQuery { pub struct NumericalQuery {
pub condition: QueryComparison, pub condition: QueryComparison,
pub value: f64, pub value: f64,