Skip to main content

postgrest_parser/error/
mod.rs

1pub mod parse;
2pub mod sql;
3
4pub use parse::ParseError;
5pub use sql::SqlError;
6
7#[derive(Debug, PartialEq, Clone)]
8pub enum Error {
9    Parse(ParseError),
10    Sql(SqlError),
11}
12
13impl std::fmt::Display for Error {
14    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        match self {
16            Error::Parse(e) => write!(f, "parse error: {}", e),
17            Error::Sql(e) => write!(f, "SQL error: {}", e),
18        }
19    }
20}
21
22impl std::error::Error for Error {
23    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
24        match self {
25            Error::Parse(e) => Some(e),
26            Error::Sql(e) => Some(e),
27        }
28    }
29}
30
31impl From<ParseError> for Error {
32    fn from(err: ParseError) -> Self {
33        Error::Parse(err)
34    }
35}
36
37impl From<SqlError> for Error {
38    fn from(err: SqlError) -> Self {
39        Error::Sql(err)
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    #[test]
48    fn test_error_from_parse() {
49        let parse_err = ParseError::UnclosedParenthesis;
50        let err = Error::from(parse_err.clone());
51        assert!(matches!(err, Error::Parse(_)));
52    }
53
54    #[test]
55    fn test_error_from_sql() {
56        let sql_err = SqlError::TableNotFound("users".to_string());
57        let err = Error::from(sql_err.clone());
58        assert!(matches!(err, Error::Sql(_)));
59    }
60
61    #[test]
62    fn test_error_display() {
63        let err = Error::Parse(ParseError::UnclosedParenthesis);
64        assert!(err.to_string().contains("parse error"));
65    }
66}