Skip to content

Commit

Permalink
Concise result handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya Mukhopadhyay committed Jul 6, 2023
1 parent dd06861 commit ac892ee
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
use std::{env, fs, process};

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);
let schema_file = &env::args().nth(1).unwrap_or(default_schema_file);
dbg!("schema_file: {}", schema_file);

// Read the schema file and parse it into a schema object.
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 = minigraf::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 schema_file = fs::read_to_string(schema_file).unwrap_or_else(|err| {
eprintln!("Error reading schema file: {}", err);
process::exit(error_codes::ERROR_SCHEMA_FILE_INVALID);
});

let schema = result.unwrap();
println!("schema: {:#?}", schema);
let schema = minigraf::parse_schema(&schema_file).unwrap_or_else(|err| {
eprintln!("Error parsing schema file: {}", err);
process::exit(minigraf::error_codes::ERROR_SCHEMA_INVALID);
});
dbg!("schema: {:#?}", schema);
}

mod error_codes {
Expand Down

0 comments on commit ac892ee

Please sign in to comment.