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("Configuration error: {0}")]
45 Config(String),
46
47 #[error("IO error: {0}")]
49 Io(#[from] std::io::Error),
50}
51
52impl QailError {
53 pub fn parse(position: usize, message: impl Into<String>) -> Self {
55 Self::Parse {
56 position,
57 message: message.into(),
58 }
59 }
60
61 pub fn missing(symbol: &'static str, description: &'static str) -> Self {
63 Self::MissingSymbol {
64 symbol,
65 description,
66 }
67 }
68}
69
70pub type QailResult<T> = Result<T, QailError>;
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76
77 #[test]
78 fn test_error_display() {
79 let err = QailError::parse(5, "unexpected character");
80 assert_eq!(
81 err.to_string(),
82 "Parse error at position 5: unexpected character"
83 );
84 }
85}