From 7f8515ac67e69b3366ee06e18269e2e2a956dd6e Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Wed, 5 Jul 2023 09:07:00 +0530 Subject: [PATCH] Check for errors before proceeding. --- src/lib.rs | 15 ++------------- src/main.rs | 21 +++++++++++++++++---- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7d12d9a..e837f1a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,14 +1,3 @@ -pub fn add(left: usize, right: usize) -> usize { - left + right -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); - } +pub mod error_codes { + pub const ERROR_SCHEMA_INVALID: i32 = 1; } diff --git a/src/main.rs b/src/main.rs index 225de17..fc20202 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,13 +4,26 @@ fn main() { // Get the schema file from the command line arguments or use the default. let default_schema_file = "schema.graphql".to_string(); let schema_file = &std::env::args().nth(1).unwrap_or(default_schema_file); - println!("schema_file: {}", schema_file); // Read the schema file and parse it into a schema object. - let schema_file = std::fs::read_to_string(schema_file).unwrap(); + let result = std::fs::read_to_string(schema_file); + if result.is_err() { + println!("Error reading schema file: {}", result.err().unwrap()); + std::process::exit(error_codes::ERROR_SCHEMA_FILE_INVALID); + } + + let schema_file = result.unwrap(); + let result = parse_schema::(&schema_file); + if result.is_err() { + println!("Error parsing schema file: {}", result.err().unwrap()); + std::process::exit(minigraf::error_codes::ERROR_SCHEMA_INVALID); + } - let ast = parse_schema::(&schema_file).unwrap().to_owned(); + let schema = result.unwrap(); + println!("schema: {:#?}", schema); +} - println!("{:#?}", ast); +mod error_codes { + pub const ERROR_SCHEMA_FILE_INVALID: i32 = 0x10000; }