toasty_core/error/
invalid_statement.rs

1use super::Error;
2
3/// Error when a statement is invalid.
4///
5/// This occurs when:
6/// - A statement references a non-existent field
7/// - A statement contains invalid operations for a given field type
8/// - A statement has incorrect structure or arguments
9///
10/// These errors are caught during statement lowering/execution, at runtime.
11/// This is distinct from schema validation errors which occur at build/migration time.
12#[derive(Debug)]
13pub(super) struct InvalidStatement {
14    pub(super) message: Box<str>,
15}
16
17impl std::error::Error for InvalidStatement {}
18
19impl core::fmt::Display for InvalidStatement {
20    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
21        write!(f, "invalid statement: {}", self.message)
22    }
23}
24
25impl Error {
26    /// Creates an invalid statement error.
27    ///
28    /// This is used when a statement is malformed or references invalid schema elements.
29    /// These errors occur during statement lowering/execution at runtime.
30    ///
31    /// # Examples
32    ///
33    /// ```
34    /// use toasty_core::Error;
35    ///
36    /// let err = Error::invalid_statement("field `foo` does not exist");
37    /// assert!(err.is_invalid_statement());
38    /// assert_eq!(err.to_string(), "invalid statement: field `foo` does not exist");
39    /// ```
40    pub fn invalid_statement(message: impl Into<String>) -> Error {
41        Error::from(super::ErrorKind::InvalidStatement(InvalidStatement {
42            message: message.into().into(),
43        }))
44    }
45
46    /// Returns `true` if this error is an invalid statement error.
47    pub fn is_invalid_statement(&self) -> bool {
48        matches!(self.kind(), super::ErrorKind::InvalidStatement(_))
49    }
50}