From e762babd9e69400ccd178ba8946168640093eb63 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 15 Apr 2023 18:26:29 +0200 Subject: feat: add deserialization --- src/lib.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 8b13789..e5086bc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,55 @@ +//! XML is awful. +#![deny(clippy::all, clippy::pedantic, unsafe_code, missing_docs)] +use crate::deserializer::{Deserializer, Error as DeserializerError}; +use crate::tagged::TagStart; +pub mod attribute; +pub mod deserializer; +pub mod tagged; + +mod event; + +/// Trait implemented by types that want to be deserializable from tagged XML elements. +pub trait DeserializeTagged: Sized +{ + /// Error type. + type Error: std::error::Error + Send + Sync + 'static; + + /// Deserializes into a new `Self`. + /// + /// # Errors + /// When or if a error is returned is decided by the type implementing this trait. + fn deserialize( + start: &TagStart, + deserializer: &mut TDeserializer, + ) -> Result; +} + +/// Result extension. +pub trait ResultExt +{ + /// Returns `Ok(None)` if `Err` is `DeserializerError::UnexpectedEvent`. + fn try_event(self) -> Result, DeserializerError>; +} + +impl ResultExt + for Result> +{ + fn try_event(self) -> Result, DeserializerError> + { + self.map_or_else( + |err| { + if let DeserializerError::UnexpectedEvent { + expected_event_name: _, + found_event: _, + } = err + { + return Ok(None); + } + + Err(err) + }, + |value| Ok(Some(value)), + ) + } +} -- cgit v1.2.3-18-g5258