pub fn parse(buf: &'r mut untrusted::Reader<'i>) -> Result<Self, Error> {
        let initial = buf.read_byte()?;

        // Pull out the "argument", which is at most eight following bytes.
        // For determinism, we *require* that the minimal encoding is used.
        let argument = match initial & 0b00011111 {
            b @ 0..=23 => b as u32,

            24 => {
                let n = buf.read_byte()?;
                if n < 24 {
                    return Err(Error::BadEncoding);
                }

                n as u32
            }

            25 => {
                let slice = buf.read_bytes(2)?.as_slice_less_safe();
                let bytes: [u8; 2] = slice.try_into().unwrap();

                let n = u16::from_be_bytes(bytes);
                if n < u8::MAX as u16 {
                    return Err(Error::BadEncoding);
                }

                n as u32
            }

            26 => {
                let slice = buf.read_bytes(4)?.as_slice_less_safe();
                let bytes: [u8; 4] = slice.try_into().unwrap();

                let n = u32::from_be_bytes(bytes);
                if n < u16::MAX as u32 {
                    return Err(Error::BadEncoding);
                }

                n
            }

            // 27 is 64-bit integers, which we don't handle currently;
            // 28-30 are reserved;
            // 31 is indefinite-length encoding, which is banned.
            _ => return Err(Error::BadEncoding),
        };

        match initial >> 5 {
            sign @ 0..=1 => {
                Ok(Item::Scalar(Scalar::Int(Int(sign == 1, argument))))
            }
            2 => Ok(Item::Scalar(Scalar::Bytes(
                buf.read_bytes(argument as usize)?.as_slice_less_safe(),
            ))),
            3 => {
                let bytes =
                    buf.read_bytes(argument as usize)?.as_slice_less_safe();
                Ok(Item::Scalar(Scalar::Utf8(
                    core::str::from_utf8(bytes)
                        .map_err(|_| Error::BadEncoding)?,
                )))
            }
            4 => Ok(Item::Array(Array { buf, len: argument })),
            5 => Ok(Item::Map(Map {
                buf,
                len: argument,
                prev_key: None,
                current_key: None,
            })),
            _ => Err(Error::BadEncoding),
        }
    }
/// Parses a single CBOR item.
Parses a single CBOR item.
Parses a single CBOR item .
pub fn parse(buf: &'r mut untrusted::Reader<'i>) -> Result<Self, Error> {
        let initial = buf.read_byte()?;
        // Pull out the "argument", which is at most eight following bytes.
        // For determinism, we *require* that the minimal encoding is used.
        let argument = match initial & 0b00011111 {
            b @ 0..=23 => b as u32,
            24 => {
                let n = buf.read_byte()?;
                if n < 24 {
                    return Err(Error::BadEncoding);
                }
                n as u32
            }
            25 => {
                let slice = buf.read_bytes(2)?.as_slice_less_safe();
                let bytes: [u8; 2] = slice.try_into().unwrap();
                let n = u16::from_be_bytes(bytes);
                if n < u8::MAX as u16 {
                    return Err(Error::BadEncoding);
                }
                n as u32
            }
            26 => {
                let slice = buf.read_bytes(4)?.as_slice_less_safe();
                let bytes: [u8; 4] = slice.try_into().unwrap();
                let n = u32::from_be_bytes(bytes);
                if n < u16::MAX as u32 {
                    return Err(Error::BadEncoding);
                }
                n
            }
            // 27 is 64-bit integers, which we don't handle currently;
            // 28-30 are reserved;
            // 31 is indefinite-length encoding, which is banned.
            _ => return Err(Error::BadEncoding),
        };
        match initial >> 5 {
            sign @ 0..=1 => {
                Ok(Item::Scalar(Scalar::Int(Int(sign == 1, argument))))
            }
            2 => Ok(Item::Scalar(Scalar::Bytes(
                buf.read_bytes(argument as usize)?.as_slice_less_safe(),
            ))),
            3 => {
                let bytes =
                    buf.read_bytes(argument as usize)?.as_slice_less_safe();
                Ok(Item::Scalar(Scalar::Utf8(
                    core::str::from_utf8(bytes)
                        .map_err(|_| Error::BadEncoding)?,
                )))
            }
            4 => Ok(Item::Array(Array { buf, len: argument })),
            5 => Ok(Item::Map(Map {
                buf,
                len: argument,
                prev_key: None,
                current_key: None,
            })),
            _ => Err(Error::BadEncoding),
        }
    }
pub fn parse ( buf : & ' r mut untrusted :: Reader < ' i > ) -> Result < Self , Error > { let initial = buf . read_byte ( ) ? ; // Pull out the "argument", which is at most eight following bytes. // For determinism, we *require* that the minimal encoding is used. let argument = match initial & 0b00011111 { b @ 0 ..= 23 => b as u32 , 24 => { let n = buf . read_byte ( ) ? ; if n < 24 { return Err ( Error :: BadEncoding ) ; } n as u32 } 25 => { let slice = buf . read_bytes ( 2 ) ? . as_slice_less_safe ( ) ; let bytes : [ u8 ; 2 ] = slice . try_into ( ) . unwrap ( ) ; let n = u16 :: from_be_bytes ( bytes ) ; if n < u8 :: MAX as u16 { return Err ( Error :: BadEncoding ) ; } n as u32 } 26 => { let slice = buf . read_bytes ( 4 ) ? . as_slice_less_safe ( ) ; let bytes : [ u8 ; 4 ] = slice . try_into ( ) . unwrap ( ) ; let n = u32 :: from_be_bytes ( bytes ) ; if n < u16 :: MAX as u32 { return Err ( Error :: BadEncoding ) ; } n } // 27 is 64-bit integers, which we don't handle currently; // 28-30 are reserved; // 31 is indefinite-length encoding, which is banned. _ => return Err ( Error :: BadEncoding ) , } ; match initial >> 5 { sign @ 0 ..= 1 => { Ok ( Item :: Scalar ( Scalar :: Int ( Int ( sign == 1 , argument ) ) ) ) } 2 => Ok ( Item :: Scalar ( Scalar :: Bytes ( buf . read_bytes ( argument as usize ) ? . as_slice_less_safe ( ) , ) ) ) , 3 => { let bytes = buf . read_bytes ( argument as usize ) ? . as_slice_less_safe ( ) ; Ok ( Item :: Scalar ( Scalar :: Utf8 ( core :: str :: from_utf8 ( bytes ) . map_err ( | _ | Error :: BadEncoding ) ? , ) ) ) } 4 => Ok ( Item :: Array ( Array { buf , len : argument } ) ) , 5 => Ok ( Item :: Map ( Map { buf , len : argument , prev_key : None , current_key : None , } ) ) , _ => Err ( Error :: BadEncoding ) , } }
Parses a single CBOR item.
Parses a single CBOR item .
buf
&'r mut untrusted::Reader<'i>
buf
&'r mut untrusted::Reader<'i>