Skip to content

Commit

Permalink
Introduced primitive error logger.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya Mukhopadhyay committed Jul 7, 2023
1 parent c2d0a49 commit bcd47b7
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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<E>(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.
Expand All @@ -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);
Expand All @@ -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;
}

0 comments on commit bcd47b7

Please sign in to comment.