2024-02-11 01:50:49 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2024-01-28 18:30:57 +00:00
|
|
|
use stanza::{
|
|
|
|
renderer::{console::Console, Renderer},
|
|
|
|
style::{HAlign, Styles},
|
|
|
|
table::{Row, Table},
|
|
|
|
};
|
2023-08-22 15:40:50 +00:00
|
|
|
use std::{
|
|
|
|
collections::BTreeMap,
|
|
|
|
fmt::{self, Display, Formatter},
|
|
|
|
};
|
|
|
|
|
2024-02-11 01:50:49 +00:00
|
|
|
use crate::value::Value;
|
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.
|
2024-02-11 01:50:49 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
|
2023-10-25 20:44:50 +00:00
|
|
|
pub struct Map {
|
2024-02-11 01:50:49 +00:00
|
|
|
inner: 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 {
|
2024-02-11 01:50:49 +00:00
|
|
|
inner: BTreeMap::new(),
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-11 01:50:49 +00:00
|
|
|
pub fn with_values(variables: BTreeMap<String, Value>) -> Self {
|
|
|
|
Map { inner: variables }
|
2024-01-23 20:35:26 +00:00
|
|
|
}
|
|
|
|
|
2024-02-11 01:50:49 +00:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.inner.len()
|
2024-01-17 15:21:00 +00:00
|
|
|
}
|
|
|
|
|
2024-02-11 01:50:49 +00:00
|
|
|
pub fn iter(&self) -> impl Iterator<Item = (&String, &Value)> {
|
|
|
|
self.inner.iter()
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
2024-02-11 01:50:49 +00:00
|
|
|
pub fn get(&self, key: &str) -> Option<&Value> {
|
|
|
|
self.inner.get(key)
|
2023-12-09 22:55:47 +00:00
|
|
|
}
|
2023-12-22 20:02:22 +00:00
|
|
|
|
2024-02-11 01:50:49 +00:00
|
|
|
pub fn set(&mut self, key: String, value: Value) {
|
|
|
|
self.inner.insert(key, value);
|
2024-01-17 19:45:34 +00:00
|
|
|
}
|
2024-01-28 18:30:57 +00:00
|
|
|
|
|
|
|
pub fn as_text_table(&self) -> Table {
|
|
|
|
let mut table = Table::with_styles(Styles::default().with(HAlign::Centred));
|
|
|
|
|
2024-02-11 01:50:49 +00:00
|
|
|
for (key, value) in &self.inner {
|
2024-01-28 18:30:57 +00:00
|
|
|
if let Value::Map(map) = value {
|
|
|
|
table.push_row(Row::new(
|
|
|
|
Styles::default(),
|
2024-02-11 01:50:49 +00:00
|
|
|
vec![
|
|
|
|
key.into(),
|
|
|
|
map.as_text_table().into(),
|
|
|
|
"".to_string().into(),
|
|
|
|
],
|
2024-01-28 18:30:57 +00:00
|
|
|
));
|
|
|
|
} else if let Value::List(list) = value {
|
|
|
|
table.push_row(Row::new(
|
|
|
|
Styles::default(),
|
2024-02-11 01:50:49 +00:00
|
|
|
vec![key.into(), list.as_text_table().into()],
|
2024-01-28 18:30:57 +00:00
|
|
|
));
|
|
|
|
} else {
|
2024-02-11 01:50:49 +00:00
|
|
|
table.push_row([key, &value.to_string()]);
|
2024-01-28 18:30:57 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if table.is_empty() {
|
|
|
|
table.push_row(vec!["", "empty map", ""])
|
|
|
|
}
|
|
|
|
|
|
|
|
table
|
|
|
|
}
|
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-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 {
|
2024-01-28 18:30:57 +00:00
|
|
|
let renderer = Console::default();
|
2023-10-26 20:00:06 +00:00
|
|
|
|
2024-01-28 18:30:57 +00:00
|
|
|
f.write_str(&renderer.render(&self.as_text_table()))
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
}
|