1- use crate :: { cbor, json, toml, xml, yaml} ;
2- use crate :: { Error , Num , Tag , Type , Val , ValR , ValX } ;
1+ use crate :: { Error , Num , Tag , Val , ValR , ValX } ;
32use alloc:: { boxed:: Box , string:: ToString , vec:: Vec } ;
43use bstr:: ByteSlice ;
54use bytes:: { BufMut , Bytes , BytesMut } ;
6- use core:: fmt;
7- use jaq_core:: box_iter:: { box_once, then, BoxIter } ;
5+ use jaq_core:: box_iter:: { box_once, BoxIter } ;
86use jaq_core:: { DataT , Exn , Native , RunPtr } ;
9- use jaq_std:: { run, unary, v, Filter , ValT as _ } ;
7+ use jaq_std:: { run, unary, v, Filter } ;
108
119impl Val {
1210 /// Return 0 for null, the absolute value for numbers, and
@@ -107,44 +105,17 @@ impl Val {
107105 _ => Err ( self . clone ( ) ) ,
108106 }
109107 }
110-
111- fn as_bytes_owned ( & self ) -> Option < Bytes > {
112- if let Self :: Str ( b, _) = self {
113- Some ( b. clone ( ) )
114- } else {
115- None
116- }
117- }
118-
119- fn as_utf8_bytes_owned ( & self ) -> Option < Bytes > {
120- self . is_utf8_str ( ) . then ( || self . as_bytes_owned ( ) ) . flatten ( )
121- }
122-
123- fn try_as_bytes_owned ( & self ) -> Result < Bytes , Error > {
124- self . as_bytes_owned ( )
125- . ok_or_else ( || Error :: typ ( self . clone ( ) , Type :: Str . as_str ( ) ) )
126- }
127-
128- fn try_as_utf8_bytes_owned ( & self ) -> Result < Bytes , Error > {
129- self . as_utf8_bytes_owned ( )
130- . ok_or_else ( || Error :: typ ( self . clone ( ) , Type :: Str . as_str ( ) ) )
131- }
132108}
133109
134110/// Box Once, Map Error.
135- fn bome < ' a > ( r : ValR ) -> BoxIter < ' a , ValX > {
111+ pub ( crate ) fn bome < ' a > ( r : ValR ) -> BoxIter < ' a , ValX > {
136112 box_once ( r. map_err ( Exn :: from) )
137113}
138114
139- /// Box Map, Map Error.
140- fn bmme < ' a > ( iter : BoxIter < ' a , ValR > ) -> BoxIter < ' a , ValX > {
141- Box :: new ( iter. map ( |r| r. map_err ( Exn :: from) ) )
142- }
143-
144115/// Functions of the standard library.
145- #[ cfg( feature = "parse " ) ]
116+ #[ cfg( feature = "formats " ) ]
146117pub fn funs < D : for < ' a > DataT < V < ' a > = Val > > ( ) -> impl Iterator < Item = Filter < Native < D > > > {
147- base_funs ( ) . chain ( parse_funs ( ) . into_vec ( ) . into_iter ( ) . map ( run) )
118+ base_funs ( ) . chain ( crate :: formats :: funs ( ) . into_vec ( ) . into_iter ( ) . map ( run) )
148119}
149120
150121/// Minimal set of filters.
@@ -154,11 +125,6 @@ pub fn base_funs<D: for<'a> DataT<V<'a> = Val>>() -> impl Iterator<Item = Filter
154125
155126fn base < D : for < ' a > DataT < V < ' a > = Val > > ( ) -> Box < [ Filter < RunPtr < D > > ] > {
156127 Box :: new ( [
157- ( "tojson" , v ( 0 ) , |cv| {
158- let mut buf = Vec :: new ( ) ;
159- json:: write ( & mut buf, & cv. 1 ) . unwrap ( ) ;
160- box_once ( Ok ( Val :: from_utf8_bytes ( buf) ) )
161- } ) ,
162128 ( "tobytes" , v ( 0 ) , |cv| {
163129 let pass = |b| Val :: Str ( b, Tag :: Bytes ) ;
164130 let fail = |v| Error :: str ( format_args ! ( "cannot convert {v} to bytes" ) ) ;
@@ -189,92 +155,3 @@ fn base<D: for<'a> DataT<V<'a> = Val>>() -> Box<[Filter<RunPtr<D>>]> {
189155 } ) ,
190156 ] )
191157}
192-
193- fn parse_fail ( i : & impl fmt:: Display , fmt : & str , e : impl fmt:: Display ) -> Error {
194- Error :: str ( format_args ! ( "cannot parse {i} as {fmt}: {e}" ) )
195- }
196-
197- fn serialise_fail ( i : & impl fmt:: Display , fmt : & str , e : impl fmt:: Display ) -> Error {
198- Error :: str ( format_args ! ( "cannot serialise {i} as {fmt}: {e}" ) )
199- }
200-
201- self_cell:: self_cell!(
202- struct BytesValRs {
203- owner: Bytes ,
204-
205- #[ not_covariant]
206- dependent: ValRs ,
207- }
208- ) ;
209-
210- impl Iterator for BytesValRs {
211- type Item = ValR ;
212- fn next ( & mut self ) -> Option < Self :: Item > {
213- self . with_dependent_mut ( |_owner, iter| iter. next ( ) )
214- }
215- }
216-
217- type ValRs < ' a > = BoxIter < ' a , ValR > ;
218-
219- fn parse_byte_str ( b : Bytes , parse : impl FnOnce ( & str ) -> ValRs ) -> ValRs < ' static > {
220- Box :: new ( BytesValRs :: new ( b, |b| {
221- then ( core:: str:: from_utf8 ( b) . map_err ( Error :: str) , parse)
222- } ) )
223- }
224-
225- fn parse_bytes ( b : Bytes , parse : impl FnOnce ( & [ u8 ] ) -> ValRs ) -> ValRs < ' static > {
226- Box :: new ( BytesValRs :: new ( b, |b| parse ( b) ) )
227- }
228-
229- #[ cfg( feature = "parse" ) ]
230- fn parse_funs < D : for < ' a > DataT < V < ' a > = Val > > ( ) -> Box < [ Filter < RunPtr < D > > ] > {
231- Box :: new ( [
232- ( "fromjson" , v ( 0 ) , |cv| {
233- bmme ( then ( cv. 1 . try_as_utf8_bytes_owned ( ) , |s| {
234- let fail = move |r : Result < _ , _ > | r. map_err ( |e| parse_fail ( & cv. 1 , "JSON" , e) ) ;
235- parse_bytes ( s, |s| Box :: new ( json:: parse_many ( s) . map ( fail) ) )
236- } ) )
237- } ) ,
238- ( "fromcbor" , v ( 0 ) , |cv| {
239- bmme ( then ( cv. 1 . try_as_bytes_owned ( ) , |s| {
240- let fail = move |r : Result < _ , _ > | r. map_err ( |e| parse_fail ( & cv. 1 , "CBOR" , e) ) ;
241- parse_bytes ( s, |s| Box :: new ( cbor:: parse_many ( s) . map ( fail) ) )
242- } ) )
243- } ) ,
244- ( "fromyaml" , v ( 0 ) , |cv| {
245- bmme ( then ( cv. 1 . try_as_utf8_bytes_owned ( ) , |s| {
246- let fail = move |r : Result < _ , _ > | r. map_err ( |e| parse_fail ( & cv. 1 , "YAML" , e) ) ;
247- parse_byte_str ( s, |s| Box :: new ( yaml:: parse_many ( s) . map ( fail) ) )
248- } ) )
249- } ) ,
250- ( "fromxml" , v ( 0 ) , |cv| {
251- bmme ( then ( cv. 1 . try_as_utf8_bytes_owned ( ) , |s| {
252- let fail = move |r : Result < _ , _ > | r. map_err ( |e| parse_fail ( & cv. 1 , "XML" , e) ) ;
253- parse_byte_str ( s, |s| Box :: new ( xml:: parse_many ( s) . map ( fail) ) )
254- } ) )
255- } ) ,
256- ( "fromtoml" , v ( 0 ) , |cv| {
257- let from_utf8 = |b| core:: str:: from_utf8 ( b) . map_err ( Error :: str) ;
258- let parse = |b| toml:: parse ( b) . map_err ( |e| parse_fail ( & cv. 1 , "TOML" , e) ) ;
259- bome ( cv. 1 . try_as_utf8_bytes ( ) . and_then ( from_utf8) . and_then ( parse) )
260- } ) ,
261- ( "tocbor" , v ( 0 ) , |cv| {
262- let mut buf = Vec :: new ( ) ;
263- cbor:: write ( & mut buf, & cv. 1 ) . unwrap ( ) ;
264- bome ( Ok ( Val :: byte_str ( buf) ) )
265- } ) ,
266- ( "toyaml" , v ( 0 ) , |cv| {
267- let mut buf = Vec :: new ( ) ;
268- yaml:: write ( & mut buf, & cv. 1 ) . unwrap ( ) ;
269- box_once ( Ok ( Val :: from_utf8_bytes ( buf) ) )
270- } ) ,
271- ( "totoml" , v ( 0 ) , |cv| {
272- let ser = toml:: serialise ( & cv. 1 ) . map_err ( |e| serialise_fail ( & cv. 1 , "TOML" , e) ) ;
273- bome ( ser. map ( |ser| Val :: utf8_str ( ser. to_string ( ) ) ) )
274- } ) ,
275- ( "toxml" , v ( 0 ) , |cv| {
276- let ser = xml:: serialise ( & cv. 1 ) . map_err ( |e| serialise_fail ( & cv. 1 , "XML" , e) ) ;
277- bome ( ser. map ( |ser| Val :: utf8_str ( ser. to_string ( ) ) ) )
278- } ) ,
279- ] )
280- }
0 commit comments