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
use std::fmt;
use std::path::PathBuf;

#[derive(Debug)]
pub enum TsGeneratorError {
    EmptyQueryNameFromAnnotation(String),
    EmptyQueryNameFromVarDecl,
    MissingAliasForFunctions(String),
    InvalidTypescriptFilePath(PathBuf),
    // Wildcard expr handler errors
    WildcardStatementWithoutTargetTables,
    WildcardStatementDeadendExpression,
    WildcardStatementUnsupportedTableExpr,
    // Expression errors
    UnknownPlaceholder(String),
}

impl fmt::Display for TsGeneratorError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::EmptyQueryNameFromAnnotation(query) => writeln!(
                f,
                "Failed to fetch query name from DB name annotation - query: {}",
                query,
            ),
            Self::EmptyQueryNameFromVarDecl => todo!(),
            Self::MissingAliasForFunctions(query) => {
                writeln!(f, "Missing alias when handling functions - query: {}", query,)
            }
            Self::InvalidTypescriptFilePath(path_buf) => {
                writeln!(f, "Invalid Typescript file path - file path: {:?}", path_buf,)
            }
            // Wildcard expr handling errors
            Self::WildcardStatementWithoutTargetTables => {
                writeln!(
                    f,
                    "Failed to handle a wildcard statement without target tables in `FROM` statement"
                )
            }
            Self::WildcardStatementDeadendExpression => {
                writeln!(
                    f,
                    "Failed to handle a wildcard statement as it reached a dead-end expression"
                )
            }
            Self::WildcardStatementUnsupportedTableExpr => {
                writeln!(f, "Unsupported table with joins statement detected")
            }
            // Expression errors
            Self::UnknownPlaceholder(placeholder) => {
                writeln!(f, "The query contains unknown placeholder {}", placeholder)
            }
        }
    }
}