scrape_core/parser/
error.rs

1//! Parser error types.
2
3use thiserror::Error;
4
5/// Result type for parser operations.
6pub type ParseResult<T> = Result<T, ParseError>;
7
8/// Errors that can occur during HTML parsing.
9#[derive(Debug, Error)]
10pub enum ParseError {
11    /// Document exceeds maximum nesting depth.
12    #[error("maximum nesting depth of {max_depth} exceeded")]
13    MaxDepthExceeded {
14        /// Configured maximum depth.
15        max_depth: usize,
16    },
17
18    /// Input is empty or contains only whitespace.
19    #[error("empty or whitespace-only input")]
20    EmptyInput,
21
22    /// Encoding error in input.
23    #[error("encoding error: {message}")]
24    EncodingError {
25        /// Description of the encoding problem.
26        message: String,
27    },
28
29    /// Internal parser error.
30    #[error("internal parser error: {0}")]
31    InternalError(String),
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37
38    #[test]
39    fn test_max_depth_exceeded_display() {
40        let err = ParseError::MaxDepthExceeded { max_depth: 512 };
41        assert_eq!(err.to_string(), "maximum nesting depth of 512 exceeded");
42    }
43
44    #[test]
45    fn test_empty_input_display() {
46        let err = ParseError::EmptyInput;
47        assert_eq!(err.to_string(), "empty or whitespace-only input");
48    }
49
50    #[test]
51    fn test_encoding_error_display() {
52        let err = ParseError::EncodingError { message: "invalid UTF-8 sequence".into() };
53        assert_eq!(err.to_string(), "encoding error: invalid UTF-8 sequence");
54    }
55
56    #[test]
57    fn test_internal_error_display() {
58        let err = ParseError::InternalError("unexpected state".into());
59        assert_eq!(err.to_string(), "internal parser error: unexpected state");
60    }
61}