htmls/interpreter/
error.rs1use std::error::Error;
2use std::fmt;
3
4#[derive(Debug, Clone)]
6pub enum InterpreterError {
7 HtmlParseError(String),
9
10 ParserError(String),
12
13 NodeSelectionError(String),
15
16 TextExtractionError(String),
18
19 AttributeExtractionError(String),
21
22 IndexOutOfBounds(usize, usize),
24
25 InvalidStep(i64),
27
28 InvalidRegex(String),
30
31 UnknownFunction(String),
33
34 MissingArgument(String),
36
37 InvalidArgument(String),
39
40 ExecutionError(String),
42
43 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
73impl From<regex::Error> for InterpreterError {
75 fn from(err: regex::Error) -> Self {
76 InterpreterError::InvalidRegex(format!("{}", err))
77 }
78}
79
80impl 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
90impl InterpreterError {
92 pub fn node_selection_error(message: impl Into<String>) -> Self {
94 InterpreterError::NodeSelectionError(message.into())
95 }
96
97 pub fn html_parse_error(message: impl Into<String>) -> Self {
99 InterpreterError::HtmlParseError(message.into())
100 }
101
102 pub fn text_extraction_error(message: impl Into<String>) -> Self {
104 InterpreterError::TextExtractionError(message.into())
105 }
106
107 pub fn attribute_extraction_error(message: impl Into<String>) -> Self {
109 InterpreterError::AttributeExtractionError(message.into())
110 }
111
112 pub fn execution_error(message: impl Into<String>) -> Self {
114 InterpreterError::ExecutionError(message.into())
115 }
116
117 pub fn unknown_function(name: impl Into<String>) -> Self {
119 InterpreterError::UnknownFunction(name.into())
120 }
121
122 pub fn missing_argument(message: impl Into<String>) -> Self {
124 InterpreterError::MissingArgument(message.into())
125 }
126
127 pub fn invalid_argument(message: impl Into<String>) -> Self {
129 InterpreterError::InvalidArgument(message.into())
130 }
131}