1
0
Fork 0
mirror of git://git.psyc.eu/libpsyc synced 2024-08-15 03:19:02 +00:00

fixed bug in PacketId::from_bytes

This commit is contained in:
lurchi 2016-09-04 18:10:19 +02:00
parent 5fb7482565
commit 2ca0949327
2 changed files with 19 additions and 6 deletions

View file

@ -31,16 +31,16 @@ pub struct PacketId<'a> {
impl<'a> PacketId<'a> {
pub fn from_bytes(bytes: &'a [u8]) -> Option<Self> {
let mut parsed: Vec<Option<&'a [u8]>> = vec![None; 5];
let mut parsed: Vec<Option<&'a [u8]>> = 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 {

View file

@ -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();