aboutsummaryrefslogtreecommitdiff
path: root/src/deserializer/mod.rs
blob: bd0c0e482bd1a43e32fbd41f76fe6e69404e8cd8 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//! Deserializer.
use std::convert::Infallible;

use crate::tagged::TagStart;
use crate::DeserializeTagged;

pub mod buffered;

/// XML deserializer.
pub trait Deserializer
{
    /// Deserializes a tagged element.
    ///
    /// # Errors
    /// Returns `Err` if deserialization fails.
    fn de_tag<De: DeserializeTagged>(
        &mut self,
        tag_name: &str,
        ignore_end: IgnoreEnd,
    ) -> Result<De, Error<De::Error>>;

    /// Deserializes a tagged element using the given function.
    ///
    /// # Errors
    /// Returns `Err` if deserialization fails.
    fn de_tag_with<Output, Err, DeserializeFn>(
        &mut self,
        tag_name: &str,
        ignore_end: IgnoreEnd,
        deserialize: DeserializeFn,
    ) -> Result<Output, Error<Err>>
    where
        Err: std::error::Error + Send + Sync + 'static,
        DeserializeFn: FnOnce(&TagStart, &mut Self) -> Result<Output, Err>;

    /// Deserializes a list of tagged elements.
    ///
    /// # Errors
    /// Returns `Err` if deserialization fails.
    fn de_tag_list<De: DeserializeTagged>(
        &mut self,
        tag_name: Option<&str>,
    ) -> Result<Vec<De>, Error<De::Error>>;

    /// Deserializes a text element.
    ///
    /// # Errors
    /// Returns `Err` if deserialization fails.
    fn de_text(&mut self) -> Result<String, Error<Infallible>>;

    /// Skips past all elements until a tagged element with the name `tag_name` is
    /// reached.
    ///
    /// # Errors
    /// Returns `Err` if unsuccessful.
    fn skip_to_tag_start(&mut self, tag_name: &str) -> Result<(), Error<Infallible>>;

    /// Skips past all elements until the end of a tagged element with the name `tag_name`
    /// is reached.
    ///
    /// # Errors
    /// Returns `Err` if unsuccessful.
    fn skip_to_tag_end(&mut self, tag_name: &str) -> Result<(), Error<Infallible>>;
}

/// Whether or not to skip the end tag of a tagged element.
///
/// **Should be `No`**.
#[derive(Debug, Default)]
pub enum IgnoreEnd
{
    /// Skip the end tag.
    ///
    /// **Will cause problems in most cases and should be used very carefully**.
    Yes,

    /// Don't skip the end tag.
    #[default]
    No,
}

/// [`Deserializer`] error.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error<DeError>
{
    /// A XML error occurred.
    #[error("A XML error occurred")]
    XMLError(#[source] XMLError),

    /// Failed to deserialize.
    #[error("Failed to deserialize")]
    DeserializeFailed(#[from] DeError),

    /// Unexpected event.
    #[error("Expected {expected_event_name} event. Found {found_event}")]
    UnexpectedEvent
    {
        /// The name of the expected event.
        expected_event_name: String,

        /// The found event.
        found_event: String,
    },

    /// Unexpected end of file.
    #[error("Unexpected end of file")]
    UnexpectedEndOfFile,
}

impl<DeError> Error<DeError>
{
    /// Returns `Self` with `DeError` as [`Infallible`].
    ///
    /// # Panics
    /// Will panic if `Self` is the `DeserializeFailed` variant.
    pub fn into_never_de_err(self) -> Error<Infallible>
    {
        match self {
            Self::XMLError(xml_err) => Error::XMLError(xml_err),
            Self::DeserializeFailed(_) => {
                panic!("is a deserialization error");
            }
            Self::UnexpectedEvent {
                expected_event_name,
                found_event,
            } => Error::UnexpectedEvent {
                expected_event_name,
                found_event,
            },
            Self::UnexpectedEndOfFile => Error::UnexpectedEndOfFile,
        }
    }
}

impl Error<Infallible>
{
    fn into_with_de_error<DeError>(self) -> Error<DeError>
    {
        match self {
            Self::XMLError(xml_err) => Error::XMLError(xml_err),
            Self::DeserializeFailed(_) => {
                unreachable!();
            }
            Self::UnexpectedEvent {
                expected_event_name,
                found_event,
            } => Error::UnexpectedEvent {
                expected_event_name,
                found_event,
            },
            Self::UnexpectedEndOfFile => Error::UnexpectedEndOfFile,
        }
    }
}

impl From<Error<Error<Infallible>>> for Error<Infallible>
{
    fn from(err: Error<Error<Infallible>>) -> Self
    {
        match err {
            Error::XMLError(xml_err) => Self::XMLError(xml_err),
            Error::DeserializeFailed(de_err) => de_err,
            Error::UnexpectedEvent {
                expected_event_name,
                found_event,
            } => Self::UnexpectedEvent {
                expected_event_name,
                found_event,
            },
            Error::UnexpectedEndOfFile => Self::UnexpectedEndOfFile,
        }
    }
}

/// XML error.
#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct XMLError(#[from] quick_xml::Error);