Debian Codegraph @

src/json/path.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/::Peekablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/Peekable#;
2
3 use pestrust-analyzer cargo pest 2.7.8 crate/::iteratorsrust-analyzer cargo pest 2.7.8 iterators/::Pairrust-analyzer cargo pest 2.7.8 iterators/pair/Pair#;
4 use pestrust-analyzer cargo pest 2.7.8 crate/::Parserrust-analyzer cargo pest 2.7.8 parser/Parser#;
5
6 use craterust-analyzer cargo handlebars 5.1.2 crate/::errorrust-analyzer cargo handlebars 5.1.2 error/::RenderErrorrust-analyzer cargo handlebars 5.1.2 error/RenderError#;
7 use craterust-analyzer cargo handlebars 5.1.2 crate/::grammarrust-analyzer cargo handlebars 5.1.2 grammar/::{HandlebarsParserrust-analyzer cargo handlebars 5.1.2 grammar/HandlebarsParser#, Rulerust-analyzer cargo handlebars 5.1.2 grammar/Rule#};
8 use craterust-analyzer cargo handlebars 5.1.2 crate/::RenderErrorReasonrust-analyzer cargo handlebars 5.1.2 error/RenderErrorReason#;
9
10 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
11 pub enum PathSegrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg# {
12 Namedrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#Named#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#),
13 Ruledrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#Ruled#(Rulerust-analyzer cargo handlebars 5.1.2 grammar/Rule#),
14 }
15
16 /// Represents the Json path in templates.
17 ///
18 /// It can be either a local variable like `@first`, `../@index`,
19 /// or a normal relative path like `a/b/c`.
20 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
21 pub enum Pathrust-analyzer cargo handlebars 5.1.2 json/path/Path# {
22 Relativerust-analyzer cargo handlebars 5.1.2 json/path/Path#Relative#((Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<PathSegrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#>, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#)),
23 Localrust-analyzer cargo handlebars 5.1.2 json/path/Path#Local#((usize, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#)),
24 }
25
26 impl Pathrust-analyzer cargo handlebars 5.1.2 json/path/Path# {
27 pub(craterust-analyzer cargo handlebars 5.1.2 crate/) fn newrust-analyzer cargo handlebars 5.1.2 json/path/impl#[Path]new().(raw: &str, segs: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<PathSegrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#>) -> Pathrust-analyzer cargo handlebars 5.1.2 json/path/Path# {
28 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((level, name)) = get_local_path_and_levelrust-analyzer cargo handlebars 5.1.2 json/path/get_local_path_and_level().(&segs) {
29 Pathrust-analyzer cargo handlebars 5.1.2 json/path/Path#::Localrust-analyzer cargo handlebars 5.1.2 json/path/Path#Local#((level, name, raw.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()))
30 } else {
31 Pathrust-analyzer cargo handlebars 5.1.2 json/path/Path#::Relativerust-analyzer cargo handlebars 5.1.2 json/path/Path#Relative#((segs, raw.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()))
32 }
33 }
34
35 pub fn parserust-analyzer cargo handlebars 5.1.2 json/path/impl#[Path]parse().(raw: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Pathrust-analyzer cargo handlebars 5.1.2 json/path/Path#, RenderErrorrust-analyzer cargo handlebars 5.1.2 error/RenderError#> {
36 HandlebarsParserrust-analyzer cargo handlebars 5.1.2 grammar/HandlebarsParser#::parserust-analyzer cargo handlebars 5.1.2 grammar/impl#[HandlebarsParser][`Parser<Rule>`]parse().(Rulerust-analyzer cargo handlebars 5.1.2 grammar/Rule#::pathrust-analyzer cargo handlebars 5.1.2 grammar/Rule#path#, raw)
37 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(|p| {
38 let parsed = p.flattenrust-analyzer cargo pest 2.7.8 iterators/pairs/impl#[`Pairs<'i, R>`]flatten().();
39 let segs = parse_json_path_from_iterrust-analyzer cargo handlebars 5.1.2 json/path/parse_json_path_from_iter().(&mut parsed.peekablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#peekable().(), raw.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().());
40 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Pathrust-analyzer cargo handlebars 5.1.2 json/path/Path#::newrust-analyzer cargo handlebars 5.1.2 json/path/impl#[Path]new().(raw, segs))
41 })
42 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_| RenderErrorReasonrust-analyzer cargo handlebars 5.1.2 error/RenderErrorReason#::InvalidJsonPathrust-analyzer cargo handlebars 5.1.2 error/RenderErrorReason#InvalidJsonPath#(raw.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
43 }
44
45 pub(craterust-analyzer cargo handlebars 5.1.2 crate/) fn rawrust-analyzer cargo handlebars 5.1.2 json/path/impl#[Path]raw().(&self) -> &str {
46 match self {
47 Pathrust-analyzer cargo handlebars 5.1.2 json/path/Path#::Relativerust-analyzer cargo handlebars 5.1.2 json/path/Path#Relative#((_, ref raw)) => raw,
48 Pathrust-analyzer cargo handlebars 5.1.2 json/path/Path#::Localrust-analyzer cargo handlebars 5.1.2 json/path/Path#Local#((_, _, ref raw)) => raw,
49 }
50 }
51
52 pub(craterust-analyzer cargo handlebars 5.1.2 crate/) fn currentrust-analyzer cargo handlebars 5.1.2 json/path/impl#[Path]current().() -> Pathrust-analyzer cargo handlebars 5.1.2 json/path/Path# {
53 Pathrust-analyzer cargo handlebars 5.1.2 json/path/Path#::Relativerust-analyzer cargo handlebars 5.1.2 json/path/Path#Relative#((Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(0), "".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()))
54 }
55
56 // for test only
57 pub(craterust-analyzer cargo handlebars 5.1.2 crate/) fn with_named_pathsrust-analyzer cargo handlebars 5.1.2 json/path/impl#[Path]with_named_paths().(name_segs: &[&str]) -> Pathrust-analyzer cargo handlebars 5.1.2 json/path/Path# {
58 let segs = name_segs
59 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
60 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|n| PathSegrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#::Namedrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#Named#((*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().n).to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()))
61 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
62 Pathrust-analyzer cargo handlebars 5.1.2 json/path/Path#::Relativerust-analyzer cargo handlebars 5.1.2 json/path/Path#Relative#((segs, name_segs.joinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]join().("/")))
63 }
64
65 // for test only
66 pub(craterust-analyzer cargo handlebars 5.1.2 crate/) fn segsrust-analyzer cargo handlebars 5.1.2 json/path/impl#[Path]segs().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&[PathSegrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#]> {
67 match self {
68 Pathrust-analyzer cargo handlebars 5.1.2 json/path/Path#::Relativerust-analyzer cargo handlebars 5.1.2 json/path/Path#Relative#((segs, _)) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(segs),
69 _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
70 }
71 }
72 }
73
74 fn get_local_path_and_levelrust-analyzer cargo handlebars 5.1.2 json/path/get_local_path_and_level().(paths: &[PathSegrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#]) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<(usize, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#)> {
75 paths.firstrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]first().().and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|seg| {
76 if seg rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().&PathSegrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#::Ruledrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#Ruled#(Rulerust-analyzer cargo handlebars 5.1.2 grammar/Rule#::path_localrust-analyzer cargo handlebars 5.1.2 grammar/Rule#path_local#) {
77 let mut level = 0;
78 while paths.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(level rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().&PathSegrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#::Ruledrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#Ruled#(Rulerust-analyzer cargo handlebars 5.1.2 grammar/Rule#::path_uprust-analyzer cargo handlebars 5.1.2 grammar/Rule#path_up#) {
79 level rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1;
80 }
81 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(PathSegrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#::Namedrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#Named#(name)) = paths.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(level rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1) {
82 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((level, name.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Clone]clone().()))
83 } else {
84 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
85 }
86 } else {
87 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
88 }
89 })
90 }
91
92 pub(craterust-analyzer cargo handlebars 5.1.2 crate/) fn parse_json_path_from_iterrust-analyzer cargo handlebars 5.1.2 json/path/parse_json_path_from_iter().<'a, I>(it: &mut Peekablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/Peekable#<I>, limit: usize) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<PathSegrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#>
93 where
94 I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = Pairrust-analyzer cargo pest 2.7.8 iterators/pair/Pair#<'a, Rulerust-analyzer cargo handlebars 5.1.2 grammar/Rule#>>,
95 {
96 let mut path_stack = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(5);
97 while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(n) = it.peekrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`]peek().() {
98 let span = n.as_spanrust-analyzer cargo pest 2.7.8 iterators/pair/impl#[`Pair<'i, R>`]as_span().();
99 if span.endrust-analyzer cargo pest 2.7.8 span/impl#[`Span<'i>`]end().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().limit {
100 break;
101 }
102
103 match n.as_rulerust-analyzer cargo pest 2.7.8 iterators/pair/impl#[`Pair<'i, R>`]as_rule().() {
104 Rulerust-analyzer cargo handlebars 5.1.2 grammar/Rule#::path_rootrust-analyzer cargo handlebars 5.1.2 grammar/Rule#path_root# => {
105 path_stack.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(PathSegrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#::Ruledrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#Ruled#(Rulerust-analyzer cargo handlebars 5.1.2 grammar/Rule#::path_rootrust-analyzer cargo handlebars 5.1.2 grammar/Rule#path_root#));
106 }
107 Rulerust-analyzer cargo handlebars 5.1.2 grammar/Rule#::path_localrust-analyzer cargo handlebars 5.1.2 grammar/Rule#path_local# => {
108 path_stack.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(PathSegrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#::Ruledrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#Ruled#(Rulerust-analyzer cargo handlebars 5.1.2 grammar/Rule#::path_localrust-analyzer cargo handlebars 5.1.2 grammar/Rule#path_local#));
109 }
110 Rulerust-analyzer cargo handlebars 5.1.2 grammar/Rule#::path_uprust-analyzer cargo handlebars 5.1.2 grammar/Rule#path_up# => {
111 path_stack.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(PathSegrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#::Ruledrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#Ruled#(Rulerust-analyzer cargo handlebars 5.1.2 grammar/Rule#::path_uprust-analyzer cargo handlebars 5.1.2 grammar/Rule#path_up#));
112 }
113 Rulerust-analyzer cargo handlebars 5.1.2 grammar/Rule#::path_idrust-analyzer cargo handlebars 5.1.2 grammar/Rule#path_id# | Rulerust-analyzer cargo handlebars 5.1.2 grammar/Rule#::path_raw_idrust-analyzer cargo handlebars 5.1.2 grammar/Rule#path_raw_id# => {
114 let name = n.as_strrust-analyzer cargo pest 2.7.8 iterators/pair/impl#[`Pair<'i, R>`]as_str().();
115 if name rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne()."this" {
116 path_stack.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(PathSegrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#::Namedrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#Named#(name.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()));
117 }
118 }
119 _ => {}
120 }
121
122 it.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`][Iterator]next().();
123 }
124
125 path_stack
126 }
127
128 pub(craterust-analyzer cargo handlebars 5.1.2 crate/) fn merge_json_pathrust-analyzer cargo handlebars 5.1.2 json/path/merge_json_path().(path_stack: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, relative_path: &[PathSegrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#]) {
129 for seg in relative_path {
130 match seg {
131 PathSegrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#::Namedrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#Named#(ref s) => {
132 path_stack.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(s.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[T][ToOwned]to_owned().());
133 }
134 PathSegrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#::Ruledrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#Ruled#(Rulerust-analyzer cargo handlebars 5.1.2 grammar/Rule#::path_rootrust-analyzer cargo handlebars 5.1.2 grammar/Rule#path_root#) => {}
135 PathSegrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#::Ruledrust-analyzer cargo handlebars 5.1.2 json/path/PathSeg#Ruled#(Rulerust-analyzer cargo handlebars 5.1.2 grammar/Rule#::path_uprust-analyzer cargo handlebars 5.1.2 grammar/Rule#path_up#) => {}
136 _ => {}
137 }
138 }
139 }