From 1a405580b30f0b877ca48cd95ca51da9e5723667 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 14 May 2023 15:46:53 +0200 Subject: feat: add Attribute functions --- src/escape.rs | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/escape.rs (limited to 'src/escape.rs') diff --git a/src/escape.rs b/src/escape.rs new file mode 100644 index 0000000..c58a5ae --- /dev/null +++ b/src/escape.rs @@ -0,0 +1,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), + + /// Unrecognized escape symbol. + #[error( + "Error while escaping character at range {0:?}: Unrecognized escape symbol: {1:?}" + )] + UnrecognizedSymbol(Range, String), + + /// Missing ; after &. + #[error("Error while escaping character at range {0:?}: Cannot find ';' after '&'")] + UnterminatedEntity(Range), + + /// 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) + } + } + } +} -- cgit v1.2.3-18-g5258