Skip to content

Commit

Permalink
Split into modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya Mukhopadhyay committed Jul 7, 2023
1 parent bcd47b7 commit d020fb5
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 25 deletions.
10 changes: 1 addition & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
use graphql_parser::{query, schema};

pub fn parse_schema(schema: &str) -> Result<schema::Document<String>, schema::ParseError> {
schema::parse_schema::<String>(schema)
}

pub fn parse_query(query: &str) -> Result<query::Document<String>, query::ParseError> {
query::parse_query::<String>(query)
}
pub use graphql_parser::{parse_query, parse_schema};
25 changes: 9 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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<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 @@ -22,16 +15,16 @@ 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,
);
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::<String>(&schema_file).unwrap_or_else(|err| {
logger::error_log(
error_codes::ERROR_INVALID_SCHEMA,
"Couldn't parse schema",
err,
Expand All @@ -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::<String>(query);
if query.is_err() {
error_log(
logger::error_log(
error_codes::ERROR_BAD_QUERY,
"Couldn't parse query",
query.err().unwrap(),
Expand Down
3 changes: 3 additions & 0 deletions src/server/error_codes.rs
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 8 additions & 0 deletions src/server/logger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use std::fmt::Display;

pub fn error_log<E>(error_code: i32, message: &str, err: E)
where
E: Display,
{
eprintln!("ERROR {}: {}: {}", error_code, message, err);
}
2 changes: 2 additions & 0 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod error_codes;
pub mod logger;

0 comments on commit d020fb5

Please sign in to comment.