qail_core/
error.rs

1//! Error types for QAIL.
2
3use thiserror::Error;
4
5/// The main error type for QAIL operations.
6#[derive(Debug, Error)]
7pub enum QailError {
8    /// Failed to parse the QAIL query string.
9    #[error("Parse error at position {position}: {message}")]
10    Parse { position: usize, message: String },
11
12    /// Invalid action (must be get, set, del, or add).
13    #[error("Invalid action: '{0}'. Expected: get, set, del, or add")]
14    InvalidAction(String),
15
16    /// Missing required symbol.
17    #[error("Missing required symbol: {symbol} ({description})")]
18    MissingSymbol {
19        symbol: &'static str,
20        description: &'static str,
21    },
22
23    /// Invalid operator.
24    #[error("Invalid operator: '{0}'")]
25    InvalidOperator(String),
26
27    /// Invalid value.
28    #[error("Invalid value: {0}")]
29    InvalidValue(String),
30
31    /// Database error.
32    #[error("Database error: {0}")]
33    Database(String),
34
35    /// Connection error.
36    #[error("Connection error: {0}")]
37    Connection(String),
38
39    /// Query execution error.
40    #[error("Execution error: {0}")]
41    Execution(String),
42
43    /// Configuration error.
44    #[error("Configuration error: {0}")]
45    Config(String),
46
47    /// IO error.
48    #[error("IO error: {0}")]
49    Io(#[from] std::io::Error),
50}
51
52impl QailError {
53    /// Create a parse error at the given position.
54    pub fn parse(position: usize, message: impl Into<String>) -> Self {
55        Self::Parse {
56            position,
57            message: message.into(),
58        }
59    }
60
61    /// Create a missing symbol error.
62    pub fn missing(symbol: &'static str, description: &'static str) -> Self {
63        Self::MissingSymbol {
64            symbol,
65            description,
66        }
67    }
68}
69
70/// Result type alias for QAIL operations.
71pub 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}