From a7b4b54f5246944e43b62ee80c192406543c381a Mon Sep 17 00:00:00 2001 From: Pavel Ivanov Date: Fri, 23 Feb 2024 20:15:57 +0100 Subject: [PATCH] fix: fixed composition logic of queries and filters (#134) --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/app.rs | 8 ++++---- src/main.rs | 10 ++++++++-- src/model.rs | 2 +- src/query.rs | 8 ++++++++ 6 files changed, 23 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e0236986..cef1b792 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -738,7 +738,7 @@ checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" [[package]] name = "hl" -version = "0.25.2-alpha.1" +version = "0.25.2" dependencies = [ "atoi", "bincode", diff --git a/Cargo.toml b/Cargo.toml index d94b0dbd..f0e6e41d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ categories = ["command-line-utilities"] description = "Utility for viewing json-formatted log files." keywords = ["cli", "human", "log"] name = "hl" -version = "0.25.2-alpha.1" +version = "0.25.2" edition = "2021" build = "build.rs" diff --git a/src/app.rs b/src/app.rs index 8e32ee8d..c174fbf9 100644 --- a/src/app.rs +++ b/src/app.rs @@ -72,10 +72,10 @@ pub struct Options { impl Options { fn filter_and_query<'a>(&'a self) -> Box { match (self.filter.is_empty(), &self.query) { - (true, None) => return Box::new(QueryNone{}), - (false, None) => return Box::new(&self.filter), - (false, Some(query)) => return Box::new(query), - (true, Some(query)) => return Box::new((&self.filter).and(query)), + (true, None) => Box::new(QueryNone{}), + (false, None) => Box::new(&self.filter), + (true, Some(query)) => Box::new(query), + (false, Some(query)) => Box::new((&self.filter).and(query)), } } } diff --git a/src/main.rs b/src/main.rs index ca0b71ed..8da25186 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,6 +22,7 @@ use hl::error::*; use hl::input::InputReference; use hl::level::{LevelValueParser, RelaxedLevel}; use hl::output::{OutputStream, Pager}; +use hl::query::Query; use hl::settings::Settings; use hl::signal::SignalHandler; use hl::theme::{Theme, ThemeOrigin}; @@ -370,9 +371,14 @@ fn run() -> Result<()> { let max_message_size = opt.max_message_size; let buffer_size = std::cmp::min(max_message_size, opt.buffer_size); - let mut query = None; + let mut query: Option = None; for q in opt.query { - query = Some(hl::query::parse(&q)?); + let right = hl::query::parse(&q)?; + if let Some(left) = query { + query = Some(hl::query::and(left, right)); + } else { + query = Some(right); + } } // Create app. diff --git a/src/model.rs b/src/model.rs index 659986cd..633bf5a5 100644 --- a/src/model.rs +++ b/src/model.rs @@ -144,7 +144,7 @@ impl RecordFilter for Box { } } -impl RecordFilter for &T { +impl RecordFilter for &T { #[inline(always)] fn apply<'a>(&self, record: &'a Record<'a>) -> bool { (**self).apply(record) diff --git a/src/query.rs b/src/query.rs index ed78de26..5b5791b6 100644 --- a/src/query.rs +++ b/src/query.rs @@ -28,6 +28,14 @@ pub fn parse(str: &str) -> Result { Ok(expression(pairs.next().unwrap())?) } +pub fn and(lhs: Query, rhs: Query) -> Query { + Box::new(And { lhs, rhs }) +} + +pub fn or(lhs: Query, rhs: Query) -> Query { + Box::new(Or { lhs, rhs }) +} + fn expression(pair: Pair) -> Result { match pair.as_rule() { Rule::expr_or => binary_op::(pair),