Skip to content

Commit

Permalink
Extracted library error codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya Mukhopadhyay committed Jul 7, 2023
1 parent be00d76 commit 4487538
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 10 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@ name = "minigraf"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
graphql-parser = "0.4.0"
2 changes: 2 additions & 0 deletions src/error_codes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub const ERROR_INVALID_SCHEMA: i32 = 1;
pub const ERROR_INVALID_QUERY: i32 = 2;
9 changes: 3 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use graphql_parser::query;
use graphql_parser::schema;

pub mod error_codes {
pub const ERROR_SCHEMA_INVALID: i32 = 1;
pub const ERROR_QUERY_INVALID: i32 = 2;
}
pub mod error_codes;

pub fn load_schema(schema: &str) -> schema::Document<String> {
let schema = schema::parse_schema::<String>(schema).unwrap_or_else(|err| {
eprintln!("Error parsing schema: {}", err);
std::process::exit(error_codes::ERROR_SCHEMA_INVALID);
std::process::exit(error_codes::ERROR_INVALID_SCHEMA);
});
dbg!("schema: {:#?}", &schema);

Expand All @@ -19,7 +16,7 @@ pub fn load_schema(schema: &str) -> schema::Document<String> {
pub fn parse_query(query: &str) -> query::Document<String> {
let query = query::parse_query::<String>(query).unwrap_or_else(|err| {
eprintln!("Error parsing query: {}", err);
std::process::exit(error_codes::ERROR_QUERY_INVALID);
std::process::exit(error_codes::ERROR_INVALID_QUERY);
});
dbg!("query: {:#?}", &query);

Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ 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);
process::exit(error_codes::ERROR_SCHEMA_FILE_INVALID);
process::exit(error_codes::ERROR_INVALID_SCHEMA_FILE);
});

minigraf::load_schema(&schema_file);
}

mod error_codes {
pub const ERROR_SCHEMA_FILE_INVALID: i32 = 0x100;
pub const ERROR_INVALID_SCHEMA_FILE: i32 = -1;
}

0 comments on commit 4487538

Please sign in to comment.