use crate::Deadline;
use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
Str(String),
Int(i64),
Float(f64),
Bool(bool),
}
impl Value {
pub fn as_str(&self) -> Option<&str> {
match self {
Value::Str(text) => Some(text),
_ => None,
}
}
pub fn as_int(&self) -> Option<i64> {
match self {
Value::Int(number) => Some(*number),
_ => None,
}
}
pub fn as_float(&self) -> Option<f64> {
match self {
Value::Float(number) => Some(*number),
_ => None,
}
}
pub fn as_bool(&self) -> Option<bool> {
match self {
Value::Bool(flag) => Some(*flag),
_ => None,
}
}
}
impl From<String> for Value {
fn from(value: String) -> Self {
Value::Str(value)
}
}
impl From<&str> for Value {
fn from(value: &str) -> Self {
Value::Str(value.to_owned())
}
}
impl From<i64> for Value {
fn from(value: i64) -> Self {
Value::Int(value)
}
}
impl From<f64> for Value {
fn from(value: f64) -> Self {
Value::Float(value)
}
}
impl From<bool> for Value {
fn from(value: bool) -> Self {
Value::Bool(value)
}
}
#[derive(Debug, Clone, Default)]
pub struct Context {
metadata: HashMap<String, Value>,
deadline: Option<Deadline>,
}
impl Context {
pub fn new() -> Self {
Context::default()
}
#[must_use]
pub fn with_deadline(mut self, deadline: Deadline) -> Self {
self.deadline = Some(deadline);
self
}
#[must_use]
pub fn with(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
self.insert(key, value);
self
}
pub fn insert(&mut self, key: impl Into<String>, value: impl Into<Value>) -> Option<Value> {
self.metadata.insert(key.into(), value.into())
}
pub fn set_deadline(&mut self, deadline: Option<Deadline>) {
self.deadline = deadline;
}
pub fn get(&self, key: &str) -> Option<&Value> {
self.metadata.get(key)
}
pub fn get_str(&self, key: &str) -> Option<&str> {
self.get(key).and_then(Value::as_str)
}
pub fn get_int(&self, key: &str) -> Option<i64> {
self.get(key).and_then(Value::as_int)
}
pub fn get_float(&self, key: &str) -> Option<f64> {
self.get(key).and_then(Value::as_float)
}
pub fn get_bool(&self, key: &str) -> Option<bool> {
self.get(key).and_then(Value::as_bool)
}
pub fn contains(&self, key: &str) -> bool {
self.metadata.contains_key(key)
}
pub fn iter(&self) -> impl Iterator<Item = (&str, &Value)> {
self.metadata
.iter()
.map(|(key, value)| (key.as_str(), value))
}
pub fn len(&self) -> usize {
self.metadata.len()
}
pub fn is_empty(&self) -> bool {
self.metadata.is_empty()
}
pub fn deadline(&self) -> Option<Deadline> {
self.deadline
}
pub fn is_expired(&self) -> bool {
self.deadline.is_some_and(|deadline| deadline.is_expired())
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;
#[test]
fn builder_round_trips_every_value_type() {
let ctx = Context::new()
.with("tenant", "acme")
.with("retries", 3i64)
.with("score", 0.5f64)
.with("trusted", true);
assert_eq!(ctx.get_str("tenant"), Some("acme"));
assert_eq!(ctx.get_int("retries"), Some(3));
assert_eq!(ctx.get_float("score"), Some(0.5));
assert_eq!(ctx.get_bool("trusted"), Some(true));
assert_eq!(ctx.len(), 4);
assert!(!ctx.is_empty());
}
#[test]
fn typed_lookups_do_not_coerce_across_types() {
let ctx = Context::new().with("retries", 3i64);
assert_eq!(ctx.get_int("retries"), Some(3));
assert_eq!(ctx.get_str("retries"), None);
assert_eq!(ctx.get_bool("retries"), None);
assert!(ctx.contains("retries"));
}
#[test]
fn a_missing_key_is_none_everywhere() {
let ctx = Context::new();
assert!(ctx.is_empty());
assert_eq!(ctx.get("absent"), None);
assert_eq!(ctx.get_str("absent"), None);
assert_eq!(ctx.get_int("absent"), None);
assert!(!ctx.contains("absent"));
}
#[test]
fn insert_replaces_and_returns_the_old_value() {
let mut ctx = Context::new().with("tier", "free");
let previous = ctx.insert("tier", "paid");
assert_eq!(previous, Some(Value::Str("free".to_owned())));
assert_eq!(ctx.get_str("tier"), Some("paid"));
assert_eq!(ctx.len(), 1);
}
#[test]
fn a_context_without_a_deadline_never_expires() {
let ctx = Context::new();
assert_eq!(ctx.deadline(), None);
assert!(!ctx.is_expired());
}
#[test]
fn expiry_tracks_the_deadline() {
let live = Context::new().with_deadline(Deadline::after(Duration::from_secs(60)));
assert!(!live.is_expired());
let dead = Context::new().with_deadline(Deadline::after(Duration::ZERO));
assert!(dead.is_expired());
}
#[test]
fn set_deadline_can_clear_an_existing_one() {
let mut ctx = Context::new().with_deadline(Deadline::after(Duration::ZERO));
assert!(ctx.is_expired());
ctx.set_deadline(None);
assert!(!ctx.is_expired());
assert_eq!(ctx.deadline(), None);
}
#[test]
fn iter_yields_every_entry() {
let ctx = Context::new().with("a", 1i64).with("b", 2i64);
let mut entries: Vec<_> = ctx
.iter()
.map(|(key, value)| (key, value.clone()))
.collect();
entries.sort_by_key(|(key, _)| *key);
assert_eq!(entries, vec![("a", Value::Int(1)), ("b", Value::Int(2))]);
}
}