pub fn next_byte(self, value: u8) -> Result<FrameNextByteResult, FrameParseError> {
        match self {
            FrameParser::WaitingForHeader => {
                if value == FRAME_HEADER {
                    Ok(FrameNextByteResult::Unfinished(FrameParser::LengthPart1 {
                        calculated_crc: CRC::new(FRAME_HEADER),
                    }))
                } else {
                    Err(FrameParseError::InvalidFrameHeader(value))
                }
            }
            FrameParser::LengthPart1 { calculated_crc } => Ok(FrameNextByteResult::Unfinished(FrameParser::LengthPart2 {
                calculated_crc: calculated_crc.calculate_next(value),
                length_part_1: value,
            })),
            FrameParser::LengthPart2 { calculated_crc, length_part_1 } => {
                let length = ((length_part_1 as u16) << 8) + (value as u16);

                // If the length is < 6 consider the frame to be invalid
                // See docs of frame why.
                if length < 6 {
                    return Err(FrameParseError::InvalidFrameLength(length));
                }

                // First 3 bytes are included in the size but will not be found int the data Vec
                // First 3 bytes = Frame header (1B) + Frame length (2B)
                let capacity = length as usize - 3;

                Ok(FrameNextByteResult::Unfinished(FrameParser::ReceiveFrameData {
                    calculated_crc: calculated_crc.calculate_next(value),
                    length,
                    data: Vec::with_capacity(capacity),
                }))
            }
            FrameParser::ReceiveFrameData {
                calculated_crc,
                length,
                mut data,
            } => {
                // Append the new data
                data.push(value);

                // Calculate the new crc
                let calculated_crc = calculated_crc.calculate_next(value);

                // Calculate the actual length
                // Data length + Frame Header (1B) + Frame Length (2B)
                let actual_length = data.len() as u16 + 3;

                // When we reached the frame length move on to CRC parsing
                let next_frame = if actual_length == length {
                    FrameParser::CRCPart1 { calculated_crc, data }
                } else {
                    FrameParser::ReceiveFrameData { calculated_crc, length, data }
                };

                // Return the new frame
                Ok(FrameNextByteResult::Unfinished(next_frame))
            }
            FrameParser::CRCPart1 { calculated_crc, data } => Ok(FrameNextByteResult::Unfinished(FrameParser::CRCPart2 {
                calculated_crc,
                data,
                received_crc_part_1: value,
            })),
            FrameParser::CRCPart2 {
                calculated_crc,
                data,
                received_crc_part_1,
            } => {
                let received = ((received_crc_part_1 as u16) << 8) + value as u16;
                let calculated = calculated_crc.as_u16();

                if received != calculated {
                    Err(FrameParseError::InvalidCRC { calculated, received })
                } else {
                    Ok(FrameNextByteResult::Finished(Frame(data)))
                }
            }
        }
    }
/// Feed the next byte into a frame and calculate the resulting frame
Feed the next byte into a frame and calculate the resulting frame
Feed the next byte into a frame and calculate the resulting frame
pub fn next_byte(self, value: u8) -> Result<FrameNextByteResult, FrameParseError> {
        match self {
            FrameParser::WaitingForHeader => {
                if value == FRAME_HEADER {
                    Ok(FrameNextByteResult::Unfinished(FrameParser::LengthPart1 {
                        calculated_crc: CRC::new(FRAME_HEADER),
                    }))
                } else {
                    Err(FrameParseError::InvalidFrameHeader(value))
                }
            }
            FrameParser::LengthPart1 { calculated_crc } => Ok(FrameNextByteResult::Unfinished(FrameParser::LengthPart2 {
                calculated_crc: calculated_crc.calculate_next(value),
                length_part_1: value,
            })),
            FrameParser::LengthPart2 { calculated_crc, length_part_1 } => {
                let length = ((length_part_1 as u16) << 8) + (value as u16);
                // If the length is < 6 consider the frame to be invalid
                // See docs of frame why.
                if length < 6 {
                    return Err(FrameParseError::InvalidFrameLength(length));
                }
                // First 3 bytes are included in the size but will not be found int the data Vec
                // First 3 bytes = Frame header (1B) + Frame length (2B)
                let capacity = length as usize - 3;
                Ok(FrameNextByteResult::Unfinished(FrameParser::ReceiveFrameData {
                    calculated_crc: calculated_crc.calculate_next(value),
                    length,
                    data: Vec::with_capacity(capacity),
                }))
            }
            FrameParser::ReceiveFrameData {
                calculated_crc,
                length,
                mut data,
            } => {
                // Append the new data
                data.push(value);
                // Calculate the new crc
                let calculated_crc = calculated_crc.calculate_next(value);
                // Calculate the actual length
                // Data length + Frame Header (1B) + Frame Length (2B)
                let actual_length = data.len() as u16 + 3;
                // When we reached the frame length move on to CRC parsing
                let next_frame = if actual_length == length {
                    FrameParser::CRCPart1 { calculated_crc, data }
                } else {
                    FrameParser::ReceiveFrameData { calculated_crc, length, data }
                };
                // Return the new frame
                Ok(FrameNextByteResult::Unfinished(next_frame))
            }
            FrameParser::CRCPart1 { calculated_crc, data } => Ok(FrameNextByteResult::Unfinished(FrameParser::CRCPart2 {
                calculated_crc,
                data,
                received_crc_part_1: value,
            })),
            FrameParser::CRCPart2 {
                calculated_crc,
                data,
                received_crc_part_1,
            } => {
                let received = ((received_crc_part_1 as u16) << 8) + value as u16;
                let calculated = calculated_crc.as_u16();
                if received != calculated {
                    Err(FrameParseError::InvalidCRC { calculated, received })
                } else {
                    Ok(FrameNextByteResult::Finished(Frame(data)))
                }
            }
        }
    }
