diff --git a/src/main.rs b/src/main.rs index fa5c1ed..330d0b5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,18 @@ -use std::{env, fs, io, process}; use std::io::Write; +use std::{env, fs, io, process}; + +mod error_codes { + pub const ERROR_INVALID_SCHEMA_FILE: i32 = 1; + pub const ERROR_INVALID_SCHEMA: i32 = 2; + pub const ERROR_BAD_QUERY: i32 = 3; +} + +fn error_log(error_code: i32, message: &str, err: E) +where + E: std::fmt::Display, +{ + eprintln!("ERROR {}: {}: {}", error_code, message, err); +} fn main() { // Get the schema file from the command line arguments or use the default. @@ -9,12 +22,20 @@ fn main() { // Read the schema file and parse it into a schema object. let schema_file = fs::read_to_string(schema_file).unwrap_or_else(|err| { - eprintln!("Error reading schema file: {}", err); + error_log( + error_codes::ERROR_INVALID_SCHEMA_FILE, + "Couldn't read schema file", + err, + ); process::exit(error_codes::ERROR_INVALID_SCHEMA_FILE); }); let schema = minigraf::parse_schema(&schema_file).unwrap_or_else(|err| { - eprintln!("Error parsing schema: {}", err); + error_log( + error_codes::ERROR_INVALID_SCHEMA, + "Couldn't parse schema", + err, + ); process::exit(error_codes::ERROR_INVALID_SCHEMA); }); dbg!("schema: {:#?}", &schema); @@ -33,14 +54,13 @@ fn main() { // Parse the query into a query object. let query = minigraf::parse_query(query); if query.is_err() { - eprintln!("Error parsing query: {}", query.unwrap_err()); + error_log( + error_codes::ERROR_BAD_QUERY, + "Couldn't parse query", + query.err().unwrap(), + ); continue; } dbg!("query: {:#?}", &query); } } - -mod error_codes { - pub const ERROR_INVALID_SCHEMA_FILE: i32 = 1; - pub const ERROR_INVALID_SCHEMA: i32 = 2; -}