commit 4fc57350cafc4c2d71b45bb8172d703581d76f86 Author: kjuulh Date: Wed May 21 08:12:54 2025 +0200 feat: add basic vessel diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..7d36638 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,2 @@ +kind: template +load: cuddle-rust-lib-plan.yaml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9c4c004 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target/ +.cuddle/ diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..1a6fb8e --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "anyhow" +version = "1.0.71" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8" + +[[package]] +name = "vessel" +version = "0.1.0" +dependencies = [ + "anyhow", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..a06ad5d --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[workspace] +members = ["crates/*"] +resolver = "2" + +[workspace.dependencies] +vessel = { path = "crates/vessel" } + +anyhow = { version = "1.0.71" } diff --git a/crates/vessel/Cargo.toml b/crates/vessel/Cargo.toml new file mode 100644 index 0000000..d590fb5 --- /dev/null +++ b/crates/vessel/Cargo.toml @@ -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 diff --git a/crates/vessel/src/lib.rs b/crates/vessel/src/lib.rs new file mode 100644 index 0000000..43167cc --- /dev/null +++ b/crates/vessel/src/lib.rs @@ -0,0 +1,77 @@ +use std::collections::BTreeMap; + +#[derive(Default, Clone, Debug)] +pub struct Vessel { + value_bag: BTreeMap, +} + +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 { + &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")) + } +} diff --git a/cuddle.yaml b/cuddle.yaml new file mode 100644 index 0000000..e792a62 --- /dev/null +++ b/cuddle.yaml @@ -0,0 +1,17 @@ +# yaml-language-server: $schema=https://git.front.kjuulh.io/kjuulh/cuddle/raw/branch/main/schemas/base.json + +base: "git@git.front.kjuulh.io:kjuulh/cuddle-rust-lib-plan.git" + +vars: + service: "vessel" + registry: kasperhermansen + +please: + project: + owner: kjuulh + repository: "vessel" + branch: main + settings: + api_url: "https://git.front.kjuulh.io" + actions: + rust: