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

Review jsonpath_rs (2) #11

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
28 changes: 28 additions & 0 deletions src/json_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub enum JsonPathToken {
Number,
}

/* Struct that represent a compiled json path query. */
#[derive(Debug)]
pub struct Query<'i> {
// query: QueryElement<'i>
Expand All @@ -30,6 +31,10 @@ pub struct QueryCompilationError {
}

impl<'i> Query<'i> {
/// Pop the last element from the compiled json path.
/// For example, if the json path is $.foo.bar then pop_last
/// will return bar and leave the json path query with foo only
/// ($.foo)
#[allow(dead_code)]
pub fn pop_last(&mut self) -> Option<(String, JsonPathToken)> {
let last = self.root.next_back();
Expand Down Expand Up @@ -57,6 +62,8 @@ impl<'i> Query<'i> {
}
}

/// Returns the amount of elements in the json path
/// Example: $.foo.bar have 2 elements
MeirShpilraien marked this conversation as resolved.
Show resolved Hide resolved
#[allow(dead_code)]
pub fn size(&mut self) -> usize {
if self.size.is_some() {
Expand All @@ -66,6 +73,11 @@ impl<'i> Query<'i> {
self.size()
}

/// Results whether or not the compiled json path is static
/// Static path is a path that is promised to have at most a single result.
/// Example:
/// static path: $.foo.bar
/// none static path: $.*.bar
#[allow(dead_code)]
pub fn is_static(&mut self) -> bool {
if self.is_static.is_some() {
Expand Down Expand Up @@ -119,6 +131,8 @@ impl std::fmt::Display for Rule {
}
}

/// Compire the given string query into a query object.
MeirShpilraien marked this conversation as resolved.
Show resolved Hide resolved
/// Returns error on compilation error.
pub(crate) fn compile(path: &str) -> Result<Query, QueryCompilationError> {
let query = JsonPathParser::parse(Rule::query, path);
match query {
Expand Down Expand Up @@ -201,6 +215,7 @@ pub trait UserPathTrackerGenerator {
fn generate(&self) -> Self::PT;
}

/* Dummy path tracker, indicating that there is no need to track results paths. */
pub struct DummyTracker;
impl UserPathTracker for DummyTracker {
fn add_str(&mut self, _s: &str) {}
Expand All @@ -210,6 +225,7 @@ impl UserPathTracker for DummyTracker {
}
}

/* A dummy path tracker generator, indicating that there is no need to track results paths. */
pub struct DummyTrackerGenerator;
impl UserPathTrackerGenerator for DummyTrackerGenerator {
type PT = DummyTracker;
Expand All @@ -224,6 +240,7 @@ pub enum PTrackerElement {
Index(usize),
}

/* An actual representation of a path that the user get as a result. */
MeirShpilraien marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Debug, PartialEq)]
pub struct PTracker {
pub elemenets: Vec<PTrackerElement>,
Expand All @@ -248,6 +265,7 @@ impl UserPathTracker for PTracker {
}
}

/* Used to generate paths trackers. */
pub struct PTrackerGenerator;
impl UserPathTrackerGenerator for PTrackerGenerator {
type PT = PTracker;
Expand All @@ -265,6 +283,12 @@ enum PathTrackerElement<'i> {
Root,
}

/* Struct that used to track paths of query results.
* This struct is used to hold the path that lead to the
* current location (when calculating the json path).
* Once we have a match we can run (in a reverse order)
* on the path tracker and add the path to the result as
* a PTracker object. */
#[derive(Clone)]
struct PathTracker<'i, 'j> {
parent: Option<&'j PathTracker<'i, 'j>>,
Expand Down Expand Up @@ -298,6 +322,7 @@ const fn create_index_tracker<'i, 'j>(
}
}

/* Enum for filter results */
enum TermEvaluationResult<'i, 'j, S: SelectValue> {
Integer(i64),
Float(f64),
Expand Down Expand Up @@ -413,6 +438,9 @@ impl<'i, 'j, S: SelectValue> TermEvaluationResult<'i, 'j, S> {
}
}

/* This struct is used to calculate a json path on a json object.
* The struct contains the query and the tracker generator that allows to create
* path tracker to tracker paths that lead to different results. */
#[derive(Debug)]
pub struct PathCalculator<'i, UPTG: UserPathTrackerGenerator> {
pub query: Option<&'i Query<'i>>,
Expand Down