aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 9cf3716139639dda6a0459a19804640189058315 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! XML is awful.
#![deny(clippy::all, clippy::pedantic, unsafe_code, missing_docs)]
use crate::deserializer::{Deserializer, Error as DeserializerError, MaybeStatic};
use crate::tagged::TagStart;

pub mod attribute;
pub mod deserializer;
pub mod tagged;

mod event;
mod util;

/// Trait implemented by types that want to be deserializable from tagged XML elements.
pub trait DeserializeTagged: Sized + MaybeStatic
{
    /// 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<TDeserializer: Deserializer>(
        start: &TagStart,
        deserializer: &mut TDeserializer,
    ) -> Result<Self, Self::Error>;
}

/// Result extension.
pub trait ResultExt<Value, DeError>
{
    /// Returns `Ok(None)` if `Err` is `DeserializerError::UnexpectedEvent`.
    fn try_event(self) -> Result<Option<Value>, DeserializerError<DeError>>;
}

impl<Value, DeError> ResultExt<Value, DeError>
    for Result<Value, DeserializerError<DeError>>
{
    fn try_event(self) -> Result<Option<Value>, DeserializerError<DeError>>
    {
        self.map_or_else(
            |err| {
                if let DeserializerError::UnexpectedEvent {
                    expected_event_name: _,
                    found_event: _,
                } = err
                {
                    return Ok(None);
                }

                Err(err)
            },
            |value| Ok(Some(value)),
        )
    }
}