xml_stinks/
escape.rs

1//! XML character escape things.
2use std::ops::Range;
3
4use quick_xml::escape::EscapeError as QuickXMLEscapeError;
5
6/// Escape/unescape error.
7#[derive(Debug, thiserror::Error)]
8#[non_exhaustive]
9#[allow(clippy::module_name_repetitions)]
10pub enum EscapeError
11{
12    /// Null character entity not allowed.
13    #[error(
14        "Error while escaping character at range {0:?}: Null character entity not allowed"
15    )]
16    EntityWithNull(Range<usize>),
17
18    /// Unrecognized escape symbol.
19    #[error(
20        "Error while escaping character at range {0:?}: Unrecognized escape symbol: {1:?}"
21    )]
22    UnrecognizedSymbol(Range<usize>, String),
23
24    /// Missing ; after &.
25    #[error("Error while escaping character at range {0:?}: Cannot find ';' after '&'")]
26    UnterminatedEntity(Range<usize>),
27
28    /// Hexadecimal value is too long too convert to UTF-8.
29    #[error("Hexadecimal value is too long to convert to UTF-8")]
30    TooLongHexadecimal,
31
32    /// Invalid hexadecimal character.
33    #[error("{0}' is not a valid hexadecimal character")]
34    InvalidHexadecimal(char),
35
36    /// Decimal value is too long to convert to UTF-8.
37    #[error("Decimal value is too long to convert to UTF-8")]
38    TooLongDecimal,
39
40    /// Invalid decimal character.
41    #[error("'{0}' is not a valid decimal character")]
42    InvalidDecimal(char),
43
44    /// Invalid codepoint.
45    #[error("'{0}' is not a valid codepoint")]
46    InvalidCodepoint(u32),
47}
48
49impl EscapeError
50{
51    pub(crate) fn from_quick_xml(err: QuickXMLEscapeError) -> Self
52    {
53        match err {
54            QuickXMLEscapeError::EntityWithNull(range) => Self::EntityWithNull(range),
55            QuickXMLEscapeError::UnrecognizedSymbol(range, symbol) => {
56                Self::UnrecognizedSymbol(range, symbol)
57            }
58            QuickXMLEscapeError::UnterminatedEntity(range) => {
59                Self::UnterminatedEntity(range)
60            }
61            QuickXMLEscapeError::TooLongHexadecimal => Self::TooLongHexadecimal,
62            QuickXMLEscapeError::InvalidHexadecimal(character) => {
63                Self::InvalidHexadecimal(character)
64            }
65            QuickXMLEscapeError::TooLongDecimal => Self::TooLongDecimal,
66            QuickXMLEscapeError::InvalidDecimal(character) => {
67                Self::InvalidDecimal(character)
68            }
69            QuickXMLEscapeError::InvalidCodepoint(codepoint) => {
70                Self::InvalidCodepoint(codepoint)
71            }
72        }
73    }
74}