diff --git a/bindings/README.md b/bindings/README.md index e799252..5dca504 100644 --- a/bindings/README.md +++ b/bindings/README.md @@ -15,7 +15,7 @@ This library exports methods: ```rust // full parsing, returns full recipe object with meta parse_recipe(input: String) -> CooklangRecipe; - // fast parsing, only metadata is parsed and returned + // fast metadata parsing, recipe text is not parsed parse_metadata(input: String) -> CooklangMetadata; // parse aisle config to use in shopping list parse_aisle_config(input: String) -> Arc; @@ -36,6 +36,7 @@ This library exports methods: // usage example: // let timer = deref_timer(recipe, 0); deref_timer(recipe: &CooklangRecipe, index: u32) -> Timer; + // combines ingredient lists into one // usage example: // let all_recipe_ingredients_combined = combine_ingredients(recipe.ingredients); @@ -54,27 +55,27 @@ This library exports methods: struct CooklangRecipe { /// Recipe metadata like title, source, etc. metadata: CooklangMetadata, - /// List of recipe sections + /// List of recipe sections, each containing blocks of content, like steps, notes, etc. sections: Vec
, /// List of all ingredients used in the recipe in order of use. Not quantity combined. ingredients: Vec, - /// List of all cookware used in the recipe + /// List of all cookware used in the recipe. cookware: Vec, - /// List of all timers used in the recipe + /// List of all timers used in the recipe. timers: Vec, } /// Represents a distinct section of a recipe, optionally with a title struct Section { - /// Optional section title (e.g., "Preparation", "Cooking", etc.) + /// Optional section title (e.g., "Dough", "Topping", etc.) title: Option, - /// List of content blocks in this section + /// List of content blocks in this section. Each block can be a step or a note. blocks: Vec, - /// References to ingredients used in this section + /// Indices of ingredients used in this section. ingredient_refs: Vec, - /// References to cookware used in this section + /// Indices of cookware used in this section. cookware_refs: Vec, - /// References to timers used in this section + /// Indices of timers used in this section. timer_refs: Vec, } @@ -90,11 +91,11 @@ This library exports methods: struct Step { /// List of items that make up this step (text and references) items: Vec, - /// References to ingredients used in this step + /// Indices of ingredients used in this step ingredient_refs: Vec, - /// References to cookware used in this step + /// Indices of cookware used in this step cookware_refs: Vec, - /// References to timers used in this step + /// Indices of timers used in this step timer_refs: Vec, } @@ -116,9 +117,7 @@ This library exports methods: /// Represents a piece of cookware used in the recipe struct Cookware { - /// Name of the name: String, - /// Optional quantity and units amount: Option, } @@ -126,7 +125,6 @@ This library exports methods: struct Timer { /// Optional timer name (e.g., "boiling", "baking", etc.) name: Option, - /// Optional quantity and units amount: Option, } @@ -134,11 +132,11 @@ This library exports methods: enum Item { /// A text item Text { value: String }, - /// An ingredient reference + /// An ingredient reference index IngredientRef { index: u32 }, - /// A cookware reference + /// A cookware reference index CookwareRef { index: u32 }, - /// A timer reference + /// A timer reference index TimerRef { index: u32 }, } @@ -152,21 +150,22 @@ This library exports methods: /// Represents a value in the recipe enum Value { - /// A number value Number { value: f64 }, - /// A range value Range { start: f64, end: f64 }, - /// A text value Text { value: String }, - /// An empty value Empty, } /// Represents the metadata of the recipe type CooklangMetadata = HashMap; - /// Represents a list of ingredients + /// Represents a list of ingredients that are grouped by name and quantity type IngredientList = HashMap; - /// Represents a grouped quantity + /// Represents a grouped quantity for multiple unit types + // \ + // |- => 1.2 + // |- => half + // |- <,Text> => pinch + // |- <,Empty> => Some type GroupedQuantity = HashMap; /// Represents a grouped quantity key @@ -179,13 +178,9 @@ This library exports methods: /// Represents the type of the grouped quantity enum QuantityType { - /// Number type Number, - /// Range type Range, - /// Text type Text, - /// Empty type Empty, } ``` diff --git a/bindings/src/lib.rs b/bindings/src/lib.rs index 0f8ca56..cc32ac4 100644 --- a/bindings/src/lib.rs +++ b/bindings/src/lib.rs @@ -48,14 +48,11 @@ pub fn parse_metadata(input: String) -> CooklangMetadata { .unwrap_output(); // converting IndexMap into HashMap - let _ = &(parsed) - .iter() - .for_each(|(key, value)| match (key.as_str(), value.as_str()) { - (Some(key), Some(value)) => { - metadata.insert(key.to_string(), value.to_string()); - } - _ => {} - }); + let _ = &(parsed).iter().for_each(|(key, value)| { + if let (Some(key), Some(value)) = (key.as_str(), value.as_str()) { + metadata.insert(key.to_string(), value.to_string()); + } + }); metadata } @@ -125,7 +122,10 @@ pub fn combine_ingredients(ingredients: &Vec) -> IngredientList { } #[uniffi::export] -pub fn combine_ingredients_selected(ingredients: &Vec, indices: &Vec) -> IngredientList { +pub fn combine_ingredients_selected( + ingredients: &[Ingredient], + indices: &Vec, +) -> IngredientList { let mut combined: IngredientList = IngredientList::default(); expand_with_ingredients(ingredients, &mut combined, indices); @@ -167,11 +167,11 @@ a test @step @salt{1%mg} more text match recipe .sections .into_iter() - .nth(0) + .next() .expect("No blocks found") .blocks .into_iter() - .nth(0) + .next() .expect("No blocks found") { Block::Step(step) => step, @@ -377,7 +377,7 @@ Cook @onions{3%large} until brown let first_section = recipe .sections .into_iter() - .nth(0) + .next() .expect("No sections found"); assert_eq!(first_section.blocks.len(), 2); @@ -434,7 +434,7 @@ simmer for 10 minutes let first_section = recipe .sections .into_iter() - .nth(0) + .next() .expect("No sections found"); assert_eq!(first_section.blocks.len(), 2); diff --git a/bindings/src/model.rs b/bindings/src/model.rs index e2c9627..a80946c 100644 --- a/bindings/src/model.rs +++ b/bindings/src/model.rs @@ -167,6 +167,7 @@ pub enum Value { Empty, } +// TODO, should be more complex and support canonical keys pub type CooklangMetadata = HashMap; trait Amountable { @@ -216,7 +217,7 @@ fn extract_value(value: &OriginalValue) -> Value { } pub fn expand_with_ingredients( - ingredients: &Vec, + ingredients: &[Ingredient], base: &mut IngredientList, addition: &Vec, ) { @@ -245,9 +246,7 @@ pub fn merge_ingredient_lists(left: &mut IngredientList, right: &IngredientList) right .iter() .for_each(|(ingredient_name, grouped_quantity)| { - let quantity = left - .entry(ingredient_name.to_string()) - .or_insert(GroupedQuantity::default()); + let quantity = left.entry(ingredient_name.to_string()).or_default(); merge_grouped_quantities(quantity, grouped_quantity); }); @@ -384,7 +383,7 @@ pub(crate) fn into_simple_recipe(recipe: &OriginalRecipe) -> CooklangRecipe { items.push(item); } blocks.push(Block::Step(Step { - items: items, + items, ingredient_refs: step_ingredient_refs.clone(), cookware_refs: step_cookware_refs.clone(), timer_refs: step_timer_refs.clone(), @@ -405,9 +404,9 @@ pub(crate) fn into_simple_recipe(recipe: &OriginalRecipe) -> CooklangRecipe { sections.push(Section { title: section.name.clone(), blocks, - ingredient_refs: ingredient_refs, - cookware_refs: cookware_refs, - timer_refs: timer_refs, + ingredient_refs, + cookware_refs, + timer_refs, }); }