vessel 0.1.3

A context propogation struct. Carries cancellation, and other useful items transparently through an application
Documentation
use std::collections::BTreeMap;

#[derive(Default, Clone, Debug)]
pub struct Vessel {
    value_bag: BTreeMap<String, String>,
}

impl Vessel {
    pub fn new() -> Self {
        Self {
            value_bag: BTreeMap::default(),
        }
    }

    pub fn with_value(&self, key: &str, value: &str) -> Self {
        let mut value_bag = self.value_bag.clone();

        value_bag.insert(key.into(), value.into());

        Self { value_bag }
    }

    pub fn get_value(&self, key: &str) -> Option<&String> {
        self.value_bag.get(key)
    }

    pub fn get_values(&self) -> &BTreeMap<String, String> {
        &self.value_bag
    }
}

#[cfg(test)]
mod test {
    use std::collections::BTreeMap;

    use crate::Vessel;

    #[test]
    fn can_add_value() {
        let vessel = Vessel::default();

        let new_vessel = vessel.with_value("some-key", "some-value");

        assert_eq!(
            &BTreeMap::from([("some-key".into(), "some-value".into())]),
            new_vessel.get_values()
        )
    }
    #[test]
    fn is_immutable() {
        let vessel = Vessel::default();

        let new_vessel = vessel.with_value("some-key", "some-value");

        assert_ne!(vessel.get_values(), new_vessel.get_values())
    }

    #[test]
    fn get_value() {
        let vessel = Vessel::default();

        let new_vessel = vessel.with_value("some-key", "some-value");

        assert_eq!(
            Some(&"some-value".to_string()),
            new_vessel.get_value("some-key")
        )
    }
    #[test]
    fn value_not_found() {
        let vessel = Vessel::default();

        let new_vessel = vessel.with_value("some-key", "some-value");

        assert_eq!(None, new_vessel.get_value("bogus"))
    }
}