@@ -3,34 +3,34 @@ use crate::{invalid_data, map_invalid_data, Format};
33use bytes:: Bytes ;
44use jaq_core:: box_iter:: { box_once, BoxIter } ;
55use jaq_json:: { Tag , Val } ;
6- use std:: io:: { self , BufRead , Read } ;
6+ use std:: io:: { self , Read } ;
77
88type Vals < ' a > = BoxIter < ' a , io:: Result < Val > > ;
99
1010/// Read input to string for certain formats.
1111///
12- /// This has to be synchronised with [`from_bufread `].
12+ /// This has to be synchronised with [`read `].
1313pub fn read_string ( fmt : Format , read : impl Read ) -> Result < String > {
1414 use Format :: * ;
1515 match fmt {
16- Raw | Json | Cbor => Ok ( String :: new ( ) ) ,
16+ Raw | Raw0 | Json | Cbor => Ok ( String :: new ( ) ) ,
1717 Toml | Xml | Yaml => io:: read_to_string ( read) ,
1818 }
1919}
2020
2121/// Convert bytes to string for certain formats.
2222///
23- /// This has to be synchronised with [`from_bytes `].
23+ /// This has to be synchronised with [`parse `].
2424pub fn bytes_str ( fmt : Format , bytes : & [ u8 ] ) -> Result < & str > {
2525 use Format :: * ;
2626 Ok ( match fmt {
27- Raw | Json | Cbor => "" ,
27+ Raw | Raw0 | Json | Cbor => "" ,
2828 Toml | Xml | Yaml => core:: str:: from_utf8 ( bytes) . map_err ( invalid_data) ?,
2929 } )
3030}
3131
32- /// Read value from [`BufRead`] or [`&str`], depending on format.
33- pub fn from_bufread < ' a > ( fmt : Format , read : impl BufRead + ' a , s : & ' a str , slurp : bool ) -> Vals < ' a > {
32+ /// Read values from [`io:: BufRead`] or [`&str`], depending on format.
33+ pub fn read < ' a > ( fmt : Format , read : impl io :: BufRead + ' a , s : & ' a str , slurp : bool ) -> Vals < ' a > {
3434 use bstr:: io:: BufReadExt ;
3535 let mut read = read;
3636 match fmt {
@@ -40,6 +40,7 @@ pub fn from_bufread<'a>(fmt: Format, read: impl BufRead + 'a, s: &'a str, slurp:
4040 box_once ( result. map ( |_| Val :: utf8_str ( buf) ) )
4141 }
4242 Format :: Raw => Box :: new ( read. byte_lines ( ) . map ( |r| r. map ( Val :: utf8_str) ) ) ,
43+ Format :: Raw0 => collect_if ( slurp, read. byte_records ( 0 ) . map ( |r| r. map ( Val :: utf8_str) ) ) ,
4344 Format :: Cbor => collect_if ( slurp, cbor:: read_many ( read) ) ,
4445 Format :: Json => collect_if ( slurp, json:: read_many ( read) ) ,
4546 Format :: Toml => box_once ( toml:: parse ( s) . map_err ( invalid_data) ) ,
@@ -48,16 +49,17 @@ pub fn from_bufread<'a>(fmt: Format, read: impl BufRead + 'a, s: &'a str, slurp:
4849 }
4950}
5051
51- /// Parse value from file or `s` , depending on format.
52- pub fn from_bytes < ' a > ( fmt : Format , bytes : & ' a Bytes , s : & ' a str , slurp : bool ) -> Vals < ' a > {
52+ /// Parse values from [`Bytes`] or [`&str`] , depending on format.
53+ pub fn parse < ' a > ( fmt : Format , bytes : & ' a Bytes , s : & ' a str , slurp : bool ) -> Vals < ' a > {
5354 use bstr:: ByteSlice ;
55+ let nul_sep = |s : & ' a [ u8 ] | s. strip_suffix ( b"\0 " ) . unwrap_or ( bytes) . split_str ( "\0 " ) ;
56+ let slice_to_str = |s| Ok ( Val :: Str ( bytes. slice_ref ( s) , Tag :: Utf8 ) ) ;
5457 match fmt {
5558 Format :: Raw if slurp => box_once ( Ok ( Val :: Str ( bytes. clone ( ) , Tag :: Utf8 ) ) ) ,
56- Format :: Raw => Box :: new (
57- ByteSlice :: lines ( & * * bytes) . map ( |line| Ok ( Val :: Str ( bytes. slice_ref ( line) , Tag :: Utf8 ) ) ) ,
58- ) ,
59+ Format :: Raw => Box :: new ( bytes. lines ( ) . map ( slice_to_str) ) ,
60+ Format :: Raw0 => collect_if ( slurp, nul_sep ( bytes) . map ( slice_to_str) ) ,
5961 Format :: Json => collect_if ( slurp, json:: parse_many ( bytes) . map ( map_invalid_data) ) ,
6062 Format :: Cbor => collect_if ( slurp, cbor:: parse_many ( bytes) . map ( map_invalid_data) ) ,
61- Format :: Toml | Format :: Xml | Format :: Yaml => from_bufread ( fmt, & [ ] [ ..] , s, slurp) ,
63+ Format :: Toml | Format :: Xml | Format :: Yaml => read ( fmt, & [ ] [ ..] , s, slurp) ,
6264 }
6365}
0 commit comments