2024-02-25 18:49:26 +00:00
|
|
|
use std::{
|
|
|
|
collections::BTreeMap,
|
|
|
|
sync::{Arc, RwLock},
|
|
|
|
};
|
|
|
|
|
2024-02-26 21:27:01 +00:00
|
|
|
use crate::{error::RwLockPoisonError, Value};
|
2024-02-25 18:49:26 +00:00
|
|
|
|
|
|
|
pub struct Context {
|
2024-02-26 21:27:01 +00:00
|
|
|
inner: Arc<RwLock<BTreeMap<String, Value>>>,
|
2024-02-25 18:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Context {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
inner: Arc::new(RwLock::new(BTreeMap::new())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-26 21:27:01 +00:00
|
|
|
pub fn with_values(values: BTreeMap<String, Value>) -> Self {
|
2024-02-25 18:49:26 +00:00
|
|
|
Self {
|
|
|
|
inner: Arc::new(RwLock::new(values)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-26 21:27:01 +00:00
|
|
|
pub fn get(&self, key: &str) -> Result<Option<Value>, RwLockPoisonError> {
|
|
|
|
let value = self.inner.read()?.get(key).cloned();
|
2024-02-25 18:49:26 +00:00
|
|
|
|
|
|
|
Ok(value)
|
|
|
|
}
|
|
|
|
|
2024-02-26 21:27:01 +00:00
|
|
|
pub fn set(&self, key: String, value: Value) -> Result<(), RwLockPoisonError> {
|
|
|
|
self.inner.write()?.insert(key, value);
|
2024-02-25 18:49:26 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|