#[derive(Debug, thiserror::Error)]
pub enum ParseError {
#[error("unexpected byte 0x{byte:02X} at offset {offset}")]
UnexpectedByte {
byte: u8,
offset: usize,
},
#[error("unexpected end of input at offset {offset}, state: {state}")]
UnexpectedEof {
offset: usize,
state: &'static str,
},
#[error("nesting depth limit exceeded ({depth} > {limit})")]
NestingTooDeep {
depth: u32,
limit: u32,
},
}
#[derive(Debug, thiserror::Error)]
pub enum SelectorError {
#[error("invalid selector: {reason}")]
Invalid {
reason: String,
},
}
#[derive(Debug, thiserror::Error)]
pub enum XPathError {
#[error("invalid xpath: {reason}")]
Invalid {
reason: String,
},
}
#[derive(Debug, thiserror::Error)]
pub enum EncodingError {
#[error("decode error: malformed {encoding} input at byte offset {offset}")]
MalformedInput {
encoding: &'static str,
offset: usize,
},
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_error_display() {
let err = ParseError::UnexpectedByte {
byte: 0xFF,
offset: 42,
};
assert_eq!(err.to_string(), "unexpected byte 0xFF at offset 42");
}
#[test]
fn selector_error_display() {
let err = SelectorError::Invalid {
reason: "unclosed bracket".to_string(),
};
assert_eq!(err.to_string(), "invalid selector: unclosed bracket");
}
#[test]
fn xpath_error_display() {
let err = XPathError::Invalid {
reason: "unexpected token".to_string(),
};
assert_eq!(err.to_string(), "invalid xpath: unexpected token");
}
#[test]
fn encoding_error_display() {
let err = EncodingError::MalformedInput {
encoding: "windows-1252",
offset: 42,
};
assert_eq!(
err.to_string(),
"decode error: malformed windows-1252 input at byte offset 42"
);
}
#[test]
fn nesting_error_display() {
let err = ParseError::NestingTooDeep {
depth: 513,
limit: 512,
};
assert_eq!(err.to_string(), "nesting depth limit exceeded (513 > 512)");
}
}