diff --git a/rust/src/packet_id.rs b/rust/src/packet_id.rs index 17a6db3..c855dcb 100644 --- a/rust/src/packet_id.rs +++ b/rust/src/packet_id.rs @@ -31,16 +31,16 @@ pub struct PacketId<'a> { impl<'a> PacketId<'a> { pub fn from_bytes(bytes: &'a [u8]) -> Option { - let mut parsed: Vec> = vec![None; 5]; + let mut parsed: Vec> = Vec::with_capacity(5); let mut parser = PsycListParser::new(); - let mut parsing_error = false; - for slice in &mut parsed { + for _i in 0..5 { match parser.parse(bytes) { - Ok(PsycListParserResult::ListElement {value: v}) => *slice = Some(v), - _ => parsing_error = true + Ok(PsycListParserResult::ListElement {value: b""}) => parsed.push(None), + Ok(PsycListParserResult::ListElement {value: v}) => parsed.push(Some(v)), + _ => break } } - if parsing_error { + if parsed.len() < 5 { None } else { let result = PacketId { diff --git a/rust/tests/test_list_parser.rs b/rust/tests/test_list_parser.rs index aab73ed..5c3ffb8 100644 --- a/rust/tests/test_list_parser.rs +++ b/rust/tests/test_list_parser.rs @@ -40,6 +40,19 @@ fn test_empty() { assert_eq!(parser.parse(&test_data).unwrap(), PsycListParserResult::Complete); } +#[test] +fn test_empty_element() { + let test_data = "|".to_string().into_bytes(); + + let mut parser = PsycListParser::new(); + + let expected = PsycListParserResult::ListElement { + value: b"" + }; + + assert_eq!(parser.parse(&test_data).unwrap(), expected); +} + #[test] fn test_incomplete() { let test_data1 = "|8 element".to_string().into_bytes();