1
0
dust/src/context.rs

40 lines
875 B
Rust
Raw Normal View History

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