Skip to content

Creating infallible queries #94

Open
@thehiddenwaffle

Description

@thehiddenwaffle

Let me start by saying this crate is awesome!

Use case similar to https://github.com//issues/92

I am working on a project that will involve using json paths, and those paths will be coming from a huge set of static strings. Because I have this set of static strings I would like to be able to perform compile time checks on them to ensure valid json path queries so that I don't have to deal with fallibility in the downstream code. To this end my goal is to simply make a macro that converts them to JpQuery structs, throwing a compiler error if any would fail to be parsed by parse_json_path.

I started looking in the source code and found js_path_process, which I could use in a new trait roughly as follows:

pub trait JsonPathCompiled: JsonPath {
    fn query_with_path_checked(&self, q: &JpQuery) -> Vec<QueryRef<Self>> {
        js_path_process(q, self).expect("When can this fail??")
    }

    fn query_only_path_checked(&self, q: &JpQuery) -> Vec<QueryPath> {
        js_path_process(q, self).expect("When can this fail??").into_iter()
            .map(|r| r.path())
            .collect::<Vec<_>>()
    }

    fn query_checked(&self, q: &JpQuery) -> Vec<&Self> {
        js_path_process(q, self).expect("When can this fail??").into_iter()
            .map(|r| r.val())
            .collect::<Vec<_>>()
    }
}
impl JsonPathCompiled for serde_json::Value {}

To my understanding the only notion success or failure in the original RFC for a valid path is either [some_stuff] for a match or the empty array [] for no matches found.

After reading #92, my real question becomes: is the Err match arm reachable code and under what circumstances would that arm be taken? I tried to dig into the source code but I'm still a bit new to rust and even a simple example would help a lot, if needed then maybe I can just "ban" that edge case with macros somehow.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions