Skip to main content

IngredientDatabase

Struct IngredientDatabase 

Source
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

Source

pub fn new_seeded(ingredients: &[Ingredient]) -> Self

Creates a new IngredientDatabase seeded with the provided Ingredients.

Source

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.

Source

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.

Source

pub fn seed(&self, ingredients: &[Ingredient])

Seeds the database with the provided Ingredients.

Source

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.

Source

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

Source

pub fn new() -> Self

Creates a new, empty IngredientDatabase.

Source

pub fn get_all_ingredients(&self) -> Vec<Ingredient>

Retrieves all Ingredients in the database.

Source

pub fn get_ingredients_by_category(&self, category: Category) -> Vec<Ingredient>

Retrieves Ingredients filtered by the specified Category.

Trait Implementations§

Source§

impl Debug for IngredientDatabase

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for IngredientDatabase

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.