| 1 | use std::collections::VecDeque; |
| 2 | |
| 3 | use crate::{ |
| 4 | collection::processes::ProcessHarvest, |
| 5 | widgets::query::{ |
| 6 | And, COMPARISON_LIST, Prefix, QueryOptions, QueryProcessor, QueryResult, error::QueryError, |
| 7 | }, |
| 8 | }; |
| 9 | |
| 10 | /// A node where either the left-hand side or the right-hand side are |
| 11 | /// considered. Note that the right-hand side is optional, as that's how I |
| 12 | /// implemented it a long time ago. |
| 13 | #[derive(Debug)] |
| 14 | pub(super) struct Or { |
| 15 | pub(super) lhs: And, |
| 16 | // TODO: Maybe don't need to box rhs? |
| 17 | pub(super) rhs: Option<Box<And>>, |
| 18 | } |
| 19 | |
| 20 | impl Or { |
| 21 | pub(super) fn check(&self, process: &ProcessHarvest, is_using_command: bool) -> bool { |
| 22 | if let Some(rhs) = &self.rhs { |
| 23 | self.lhs.check(process, is_using_command) || rhs.check(process, is_using_command) |
| 24 | } else { |
| 25 | self.lhs.check(process, is_using_command) |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | impl QueryProcessor for Or { |
| 31 | fn process(query: &mut VecDeque<String>, options: &QueryOptions) -> QueryResult<Self> |
| 32 | where |
| 33 | Self: Sized, |
| 34 | { |
| 35 | const OR_LIST: [&str; 2] = ["or", "||"]; |
| 36 | |
| 37 | let mut lhs = And::process(query, options)?; |
| 38 | let mut rhs: Option<Box<And>> = None; |
| 39 | |
| 40 | while let Some(queue_top) = query.front() { |
| 41 | let current_lowercase = queue_top.to_lowercase(); |
| 42 | if OR_LIST.contains(¤t_lowercase.as_str()) { |
| 43 | query.pop_front(); |
| 44 | rhs = Some(Box::new(And::process(query, options)?)); |
| 45 | |
| 46 | if let Some(queue_next) = query.front() { |
| 47 | if OR_LIST.contains(&queue_next.to_lowercase().as_str()) { |
| 48 | // Must merge LHS and RHS |
| 49 | lhs = And { |
| 50 | lhs: Prefix::Or(Box::new(Or { lhs, rhs })), |
| 51 | rhs: None, |
| 52 | }; |
| 53 | rhs = None; |
| 54 | } |
| 55 | } else { |
| 56 | break; |
| 57 | } |
| 58 | } else if COMPARISON_LIST.contains(¤t_lowercase.as_str()) { |
| 59 | return Err(QueryError::new("Comparison not valid here")); |
| 60 | } else { |
| 61 | break; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | Ok(Or { lhs, rhs }) |
| 66 | } |
| 67 | } |