use std::collections::BTreeMap;
use renox::grid::{Column, Kind};
use serde::Serialize;
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct Entry {
key: String,
label: String,
format: Option<String>,
labels: BTreeMap<String, String>,
badge: bool,
copyable: bool,
span: Option<String>,
}
impl Entry {
pub fn new(key: &str, label: &str) -> Self {
Self {
key: key.to_owned(),
label: label.to_owned(),
format: None,
labels: BTreeMap::new(),
badge: false,
copyable: false,
span: None,
}
}
pub fn text(key: &str, label: &str) -> Self {
Self::new(key, label)
}
pub fn format(mut self, format: &str) -> Self {
self.format = Some(format.to_owned());
self
}
pub fn labels<V: Into<String>, L: Into<String>>(
mut self,
labels: impl IntoIterator<Item = (V, L)>,
) -> Self {
self.labels = labels
.into_iter()
.map(|(value, label)| (value.into(), label.into()))
.collect();
self
}
pub fn badge(mut self) -> Self {
self.badge = true;
self
}
pub fn copyable(mut self) -> Self {
self.copyable = true;
self
}
pub fn span_full(mut self) -> Self {
self.span = Some("full".into());
self
}
pub fn from_column(column: &Column) -> Option<Self> {
let entry = Self::new(column.key(), column.label());
let entry = match column.kind() {
Kind::Custom => return None,
Kind::Number => entry.format("number"),
Kind::Money => entry.format("money"),
Kind::Date => entry.format("date"),
Kind::DateTime => entry.format("datetime"),
Kind::Bool => entry.format("bool"),
Kind::Image => entry.format("image"),
Kind::Color => entry.format("color"),
Kind::Select | Kind::Tags => entry.labels(column.options().iter().cloned()).badge(),
_ => entry,
};
Some(entry)
}
pub fn key(&self) -> &str {
&self.key
}
}