use serde::{ de::{MapAccess, Visitor}, ser::SerializeMap, Deserialize, Serialize, }; use stanza::{ renderer::{console::Console, Renderer}, style::{HAlign, Styles}, table::{Row, Table}, }; use std::{ cmp::Ordering, collections::BTreeMap, fmt::{self, Display, Formatter}, marker::PhantomData, sync::{Arc, RwLock, RwLockReadGuard}, }; use crate::{value::Value, Result, Structure, Type}; /// 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. #[derive(Clone, Debug)] pub struct Map { variables: Arc>>, structure: Option, } impl Map { /// Creates a new instace. pub fn new() -> Self { Map { variables: Arc::new(RwLock::new(BTreeMap::new())), structure: None, } } pub fn from_structure(structure: Structure) -> Self { let mut variables = BTreeMap::new(); for (key, (value_option, r#type)) in structure.inner() { variables.insert( key.clone(), ( value_option.clone().unwrap_or(Value::none()), r#type.clone(), ), ); } Map { variables: Arc::new(RwLock::new(variables)), structure: Some(structure), } } pub fn with_variables(variables: BTreeMap) -> Self { Map { variables: Arc::new(RwLock::new(variables)), structure: None, } } pub fn clone_from(other: &Self) -> Result { let mut new_map = BTreeMap::new(); for (key, (value, r#type)) in other.variables()?.iter() { new_map.insert(key.clone(), (value.clone(), r#type.clone())); } Ok(Map { variables: Arc::new(RwLock::new(new_map)), structure: other.structure.clone(), }) } pub fn variables(&self) -> Result>> { Ok(self.variables.read()?) } pub fn set(&self, key: String, value: Value) -> Result> { log::info!("Setting variable {key} = {value}"); let value_type = value.r#type(); let previous = self .variables .write()? .insert(key, (value, value_type.clone())); Ok(previous) } pub fn set_type(&self, key: String, r#type: Type) -> Result> { log::info!("Setting type {key} = {}", r#type); let previous = self.variables.write()?.insert(key, (Value::none(), r#type)); Ok(previous) } pub fn as_text_table(&self) -> Table { let variables = self.variables.read().unwrap().clone().into_iter(); let mut table = Table::with_styles(Styles::default().with(HAlign::Centred)); for (key, (value, r#type)) in variables { if let Value::Map(map) = value { table.push_row(Row::new( Styles::default(), vec![key.into(), map.as_text_table().into(), "".into()], )); } else if let Value::List(list) = value { table.push_row(Row::new( Styles::default(), vec![ key.into(), list.as_text_table().into(), r#type.to_string().into(), ], )); } else { table.push_row([key, value.to_string(), r#type.to_string()]); }; } if table.is_empty() { table.push_row(vec!["", "empty map", ""]) } table } } impl Default for Map { fn default() -> Self { Self::new() } } 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 { Some(self.cmp(other)) } } impl Display for Map { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { let renderer = Console::default(); f.write_str(&renderer.render(&self.as_text_table())) } } impl Serialize for Map { fn serialize(&self, serializer: S) -> std::result::Result where S: serde::Serializer, { let variables = self.variables.read().unwrap(); let mut map = serializer.serialize_map(Some(variables.len()))?; for (key, (value, _type)) in variables.iter() { map.serialize_entry(key, value)?; } map.end() } } struct MapVisitor { marker: PhantomData Map>, } impl MapVisitor { fn new() -> Self { MapVisitor { marker: PhantomData, } } } impl<'de> Visitor<'de> for MapVisitor { type Value = Map; fn expecting(&self, formatter: &mut Formatter) -> fmt::Result { formatter.write_str("Any valid whale data.") } fn visit_map(self, mut access: M) -> std::result::Result where M: MapAccess<'de>, { let map = Map::new(); { while let Some((key, value)) = access.next_entry::()? { map.set(key, value).unwrap(); } } Ok(map) } } impl<'de> Deserialize<'de> for Map { fn deserialize(deserializer: D) -> std::result::Result where D: serde::Deserializer<'de>, { deserializer.deserialize_any(MapVisitor::new()) } }