1
0
dust/src/context.rs

85 lines
2.1 KiB
Rust
Raw Normal View History

2024-02-10 23:29:11 +00:00
use std::{
collections::HashMap,
2024-02-11 00:31:47 +00:00
sync::{Arc, RwLock},
2024-02-10 23:29:11 +00:00
};
use crate::{error::rw_lock_error::RwLockError, Type, Value};
#[derive(Clone)]
pub enum ValueData {
Value {
inner: Value,
runtime_uses: Arc<RwLock<u16>>,
},
ExpectedType {
inner: Type,
},
}
pub struct Context {
inner: Arc<RwLock<HashMap<String, ValueData>>>,
}
impl Context {
pub fn new() -> Self {
Self {
inner: Arc::new(RwLock::new(HashMap::new())),
}
}
pub fn inherit_from(other: &Context) -> Result<Context, RwLockError> {
let mut new_variables = HashMap::new();
2024-02-11 00:31:47 +00:00
for (identifier, value_data) in other.inner.read()?.iter() {
2024-02-10 23:29:11 +00:00
new_variables.insert(identifier.clone(), value_data.clone());
}
Ok(Context {
inner: Arc::new(RwLock::new(new_variables)),
})
}
2024-02-11 00:31:47 +00:00
pub fn get_value(&self, key: &str) -> Result<Option<Value>, RwLockError> {
2024-02-10 23:29:11 +00:00
if let Some(value_data) = self.inner.read()?.get(key) {
if let ValueData::Value { inner, .. } = value_data {
2024-02-11 00:31:47 +00:00
Ok(Some(inner.clone()))
2024-02-10 23:29:11 +00:00
} else {
Ok(None)
}
} else {
Ok(None)
}
}
pub fn get_type(&self, key: &str) -> Result<Option<Type>, RwLockError> {
if let Some(value_data) = self.inner.read()?.get(key) {
match value_data {
ValueData::Value { inner, .. } => Ok(Some(inner.r#type())),
ValueData::ExpectedType { inner, .. } => Ok(Some(inner.clone())),
}
} else {
Ok(None)
}
}
pub fn set_value(&self, key: String, value: Value) -> Result<(), RwLockError> {
self.inner.write()?.insert(
key,
ValueData::Value {
inner: value,
runtime_uses: Arc::new(RwLock::new(0)),
},
);
Ok(())
}
pub fn set_type(&self, key: String, r#type: Type) -> Result<(), RwLockError> {
self.inner
.write()?
.insert(key, ValueData::ExpectedType { inner: r#type });
Ok(())
}
}