htmls/interpreter/
error.rs

1use std::error::Error;
2use std::fmt;
3
4/// Interpreter error
5#[derive(Debug, Clone)]
6pub enum InterpreterError {
7    /// HTML parsing error
8    HtmlParseError(String),
9
10    /// Syntax parsing error
11    ParserError(String),
12
13    /// Node selection error
14    NodeSelectionError(String),
15
16    /// Text extraction error
17    TextExtractionError(String),
18
19    /// Attribute extraction error
20    AttributeExtractionError(String),
21
22    /// Index out of bounds
23    IndexOutOfBounds(usize, usize),
24
25    /// Invalid step value
26    InvalidStep(i64),
27
28    /// Regular expression error
29    InvalidRegex(String),
30
31    /// Unknown function
32    UnknownFunction(String),
33
34    /// Missing argument
35    MissingArgument(String),
36
37    /// Invalid argument
38    InvalidArgument(String),
39
40    /// Execution error
41    ExecutionError(String),
42
43    /// Result limit exceeded
44    ResultLimitExceeded(usize),
45}
46
47impl fmt::Display for InterpreterError {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        match self {
50            InterpreterError::HtmlParseError(msg) => write!(f, "HTML parsing error: {}", msg),
51            InterpreterError::ParserError(msg) => write!(f, "Syntax parsing error: {}", msg),
52            InterpreterError::NodeSelectionError(msg) => write!(f, "Node selection error: {}", msg),
53            InterpreterError::TextExtractionError(msg) => write!(f, "Text extraction error: {}", msg),
54            InterpreterError::AttributeExtractionError(msg) => write!(f, "Attribute extraction error: {}", msg),
55            InterpreterError::IndexOutOfBounds(idx, len) => {
56                write!(f, "Index out of bounds: index {} is out of range 0-{}", idx, len - 1)
57            }
58            InterpreterError::InvalidStep(step) => write!(f, "Invalid step: step cannot be {}", step),
59            InterpreterError::InvalidRegex(msg) => write!(f, "Invalid regular expression: {}", msg),
60            InterpreterError::UnknownFunction(name) => write!(f, "Unknown function: {}", name),
61            InterpreterError::MissingArgument(msg) => write!(f, "Missing argument: {}", msg),
62            InterpreterError::InvalidArgument(msg) => write!(f, "Invalid argument: {}", msg),
63            InterpreterError::ExecutionError(msg) => write!(f, "Execution error: {}", msg),
64            InterpreterError::ResultLimitExceeded(limit) => {
65                write!(f, "Result limit exceeded: more than {} results", limit)
66            }
67        }
68    }
69}
70
71impl Error for InterpreterError {}
72
73// Implement conversion for regex errors
74impl From<regex::Error> for InterpreterError {
75    fn from(err: regex::Error) -> Self {
76        InterpreterError::InvalidRegex(format!("{}", err))
77    }
78}
79
80// Implement conversion for common I/O errors
81impl From<std::io::Error> for InterpreterError {
82    fn from(err: std::io::Error) -> Self {
83        InterpreterError::ExecutionError(format!("IO error: {}", err))
84    }
85}
86
87
88pub type InterpreterResult<T> = Result<T, InterpreterError>;
89
90/// Error helper methods
91impl InterpreterError {
92    /// Create a node selection error
93    pub fn node_selection_error(message: impl Into<String>) -> Self {
94        InterpreterError::NodeSelectionError(message.into())
95    }
96
97    /// Create an HTML parsing error
98    pub fn html_parse_error(message: impl Into<String>) -> Self {
99        InterpreterError::HtmlParseError(message.into())
100    }
101
102    /// Create a text extraction error
103    pub fn text_extraction_error(message: impl Into<String>) -> Self {
104        InterpreterError::TextExtractionError(message.into())
105    }
106
107    /// Create an attribute extraction error
108    pub fn attribute_extraction_error(message: impl Into<String>) -> Self {
109        InterpreterError::AttributeExtractionError(message.into())
110    }
111
112    /// Create an execution error
113    pub fn execution_error(message: impl Into<String>) -> Self {
114        InterpreterError::ExecutionError(message.into())
115    }
116
117    /// Create an unknown function error
118    pub fn unknown_function(name: impl Into<String>) -> Self {
119        InterpreterError::UnknownFunction(name.into())
120    }
121
122    /// Create a missing argument error
123    pub fn missing_argument(message: impl Into<String>) -> Self {
124        InterpreterError::MissingArgument(message.into())
125    }
126
127    /// Create an invalid argument error
128    pub fn invalid_argument(message: impl Into<String>) -> Self {
129        InterpreterError::InvalidArgument(message.into())
130    }
131}