Skip to content
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
34 changes: 34 additions & 0 deletions src/history/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ pub enum SearchDirection {
Forward,
}

/// A typed expected value for [`SearchFilter::more_info_json`] filters.
///
/// Each variant maps to a JSON scalar type and produces a type-precise SQL comparison
/// using `json_type()` to avoid collisions between JSON booleans, integers, and strings.
/// Arrays and objects are intentionally unsupported.
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum JsonFilterValue {
/// Match a JSON `null` at the given path.
Null,
/// Match a JSON boolean (`true` or `false`) at the given path.
Bool(bool),
/// Match a JSON integer at the given path. Does not match JSON `true`/`false` or strings.
Integer(i64),
/// Match a JSON floating-point number at the given path.
Real(f64),
/// Match a JSON string at the given path. Does not match numbers or booleans.
Text(String),
}

/// Defines additional filters for querying the [`History`]
pub struct SearchFilter {
/// Query for the command line content
Expand All @@ -53,6 +73,19 @@ pub struct SearchFilter {
pub exit_successful: Option<bool>,
/// Filter on the session id
pub session: Option<HistorySessionId>,
/// Filter by JSON path in the `more_info` column using SQLite's `json_type()` /
/// `json_extract()`.
///
/// Each entry is `(json_path, expected_value)`. The [`JsonFilterValue`] variant
/// determines the exact SQL comparison used, preventing collisions between JSON
/// booleans, integers, and strings (e.g. `Bool(true)` will not match integer `1`
/// or string `"true"`).
///
/// Rows where `more_info` is SQL `NULL` or the JSON path does not exist will not match.
/// Only evaluated by [`crate::SqliteBackedHistory`] typed search methods
/// (`search_with_extra`); the non-typed [`History`] trait methods and
/// [`crate::FileBackedHistory`] ignore this field.
pub more_info_json: Option<Vec<(String, JsonFilterValue)>>,
}

impl SearchFilter {
Expand Down Expand Up @@ -88,6 +121,7 @@ impl SearchFilter {
cwd_prefix: None,
exit_successful: None,
session,
more_info_json: None,
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/history/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ mod sqlite_backed;
pub use sqlite_backed::SqliteBackedHistory;

pub use base::{
CommandLineSearch, History, HistoryNavigationQuery, SearchDirection, SearchFilter, SearchQuery,
CommandLineSearch, History, HistoryNavigationQuery, JsonFilterValue, SearchDirection,
SearchFilter, SearchQuery,
};
pub use cursor::HistoryCursor;
pub use item::{
Expand Down
Loading
Loading