diff --git a/src/transmission.rs b/src/transmission.rs index 061df4e..91d2cb3 100644 --- a/src/transmission.rs +++ b/src/transmission.rs @@ -41,6 +41,27 @@ impl Protocol300Transmission { [0x04] => Ok(Self::Eot), [0x15] => Ok(Self::Nack), [0x16, 0x00, 0x00] => Ok(Self::Init), + [0x41, ..] => Ok(Self::Message(Protocol300Message::try_from_bytes(bytes)?)),//TODO fail if too many bytes + b => { + error!("Failed to parse Protocol300 transmission: invalid byte sequence (length: {}, first byte: 0x{:02X})", + bytes.len(), + bytes.first().unwrap_or(&0x00)); + Err(FromBytesError::InvalidBytes(b.to_owned())) + }, + } + } + + /// Try to convert bytes to a Protocol 300 Transmission, but ignores any bytes that are too much + /// + /// # Errors + /// Returns a `FromBytesError` if the conversion fails in some way + pub fn try_from_bytes_incomplete(bytes: &[u8]) -> Result { + match bytes { + [0x06, ..] => Ok(Self::Ack), + [0x05, ..] => Ok(Self::Enq), + [0x04, ..] => Ok(Self::Eot), + [0x15, ..] => Ok(Self::Nack), + [0x16, 0x00, 0x00, ..] => Ok(Self::Init), [0x41, ..] => Ok(Self::Message(Protocol300Message::try_from_bytes(bytes)?)), b => { error!("Failed to parse Protocol300 transmission: invalid byte sequence (length: {}, first byte: 0x{:02X})",