basalt_api/recipes/handle.rs
1//! Long-lived handle to the recipe registry.
2//!
3//! [`RecipeRegistryHandle`] abstracts the mutable recipe registry so
4//! that [`RecipeRegistrar`](super::RecipeRegistrar) and
5//! [`PluginRegistrar`](crate::PluginRegistrar) reference a trait object
6//! instead of a concrete registry type. The concrete implementation
7//! lives in `basalt-recipes`.
8
9use crate::recipes::{OwnedShapedRecipe, OwnedShapelessRecipe, Recipe, RecipeId};
10
11/// Long-lived handle to the recipe registry runtime.
12///
13/// Implemented by `basalt_recipes::RecipeRegistry` (production) and
14/// mock types (tests). Plugins receive a `&mut dyn
15/// RecipeRegistryHandle` indirectly via
16/// [`RecipeRegistrar`](super::RecipeRegistrar).
17///
18/// Methods mirror the inherent API of `RecipeRegistry` — see each
19/// method's doc for semantics.
20pub trait RecipeRegistryHandle {
21 /// Inserts a shaped recipe.
22 ///
23 /// The caller is responsible for id uniqueness. No duplicate
24 /// checking is performed.
25 fn add_shaped(&mut self, recipe: OwnedShapedRecipe);
26
27 /// Inserts a shapeless recipe.
28 ///
29 /// The caller is responsible for sorting `recipe.ingredients`
30 /// ascending and for id uniqueness.
31 fn add_shapeless(&mut self, recipe: OwnedShapelessRecipe);
32
33 /// Removes the recipe with the given id.
34 ///
35 /// Searches both shaped and shapeless registries. Returns the
36 /// removed recipe or `None` if not present.
37 fn remove_by_id(&mut self, id: &RecipeId) -> Option<Recipe>;
38
39 /// Removes every recipe (shaped and shapeless) producing the given
40 /// `result_id`.
41 ///
42 /// Returns the ids of the removed recipes in registry order —
43 /// first shaped, then shapeless.
44 fn remove_by_result(&mut self, result_id: i32) -> Vec<RecipeId>;
45
46 /// Removes every recipe and returns their ids.
47 ///
48 /// Returned in registry order — first shaped, then shapeless.
49 fn clear(&mut self) -> Vec<RecipeId>;
50
51 /// Returns `true` if a recipe with the given id is registered.
52 fn contains(&self, id: &RecipeId) -> bool;
53
54 /// Returns a clone of the recipe with the given id, or `None`.
55 ///
56 /// Searches shaped recipes first then shapeless.
57 fn find_by_id(&self, id: &RecipeId) -> Option<Recipe>;
58
59 /// Returns the number of registered shaped recipes.
60 fn shaped_count(&self) -> usize;
61
62 /// Returns the number of registered shapeless recipes.
63 fn shapeless_count(&self) -> usize;
64}