aboutsummaryrefslogtreecommitdiff
path: root/src/deserialization/mod.rs
blob: b86c2aff224a6d10b16926c0ba1a60e66bb69242 (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
use std::error::Error;
use std::ops::Deref;

use quick_xml::events::{BytesStart, Event};

pub mod buffer_deserializer;

pub trait Deserialize: Sized
{
    type Error: Error + Send + Sync + 'static;

    fn deserialize<TDeserializer: Deserializer>(
        start: &BytesStart,
        deserializer: &mut TDeserializer,
    ) -> Result<Self, Self::Error>;
}

#[cfg_attr(test, ridicule::automock)]
pub trait Deserializer
{
    fn de_tag<De: Deserialize>(
        &mut self,
        tag_name: &str,
        ignore_end: IgnoreEnd,
    ) -> Result<De, DeserializerError>;

    fn de_tag_with<Output, Err, DeserializeFn>(
        &mut self,
        tag_name: &str,
        ignore_end: IgnoreEnd,
        deserialize: DeserializeFn,
    ) -> Result<Output, DeserializerError>
    where
        Err: Error + Send + Sync + 'static,
        DeserializeFn: FnOnce(&BytesStart, &mut Self) -> Result<Output, Err>;

    fn de_tag_list<De: Deserialize>(
        &mut self,
        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>;

    fn skip_to_tag_end(&mut self, tag_name: &str) -> Result<(), DeserializerError>;
}

pub enum IgnoreEnd
{
    Yes,
    No,
}

/// Function pointer type passable to [`Deserializer::de_tag_with`].
pub type DeserializeWithFn<Output, Err, Deserializer> =
    fn(&BytesStart, &mut Deserializer) -> Result<Output, Err>;

#[derive(Debug, thiserror::Error)]
pub enum DeserializerError
{
    #[error("Failed to read")]
    ReadFailed(#[from] quick_xml::Error),

    #[error("Failed to deserialize {0}")]
    DeserializeFailed(&'static str, #[source] WrappedDeserializeError),

    #[error("Expected {expected_event_name} event. Found {found_event:?}")]
    UnexpectedEvent
    {
        expected_event_name: String,
        found_event: Event<'static>,
    },

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

#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct WrappedDeserializeError(Box<dyn Error + Send + Sync>);

impl WrappedDeserializeError
{
    fn new<Err: Error + Send + Sync + 'static>(err: Err) -> Self
    {
        Self(Box::new(err))
    }
}

impl Deref for WrappedDeserializeError
{
    type Target = dyn Error;

    fn deref(&self) -> &Self::Target
    {
        self.0.as_ref()
    }
}

pub trait ResultExt<Value>
{
    fn try_event(self) -> Result<Option<Value>, DeserializerError>;
}

impl<Value> ResultExt<Value> for Result<Value, DeserializerError>
{
    fn try_event(self) -> Result<Option<Value>, 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)),
        )
    }
}