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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (C) 2019-2024 Aleo Systems Inc.
// This file is part of the Leo library.

// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

/// Contains the ASG error definitions.
use crate::LeoMessageCode;

/// Contains the AST error definitions.
pub mod ast;
pub use self::ast::*;

/// Contains the CLI error definitions.
pub mod cli;
pub use self::cli::*;

/// Contains the Compiler error definitions.
pub mod compiler;
pub use self::compiler::*;

/// Contains the Flattener error definitions.
pub mod flattener;
pub use self::flattener::*;

/// Contains the Loop Unroller error definitions.
pub mod loop_unroller;
pub use self::loop_unroller::*;

/// Contains the Package error definitions.
pub mod package;
pub use self::package::*;

/// Contains the Parser error definitions.
pub mod parser;
pub use self::parser::*;

/// Contains the Type Checker error definitions.
pub mod type_checker;
pub use self::type_checker::*;

/// Contains the Utils error definitions.
pub mod utils;
pub use self::utils::*;

/// The LeoError type that contains all sub error types.
/// This allows a unified error type throughout the Leo crates.
#[derive(Debug, Error)]
pub enum LeoError {
    /// Represents an AST Error in a Leo Error.
    #[error(transparent)]
    AstError(#[from] AstError),
    /// Represents a CLI Error in a Leo Error.
    #[error(transparent)]
    CliError(#[from] CliError),
    /// Represents a Compiler Error in a Leo Error.
    #[error(transparent)]
    CompilerError(#[from] CompilerError),
    /// Represents a Package Error in a Leo Error.
    #[error(transparent)]
    PackageError(#[from] PackageError),
    /// Represents a Parser Error in a Leo Error.
    #[error(transparent)]
    ParserError(#[from] ParserError),
    /// Represents a Type Checker Error in a Leo Error.
    #[error(transparent)]
    TypeCheckerError(#[from] TypeCheckerError),
    /// Represents a Loop Unroller Error in a Leo Error.
    #[error(transparent)]
    LoopUnrollerError(#[from] LoopUnrollerError),
    /// Represents a Flatten Error in a Leo Error.
    #[error(transparent)]
    FlattenError(#[from] FlattenError),
    /// Purely for just exiting with the correct status code and
    /// not re-displaying an error.
    #[error("")]
    LastErrorCode(i32),
    /// Represents a Utils Error in a Leo Error.
    #[error(transparent)]
    UtilError(#[from] UtilError),
    /// Anyhow errors.
    #[error(transparent)]
    Anyhow(#[from] anyhow::Error),
}

impl LeoError {
    /// Implement error code for each type of Error.
    pub fn error_code(&self) -> String {
        use LeoError::*;

        match self {
            AstError(error) => error.error_code(),
            CompilerError(error) => error.error_code(),
            CliError(error) => error.error_code(),
            ParserError(error) => error.error_code(),
            PackageError(error) => error.error_code(),
            TypeCheckerError(error) => error.error_code(),
            LoopUnrollerError(error) => error.error_code(),
            FlattenError(error) => error.error_code(),
            UtilError(error) => error.error_code(),
            LastErrorCode(_) => unreachable!(),
            Anyhow(_) => "SnarkVM Error".to_string(), // todo: implement error codes for snarkvm errors.
        }
    }

    /// Implement exit code for each type of Error.
    pub fn exit_code(&self) -> i32 {
        use LeoError::*;

        match self {
            AstError(error) => error.exit_code(),
            CompilerError(error) => error.exit_code(),
            CliError(error) => error.exit_code(),
            ParserError(error) => error.exit_code(),
            PackageError(error) => error.exit_code(),
            TypeCheckerError(error) => error.exit_code(),
            LoopUnrollerError(error) => error.exit_code(),
            FlattenError(error) => error.exit_code(),
            UtilError(error) => error.exit_code(),
            LastErrorCode(code) => *code,
            Anyhow(_) => 11000, // todo: implement exit codes for snarkvm errors.
        }
    }
}

/// The LeoWarning type that contains all sub warning types.
/// This allows a unified warning type throughout the Leo crates.
#[derive(Debug, Error)]
pub enum LeoWarning {
    /// Represents an Parser Warning in a Leo Warning.
    #[error(transparent)]
    ParserWarning(#[from] ParserWarning),
    /// Represents a Type Checker Warning in a Leo Warning.
    #[error(transparent)]
    TypeCheckerWarning(#[from] TypeCheckerWarning),
}

impl LeoWarning {
    /// Implement warning code for each type of Warning.
    pub fn error_code(&self) -> String {
        use LeoWarning::*;

        match self {
            ParserWarning(warning) => warning.warning_code(),
            TypeCheckerWarning(warning) => warning.warning_code(),
        }
    }
}

/// A global result type for all Leo crates, that defaults the errors to be a LeoError.
pub type Result<T, E = LeoError> = core::result::Result<T, E>;