1use thiserror::Error;
4
5#[derive(Debug, Error)]
6pub enum QailError {
7 #[error("Parse error at position {position}: {message}")]
9 Parse { position: usize, message: String },
10
11 #[error("Invalid action: '{0}'. Expected: get, set, del, or add")]
13 InvalidAction(String),
14
15 #[error("Missing required symbol: {symbol} ({description})")]
16 MissingSymbol {
17 symbol: &'static str,
18 description: &'static str,
19 },
20
21 #[error("Invalid operator: '{0}'")]
22 InvalidOperator(String),
23
24 #[error("Invalid value: {0}")]
25 InvalidValue(String),
26
27 #[error("Database error: {0}")]
28 Database(String),
29
30 #[error("Connection error: {0}")]
31 Connection(String),
32
33 #[error("Execution error: {0}")]
34 Execution(String),
35
36 #[error("Validation error: {0}")]
37 Validation(String),
38
39 #[error("Configuration error: {0}")]
40 Config(String),
41
42 #[error("IO error: {0}")]
43 Io(#[from] std::io::Error),
44}
45
46impl QailError {
47 pub fn parse(position: usize, message: impl Into<String>) -> Self {
49 Self::Parse {
50 position,
51 message: message.into(),
52 }
53 }
54
55 pub fn missing(symbol: &'static str, description: &'static str) -> Self {
57 Self::MissingSymbol {
58 symbol,
59 description,
60 }
61 }
62}
63
64pub type QailResult<T> = Result<T, QailError>;
66
67#[cfg(test)]
68mod tests {
69 use super::*;
70
71 #[test]
72 fn test_error_display() {
73 let err = QailError::parse(5, "unexpected character");
74 assert_eq!(
75 err.to_string(),
76 "Parse error at position 5: unexpected character"
77 );
78 }
79}