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