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    /// Schema validation error.
44    #[error("Validation error: {0}")]
45    Validation(String),
46
47    /// Configuration error.
48    #[error("Configuration error: {0}")]
49    Config(String),
50
51    /// IO error.
52    #[error("IO error: {0}")]
53    Io(#[from] std::io::Error),
54}
55
56impl QailError {
57    /// Create a parse error at the given position.
58    pub fn parse(position: usize, message: impl Into<String>) -> Self {
59        Self::Parse {
60            position,
61            message: message.into(),
62        }
63    }
64
65    /// Create a missing symbol error.
66    pub fn missing(symbol: &'static str, description: &'static str) -> Self {
67        Self::MissingSymbol {
68            symbol,
69            description,
70        }
71    }
72}
73
74/// Result type alias for QAIL operations.
75pub 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}