Grocery list: Fix filtering of recipes

This commit is contained in:
Matteo Settenvini 2022-08-08 08:06:17 +02:00
parent 5e796365f1
commit e69d8fc24c
Signed by: matteo
GPG key ID: 8576CC1AD97D42DF
4 changed files with 46 additions and 39 deletions

View file

@ -40,7 +40,7 @@ async fn map_events_to_recipe_ids(
let all_events = api_client.get_events(calendar_name, date_range).await?;
let recipe_id_regex: Regex = Regex::new(r"cookbook@(\d+)").unwrap();
let recipe_id_regex: Regex = Regex::new(r"^cookbook@(\d+)$").unwrap();
let recipe_ids = all_events
.iter()
.flat_map(|event| event.property_value("DESCRIPTION"))
@ -55,7 +55,7 @@ async fn map_events_to_recipe_ids(
async fn get_ingredients<RecipeIds>(
api_client: &ApiClient,
recipe_ids: RecipeIds,
) -> Result<Vec<Ingredient>>
) -> Result<Vec<(Ingredient, String)>>
where
RecipeIds: IntoIterator<Item = usize>,
{
@ -74,7 +74,10 @@ where
response.json::<Recipe>().await.map(|r| {
log::info!("Retrieved ingredients for '{}'", r.name);
let recipe_name = r.name.clone();
r.ingredients
.into_iter()
.map(move |i| (i, recipe_name.clone()))
})
});
@ -82,7 +85,7 @@ where
Ok(ingredients.into_iter().flatten().collect())
}
fn merge_ingredients(mut ingredients: Vec<Ingredient>) -> Vec<Ingredient> {
fn merge_ingredients(mut ingredients: Vec<(Ingredient, String)>) -> Vec<(Ingredient, String)> {
ingredients.sort();
// TODO actual merging
@ -90,7 +93,7 @@ fn merge_ingredients(mut ingredients: Vec<Ingredient>) -> Vec<Ingredient> {
ingredients
}
fn prepare_grocery_list(ingredients: &Vec<Ingredient>) -> Result<String> {
fn prepare_grocery_list(ingredients: &Vec<(Ingredient, String)>) -> Result<String> {
let mut out = String::new();
use std::fmt::Write;
@ -101,8 +104,8 @@ fn prepare_grocery_list(ingredients: &Vec<Ingredient>) -> Result<String> {
)?;
writeln!(out)?; // leave an empty line
for ingredient in ingredients {
let ingredient = ingredient.0.as_str();
writeln!(out, "- [ ] {}", ingredient)?;
let ingredient_s = ingredient.0 .0.as_str();
writeln!(out, "- [ ] {} ({})", ingredient_s, ingredient.1)?;
}
Ok(out)