diff --git a/src/networking.rs b/src/networking.rs index 3baabc9..3b52dec 100644 --- a/src/networking.rs +++ b/src/networking.rs @@ -1,12 +1,24 @@ use rocket::form::Form; -use crate::structures::RecipeForm; +use crate::structures::{RecipeForm, Tag}; #[get("/test")] pub fn test() -> String { return "Hello! :3".to_string(); } +pub fn parse_tags(tags: String) -> Vec { + let split: Vec<&str> = tags.split(",").collect(); + let mut tags_vec: Vec = Vec::new(); + for i in split { + tags_vec.push(Tag::new(i + .replace(" ", "") + .replace("#", ""))); + } + return tags_vec; +} + #[post("/new-recipe", data = "")] pub fn new_recipe(recipe: Form) -> String { - return recipe.name.clone(); + + return recipe.recipe_name.to_owned(); } diff --git a/src/structures.rs b/src/structures.rs index 43671b3..cfca03a 100644 --- a/src/structures.rs +++ b/src/structures.rs @@ -82,7 +82,9 @@ pub struct Tag(pub char, pub String); impl Tag { pub fn new(s: String) -> Tag { - Tag('#', s) + let mut fuck = s; + fuck.make_ascii_lowercase(); + Tag('#', fuck) } pub fn to_string(&self) -> String { @@ -107,8 +109,8 @@ impl Tag { #[derive(FromForm)] pub struct RecipeForm { - pub name: String, - pub author: String, + pub recipe_name: String, + pub author_name: String, pub tags: String, pub prep_time: String, pub cooking_time: String, diff --git a/src/tests.rs b/src/tests.rs index 49422a4..9eed194 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -2,6 +2,7 @@ mod tests { // Note this useful idiom: importing names from outer (for mod tests) scope. use crate::structures::*; + use crate::networking::{parse_tags}; #[test] fn test_tag_new() { @@ -22,4 +23,13 @@ mod tests { fn test_shortcode_conversion() { assert_eq!(construct_shortcode("Test SHORtcode".to_string()), "test-shortcode".to_string()); } + + #[test] + fn test_parse_tags() { + assert_eq!(parse_tags("one,TWO ,tHRee, #four".to_string()), vec![ + Tag('#',"one".to_string()), + Tag('#',"two".to_string()), + Tag('#',"three".to_string()), + Tag('#',"four".to_string())]); + } }