1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

//! Error handling.
//!
//! ToqlError represents all library errors and wraps errors from the Pest parser and the optional database crate.
//!
use crate::query_parser::Rule;
use std::fmt;
use crate::sql_builder::SqlBuilderError;

use pest::error::Error as PestError;

 #[cfg(feature = "mysqldb")]
use mysql::error::Error;

/// Represents all errors
#[derive(Debug)]
 pub enum ToqlError {
    /// No single record found for the Toql query.
    NotFound,
    /// Many records found, while exactly one was expected.
    NotUnique,
    /// The query parser encountered a syntax error.
    QueryParserError(pest::error::Error<Rule>),
    /// The query encoding was not valid UTF-8.
    EncodingError(std::str::Utf8Error),
    /// No mapper was found for a given struct. Contains the struct name.
    MapperMissing(String),
    /// TODO unclear
    ValueMissing(String),
    /// SQL Builder failed to turn Toql query into SQL query.
    SqlBuilderError(SqlBuilderError),
    #[cfg(feature = "mysqldb")]
    /// MySQL failed to run the SQL query. For feature `mysql`
    MySqlError(Error)
} 

/// A result with a [`ToqlError`](struct.ToqlError.html)
pub type Result<T> = std::result::Result<T, ToqlError>;


impl From<SqlBuilderError> for ToqlError {
        fn from(err: SqlBuilderError) -> ToqlError {
        ToqlError::SqlBuilderError(err)
    }
}

#[cfg(feature = "mysqldb")]
impl From<Error> for ToqlError {
        fn from(err: Error) -> ToqlError {
        ToqlError::MySqlError(err)
    }
}

impl From<PestError<Rule>> for ToqlError {
        fn from(err: PestError<Rule>) -> ToqlError {
        ToqlError::QueryParserError(err)
    }
}

impl fmt::Display for ToqlError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ToqlError::NotFound =>
                write!(f, "no result found"),
            ToqlError::NotUnique =>
                write!(f, "no unique result found "),
            ToqlError::MapperMissing(ref s) =>
                write!(f, "no mapper found for `{}`", s),
            ToqlError::ValueMissing(ref s) =>
                write!(f, "no value found for `{}`", s),
            #[cfg(feature = "mysqldb")]
            ToqlError::MySqlError (ref e) => e.fmt(f),
            ToqlError::SqlBuilderError (ref e) => e.fmt(f),
            ToqlError::EncodingError (ref e) => e.fmt(f),
            ToqlError::QueryParserError (ref e) => e.fmt(f),
        }
    }
}