aboutsummaryrefslogtreecommitdiff
path: root/src/deserialization
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-03-26 18:50:31 +0200
committerHampusM <hampus@hampusmat.com>2023-03-26 18:50:31 +0200
commit86f41e49f87764f9afd4be1d0d008a320dcfc331 (patch)
tree3e7cf94bcecd42ccf2f1e8eb9928a377d4fdb1bd /src/deserialization
parentc8e5944d697b33738c7ba01694dde5e360470043 (diff)
feat: add deserializing API interface definitions
Diffstat (limited to 'src/deserialization')
-rw-r--r--src/deserialization/buffer_deserializer.rs40
-rw-r--r--src/deserialization/mod.rs3
2 files changed, 38 insertions, 5 deletions
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<De: Deserialize>(&mut self) -> Result<Vec<De>, 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::<De>(),
+ WrappedDeserializeError::new(err),
+ )
+ })?;
+
+ self.read_end_event(start.name().as_ref())?;
deserialized_items.push(deserialized);
}
@@ -193,12 +224,11 @@ impl<Source> BufferDeserializer<Source>
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<Self, Self::Error>;
}
+#[cfg_attr(test, ridicule::automock)]
pub trait Deserializer
{
fn de_tag<De: Deserialize>(
@@ -38,6 +39,8 @@ pub trait Deserializer
tag_name: &str,
) -> Result<Vec<De>, DeserializerError>;
+ fn de_list<De: Deserialize>(&mut self) -> Result<Vec<De>, DeserializerError>;
+
fn de_text(&mut self) -> Result<String, DeserializerError>;
fn skip_to_tag_start(&mut self, tag_name: &str) -> Result<(), DeserializerError>;