pub struct IngredientDatabase { /* private fields */ }Expand description
Provides an in-memory database for looking up ingredient definitions by name
Since ingredient objects are lightweight, in most use cases keeping many or all of them in
memory should not be an issue. Holding them in this IngredientDatabase greatly simplifies
the setup and process of looking up ingredient definitions, obviating the need for lookups from
an external database. It should also provide performance improvements.
Lastly, and the primary motivation behind this class, it’s supported in WASM, where it can provide significant performance improvements if ingredient lookup is done on the WASM side, compared to managing ingredient definitions and lookup on the JS side and bridging them to WASM when requesting operations; JS <-> WASM bridging is very slow, so it’s almost always more performant to keep as much as possible on the WASM side. It’s still possible to seed the database from the JS side, then subsequent looks can be done within WASM.
Implementations§
Source§impl IngredientDatabase
impl IngredientDatabase
Sourcepub fn new_seeded(ingredients: &[Ingredient]) -> Self
pub fn new_seeded(ingredients: &[Ingredient]) -> Self
Creates a new IngredientDatabase seeded with the provided Ingredients.
Sourcepub fn new_seeded_from_specs(specs: &[IngredientSpec]) -> Result<Self>
pub fn new_seeded_from_specs(specs: &[IngredientSpec]) -> Result<Self>
Creates a new IngredientDatabase seeded with the provided IngredientSpecs
§Errors
Returns an Error if any of the provided specs cannot be converted into an
Ingredient. This would likely be an error converting a spec into a
Composition due to invalid values, e.g. negative percentages, not summing to 100%, etc.
Sourcepub fn new_seeded_from_embedded_data() -> Self
pub fn new_seeded_from_embedded_data() -> Self
Creates a new IngredientDatabase seeded with all embedded ingredient specifications.
This function requires the data feature to be enabled.
Sourcepub fn seed(&self, ingredients: &[Ingredient])
pub fn seed(&self, ingredients: &[Ingredient])
Seeds the database with the provided Ingredients.
Sourcepub fn seed_from_specs(&self, specs: &[IngredientSpec]) -> Result<()>
pub fn seed_from_specs(&self, specs: &[IngredientSpec]) -> Result<()>
Seeds the database with the provided IngredientSpecs.
§Errors
Returns an Error if any of the provided specs cannot be converted into an
Ingredient. This would likely be an error converting a spec into a
Composition due to invalid values, e.g. negative percentages, not summing to 100%, etc.
Sourcepub fn get_ingredient_by_name(&self, name: &str) -> Result<Ingredient>
pub fn get_ingredient_by_name(&self, name: &str) -> Result<Ingredient>
Retrieves an Ingredient by its name.
§Errors
Returns an Error::IngredientNotFound if no ingredient with the specified name is found.
Source§impl IngredientDatabase
impl IngredientDatabase
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new, empty IngredientDatabase.
Sourcepub fn get_all_ingredients(&self) -> Vec<Ingredient>
pub fn get_all_ingredients(&self) -> Vec<Ingredient>
Retrieves all Ingredients in the database.
Sourcepub fn get_ingredients_by_category(&self, category: Category) -> Vec<Ingredient>
pub fn get_ingredients_by_category(&self, category: Category) -> Vec<Ingredient>
Retrieves Ingredients filtered by the specified Category.