aboutsummaryrefslogtreecommitdiff
path: root/src/escape.rs
blob: c58a5ae0c2711e62f40ee7eb879ebc9db74c351b (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
//! XML character escape things.
use std::ops::Range;

use quick_xml::escape::EscapeError as QuickXMLEscapeError;

/// Escape/unescape error.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
#[allow(clippy::module_name_repetitions)]
pub enum EscapeError
{
    /// Null character entity not allowed.
    #[error(
        "Error while escaping character at range {0:?}: Null character entity not allowed"
    )]
    EntityWithNull(Range<usize>),

    /// Unrecognized escape symbol.
    #[error(
        "Error while escaping character at range {0:?}: Unrecognized escape symbol: {1:?}"
    )]
    UnrecognizedSymbol(Range<usize>, String),

    /// Missing ; after &.
    #[error("Error while escaping character at range {0:?}: Cannot find ';' after '&'")]
    UnterminatedEntity(Range<usize>),

    /// Hexadecimal value is too long too convert to UTF-8.
    #[error("Hexadecimal value is too long to convert to UTF-8")]
    TooLongHexadecimal,

    /// Invalid hexadecimal character.
    #[error("{0}' is not a valid hexadecimal character")]
    InvalidHexadecimal(char),

    /// Decimal value is too long to convert to UTF-8.
    #[error("Decimal value is too long to convert to UTF-8")]
    TooLongDecimal,

    /// Invalid decimal character.
    #[error("'{0}' is not a valid decimal character")]
    InvalidDecimal(char),

    /// Invalid codepoint.
    #[error("'{0}' is not a valid codepoint")]
    InvalidCodepoint(u32),
}

impl EscapeError
{
    pub(crate) fn from_quick_xml(err: QuickXMLEscapeError) -> Self
    {
        match err {
            QuickXMLEscapeError::EntityWithNull(range) => Self::EntityWithNull(range),
            QuickXMLEscapeError::UnrecognizedSymbol(range, symbol) => {
                Self::UnrecognizedSymbol(range, symbol)
            }
            QuickXMLEscapeError::UnterminatedEntity(range) => {
                Self::UnterminatedEntity(range)
            }
            QuickXMLEscapeError::TooLongHexadecimal => Self::TooLongHexadecimal,
            QuickXMLEscapeError::InvalidHexadecimal(character) => {
                Self::InvalidHexadecimal(character)
            }
            QuickXMLEscapeError::TooLongDecimal => Self::TooLongDecimal,
            QuickXMLEscapeError::InvalidDecimal(character) => {
                Self::InvalidDecimal(character)
            }
            QuickXMLEscapeError::InvalidCodepoint(codepoint) => {
                Self::InvalidCodepoint(codepoint)
            }
        }
    }
}