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