From 86f41e49f87764f9afd4be1d0d008a320dcfc331 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 26 Mar 2023 18:50:31 +0200 Subject: feat: add deserializing API interface definitions --- src/deserialization/buffer_deserializer.rs | 40 ++++++++++++++++++++++++++---- src/deserialization/mod.rs | 3 +++ 2 files changed, 38 insertions(+), 5 deletions(-) (limited to 'src/deserialization') diff --git a/src/deserialization/buffer_deserializer.rs b/src/deserialization/buffer_deserializer.rs index 652e1ff..a4e7b2f 100644 --- a/src/deserialization/buffer_deserializer.rs +++ b/src/deserialization/buffer_deserializer.rs @@ -98,7 +98,7 @@ where }; if let IgnoreEnd::No = ignore_end { - self.read_end_event(tag_name)?; + self.read_end_event(tag_name.as_bytes())?; } Ok(deserialized) @@ -132,7 +132,38 @@ where ) })?; - self.read_end_event(tag_name)?; + self.read_end_event(tag_name.as_bytes())?; + + deserialized_items.push(deserialized); + } + + Ok(deserialized_items) + } + + fn de_list(&mut self) -> Result, DeserializerError> + { + let mut deserialized_items = Vec::new(); + + loop { + let start = match read_event!(self) { + Event::Start(start) => start, + Event::Comment(_) => { + continue; + } + event => { + self.leftover_event = Some(event.into_owned()); + break; + } + }; + + let deserialized = De::deserialize(&start, self).map_err(|err| { + DeserializerError::DeserializeFailed( + type_name::(), + WrappedDeserializeError::new(err), + ) + })?; + + self.read_end_event(start.name().as_ref())?; deserialized_items.push(deserialized); } @@ -193,12 +224,11 @@ impl BufferDeserializer where Source: BufRead, { - fn read_end_event(&mut self, tag_name: &str) -> Result<(), DeserializerError> + fn read_end_event(&mut self, tag_name: &[u8]) -> Result<(), DeserializerError> { let event = read_event!(self); - if matches!(&event, Event::End(end) if end.name().as_ref() == tag_name.as_bytes()) - { + if matches!(&event, Event::End(end) if end.name().as_ref() == tag_name) { return Ok(()); } diff --git a/src/deserialization/mod.rs b/src/deserialization/mod.rs index fa25e4b..b86c2af 100644 --- a/src/deserialization/mod.rs +++ b/src/deserialization/mod.rs @@ -15,6 +15,7 @@ pub trait Deserialize: Sized ) -> Result; } +#[cfg_attr(test, ridicule::automock)] pub trait Deserializer { fn de_tag( @@ -38,6 +39,8 @@ pub trait Deserializer tag_name: &str, ) -> Result, DeserializerError>; + fn de_list(&mut self) -> Result, DeserializerError>; + fn de_text(&mut self) -> Result; fn skip_to_tag_start(&mut self, tag_name: &str) -> Result<(), DeserializerError>; -- cgit v1.2.3-18-g5258