/ errors / src / errors / mod.rs
mod.rs
  1  // Copyright (C) 2019-2025 Alpha-Delta Network Inc.
  2  // This file is part of the ADL library.
  3  
  4  // The ADL library is free software: you can redistribute it and/or modify
  5  // it under the terms of the GNU General Public License as published by
  6  // the Free Software Foundation, either version 3 of the License, or
  7  // (at your option) any later version.
  8  
  9  // The ADL library is distributed in the hope that it will be useful,
 10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 12  // GNU General Public License for more details.
 13  
 14  // You should have received a copy of the GNU General Public License
 15  // along with the ADL library. If not, see <https://www.gnu.org/licenses/>.
 16  
 17  /// Contains the ASG error definitions.
 18  use crate::AdlMessageCode;
 19  
 20  /// Contains the AST error definitions.
 21  mod ast;
 22  pub use self::ast::*;
 23  
 24  /// Contains the CLI error definitions.
 25  mod cli;
 26  pub use self::cli::*;
 27  
 28  /// Contains the Compiler error definitions.
 29  mod compiler;
 30  pub use self::compiler::*;
 31  
 32  /// Contains the Flattener error definitions.
 33  mod flattener;
 34  pub use self::flattener::*;
 35  
 36  /// Contains the Loop Unroller error definitions.
 37  mod loop_unroller;
 38  pub use self::loop_unroller::*;
 39  
 40  mod interpreter_halt;
 41  pub use self::interpreter_halt::*;
 42  
 43  /// Contains the Package error definitions.
 44  mod package;
 45  pub use self::package::*;
 46  
 47  /// Contains the Parser error definitions.
 48  mod parser;
 49  pub use self::parser::*;
 50  
 51  /// Contains the Static Analyzer error definitions.
 52  mod static_analyzer;
 53  pub use self::static_analyzer::*;
 54  
 55  /// Contains the Type Checker error definitions.
 56  mod type_checker;
 57  pub use self::type_checker::*;
 58  
 59  /// Contains the Name Validation error definitions.
 60  mod name_validation;
 61  pub use self::name_validation::*;
 62  
 63  /// Contains the Utils error definitions.
 64  mod utils;
 65  pub use self::utils::*;
 66  
 67  /// The AdlError type that contains all sub error types.
 68  /// This allows a unified error type throughout the Adl crates.
 69  #[derive(Debug, Error)]
 70  pub enum AdlError {
 71      /// Represents an AST Error in a Adl Error.
 72      #[error(transparent)]
 73      AstError(#[from] AstError),
 74      /// Represents a CLI Error in a Adl Error.
 75      #[error(transparent)]
 76      CliError(#[from] CliError),
 77      /// Represents a Compiler Error in a Adl Error.
 78      #[error(transparent)]
 79      CompilerError(#[from] CompilerError),
 80      #[error(transparent)]
 81      InterpreterHalt(#[from] InterpreterHalt),
 82      /// Represents a Package Error in a Adl Error.
 83      #[error(transparent)]
 84      PackageError(#[from] PackageError),
 85      /// Represents a Parser Error in a Adl Error.
 86      #[error(transparent)]
 87      ParserError(#[from] ParserError),
 88      /// Represents a Static Analyzer Error in a Adl Error.
 89      #[error(transparent)]
 90      StaticAnalyzerError(#[from] StaticAnalyzerError),
 91      /// Represents a Type Checker Error in a Adl Error.
 92      #[error(transparent)]
 93      TypeCheckerError(#[from] TypeCheckerError),
 94      /// Represents a Name Validation Error in a Adl Error.
 95      #[error(transparent)]
 96      NameValidationError(#[from] NameValidationError),
 97      /// Represents a Loop Unroller Error in a Adl Error.
 98      #[error(transparent)]
 99      LoopUnrollerError(#[from] LoopUnrollerError),
100      /// Represents a Flatten Error in a Adl Error.
101      #[error(transparent)]
102      FlattenError(#[from] FlattenError),
103      /// Purely for just exiting with the correct status code and
104      /// not re-displaying an error.
105      #[error("")]
106      LastErrorCode(i32),
107      /// Represents a Utils Error in a Adl Error.
108      #[error(transparent)]
109      UtilError(#[from] UtilError),
110      /// Anyhow errors.
111      #[error(transparent)]
112      Anyhow(#[from] anyhow::Error),
113  }
114  
115  impl AdlError {
116      /// Implement error code for each type of Error.
117      pub fn error_code(&self) -> String {
118          use AdlError::*;
119  
120          match self {
121              AstError(error) => error.error_code(),
122              CompilerError(error) => error.error_code(),
123              CliError(error) => error.error_code(),
124              ParserError(error) => error.error_code(),
125              PackageError(error) => error.error_code(),
126              StaticAnalyzerError(error) => error.error_code(),
127              TypeCheckerError(error) => error.error_code(),
128              NameValidationError(error) => error.error_code(),
129              LoopUnrollerError(error) => error.error_code(),
130              FlattenError(error) => error.error_code(),
131              UtilError(error) => error.error_code(),
132              LastErrorCode(_) => unreachable!(),
133              Anyhow(_) => "SnarkVM Error".to_string(), // todo: implement error codes for alphavm errors.
134              InterpreterHalt(_) => "Interpreter Halt".to_string(),
135          }
136      }
137  
138      /// Implement exit code for each type of Error.
139      pub fn exit_code(&self) -> i32 {
140          use AdlError::*;
141  
142          match self {
143              AstError(error) => error.exit_code(),
144              CompilerError(error) => error.exit_code(),
145              CliError(error) => error.exit_code(),
146              ParserError(error) => error.exit_code(),
147              PackageError(error) => error.exit_code(),
148              StaticAnalyzerError(error) => error.exit_code(),
149              TypeCheckerError(error) => error.exit_code(),
150              NameValidationError(error) => error.exit_code(),
151              LoopUnrollerError(error) => error.exit_code(),
152              FlattenError(error) => error.exit_code(),
153              UtilError(error) => error.exit_code(),
154              LastErrorCode(code) => *code,
155              Anyhow(_) => 11000, // todo: implement exit codes for alphavm errors.
156              InterpreterHalt(_) => 1,
157          }
158      }
159  }
160  
161  /// The AdlWarning type that contains all sub warning types.
162  /// This allows a unified warning type throughout the Adl crates.
163  #[derive(Debug, Error, Hash, PartialEq, Eq)]
164  pub enum AdlWarning {
165      /// Represents an Parser Warning in a Adl Warning.
166      #[error(transparent)]
167      ParserWarning(#[from] ParserWarning),
168      /// Represents a Static Analyzer Warning in a Adl Warning.
169      #[error(transparent)]
170      StaticAnalyzerWarning(#[from] StaticAnalyzerWarning),
171      /// Represents a Type Checker Warning in a Adl Warning.
172      #[error(transparent)]
173      TypeCheckerWarning(#[from] TypeCheckerWarning),
174  }
175  
176  impl AdlWarning {
177      /// Implement warning code for each type of Warning.
178      pub fn error_code(&self) -> String {
179          use AdlWarning::*;
180  
181          match self {
182              ParserWarning(warning) => warning.warning_code(),
183              TypeCheckerWarning(warning) => warning.warning_code(),
184              StaticAnalyzerWarning(warning) => warning.warning_code(),
185          }
186      }
187  }
188  
189  /// A global result type for all Adl crates, that defaults the errors to be a AdlError.
190  pub type Result<T, E = AdlError> = core::result::Result<T, E>;