use std::ops::Range;
use quick_xml::escape::EscapeError as QuickXMLEscapeError;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
#[allow(clippy::module_name_repetitions)]
pub enum EscapeError
{
#[error(
"Error while escaping character at range {0:?}: Null character entity not allowed"
)]
EntityWithNull(Range<usize>),
#[error(
"Error while escaping character at range {0:?}: Unrecognized escape symbol: {1:?}"
)]
UnrecognizedSymbol(Range<usize>, String),
#[error("Error while escaping character at range {0:?}: Cannot find ';' after '&'")]
UnterminatedEntity(Range<usize>),
#[error("Hexadecimal value is too long to convert to UTF-8")]
TooLongHexadecimal,
#[error("{0}' is not a valid hexadecimal character")]
InvalidHexadecimal(char),
#[error("Decimal value is too long to convert to UTF-8")]
TooLongDecimal,
#[error("'{0}' is not a valid decimal character")]
InvalidDecimal(char),
#[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)
}
}
}
}