1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use crate::input_field::InputFieldIngredient;
use crate::{AsId, Durability, Runtime};
use std::hash::Hash;

#[must_use]
pub struct Setter<'setter, K, F> {
    runtime: &'setter mut Runtime,
    key: K,
    ingredient: &'setter mut InputFieldIngredient<K, F>,
    durability: Durability,
}

impl<'setter, K, F> Setter<'setter, K, F>
where
    K: Eq + Hash + AsId,
{
    pub fn new(
        runtime: &'setter mut Runtime,
        key: K,
        ingredient: &'setter mut InputFieldIngredient<K, F>,
    ) -> Self {
        Setter {
            runtime,
            key,
            ingredient,
            durability: Durability::LOW,
        }
    }

    pub fn with_durability(self, durability: Durability) -> Self {
        Setter { durability, ..self }
    }

    pub fn to(self, value: F) -> F {
        self.ingredient
            .store_mut(self.runtime, self.key, value, self.durability)
            .unwrap()
    }
}