Skip to content

Commit

Permalink
new: record filtering refactored for greater flexibility
Browse files Browse the repository at this point in the history
  • Loading branch information
pamburus committed Oct 11, 2023
1 parent daa0849 commit fd331dc
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 71 deletions.
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.20.0-beta.14.8.1"
version = "0.20.0-beta.14.8.2"
edition = "2021"
build = "build.rs"

Expand Down
204 changes: 135 additions & 69 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,75 +40,8 @@ impl<'a> Record<'a> {
self.extra.iter().chain(self.extrax.iter())
}

pub fn matches(&self, filter: &Filter) -> bool {
if filter.is_empty() {
return true;
}

if filter.since.is_some() || filter.until.is_some() {
if let Some(ts) = self.ts.as_ref().and_then(|ts| ts.parse()) {
if let Some(since) = filter.since {
if ts < since {
return false;
}
}
if let Some(until) = filter.until {
if ts > until {
return false;
}
}
}
}

if let Some(bound) = &filter.level {
if let Some(level) = self.level.as_ref() {
if level > bound {
return false;
}
}
}

if !filter.fields.0.is_empty() {
for field in filter.fields.0.iter() {
match &field.key[..] {
"msg" | "message" => {
if !field.match_value(self.message.map(|x| x.get()), true) {
return false;
}
}
"logger" => {
if !field.match_value(self.logger, false) {
return false;
}
}
"caller" => {
if !field.match_value(self.caller, false) {
return false;
}
}
_ => {
let mut matched = false;
for (k, v) in self.fields() {
match field.match_key(*k) {
None => {}
Some(KeyMatch::Full) => {
let escaped = v.get().starts_with('"');
matched |= field.match_value(Some(v.get()), escaped);
}
Some(KeyMatch::Partial(subkey)) => {
matched |= field.match_value_partial(subkey, *v);
}
}
}
if !matched {
return false;
}
}
}
}
}

return true;
pub fn matches<F: RecordFilter>(&self, filter: &F) -> bool {
filter.apply(self)
}

fn with_capacity(capacity: usize) -> Self {
Expand All @@ -130,6 +63,54 @@ impl<'a> Record<'a> {

// ---

pub trait RecordFilter {
fn apply<'a>(&self, record: &'a Record<'a>) -> bool;

fn and<F>(self, rhs: F) -> RecordFilterAnd<Self, F>
where
Self: Sized,
F: RecordFilter,
{
RecordFilterAnd { lhs: self, rhs }
}

fn or<F>(self, rhs: F) -> RecordFilterOr<Self, F>
where
Self: Sized,
F: RecordFilter,
{
RecordFilterOr { lhs: self, rhs }
}
}

// ---

pub struct RecordFilterAnd<L: RecordFilter, R: RecordFilter> {
lhs: L,
rhs: R,
}

impl<L: RecordFilter, R: RecordFilter> RecordFilter for RecordFilterAnd<L, R> {
fn apply<'a>(&self, record: &'a Record<'a>) -> bool {
self.lhs.apply(record) && self.rhs.apply(record)
}
}

// ---

pub struct RecordFilterOr<L: RecordFilter, R: RecordFilter> {
lhs: L,
rhs: R,
}

impl<L: RecordFilter, R: RecordFilter> RecordFilter for RecordFilterOr<L, R> {
fn apply<'a>(&self, record: &'a Record<'a>) -> bool {
self.lhs.apply(record) || self.rhs.apply(record)
}
}

// ---

#[derive(Default)]
pub struct ParserSettings {
fields: HashMap<String, (FieldSettings, usize)>,
Expand Down Expand Up @@ -565,6 +546,48 @@ impl FieldFilter {
}
}

impl RecordFilter for FieldFilter {
fn apply<'a>(&self, record: &'a Record<'a>) -> bool {
match &self.key[..] {
"msg" | "message" => {
if !self.match_value(record.message.map(|x| x.get()), true) {
return false;
}
}
"logger" => {
if !self.match_value(record.logger, false) {
return false;
}
}
"caller" => {
if !self.match_value(record.caller, false) {
return false;
}
}
_ => {
let mut matched = false;
for (k, v) in record.fields() {
match self.match_key(*k) {
None => {}
Some(KeyMatch::Full) => {
let escaped = v.get().starts_with('"');
matched |= self.match_value(Some(v.get()), escaped);
}
Some(KeyMatch::Partial(subkey)) => {
matched |= self.match_value_partial(subkey, *v);
}
}
}
if !matched {
return false;
}
}
}

true
}
}

// ---

#[derive(Debug, Default)]
Expand All @@ -580,6 +603,12 @@ impl FieldFilterSet {
}
}

impl RecordFilter for FieldFilterSet {
fn apply<'a>(&self, record: &'a Record<'a>) -> bool {
self.0.iter().all(|field| field.apply(record))
}
}

// ---

#[derive(Debug, Default)]
Expand All @@ -596,6 +625,43 @@ impl Filter {
}
}

impl RecordFilter for Filter {
fn apply<'a>(&self, record: &'a Record<'a>) -> bool {
if self.is_empty() {
return true;
}

if self.since.is_some() || self.until.is_some() {
if let Some(ts) = record.ts.as_ref().and_then(|ts| ts.parse()) {
if let Some(since) = self.since {
if ts < since {
return false;
}
}
if let Some(until) = self.until {
if ts > until {
return false;
}
}
}
}

if let Some(bound) = &self.level {
if let Some(level) = record.level.as_ref() {
if level > bound {
return false;
}
}
}

if !self.fields.apply(record) {
return false;
}

true
}
}

// ---

pub struct Object<'a> {
Expand Down

0 comments on commit fd331dc

Please sign in to comment.