koron_query_parser/
error.rs

1use thiserror::Error;
2
3/// Koron errors.
4#[allow(clippy::module_name_repetitions)]
5#[derive(Clone, Debug, Error, PartialEq, Eq)]
6pub enum ParseError {
7    #[error("malformed query: {message}")]
8    MalformedQuery { message: String },
9    #[error("statement not supported: {message}")]
10    Unsupported { message: String },
11    #[error("internal: {message}")]
12    Internal { message: String },
13}
14
15macro_rules! impl_malformed_from {
16    ($err:ty) => {
17        impl From<$err> for ParseError {
18            fn from(e: $err) -> Self {
19                Self::MalformedQuery {
20                    message: e.to_string(),
21                }
22            }
23        }
24    };
25}
26
27impl_malformed_from!(sqlparser::parser::ParserError);
28
29impl From<String> for ParseError {
30    fn from(e: String) -> Self {
31        Self::Internal { message: e }
32    }
33}
34
35/// Constructs a `ParseError::Unsupported{message: $msg}`.
36#[macro_export]
37macro_rules! unsupported {
38    ($msg:literal) => {{
39        ParseError::Unsupported { message: $msg }
40    }};
41    ($msg:expr) => {{
42        ParseError::Unsupported { message: $msg }
43    }};
44}
45
46/// Constructs a `ParseError::Internal{message: $msg}`.
47#[macro_export]
48macro_rules! internal {
49    ($msg:literal) => {{
50        ParseError::Internal { message: $msg }
51    }};
52    ($msg:expr) => {{
53        ParseError::Internal { message: $msg }
54    }};
55}
56
57/// Constructs a `ParseError::MalformedQuery{message: $msg}`.
58#[macro_export]
59macro_rules! malformed_query {
60    ($msg:literal) => {{
61        ParseError::MalformedQuery { message: $msg }
62    }};
63    ($msg:expr) => {{
64        ParseError::MalformedQuery { message: $msg }
65    }};
66}
67
68#[cfg(test)]
69mod tests {
70    use super::ParseError;
71
72    #[test]
73    fn to_string() {
74        let mut error = internal!("test.".to_string());
75        assert_eq!(error.to_string(), "internal: test.".to_string());
76
77        error = malformed_query!("test.".to_string());
78        assert_eq!(error.to_string(), "malformed query: test.".to_string());
79
80        error = unsupported!("test.".to_string());
81        assert_eq!(
82            error.to_string(),
83            "statement not supported: test.".to_string()
84        );
85    }
86}