From 2efe2ab213f2514ba2bb75de982a80bc3306a2a2 Mon Sep 17 00:00:00 2001 From: Sayan Nandan Date: Mon, 10 May 2021 10:33:42 +0530 Subject: [PATCH] Implement parsing of datagroup elements --- server/src/protocol/parserv2.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/server/src/protocol/parserv2.rs b/server/src/protocol/parserv2.rs index 686989d1..90a2adc3 100644 --- a/server/src/protocol/parserv2.rs +++ b/server/src/protocol/parserv2.rs @@ -149,6 +149,19 @@ impl<'a> Parser<'a> { Err(ParseError::UnexpectedByte) } } + /// This will read a datagroup element and return an **owned vector** containing the bytes + /// for the next datagroup element + fn parse_next_datagroup_element(&mut self) -> ParseResult> { + // So we need to read the sizeline for this element first! + let element_sizeline = self.read_sizeline()?; + println!("We're expecting: {} bytes", element_sizeline); + // Now we want to read the element itself + let ret = self.read_until(element_sizeline)?.to_owned(); + // Now move the cursor ahead as read_until doesn't do anything with the newline + self.incr_cursor(); + // We'll just return this since that's all we have to do! + Ok(ret) + } } #[test] @@ -177,3 +190,14 @@ fn test_actiongroup_size_parse() { ); assert_eq!(parser.cursor, dataframe_layout.len()); } + +#[test] +fn test_read_datagroup_element() { + let element_with_block = "#5\nsayan\n".as_bytes(); + let mut parser = Parser::new(&element_with_block); + assert_eq!( + String::from("sayan").into_bytes(), + parser.parse_next_datagroup_element().unwrap() + ); + assert_eq!(parser.cursor, element_with_block.len()); +}