diff --git a/ast/src/errors/mod.rs b/ast/src/errors/mod.rs
index a7aefbc826..9ba0acfb32 100644
--- a/ast/src/errors/mod.rs
+++ b/ast/src/errors/mod.rs
@@ -19,3 +19,25 @@ pub use deprecated::*;
pub mod error;
pub use error::*;
+
+use error::Error as FormattedError;
+
+use leo_grammar::ParserError;
+
+#[derive(Debug, Error)]
+pub enum AstError {
+ #[error("{}", _0)]
+ DeprecatedError(#[from] DeprecatedError),
+
+ #[error("{}", _0)]
+ Error(#[from] FormattedError),
+
+ #[error("{}", _0)]
+ IoError(#[from] std::io::Error),
+
+ #[error("{}", _0)]
+ ParserError(#[from] ParserError),
+
+ #[error("{}", _0)]
+ JsonError(#[from] serde_json::error::Error),
+}
diff --git a/ast/src/main.rs b/ast/src/main.rs
index c04c1f1a56..26570bc98a 100644
--- a/ast/src/main.rs
+++ b/ast/src/main.rs
@@ -14,11 +14,11 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see .
-use leo_ast::Ast;
-use leo_grammar::{Grammar, ParserError};
+use leo_ast::{Ast, AstError};
+use leo_grammar::Grammar;
use std::{env, fs, path::Path};
-fn to_leo_tree(filepath: &Path) -> Result {
+fn to_leo_tree(filepath: &Path) -> Result {
// Loads the Leo code as a string from the given file path.
let program_filepath = filepath.to_path_buf();
let program_string = Grammar::load_file(&program_filepath)?;
@@ -27,21 +27,14 @@ fn to_leo_tree(filepath: &Path) -> Result {
let ast = Grammar::new(&program_filepath, &program_string)?;
// Parse the pest ast and constructs a ast.
- let leo_ast_result = Ast::new("leo_tree", &ast);
+ let leo_ast = Ast::new("leo_tree", &ast)?;
- match leo_ast_result {
- Ok(leo_ast) => {
- // Serializes the tree into JSON format.
- let serialized_leo_ast = Ast::to_json_string(&leo_ast)?;
+ let serialized_leo_ast = Ast::to_json_string(&leo_ast)?;
- Ok(serialized_leo_ast)
- }
- // How to deal with this?
- Err(_) => Err(ParserError::SyntaxTreeError),
- }
+ Ok(serialized_leo_ast)
}
-fn main() -> Result<(), ParserError> {
+fn main() -> Result<(), AstError> {
// Parse the command-line arguments as strings.
let cli_arguments = env::args().collect::>();