@@ -14,7 +14,7 @@ pub struct JsonPathParser;
1414#[ derive( Debug ) ]
1515pub struct Query < ' i > {
1616 // query: QueryElement<'i>
17- query : Pairs < ' i , Rule > ,
17+ pub query : Pairs < ' i , Rule > ,
1818}
1919
2020#[ derive( Debug ) ]
@@ -107,6 +107,43 @@ impl UserPathTrackerGenerator for DummyTrackerGenerator {
107107 }
108108}
109109
110+ #[ derive( Debug , PartialEq ) ]
111+ pub enum PTrackerElement {
112+ Key ( String ) ,
113+ Index ( usize ) ,
114+ }
115+
116+ #[ derive( Debug , PartialEq ) ]
117+ pub struct PTracker {
118+ pub elemenets : Vec < PTrackerElement > ,
119+ }
120+ impl UserPathTracker for PTracker {
121+ fn add_str ( & mut self , s : & str ) {
122+ self . elemenets . push ( PTrackerElement :: Key ( s. to_string ( ) ) )
123+ }
124+
125+ fn add_index ( & mut self , i : usize ) {
126+ self . elemenets . push ( PTrackerElement :: Index ( i) )
127+ }
128+
129+ fn to_string_path ( self ) -> Vec < String > {
130+ self . elemenets . into_iter ( ) . map ( |e|{
131+ match e {
132+ PTrackerElement :: Key ( s) => s,
133+ PTrackerElement :: Index ( i) => i. to_string ( ) ,
134+ }
135+ } ) . collect ( )
136+ }
137+ }
138+
139+ pub struct PTrackerGenerator ;
140+ impl UserPathTrackerGenerator for PTrackerGenerator {
141+ type PT = PTracker ;
142+ fn generate ( & self ) -> Self :: PT {
143+ PTracker { elemenets : Vec :: new ( ) }
144+ }
145+ }
146+
110147#[ derive( Clone ) ]
111148enum PathTrackerElement < ' i > {
112149 Index ( usize ) ,
@@ -279,8 +316,8 @@ impl<'i, 'j, S:SelectValue> TermEvaluationResult<'i, 'j, S> {
279316
280317#[ derive( Debug ) ]
281318pub struct PathCalculator < ' i , UPTG : UserPathTrackerGenerator > {
282- query : Option < & ' i Query < ' i > > ,
283- tracker_generator : Option < UPTG > ,
319+ pub query : Option < & ' i Query < ' i > > ,
320+ pub tracker_generator : Option < UPTG > ,
284321}
285322
286323#[ derive( Debug , PartialEq ) ]
@@ -785,9 +822,8 @@ impl<'i, UPTG: UserPathTrackerGenerator> PathCalculator<'i, UPTG> {
785822 }
786823 }
787824
788- pub fn calc_with_paths < ' j : ' i , S : SelectValue > ( & self , json : & ' j S ) -> Vec < CalculationResult < ' j , S , UPTG :: PT > >
825+ pub fn calc_with_paths_on_root < ' j : ' i , S : SelectValue > ( & self , json : & ' j S , root : Pair < Rule > ) -> Vec < CalculationResult < ' j , S , UPTG :: PT > >
789826 {
790- let root = self . query . unwrap ( ) . query . clone ( ) . next ( ) . unwrap ( ) ;
791827 let mut calc_data = PathCalculatorData {
792828 results : Vec :: new ( ) ,
793829 root : json,
@@ -799,18 +835,10 @@ impl<'i, UPTG: UserPathTrackerGenerator> PathCalculator<'i, UPTG> {
799835 }
800836 calc_data. results . drain ( ..) . collect ( )
801837 }
802-
803- pub fn calc_once < ' j , ' p , S : SelectValue > ( mut q : Query < ' j > , json : & ' p S ) -> Vec < & ' p S > {
804- let root = q. query . next ( ) . unwrap ( ) ;
805- let mut calc_data = PathCalculatorData {
806- results : Vec :: new ( ) ,
807- root : json,
808- } ;
809- PathCalculator :: < ' p , DummyTrackerGenerator > {
810- query : None ,
811- tracker_generator : None ,
812- } . calc_internal ( root. into_inner ( ) , json, None , & mut calc_data, true ) ;
813- calc_data. results . into_iter ( ) . map ( |e : CalculationResult < ' p , S , DummyTracker > | e. res ) . collect ( )
838+
839+ pub fn calc_with_paths < ' j : ' i , S : SelectValue > ( & self , json : & ' j S ) -> Vec < CalculationResult < ' j , S , UPTG :: PT > >
840+ {
841+ self . calc_with_paths_on_root ( json, self . query . unwrap ( ) . query . clone ( ) . next ( ) . unwrap ( ) )
814842 }
815843
816844 pub fn calc < ' j : ' i , S : SelectValue > ( & self , json : & ' j S ) -> Vec < & ' j S >
0 commit comments