diff --git a/src/lib.rs b/src/lib.rs index f534b97..a3022a6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1 @@ -use graphql_parser::{query, schema}; - -pub fn parse_schema(schema: &str) -> Result, schema::ParseError> { - schema::parse_schema::(schema) -} - -pub fn parse_query(query: &str) -> Result, query::ParseError> { - query::parse_query::(query) -} +pub use graphql_parser::{parse_query, parse_schema}; diff --git a/src/main.rs b/src/main.rs index 330d0b5..576a422 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,11 @@ 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; -} +use minigraf::{parse_query, parse_schema}; +use server::{error_codes, logger}; + +mod server; -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. @@ -22,7 +15,7 @@ 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| { - error_log( + logger::error_log( error_codes::ERROR_INVALID_SCHEMA_FILE, "Couldn't read schema file", err, @@ -30,8 +23,8 @@ fn main() { process::exit(error_codes::ERROR_INVALID_SCHEMA_FILE); }); - let schema = minigraf::parse_schema(&schema_file).unwrap_or_else(|err| { - error_log( + let schema = parse_schema::(&schema_file).unwrap_or_else(|err| { + logger::error_log( error_codes::ERROR_INVALID_SCHEMA, "Couldn't parse schema", err, @@ -52,9 +45,9 @@ fn main() { let query = query.trim(); // Parse the query into a query object. - let query = minigraf::parse_query(query); + let query = parse_query::(query); if query.is_err() { - error_log( + logger::error_log( error_codes::ERROR_BAD_QUERY, "Couldn't parse query", query.err().unwrap(), diff --git a/src/server/error_codes.rs b/src/server/error_codes.rs new file mode 100644 index 0000000..a7f30d4 --- /dev/null +++ b/src/server/error_codes.rs @@ -0,0 +1,3 @@ +pub const ERROR_INVALID_SCHEMA_FILE: i32 = 1; +pub const ERROR_INVALID_SCHEMA: i32 = 2; +pub const ERROR_BAD_QUERY: i32 = 3; diff --git a/src/server/logger.rs b/src/server/logger.rs new file mode 100644 index 0000000..4602a33 --- /dev/null +++ b/src/server/logger.rs @@ -0,0 +1,8 @@ +use std::fmt::Display; + +pub fn error_log(error_code: i32, message: &str, err: E) +where + E: Display, +{ + eprintln!("ERROR {}: {}: {}", error_code, message, err); +} diff --git a/src/server/mod.rs b/src/server/mod.rs new file mode 100644 index 0000000..3e6e488 --- /dev/null +++ b/src/server/mod.rs @@ -0,0 +1,2 @@ +pub mod error_codes; +pub mod logger;