Skip to content

Commit

Permalink
feat: Add error message for table name not ending with Collection
Browse files Browse the repository at this point in the history
  • Loading branch information
floris-xlx committed Jun 12, 2024
1 parent 5767ed6 commit 27dd186
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
12 changes: 12 additions & 0 deletions src/graphql/error_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,16 @@ pub fn field_does_not_exist_on_table(field: &str, table: &str) -> String {
println!("{}", error);

"Field does not exist on table".to_string()
}

pub fn table_name_does_not_end_with_collection(table_name: &str) -> String {
let error: String = format!("\x1b[1;31mTable name does not end with \x1b[1;34m`Collection`\x1b[1;31m: {}\x1b[0m", table_name).to_string();
let arrow_amount: String = "^".repeat(table_name.len());

println!("{}", error);
println!("\x1b[1;34m{}\x1b[0m\x1b[1;32mCollection\x1b[0m", table_name);
println!("\x1b[1;34m{}\x1b[0m", arrow_amount);
println!("Add Collection to the end of the table name and try again");

"Table name does not end with Collection".to_string()
}
36 changes: 24 additions & 12 deletions src/graphql/parse.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::{Result, Error};
use serde_json::Value;
use crate::graphql::error_types::{illegal_table_name, table_does_not_exist, field_does_not_exist_on_table, table_name_does_not_end_with_collection};


pub fn parse_outer(query: &Value) -> bool {
Expand All @@ -18,20 +19,31 @@ pub fn parse_outer(query: &Value) -> bool {

pub fn get_table_name(query: &Value) -> Result<String, Error> {
if parse_outer(query) {
let table_name: String = query["query"].as_str().unwrap_or("").to_string();

// remove the brackets
let table_name: String = table_name.replace("{", "").replace("}", "");

// first word is table name
let table_name: String = table_name.split_whitespace().next().unwrap().to_string();

// break the word off as soon as its not alphanumeric
let table_name: String = table_name.chars().take_while(|c| c.is_alphanumeric()).collect();

let query_str: &str = query["query"].as_str().unwrap_or("");
println!("Query: {}", query_str);
// remove all the { } and then get the first alphanumeric word from the query
let query_str: String = query_str.replace("{", "").replace("}", "");
let query_str: String = query_str.trim().to_string();
let query_str: Vec<&str> = query_str.split_whitespace().collect();
let mut table_name: String = query_str[0].to_string();

// remove all beyond the last alphanumeric char
for (i, c) in table_name.chars().enumerate() {
if !c.is_alphanumeric() {
table_name = table_name[..i].to_string();
break;
}
}

// if the table name doesnt end with Collection, add it
if !table_name.ends_with("Collection") {
table_name_does_not_end_with_collection(&table_name);
}

Ok(table_name)
} else {
Err(Error::msg("Invalid outer structure"))
}
}
}


4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ async fn main() {
usersCollection(first: 1) {
edges {
node {
user_id
user_id,
username,
email
}
}
}
Expand Down

0 comments on commit 27dd186

Please sign in to comment.