feat: add basic vessel

This commit is contained in:
2025-05-21 08:12:54 +02:00
commit 4fc57350ca
7 changed files with 131 additions and 0 deletions

9
crates/vessel/Cargo.toml Normal file
View File

@@ -0,0 +1,9 @@
[package]
name = "vessel"
version = "0.1.0"
edition = "2024"
description = "A context propogation struct. Carries cancellation, and other useful items transparently through an application"
license = "MIT"
[dependencies]
anyhow.workspace = true

77
crates/vessel/src/lib.rs Normal file
View File

@@ -0,0 +1,77 @@
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"))
}
}