Skip to content

Commit

Permalink
lib behaves more like a lib.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya Mukhopadhyay committed Jul 7, 2023
1 parent 4487538 commit c2d0a49
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
2 changes: 0 additions & 2 deletions src/error_codes.rs

This file was deleted.

25 changes: 5 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
use graphql_parser::query;
use graphql_parser::schema;
use graphql_parser::{query, schema};

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_INVALID_SCHEMA);
});
dbg!("schema: {:#?}", &schema);

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

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_INVALID_QUERY);
});
dbg!("query: {:#?}", &query);

query
pub fn parse_query(query: &str) -> Result<query::Document<String>, query::ParseError> {
query::parse_query::<String>(query)
}
32 changes: 29 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{env, fs, process};
use std::{env, fs, io, process};
use std::io::Write;

fn main() {
// Get the schema file from the command line arguments or use the default.
Expand All @@ -12,9 +13,34 @@ fn main() {
process::exit(error_codes::ERROR_INVALID_SCHEMA_FILE);
});

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

// Start a Read-Eval-Print-Loop (REPL) for the user to enter queries.
loop {
// Prompt the user for a query.
print!("> ");
io::stdout().flush().unwrap();

// Get the query from the user.
let mut query = String::new();
io::stdin().read_line(&mut query).unwrap();
let query = query.trim();

// Parse the query into a query object.
let query = minigraf::parse_query(query);
if query.is_err() {
eprintln!("Error parsing query: {}", query.unwrap_err());
continue;
}
dbg!("query: {:#?}", &query);
}
}

mod error_codes {
pub const ERROR_INVALID_SCHEMA_FILE: i32 = -1;
pub const ERROR_INVALID_SCHEMA_FILE: i32 = 1;
pub const ERROR_INVALID_SCHEMA: i32 = 2;
}

0 comments on commit c2d0a49

Please sign in to comment.