1+ use jiter:: JsonValue ;
2+ use num_bigint:: { BigInt , Sign } ;
13use std:: fmt;
24use std:: mem:: { align_of, size_of} ;
35
4- use jiter:: JsonValue ;
5-
66use crate :: array:: {
77 header_array_to_json, header_array_write_to_json, i64_array_slice, i64_array_to_json, u8_array_slice,
88 u8_array_to_json, HetArray ,
@@ -56,16 +56,16 @@ impl<'b> Decoder<'b> {
5656 Header :: Null => Ok ( JsonValue :: Null ) ,
5757 Header :: Bool ( b) => Ok ( JsonValue :: Bool ( b) ) ,
5858 Header :: Int ( n) => n. decode_i64 ( self ) . map ( JsonValue :: Int ) ,
59- Header :: IntBig ( i ) => todo ! ( "decoding for bigint {i:?}" ) ,
59+ Header :: IntBig ( s , l ) => self . take_big_int ( s , l ) . map ( JsonValue :: BigInt ) ,
6060 Header :: Float ( n) => n. decode_f64 ( self ) . map ( JsonValue :: Float ) ,
61- Header :: Str ( l) => self . decode_str ( l) . map ( |s| JsonValue :: Str ( s. into ( ) ) ) ,
61+ Header :: Str ( l) => self . take_str_len ( l) . map ( |s| JsonValue :: Str ( s. into ( ) ) ) ,
6262 Header :: Object ( length) => {
6363 let obj = Object :: decode_header ( self , length) ?;
64- obj. to_json ( self ) . map ( JsonValue :: Object )
64+ obj. to_value ( self ) . map ( JsonValue :: Object )
6565 }
6666 Header :: HetArray ( length) => {
6767 let het = HetArray :: decode_header ( self , length) ?;
68- het. to_json ( self ) . map ( JsonValue :: Array )
68+ het. to_value ( self ) . map ( JsonValue :: Array )
6969 }
7070 Header :: U8Array ( length) => u8_array_to_json ( self , length) . map ( JsonValue :: Array ) ,
7171 Header :: HeaderArray ( length) => header_array_to_json ( self , length) . map ( JsonValue :: Array ) ,
@@ -81,13 +81,16 @@ impl<'b> Decoder<'b> {
8181 let i = n. decode_i64 ( self ) ?;
8282 writer. write_value ( i) ?;
8383 }
84- Header :: IntBig ( i) => todo ! ( "decoding for bigint {i:?}" ) ,
84+ Header :: IntBig ( s, l) => {
85+ let int = self . take_big_int ( s, l) ?;
86+ writer. write_value ( int) ?;
87+ }
8588 Header :: Float ( n) => {
8689 let f = n. decode_f64 ( self ) ?;
8790 writer. write_value ( f) ?;
8891 }
8992 Header :: Str ( l) => {
90- let s = self . decode_str ( l) ?;
93+ let s = self . take_str_len ( l) ?;
9194 writer. write_value ( s) ?;
9295 }
9396 Header :: Object ( length) => {
@@ -130,32 +133,23 @@ impl<'b> Decoder<'b> {
130133 Ok ( t)
131134 }
132135
133- pub fn decode_str ( & mut self , length : Length ) -> DecodeResult < & ' b str > {
136+ fn take_str_len ( & mut self , length : Length ) -> DecodeResult < & ' b str > {
134137 let len = length. decode ( self ) ?;
135- if len == 0 {
136- Ok ( "" )
137- } else {
138- self . take_str ( len)
139- }
138+ self . take_str ( len)
140139 }
141140
142- pub fn decode_bytes ( & mut self , length : Length ) -> DecodeResult < & ' b [ u8 ] > {
143- let len = length. decode ( self ) ?;
144- if len == 0 {
145- Ok ( b"" )
141+ pub fn take_str ( & mut self , length : usize ) -> DecodeResult < & ' b str > {
142+ if length == 0 {
143+ Ok ( "" )
146144 } else {
147- self . take_slice ( len)
145+ let end = self . index + length;
146+ let slice = self . bytes . get ( self . index ..end) . ok_or_else ( || self . eof ( ) ) ?;
147+ let s = simdutf8:: basic:: from_utf8 ( slice) . map_err ( |e| DecodeError :: from_utf8_error ( self . index , e) ) ?;
148+ self . index = end;
149+ Ok ( s)
148150 }
149151 }
150152
151- pub fn take_str ( & mut self , length : usize ) -> DecodeResult < & ' b str > {
152- let end = self . index + length;
153- let slice = self . bytes . get ( self . index ..end) . ok_or_else ( || self . eof ( ) ) ?;
154- let s = simdutf8:: basic:: from_utf8 ( slice) . map_err ( |e| DecodeError :: from_utf8_error ( self . index , e) ) ?;
155- self . index = end;
156- Ok ( s)
157- }
158-
159153 pub fn take_u8 ( & mut self ) -> DecodeResult < u8 > {
160154 self . next ( ) . ok_or_else ( || self . eof ( ) )
161155 }
@@ -187,9 +181,10 @@ impl<'b> Decoder<'b> {
187181 Ok ( i64:: from_le_bytes ( slice. try_into ( ) . unwrap ( ) ) )
188182 }
189183
190- pub fn take_f32 ( & mut self ) -> DecodeResult < f32 > {
191- let slice = self . take_slice ( 4 ) ?;
192- Ok ( f32:: from_le_bytes ( slice. try_into ( ) . unwrap ( ) ) )
184+ pub fn take_big_int ( & mut self , sign : Sign , length : Length ) -> DecodeResult < BigInt > {
185+ let size = length. decode ( self ) ?;
186+ let slice = self . take_slice ( size) ?;
187+ Ok ( BigInt :: from_bytes_le ( sign, slice) )
193188 }
194189
195190 pub fn take_f64 ( & mut self ) -> DecodeResult < f64 > {
0 commit comments