pub fn new(data: Vec<u8>) -> Result<Self, Error> {
        // Determine the packet type. The MSB of the fifth byte determines packet type.
        let len = data.len();
        if (len > 4) && (data[4] & 0x80 != 0) {
            // Initialization packet.
            if len < (INIT_HEADER_LENGTH + INIT_MIN_PAYLOAD_LENGTH) as usize {
                return Err(format_err!("Data too short for initialization packet"));
            };
            let mut buf = data.into_buf();
            Packet::initialization(
                /*channel*/ buf.get_u32_be(),
                /*command*/ Command::try_from(buf.get_u8() & 0x7F)?,
                /*message length*/ buf.get_u16_be(),
                /*payload*/ buf.bytes(),
            )
        } else {
            // Continuation packet.
            if len < (CONT_HEADER_LENGTH + CONT_MIN_PAYLOAD_LENGTH) as usize {
                return Err(format_err!("Data too short for continuation packet"));
            }
            let mut buf = data.into_buf();
            Packet::continuation(
                /*channel*/ buf.get_u32_be(),
                /*sequence*/ buf.get_u8(),
                /*payload*/ buf.bytes(),
            )
        }
    }
/// Create a new packet using the supplied data if it is valid, or return an informative
/// error otherwise.
Create a new packet using the supplied data if it is valid, or return an informative
error otherwise.
Create a new packet using the supplied data if it is valid or return an informative error otherwise .
pub fn new(data: Vec<u8>) -> Result<Self, Error> {
        // Determine the packet type. The MSB of the fifth byte determines packet type.
        let len = data.len();
        if (len > 4) && (data[4] & 0x80 != 0) {
            // Initialization packet.
            if len < (INIT_HEADER_LENGTH + INIT_MIN_PAYLOAD_LENGTH) as usize {
                return Err(format_err!("Data too short for initialization packet"));
            };
            let mut buf = data.into_buf();
            Packet::initialization(
                /*channel*/ buf.get_u32_be(),
                /*command*/ Command::try_from(buf.get_u8() & 0x7F)?,
                /*message length*/ buf.get_u16_be(),
                /*payload*/ buf.bytes(),
            )
        } else {
            // Continuation packet.
            if len < (CONT_HEADER_LENGTH + CONT_MIN_PAYLOAD_LENGTH) as usize {
                return Err(format_err!("Data too short for continuation packet"));
            }
            let mut buf = data.into_buf();
            Packet::continuation(
                /*channel*/ buf.get_u32_be(),
                /*sequence*/ buf.get_u8(),
                /*payload*/ buf.bytes(),
            )
        }
    }
pub fn new ( data : Vec < u8 > ) -> Result < Self , Error > { // Determine the packet type. The MSB of the fifth byte determines packet type. let len = data . len ( ) ; if ( len > 4 ) && ( data [ 4 ] & 0x80 != 0 ) { // Initialization packet. if len < ( INIT_HEADER_LENGTH + INIT_MIN_PAYLOAD_LENGTH ) as usize { return Err ( format_err ! ( " " ) ) ; } ; let mut buf = data . into_buf ( ) ; Packet :: initialization ( /*channel*/ buf . get_u32_be ( ) , /*command*/ Command :: try_from ( buf . get_u8 ( ) & 0x7F ) ? , /*message length*/ buf . get_u16_be ( ) , /*payload*/ buf . bytes ( ) , ) } else { // Continuation packet. if len < ( CONT_HEADER_LENGTH + CONT_MIN_PAYLOAD_LENGTH ) as usize { return Err ( format_err ! ( " " ) ) ; } let mut buf = data . into_buf ( ) ; Packet :: continuation ( /*channel*/ buf . get_u32_be ( ) , /*sequence*/ buf . get_u8 ( ) , /*payload*/ buf . bytes ( ) , ) } }
Create a new packet using the supplied data if it is valid, or return an informative
error otherwise.
Create a new packet using the supplied data if it is valid or return an informative error otherwise .
data
Vec<u8>