Skip to main content

kevy_sql/
declaration_error.rs

1//! Why a `TABLE.DECLARE` argv could not be read back or rendered as SQL.
2
3use std::fmt;
4
5/// Why [`crate::table_ddl`] refused a declaration: it does not read as a
6/// `TABLE.DECLARE`, or it names something SQL text cannot spell.
7///
8/// ```
9/// let decl: Vec<String> = ["TABLE.LIST"].iter().map(|w| w.to_string()).collect();
10/// let e = kevy_sql::table_ddl(&decl).unwrap_err();
11/// assert!(matches!(e, kevy_sql::DeclarationError::NotADeclaration(_)));
12/// assert_eq!(e.to_string(), "not a TABLE.DECLARE declaration: TABLE.LIST");
13/// ```
14#[derive(Debug, Clone, PartialEq, Eq, Hash)]
15#[non_exhaustive]
16pub enum DeclarationError {
17    /// The argv does not open with `TABLE.DECLARE name PREFIX p PK col`;
18    /// carries the argv joined by spaces.
19    ///
20    /// ```
21    /// let decl: Vec<String> = ["TABLE.LIST"].iter().map(|w| w.to_string()).collect();
22    /// let e = kevy_sql::table_ddl(&decl).unwrap_err();
23    /// assert_eq!(e, kevy_sql::DeclarationError::NotADeclaration("TABLE.LIST".into()));
24    /// assert_eq!(e.to_string(), "not a TABLE.DECLARE declaration: TABLE.LIST");
25    /// ```
26    NotADeclaration(String),
27    /// A clause that ends before its arguments do; carries its keyword.
28    ///
29    /// ```
30    /// let decl: Vec<String> =
31    ///     "TABLE.DECLARE t PREFIX t: PK id COLUMN id i64 COLUMN n".split(' ').map(String::from).collect();
32    /// let e = kevy_sql::table_ddl(&decl).unwrap_err();
33    /// assert_eq!(e, kevy_sql::DeclarationError::CutShort("COLUMN".into()));
34    /// assert_eq!(e.to_string(), "COLUMN is cut short");
35    /// ```
36    CutShort(String),
37    /// A column type other than i64, f64 or str; carries it lowercased.
38    ///
39    /// ```
40    /// let decl: Vec<String> =
41    ///     "TABLE.DECLARE t PREFIX t: PK id COLUMN id BLOB".split(' ').map(String::from).collect();
42    /// let e = kevy_sql::table_ddl(&decl).unwrap_err();
43    /// assert_eq!(e, kevy_sql::DeclarationError::ColumnType("blob".into()));
44    /// assert_eq!(e.to_string(), "column type 'blob' is not i64|f64|str");
45    /// ```
46    ColumnType(String),
47    /// A word where a clause keyword belongs.
48    ///
49    /// ```
50    /// let decl: Vec<String> =
51    ///     "TABLE.DECLARE t PREFIX t: PK id COLUMN id i64 FROB".split(' ').map(String::from).collect();
52    /// let e = kevy_sql::table_ddl(&decl).unwrap_err();
53    /// assert_eq!(e, kevy_sql::DeclarationError::UnknownClause("FROB".into()));
54    /// assert_eq!(e.to_string(), "unknown clause 'FROB'");
55    /// ```
56    UnknownClause(String),
57    /// An `ORDERPATH` without `<name> ON <col>`.
58    ///
59    /// ```
60    /// let decl: Vec<String> =
61    ///     "TABLE.DECLARE t PREFIX t: PK id COLUMN id i64 ORDERPATH p AT id".split(' ').map(String::from).collect();
62    /// let e = kevy_sql::table_ddl(&decl).unwrap_err();
63    /// assert_eq!(e, kevy_sql::DeclarationError::OrderpathShape);
64    /// assert_eq!(e.to_string(), "ORDERPATH needs <name> ON <col>");
65    /// ```
66    OrderpathShape,
67    /// A name containing `"`, which this SQL dialect cannot quote.
68    ///
69    /// ```
70    /// let decl: Vec<String> =
71    ///     "TABLE.DECLARE t\"x PREFIX t: PK id COLUMN id i64".split(' ').map(String::from).collect();
72    /// let e = kevy_sql::table_ddl(&decl).unwrap_err();
73    /// assert_eq!(e, kevy_sql::DeclarationError::Unspellable("t\"x".into()));
74    /// assert_eq!(e.to_string(), "the name 't\"x' contains '\"', which has no SQL spelling here");
75    /// ```
76    Unspellable(String),
77}
78
79impl fmt::Display for DeclarationError {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        match self {
82            Self::NotADeclaration(argv) => write!(f, "not a TABLE.DECLARE declaration: {argv}"),
83            Self::CutShort(word) => write!(f, "{word} is cut short"),
84            Self::ColumnType(ty) => write!(f, "column type '{ty}' is not i64|f64|str"),
85            Self::UnknownClause(word) => write!(f, "unknown clause '{word}'"),
86            Self::OrderpathShape => f.write_str("ORDERPATH needs <name> ON <col>"),
87            Self::Unspellable(name) => {
88                write!(f, "the name '{name}' contains '\"', which has no SQL spelling here")
89            }
90        }
91    }
92}
93
94impl std::error::Error for DeclarationError {}