Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: Simplified query composition API #137

Merged
merged 1 commit into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.3-alpha.2"
version = "0.25.3-alpha.3"
edition = "2021"
build = "build.rs"

Expand Down
9 changes: 4 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,21 @@ use sha2::{Digest, Sha256};
use std::num::{NonZeroU32, NonZeroUsize};

// local imports
use crate::datefmt::{DateTimeFormat, DateTimeFormatter};
use crate::{error::*, QueryNone};
use crate::datefmt::{DateTimeFormat, DateTimeFormatter};
use crate::fmtx::aligned_left;
use crate::fsmon::{self, EventKind};
use crate::formatting::{RecordFormatter, RecordWithSourceFormatter, RawRecordFormatter};
use crate::fsmon::{self, EventKind};
use crate::IncludeExcludeKeyFilter;
use crate::index::{Indexer, Timestamp};
use crate::input::{BlockLine, InputHolder, InputReference, Input};
use crate::model::{Filter, Parser, ParserSettings, RawRecord, Record, RecordFilter, RecordWithSourceConstructor};
use crate::query::Query;
use crate::scanning::{BufFactory, Scanner, Segment, SegmentBuf, SegmentBufFactory};
use crate::serdex::StreamDeserializerWithOffsets;
use crate::settings::{Fields, Formatting};
use crate::theme::{Element, StylingPush, Theme};
use crate::timezone::Tz;
use crate::IncludeExcludeKeyFilter;

// TODO: merge Options to Settings and replace Options with Settings.

Expand Down Expand Up @@ -80,8 +81,6 @@ impl Options {
}
}

type Query = Box<dyn RecordFilter + Sync>;

pub struct FieldOptions {
pub filter: Arc<IncludeExcludeKeyFilter>,
pub settings: Fields,
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,9 @@ fn run() -> Result<()> {

let mut query: Option<Query> = None;
for q in opt.query {
let right = hl::query::parse(&q)?;
let right = Query::parse(&q)?;
if let Some(left) = query {
query = Some(hl::query::and(left, right));
query = Some(left.and(right));
} else {
query = Some(right);
}
Expand Down
48 changes: 34 additions & 14 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,43 @@ use crate::types::FieldKind;
#[grammar = "query.pest"]
pub struct QueryParser;

pub type Query = Box<dyn RecordFilter + Sync>;
pub struct Query {
filter: Box<dyn RecordFilter + Sync>,
}

// ---
impl Query {
pub fn parse(str: &str) -> Result<Self> {
let mut pairs = QueryParser::parse(Rule::input, str)?;
Ok(expression(pairs.next().unwrap())?)
}

pub fn parse(str: &str) -> Result<Query> {
let mut pairs = QueryParser::parse(Rule::input, str)?;
Ok(expression(pairs.next().unwrap())?)
}
pub fn and(self, rhs: Query) -> Query {
Query::new(And { lhs: self, rhs })
}

pub fn or(self, rhs: Query) -> Query {
Query::new(Or { lhs: self, rhs })
}

pub fn and(lhs: Query, rhs: Query) -> Query {
Box::new(And { lhs, rhs })
pub fn not(self) -> Query {
Query::new(Not { arg: self })
}

fn new<F: RecordFilter + Sync + 'static>(filter: F) -> Self {
Self {
filter: Box::new(filter),
}
}
}

pub fn or(lhs: Query, rhs: Query) -> Query {
Box::new(Or { lhs, rhs })
impl RecordFilter for Query {
fn apply<'a>(&self, record: &'a Record<'a>) -> bool {
self.filter.apply(record)
}
}

// ---

fn expression(pair: Pair<Rule>) -> Result<Query> {
match pair.as_rule() {
Rule::expr_or => binary_op::<Or>(pair),
Expand All @@ -50,15 +70,15 @@ fn binary_op<Op: BinaryOp + Sync + 'static>(pair: Pair<Rule>) -> Result<Query> {
let mut inner = pair.into_inner();
let mut result = expression(inner.next().unwrap())?;
for inner in inner {
result = Box::new(Op::new(result, expression(inner)?));
result = Query::new(Op::new(result, expression(inner)?));
}
Ok(result)
}

fn not(pair: Pair<Rule>) -> Result<Query> {
assert_eq!(pair.as_rule(), Rule::expr_not);

Ok(Box::new(Not {
Ok(Query::new(Not {
arg: expression(pair.into_inner().next().unwrap())?,
}))
}
Expand Down Expand Up @@ -124,7 +144,7 @@ fn field_filter(pair: Pair<Rule>) -> Result<Query> {
_ => unreachable!(),
};

Ok(Box::new(FieldFilter::new(
Ok(Query::new(FieldFilter::new(
parse_field_name(lhs)?.borrowed(),
match_policy,
if negated {
Expand Down Expand Up @@ -276,7 +296,7 @@ impl<F: Fn(Level) -> bool + Send + Sync + 'static> LevelFilter<F> {
}

fn query(f: F) -> Query {
Box::new(Self::new(f))
Query::new(Self::new(f))
}
}

Expand Down
Loading