feat: add basic vessel

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

2
.drone.yml Normal file
View File

@ -0,0 +1,2 @@
kind: template
load: cuddle-rust-lib-plan.yaml

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
target/
.cuddle/

16
Cargo.lock generated Normal file
View File

@ -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",
]

8
Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[workspace]
members = ["crates/*"]
resolver = "2"
[workspace.dependencies]
vessel = { path = "crates/vessel" }
anyhow = { version = "1.0.71" }

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"))
}
}

17
cuddle.yaml Normal file
View File

@ -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: