aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: fb42cdf40f63b553e01452580d1d3fd87467fb6b (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//! XML is awful.
//!
//! # Examples
//! ```
//! use std::convert::Infallible;
//!
//! use xml_stinks::deserializer::buffered::Buffered as BufferedDeserializer;
//! use xml_stinks::deserializer::{Deserializer, IgnoreEnd};
//! use xml_stinks::tagged::TagStart;
//! use xml_stinks::DeserializeTagged;
//!
//! let mut deserializer =
//!     BufferedDeserializer::new("<foo>Baz<bar>DF</bar></foo>".as_bytes());
//!
//! let foo = deserializer.de_tag::<Foo>("foo", IgnoreEnd::No).unwrap();
//!
//! assert_eq!(&foo.name, "Baz");
//! assert_eq!(foo.bar.num, 223);
//!
//! struct Foo
//! {
//!     name: String,
//!     bar: Bar,
//! }
//!
//! impl DeserializeTagged for Foo
//! {
//!     type Error = Infallible;
//!
//!     fn deserialize<TDeserializer: Deserializer>(
//!         start: &TagStart,
//!         deserializer: &mut TDeserializer,
//!     ) -> Result<Self, Self::Error>
//!     {
//!         let name = deserializer.de_text().unwrap();
//!
//!         let bar = deserializer.de_tag::<Bar>("bar", IgnoreEnd::No).unwrap();
//!
//!         Ok(Self { name, bar })
//!     }
//! }
//!
//! struct Bar
//! {
//!     num: u32,
//! }
//!
//! impl DeserializeTagged for Bar
//! {
//!     type Error = Infallible;
//!
//!     fn deserialize<TDeserializer: Deserializer>(
//!         start: &TagStart,
//!         deserializer: &mut TDeserializer,
//!     ) -> Result<Self, Self::Error>
//!     {
//!         let num = u32::from_str_radix(&deserializer.de_text().unwrap(), 16).unwrap();
//!
//!         Ok(Self { num })
//!     }
//! }
//! ```
#![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 escape;
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`.
    ///
    /// # Errors
    /// Returns `Err` if a `Deserializer` error occurs.
    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)),
        )
    }
}