Skip to main content

qail_core/
error.rs

1//! Error types for QAIL.
2
3use thiserror::Error;
4
5/// Error types 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 {
11        /// Byte offset of the error.
12        position: usize,
13        /// Human-readable error message.
14        message: String,
15    },
16
17    /// Invalid action (must be get, set, del, or add).
18    #[error("Invalid action: '{0}'. Expected: get, set, del, or add")]
19    InvalidAction(String),
20
21    /// Required syntax symbol is missing.
22    #[error("Missing required symbol: {symbol} ({description})")]
23    MissingSymbol {
24        /// The missing symbol.
25        symbol: &'static str,
26        /// Description of the expected symbol.
27        description: &'static str,
28    },
29
30    /// Invalid operator in expression.
31    #[error("Invalid operator: '{0}'")]
32    InvalidOperator(String),
33
34    /// Invalid value in expression.
35    #[error("Invalid value: {0}")]
36    InvalidValue(String),
37
38    /// Database-layer error.
39    #[error("Database error: {0}")]
40    Database(String),
41
42    /// Connection-layer error.
43    #[error("Connection error: {0}")]
44    Connection(String),
45
46    /// Execution-layer error.
47    #[error("Execution error: {0}")]
48    Execution(String),
49
50    /// Validation error.
51    #[error("Validation error: {0}")]
52    Validation(String),
53
54    /// Configuration error.
55    #[error("Configuration error: {0}")]
56    Config(String),
57
58    /// I/O error.
59    #[error("IO error: {0}")]
60    Io(#[from] std::io::Error),
61}
62
63impl QailError {
64    /// Create a parse error at the given position.
65    pub fn parse(position: usize, message: impl Into<String>) -> Self {
66        Self::Parse {
67            position,
68            message: message.into(),
69        }
70    }
71
72    /// Create a missing symbol error.
73    pub fn missing(symbol: &'static str, description: &'static str) -> Self {
74        Self::MissingSymbol {
75            symbol,
76            description,
77        }
78    }
79}
80
81/// Result type alias for QAIL operations.
82pub 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}