pub fn next_byte ( self , value : u8 ) -> Result < FrameNextByteResult , FrameParseError > { match self { FrameParser :: WaitingForHeader => { if value == FRAME_HEADER { Ok ( FrameNextByteResult :: Unfinished ( FrameParser :: LengthPart1 { calculated_crc : CRC :: new ( FRAME_HEADER ) , } ) ) } else { Err ( FrameParseError :: InvalidFrameHeader ( value ) ) } } FrameParser :: LengthPart1 { calculated_crc } => Ok ( FrameNextByteResult :: Unfinished ( FrameParser :: LengthPart2 { calculated_crc : calculated_crc . calculate_next ( value ) , length_part_1 : value , } ) ) , FrameParser :: LengthPart2 { calculated_crc , length_part_1 } => { let length = ( ( length_part_1 as u16 ) << 8 ) + ( value as u16 ) ; // If the length is < 6 consider the frame to be invalid // See docs of frame why. if length < 6 { return Err ( FrameParseError :: InvalidFrameLength ( length ) ) ; } // First 3 bytes are included in the size but will not be found int the data Vec // First 3 bytes = Frame header (1B) + Frame length (2B) let capacity = length as usize - 3 ; Ok ( FrameNextByteResult :: Unfinished ( FrameParser :: ReceiveFrameData { calculated_crc : calculated_crc . calculate_next ( value ) , length , data : Vec :: with_capacity ( capacity ) , } ) ) } FrameParser :: ReceiveFrameData { calculated_crc , length , mut data , } => { // Append the new data data . push ( value ) ; // Calculate the new crc let calculated_crc = calculated_crc . calculate_next ( value ) ; // Calculate the actual length // Data length + Frame Header (1B) + Frame Length (2B) let actual_length = data . len ( ) as u16 + 3 ; // When we reached the frame length move on to CRC parsing let next_frame = if actual_length == length { FrameParser :: CRCPart1 { calculated_crc , data } } else { FrameParser :: ReceiveFrameData { calculated_crc , length , data } } ; // Return the new frame Ok ( FrameNextByteResult :: Unfinished ( next_frame ) ) } FrameParser :: CRCPart1 { calculated_crc , data } => Ok ( FrameNextByteResult :: Unfinished ( FrameParser :: CRCPart2 { calculated_crc , data , received_crc_part_1 : value , } ) ) , FrameParser :: CRCPart2 { calculated_crc , data , received_crc_part_1 , } => { let received = ( ( received_crc_part_1 as u16 ) << 8 ) + value as u16 ; let calculated = calculated_crc . as_u16 ( ) ; if received != calculated { Err ( FrameParseError :: InvalidCRC { calculated , received } ) } else { Ok ( FrameNextByteResult :: Finished ( Frame ( data ) ) ) } } } }
Feed the next byte into a frame and calculate the resulting frame
Feed the next byte into a frame and calculate the resulting frame
self
value
u8