2024-02-10 23:29:11 +00:00
|
|
|
use std::{
|
2024-02-11 19:10:11 +00:00
|
|
|
cmp::Ordering,
|
|
|
|
collections::BTreeMap,
|
2024-02-11 01:50:49 +00:00
|
|
|
sync::{Arc, RwLock, RwLockReadGuard},
|
2024-02-10 23:29:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
use crate::{error::rw_lock_error::RwLockError, Type, Value};
|
|
|
|
|
2024-02-11 20:26:09 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2024-02-10 23:29:11 +00:00
|
|
|
pub struct Context {
|
2024-02-11 19:10:11 +00:00
|
|
|
inner: Arc<RwLock<BTreeMap<String, ValueData>>>,
|
2024-02-10 23:29:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Context {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
2024-02-11 19:10:11 +00:00
|
|
|
inner: Arc::new(RwLock::new(BTreeMap::new())),
|
2024-02-10 23:29:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-11 19:10:11 +00:00
|
|
|
pub fn inner(&self) -> Result<RwLockReadGuard<BTreeMap<String, ValueData>>, RwLockError> {
|
2024-02-11 01:50:49 +00:00
|
|
|
Ok(self.inner.read()?)
|
|
|
|
}
|
|
|
|
|
2024-02-11 20:26:09 +00:00
|
|
|
pub fn with_variables_from(other: &Context) -> Result<Context, RwLockError> {
|
2024-02-11 19:10:11 +00:00
|
|
|
let mut new_variables = BTreeMap::new();
|
2024-02-10 23:29:11 +00:00
|
|
|
|
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 20:26:09 +00:00
|
|
|
pub fn inherit_from(&self, other: &Context) -> Result<(), RwLockError> {
|
|
|
|
let mut self_variables = self.inner.write()?;
|
|
|
|
|
|
|
|
for (identifier, value_data) in other.inner.read()?.iter() {
|
2024-02-12 21:51:06 +00:00
|
|
|
let existing_data = self_variables.get(identifier);
|
|
|
|
|
|
|
|
if let Some(ValueData::Value { .. }) = existing_data {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
self_variables.insert(identifier.clone(), value_data.clone());
|
|
|
|
}
|
2024-02-11 20:26:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
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-13 13:10:34 +00:00
|
|
|
return Ok(Some(inner.clone()));
|
2024-02-10 23:29:11 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-13 13:10:34 +00:00
|
|
|
|
|
|
|
Ok(None)
|
2024-02-10 23:29:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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(())
|
|
|
|
}
|
2024-02-12 20:48:43 +00:00
|
|
|
|
|
|
|
pub fn unset(&self, key: &str) -> Result<(), RwLockError> {
|
|
|
|
self.inner.write()?.remove(key);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-02-10 23:29:11 +00:00
|
|
|
}
|
2024-02-11 18:54:27 +00:00
|
|
|
|
2024-02-11 19:10:11 +00:00
|
|
|
impl Default for Context {
|
|
|
|
fn default() -> Self {
|
|
|
|
Context::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-11 18:54:27 +00:00
|
|
|
impl Eq for Context {}
|
|
|
|
|
|
|
|
impl PartialEq for Context {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
let self_variables = self.inner().unwrap();
|
|
|
|
let other_variables = other.inner().unwrap();
|
|
|
|
|
|
|
|
if self_variables.len() != other_variables.len() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ((left_key, left_value_data), (right_key, right_value_data)) in
|
|
|
|
self_variables.iter().zip(other_variables.iter())
|
|
|
|
{
|
|
|
|
if left_key != right_key || left_value_data != right_value_data {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd for Context {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for Context {
|
2024-02-11 19:10:11 +00:00
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
let left = self.inner().unwrap();
|
|
|
|
let right = other.inner().unwrap();
|
2024-02-11 18:54:27 +00:00
|
|
|
|
2024-02-11 19:10:11 +00:00
|
|
|
left.cmp(&right)
|
2024-02-11 18:54:27 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-12 20:48:43 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum ValueData {
|
|
|
|
Value {
|
|
|
|
inner: Value,
|
|
|
|
runtime_uses: Arc<RwLock<u16>>,
|
|
|
|
},
|
|
|
|
ExpectedType {
|
|
|
|
inner: Type,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for ValueData {}
|
|
|
|
|
|
|
|
impl PartialEq for ValueData {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
match (self, other) {
|
|
|
|
(
|
|
|
|
ValueData::Value {
|
|
|
|
inner: left_inner,
|
|
|
|
runtime_uses: left_runtime_uses,
|
|
|
|
},
|
|
|
|
ValueData::Value {
|
|
|
|
inner: right_inner,
|
|
|
|
runtime_uses: right_runtime_uses,
|
|
|
|
},
|
|
|
|
) => {
|
|
|
|
if left_inner != right_inner {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
*left_runtime_uses.read().unwrap() == *right_runtime_uses.read().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(
|
|
|
|
ValueData::ExpectedType { inner: left_inner },
|
|
|
|
ValueData::ExpectedType { inner: right_inner },
|
|
|
|
) => left_inner == right_inner,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd for ValueData {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for ValueData {
|
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
use Ordering::*;
|
|
|
|
|
|
|
|
match (self, other) {
|
|
|
|
(
|
|
|
|
ValueData::Value {
|
|
|
|
inner: inner_left, ..
|
|
|
|
},
|
|
|
|
ValueData::Value {
|
|
|
|
inner: inner_right, ..
|
|
|
|
},
|
|
|
|
) => inner_left.cmp(inner_right),
|
|
|
|
(ValueData::Value { .. }, _) => Greater,
|
|
|
|
(
|
|
|
|
ValueData::ExpectedType { inner: inner_left },
|
|
|
|
ValueData::ExpectedType { inner: inner_right },
|
|
|
|
) => inner_left.cmp(inner_right),
|
|
|
|
(ValueData::ExpectedType { .. }, _) => Less,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|