Skip to content

Commit

Permalink
docs: updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
dubadub committed Dec 10, 2024
1 parent f221524 commit 9f60b08
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 49 deletions.
51 changes: 23 additions & 28 deletions bindings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<AisleConfig>;
Expand All @@ -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);
Expand All @@ -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<Section>,
/// List of all ingredients used in the recipe in order of use. Not quantity combined.
ingredients: Vec<Ingredient>,
/// List of all cookware used in the recipe
/// List of all cookware used in the recipe.
cookware: Vec<Cookware>,
/// List of all timers used in the recipe
/// List of all timers used in the recipe.
timers: Vec<Timer>,
}

/// 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<String>,
/// 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<Block>,
/// References to ingredients used in this section
/// Indices of ingredients used in this section.
ingredient_refs: Vec<u32>,
/// References to cookware used in this section
/// Indices of cookware used in this section.
cookware_refs: Vec<u32>,
/// References to timers used in this section
/// Indices of timers used in this section.
timer_refs: Vec<u32>,
}

Expand All @@ -90,11 +91,11 @@ This library exports methods:
struct Step {
/// List of items that make up this step (text and references)
items: Vec<Item>,
/// References to ingredients used in this step
/// Indices of ingredients used in this step
ingredient_refs: Vec<u32>,
/// References to cookware used in this step
/// Indices of cookware used in this step
cookware_refs: Vec<u32>,
/// References to timers used in this step
/// Indices of timers used in this step
timer_refs: Vec<u32>,
}

Expand All @@ -116,29 +117,26 @@ 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<Amount>,
}

/// Represents a timer in the recipe
struct Timer {
/// Optional timer name (e.g., "boiling", "baking", etc.)
name: Option<String>,
/// Optional quantity and units
amount: Option<Amount>,
}

/// Represents an item in the recipe
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 },
}

Expand All @@ -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<String, String>;
/// Represents a list of ingredients
/// Represents a list of ingredients that are grouped by name and quantity
type IngredientList = HashMap<String, GroupedQuantity>;
/// Represents a grouped quantity
/// Represents a grouped quantity for multiple unit types
// \
// |- <litre,Number> => 1.2
// |- <litre,Text> => half
// |- <,Text> => pinch
// |- <,Empty> => Some
type GroupedQuantity = HashMap<GroupedQuantityKey, Value>;

/// Represents a grouped quantity key
Expand All @@ -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,
}
```
Expand Down
26 changes: 13 additions & 13 deletions bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -125,7 +122,10 @@ pub fn combine_ingredients(ingredients: &Vec<Ingredient>) -> IngredientList {
}

#[uniffi::export]
pub fn combine_ingredients_selected(ingredients: &Vec<Ingredient>, indices: &Vec<u32>) -> IngredientList {
pub fn combine_ingredients_selected(
ingredients: &[Ingredient],
indices: &Vec<u32>,
) -> IngredientList {
let mut combined: IngredientList = IngredientList::default();

expand_with_ingredients(ingredients, &mut combined, indices);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
15 changes: 7 additions & 8 deletions bindings/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ pub enum Value {
Empty,
}

// TODO, should be more complex and support canonical keys
pub type CooklangMetadata = HashMap<String, String>;

trait Amountable {
Expand Down Expand Up @@ -216,7 +217,7 @@ fn extract_value(value: &OriginalValue) -> Value {
}

pub fn expand_with_ingredients(
ingredients: &Vec<Ingredient>,
ingredients: &[Ingredient],
base: &mut IngredientList,
addition: &Vec<ComponentRef>,
) {
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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(),
Expand All @@ -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,
});
}

Expand Down

0 comments on commit 9f60b08

Please sign in to comment.