more-options 4.0.0

Provides support for options
Documentation
use crate::{validation::Error, Cache, Factory, Ref, Snapshot, Value};

/// Represents an object that manages options and [option snapshots](Snapshot).
pub struct Manager<T: Value> {
    factory: Ref<dyn Factory<T>>,
    cache: Cache<T>,
}

impl<T: Value> Manager<T> {
    /// Initializes a new options manager.
    ///
    /// # Arguments
    ///
    /// * `factory` - The [factory](Factory) used to create new options
    #[inline]
    pub fn new(factory: Ref<dyn Factory<T>>) -> Self {
        Self {
            factory,
            cache: Default::default(),
        }
    }
}

impl<T: Value> Snapshot<T> for Manager<T> {
    fn get_named(&self, name: &str) -> Result<Ref<T>, Error> {
        self.cache.get_or_add(name, &|n| self.factory.create(n))
    }
}