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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use std::fmt;

use xml::common::TextPosition;

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Error {
    Schema { pos: TextPosition, kind: ErrorKind },
    Xml(xml::reader::Error),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use Error::*;
        match self {
            Schema { pos, kind } => write!(f, "{pos} {kind}"),
            Xml(e) => e.fmt(f),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        use Error::*;
        use ErrorKind::*;
        match self {
            Schema { kind, .. } => match kind {
                InvalidDate(e) => Some(e),
                InvalidFloatAttributeValue(_, e) => Some(e),
                InvalidFloatElementValue(_, e) => Some(e),
                InvalidIntegerAttributeValue(_, e) => Some(e),
                InvalidIntegerElementValue(_, e) => Some(e),
                InvalidLanguage(e) => Some(e),
                MissingAttribute(_) => None,
                MissingContent => None,
                MissingElement(_) => None,
                MissingSectionContent => None,
                UnbalancedElement(_) => None,
                UnrecognizedAttributeValue(_, _) => None,
                UnrecognizedElement(_) => None,
                UnrecognizedElementValue(_, _) => None,
                UnrecognizedRootElement => None,
            },
            Xml(e) => Some(e),
        }
    }
}

impl From<xml::reader::Error> for Error {
    fn from(value: xml::reader::Error) -> Self {
        Error::Xml(value)
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ErrorKind {
    InvalidDate(chrono::ParseError),
    InvalidFloatAttributeValue(String, std::num::ParseFloatError),
    InvalidFloatElementValue(String, std::num::ParseFloatError),
    InvalidIntegerAttributeValue(String, std::num::ParseIntError),
    InvalidIntegerElementValue(String, std::num::ParseIntError),
    InvalidLanguage(language_tags::ParseError),
    MissingAttribute(String),
    MissingContent,
    MissingElement(String),
    MissingSectionContent,
    UnbalancedElement(String),
    UnrecognizedAttributeValue(String, String),
    UnrecognizedElement(String),
    UnrecognizedElementValue(String, String),
    UnrecognizedRootElement,
}

impl fmt::Display for ErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use ErrorKind::*;
        match self {
            InvalidDate(e) => write!(f, "invalid date value: {e}"),
            InvalidFloatAttributeValue(name, e) => write!(f, "invalid float attribute={name} value: {e}"),
            InvalidFloatElementValue(name, e) => write!(f, "invalid float element={name} content: {e}"),
            InvalidIntegerAttributeValue(name, e) => write!(f, "invalid integer attribute={name} value: {e}"),
            InvalidIntegerElementValue(name, e) => write!(f, "invalid integer element={name} content: {e}"),
            InvalidLanguage(e) => write!(f, "invalid language value: {e}"),
            MissingAttribute(name) => write!(f, "expected attribute={name}"),
            MissingContent => write!(f, "expected text content"),
            MissingElement(name) => write!(f, "expected element={name}"),
            MissingSectionContent => write!(f, "required section content was omitted (either child sections or section parts) while specifying optional section elements"),
            UnbalancedElement(name) => write!(f, "unbalanced element={name}"),
            UnrecognizedAttributeValue(name, value) => write!(f, "unrecognized attribute={name} value={value}"),
            UnrecognizedElement(name) => write!(f, "unrecognized element={name}"),
            UnrecognizedElementValue(name, value) => write!(f, "unrecognized element={name} value={value}"),
            UnrecognizedRootElement => write!(f, "expected to process end of the document after fiction book parsing, received unrecognized element instead"),
        }
    }
}

impl Error {
    pub(crate) fn invalid_date(error: chrono::ParseError, pos: TextPosition) -> Error {
        Error::Schema {
            pos,
            kind: ErrorKind::InvalidDate(error),
        }
    }

    pub(crate) fn invalid_float_attribute_value(
        name: &str,
        error: std::num::ParseFloatError,
        pos: TextPosition,
    ) -> Error {
        Error::Schema {
            pos,
            kind: ErrorKind::InvalidFloatAttributeValue(name.into(), error),
        }
    }

    pub(crate) fn invalid_integer_element_value(
        name: &str,
        error: std::num::ParseIntError,
        pos: TextPosition,
    ) -> Error {
        Error::Schema {
            pos,
            kind: ErrorKind::InvalidIntegerElementValue(name.into(), error),
        }
    }

    pub(crate) fn invalid_integer_attribute_value(
        name: &str,
        error: std::num::ParseIntError,
        pos: TextPosition,
    ) -> Error {
        Error::Schema {
            pos,
            kind: ErrorKind::InvalidIntegerAttributeValue(name.into(), error),
        }
    }

    pub(crate) fn invalid_float_element_value(
        name: &str,
        error: std::num::ParseFloatError,
        pos: TextPosition,
    ) -> Error {
        Error::Schema {
            pos,
            kind: ErrorKind::InvalidFloatElementValue(name.into(), error),
        }
    }

    pub(crate) fn invalid_language(error: language_tags::ParseError, pos: TextPosition) -> Error {
        Error::Schema {
            pos,
            kind: ErrorKind::InvalidLanguage(error),
        }
    }

    pub(crate) fn missing_attribute(name: &str, pos: TextPosition) -> Error {
        Error::Schema {
            pos,
            kind: ErrorKind::MissingAttribute(name.into()),
        }
    }

    pub(crate) fn missing_content(pos: TextPosition) -> Error {
        Error::Schema {
            pos,
            kind: ErrorKind::MissingContent,
        }
    }

    pub(crate) fn missing_element(name: &str, pos: TextPosition) -> Error {
        Error::Schema {
            pos,
            kind: ErrorKind::MissingElement(name.into()),
        }
    }

    pub(crate) fn missing_section_content(pos: TextPosition) -> Error {
        Error::Schema {
            pos,
            kind: ErrorKind::MissingSectionContent,
        }
    }

    pub(crate) fn unbalanced_element(name: &str, pos: TextPosition) -> Error {
        Error::Schema {
            pos,
            kind: ErrorKind::UnbalancedElement(name.into()),
        }
    }

    pub(crate) fn unrecognized_attribute_value(
        name: &str,
        value: String,
        pos: TextPosition,
    ) -> Error {
        Error::Schema {
            pos,
            kind: ErrorKind::UnrecognizedAttributeValue(name.into(), value),
        }
    }

    pub(crate) fn unrecognized_element(name: &str, pos: TextPosition) -> Error {
        Error::Schema {
            pos,
            kind: ErrorKind::UnrecognizedElement(name.into()),
        }
    }

    pub(crate) fn unrecognized_element_value(
        name: &str,
        value: String,
        pos: TextPosition,
    ) -> Error {
        Error::Schema {
            pos,
            kind: ErrorKind::UnrecognizedElementValue(name.into(), value),
        }
    }

    pub(crate) fn unrecognized_root_element(pos: TextPosition) -> Error {
        Error::Schema {
            pos,
            kind: ErrorKind::UnrecognizedRootElement,
        }
    }
}