2023-10-27 01:23:39 +00:00
|
|
|
use serde::Serialize;
|
2023-08-22 15:40:50 +00:00
|
|
|
use std::{
|
2023-10-26 20:00:06 +00:00
|
|
|
cmp::Ordering,
|
2023-08-22 15:40:50 +00:00
|
|
|
collections::BTreeMap,
|
|
|
|
fmt::{self, Display, Formatter},
|
2023-10-29 23:31:06 +00:00
|
|
|
sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard},
|
2023-08-22 15:40:50 +00:00
|
|
|
};
|
|
|
|
|
2023-10-29 23:31:06 +00:00
|
|
|
use crate::{value::Value, List, Table};
|
2023-08-22 15:40:50 +00:00
|
|
|
|
2023-08-24 04:26:37 +00:00
|
|
|
/// A collection dust variables comprised of key-value pairs.
|
|
|
|
///
|
|
|
|
/// The inner value is a BTreeMap in order to allow VariableMap instances to be sorted and compared
|
|
|
|
/// to one another.
|
2023-10-26 20:00:06 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2023-10-25 20:44:50 +00:00
|
|
|
pub struct Map {
|
2023-10-26 20:00:06 +00:00
|
|
|
variables: Arc<RwLock<BTreeMap<String, Value>>>,
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
2023-10-25 20:44:50 +00:00
|
|
|
impl Map {
|
2023-08-22 15:40:50 +00:00
|
|
|
/// Creates a new instace.
|
|
|
|
pub fn new() -> Self {
|
2023-10-25 20:44:50 +00:00
|
|
|
Map {
|
2023-10-26 20:00:06 +00:00
|
|
|
variables: Arc::new(RwLock::new(BTreeMap::new())),
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-27 02:35:59 +00:00
|
|
|
pub fn clone_from(other: &Self) -> Self {
|
|
|
|
let mut new_map = BTreeMap::new();
|
|
|
|
|
2023-10-30 22:30:41 +00:00
|
|
|
for (key, value) in other.variables().iter() {
|
2023-10-27 02:35:59 +00:00
|
|
|
new_map.insert(key.clone(), value.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
Map {
|
|
|
|
variables: Arc::new(RwLock::new(new_map)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-29 23:31:06 +00:00
|
|
|
pub fn variables(&self) -> RwLockReadGuard<BTreeMap<String, Value>> {
|
|
|
|
self.variables.read().unwrap()
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
2023-10-29 23:31:06 +00:00
|
|
|
pub fn variables_mut(&self) -> RwLockWriteGuard<BTreeMap<String, Value>> {
|
|
|
|
self.variables.write().unwrap()
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the number of stored variables.
|
|
|
|
pub fn len(&self) -> usize {
|
2023-10-26 20:00:06 +00:00
|
|
|
self.variables.read().unwrap().len()
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if the length is zero.
|
|
|
|
pub fn is_empty(&self) -> bool {
|
2023-10-26 20:00:06 +00:00
|
|
|
self.variables.read().unwrap().is_empty()
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-25 20:44:50 +00:00
|
|
|
impl Default for Map {
|
2023-08-22 15:40:50 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 20:00:06 +00:00
|
|
|
impl Eq for Map {}
|
|
|
|
|
|
|
|
impl PartialEq for Map {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
let left = self.variables.read().unwrap().clone().into_iter();
|
|
|
|
let right = other.variables.read().unwrap().clone().into_iter();
|
|
|
|
|
|
|
|
left.eq(right)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for Map {
|
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
let left = self.variables.read().unwrap().clone().into_iter();
|
|
|
|
let right = other.variables.read().unwrap().clone().into_iter();
|
|
|
|
|
|
|
|
left.cmp(right)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd for Map {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
let left = self.variables.read().unwrap().clone().into_iter();
|
|
|
|
let right = other.variables.read().unwrap().clone().into_iter();
|
|
|
|
|
|
|
|
left.partial_cmp(right)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-25 20:44:50 +00:00
|
|
|
impl Display for Map {
|
2023-08-22 15:40:50 +00:00
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
2023-10-30 19:48:43 +00:00
|
|
|
writeln!(f, "{{")?;
|
2023-10-26 20:00:06 +00:00
|
|
|
|
|
|
|
let variables = self.variables.read().unwrap().clone().into_iter();
|
|
|
|
|
|
|
|
for (key, value) in variables {
|
2023-10-30 19:48:43 +00:00
|
|
|
writeln!(f, " {key} = {value}")?;
|
2023-10-14 18:18:13 +00:00
|
|
|
}
|
|
|
|
write!(f, "}}")
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-25 20:44:50 +00:00
|
|
|
impl From<&Table> for Map {
|
2023-08-22 15:40:50 +00:00
|
|
|
fn from(value: &Table) -> Self {
|
2023-10-29 23:31:06 +00:00
|
|
|
let map = Map::new();
|
2023-08-22 15:40:50 +00:00
|
|
|
|
|
|
|
for (row_index, row) in value.rows().iter().enumerate() {
|
2023-10-29 23:31:06 +00:00
|
|
|
map.variables_mut()
|
|
|
|
.insert(
|
|
|
|
row_index.to_string(),
|
|
|
|
Value::List(List::with_items(row.clone())),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
map
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-25 20:44:50 +00:00
|
|
|
impl Serialize for Map {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: serde::Serializer,
|
|
|
|
{
|
|
|
|
self.variables.serialize(serializer)
|
|
|
|
}
|
|
|
|
}
|