11use crate :: framework:: implementation:: Implementation ;
2- use ouroboros :: self_referencing ;
2+ use rsonpath :: { engine :: Compiler , input :: MmapInput } ;
33use rsonpath:: {
4- engine:: main:: MainEngine ,
4+ engine:: { main:: MainEngine , Engine } ,
55 input:: OwnedBytes ,
66 result:: { Match , Sink } ,
77} ;
8- use rsonpath:: {
9- engine:: { Compiler , Engine } ,
10- input:: MmapInput ,
11- } ;
12- use rsonpath_syntax:: JsonPathQuery ;
138use std:: { convert:: Infallible , fmt:: Display , fs, io} ;
149use thiserror:: Error ;
1510
@@ -18,16 +13,8 @@ pub struct RsonpathCount {}
1813pub struct RsonpathMmap { }
1914pub struct RsonpathMmapCount { }
2015
21- #[ self_referencing( ) ]
22- pub struct RsonpathQuery {
23- query : JsonPathQuery ,
24- #[ borrows( query) ]
25- #[ not_covariant]
26- engine : MainEngine < ' this > ,
27- }
28-
2916impl Implementation for Rsonpath {
30- type Query = RsonpathQuery ;
17+ type Query = MainEngine ;
3118
3219 type File = OwnedBytes < Vec < u8 > > ;
3320
@@ -52,25 +39,20 @@ impl Implementation for Rsonpath {
5239
5340 fn compile_query ( & self , query : & str ) -> Result < Self :: Query , Self :: Error > {
5441 let query = rsonpath_syntax:: parse ( query) . unwrap ( ) ;
42+ let engine = MainEngine :: compile_query ( & query) . map_err ( RsonpathError :: CompilerError ) ?;
5543
56- let rsonpath = RsonpathQuery :: try_new ( query, |query| {
57- MainEngine :: compile_query ( query) . map_err ( RsonpathError :: CompilerError )
58- } ) ?;
59-
60- Ok ( rsonpath)
44+ Ok ( engine)
6145 }
6246
6347 fn run ( & self , query : & Self :: Query , file : & Self :: File ) -> Result < Self :: Result < ' _ > , Self :: Error > {
64- query
65- . with_engine ( |engine| engine. matches ( file, & mut VoidSink ) )
66- . map_err ( RsonpathError :: EngineError ) ?;
48+ query. matches ( file, & mut VoidSink ) . map_err ( RsonpathError :: EngineError ) ?;
6749
6850 Ok ( "[not collected]" )
6951 }
7052}
7153
7254impl Implementation for RsonpathCount {
73- type Query = RsonpathQuery ;
55+ type Query = MainEngine ;
7456
7557 type File = OwnedBytes < Vec < u8 > > ;
7658
@@ -95,25 +77,20 @@ impl Implementation for RsonpathCount {
9577
9678 fn compile_query ( & self , query : & str ) -> Result < Self :: Query , Self :: Error > {
9779 let query = rsonpath_syntax:: parse ( query) . unwrap ( ) ;
80+ let engine = MainEngine :: compile_query ( & query) . map_err ( RsonpathError :: CompilerError ) ?;
9881
99- let rsonpath = RsonpathQuery :: try_new ( query, |query| {
100- MainEngine :: compile_query ( query) . map_err ( RsonpathError :: CompilerError )
101- } ) ?;
102-
103- Ok ( rsonpath)
82+ Ok ( engine)
10483 }
10584
10685 fn run ( & self , query : & Self :: Query , file : & Self :: File ) -> Result < Self :: Result < ' _ > , Self :: Error > {
107- query
108- . with_engine ( |engine| engine. count ( file) )
109- . map_err ( RsonpathError :: EngineError ) ?;
86+ query. count ( file) . map_err ( RsonpathError :: EngineError ) ?;
11087
11188 Ok ( "[not collected]" )
11289 }
11390}
11491
11592impl Implementation for RsonpathMmap {
116- type Query = RsonpathQuery ;
93+ type Query = MainEngine ;
11794
11895 type File = MmapInput ;
11996
@@ -138,25 +115,20 @@ impl Implementation for RsonpathMmap {
138115
139116 fn compile_query ( & self , query : & str ) -> Result < Self :: Query , Self :: Error > {
140117 let query = rsonpath_syntax:: parse ( query) . unwrap ( ) ;
118+ let engine = MainEngine :: compile_query ( & query) . map_err ( RsonpathError :: CompilerError ) ?;
141119
142- let rsonpath = RsonpathQuery :: try_new ( query, |query| {
143- MainEngine :: compile_query ( query) . map_err ( RsonpathError :: CompilerError )
144- } ) ?;
145-
146- Ok ( rsonpath)
120+ Ok ( engine)
147121 }
148122
149123 fn run ( & self , query : & Self :: Query , file : & Self :: File ) -> Result < Self :: Result < ' _ > , Self :: Error > {
150- query
151- . with_engine ( |engine| engine. matches ( file, & mut VoidSink ) )
152- . map_err ( RsonpathError :: EngineError ) ?;
124+ query. matches ( file, & mut VoidSink ) . map_err ( RsonpathError :: EngineError ) ?;
153125
154126 Ok ( "[not collected]" )
155127 }
156128}
157129
158130impl Implementation for RsonpathMmapCount {
159- type Query = RsonpathQuery ;
131+ type Query = MainEngine ;
160132
161133 type File = MmapInput ;
162134
@@ -181,18 +153,13 @@ impl Implementation for RsonpathMmapCount {
181153
182154 fn compile_query ( & self , query : & str ) -> Result < Self :: Query , Self :: Error > {
183155 let query = rsonpath_syntax:: parse ( query) . unwrap ( ) ;
156+ let engine = MainEngine :: compile_query ( & query) . map_err ( RsonpathError :: CompilerError ) ?;
184157
185- let rsonpath = RsonpathQuery :: try_new ( query, |query| {
186- MainEngine :: compile_query ( query) . map_err ( RsonpathError :: CompilerError )
187- } ) ?;
188-
189- Ok ( rsonpath)
158+ Ok ( engine)
190159 }
191160
192161 fn run ( & self , query : & Self :: Query , file : & Self :: File ) -> Result < Self :: Result < ' _ > , Self :: Error > {
193- query
194- . with_engine ( |engine| engine. count ( file) )
195- . map_err ( RsonpathError :: EngineError ) ?;
162+ query. count ( file) . map_err ( RsonpathError :: EngineError ) ?;
196163
197164 Ok ( "[not collected]" )
198165 }
0 commit comments