Skip to content

Commit

Permalink
Merge pull request #113 from suchapalaver/fix/miscellaneous-small-fixes
Browse files Browse the repository at this point in the history
Reduce JSON test cases size and make 'Items::add_item' and 'add_recipe' methods idempotent
  • Loading branch information
suchapalaver authored Jan 3, 2024
2 parents 3b04cb2 + ffab787 commit 8501d98
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 782 deletions.
8 changes: 4 additions & 4 deletions crates/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ pub struct Api {

impl Api {
pub async fn init(store: StoreType) -> Result<ApiDispatch, ApiError> {
info!("Initializing API with store type: {:?}", store);
info!("Initializing API with store type: {store}");

let store = Store::from_store_type(store).await?.init().await?;

let api = Api { store };
let api = Api {
store: Store::from_store_type(store).await?.init().await?,
};

let (tx, mut rx) = mpsc::channel::<ApiSendWithReply>(10);
let dispatch = ApiDispatch { tx };
Expand Down
17 changes: 14 additions & 3 deletions crates/common/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ impl Item {
}

pub fn add_recipe(&mut self, recipe: &str) {
self.recipes
.get_or_insert_with(Vec::new)
.push(recipe.into());
let recipe = recipe.into();
if let Some(recipes) = &mut self.recipes {
if !recipes.contains(&recipe) {
recipes.push(recipe);
}
} else {
self.recipes = Some(vec![recipe]);
}
}

pub fn delete_recipe(&mut self, name: &str) {
Expand Down Expand Up @@ -70,6 +75,12 @@ impl fmt::Display for Item {
}
}

impl From<&Name> for Item {
fn from(name: &Name) -> Self {
Self::new(name.as_str())
}
}

#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
pub struct Name(String);

Expand Down
12 changes: 10 additions & 2 deletions crates/common/src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ impl Items {
}

pub fn add_item(&mut self, item: Item) {
self.collection.push(item);
if !self.collection.iter().any(|i| i.name() == item.name()) {
self.collection.push(item);
}
}

pub fn delete_item(&mut self, name: &str) {
Expand Down Expand Up @@ -75,9 +77,15 @@ impl Items {
}

pub fn add_recipe(&mut self, name: &str, ingredients: &str) {
let ingredients = Ingredients::from_input_string(ingredients);

ingredients
.iter()
.for_each(|ingredient| self.add_item(ingredient.into()));

self.collection
.iter_mut()
.filter(|item| Ingredients::from_input_string(ingredients).contains(item.name()))
.filter(|item| ingredients.contains(item.name()))
.for_each(|item| item.add_recipe(name));

self.recipes.push(name.into());
Expand Down
4 changes: 4 additions & 0 deletions crates/common/src/recipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ impl Recipe {
pub fn as_str(&self) -> &str {
&self.0
}

pub fn from_input_string(s: &str) -> Self {
Self::from(s)
}
}

impl From<&str> for Recipe {
Expand Down
28 changes: 13 additions & 15 deletions crates/gust/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use common::{
commands::{Add, ApiCommand, Delete, Read, Update},
item::{Name, Section},
recipes::Ingredients,
recipes::{Ingredients, Recipe},
};

use clap::ArgMatches;
Expand All @@ -28,34 +28,32 @@ impl TryFrom<ArgMatches> for GustCommand {
matches.get_one::<String>("recipe"),
matches.get_one::<String>("ingredients"),
) {
let (recipe, ingredients) = (
recipe.as_str().into(),
Ingredients::from_input_string(ingredients.trim()),
);

Add::recipe_from_name_and_ingredients(recipe, ingredients)
Add::recipe_from_name_and_ingredients(
Recipe::from_input_string(recipe),
Ingredients::from_input_string(ingredients),
)
} else if let Some(name) = matches.get_one::<String>("item") {
Add::item_from_name_and_section(
Name::from(name.trim()),
Name::from(name.as_str()),
matches
.get_one::<String>("section")
.map(|section| Section::from(section.trim())),
)
} else if let Some(item) = matches.get_one::<String>("checklist-item") {
Add::checklist_item_from_name(Name::from(item.trim()))
Add::checklist_item_from_name(Name::from(item.as_str()))
} else {
match matches.subcommand() {
Some(("checklist", matches)) => Add::checklist_item_from_name(Name::from(
matches
.get_one::<String>("item")
.expect("item required")
.trim(),
.as_str(),
)),
Some(("list", matches)) => {
if let Some(name) = matches.get_one::<String>("recipe") {
Add::list_recipe_from_name(name.as_str().into())
} else if let Some(name) = matches.get_one::<String>("item") {
Add::list_item_from_name(Name::from(name.trim()))
Add::list_item_from_name(Name::from(name.as_str()))
} else {
unimplemented!()
}
Expand All @@ -68,14 +66,14 @@ impl TryFrom<ArgMatches> for GustCommand {
if let Some(name) = matches.get_one::<String>("recipe") {
Delete::recipe_from_name(name.as_str().into())
} else if let Some(name) = matches.get_one::<String>("item") {
Delete::item_from_name(Name::from(name.trim()))
Delete::item_from_name(Name::from(name.as_str()))
} else {
match matches.subcommand() {
Some(("checklist", matches)) => {
let Some(name) = matches.get_one::<String>("checklist-item") else {
unimplemented!()
};
Delete::ChecklistItem(Name::from(name.trim()))
Delete::ChecklistItem(Name::from(name.as_str()))
}
_ => unimplemented!(),
}
Expand All @@ -85,14 +83,14 @@ impl TryFrom<ArgMatches> for GustCommand {
let Some(url) = matches.get_one::<String>("url") else {
unreachable!("Providing a URL is required")
};
let url: Url = Url::parse(url.trim())?;
let url: Url = Url::parse(url)?;
Ok(GustCommand::FetchRecipe(url))
}
Some(("read", matches)) => Ok(GustCommand::Read(
if let Some(name) = matches.get_one::<String>("recipe") {
Read::recipe_from_name(name.as_str().into())
} else if let Some(name) = matches.get_one::<String>("item") {
Read::item_from_name(Name::from(name.trim()))
Read::item_from_name(Name::from(name.as_str()))
} else {
match matches.subcommand() {
Some(("checklist", _matches)) => Read::Checklist,
Expand Down
Loading

0 comments on commit 8501d98

Please sign in to comment.