1use std::collections::BTreeMap;
7
8use renox::grid::{Column, Kind};
9use serde::Serialize;
10
11#[derive(Debug, Clone, Serialize)]
27#[non_exhaustive]
28pub struct Entry {
29 key: String,
30 label: String,
31 format: Option<String>,
32 labels: BTreeMap<String, String>,
33 badge: bool,
34 copyable: bool,
35 span: Option<String>,
36}
37
38impl Entry {
39 pub fn new(key: &str, label: &str) -> Self {
42 Self {
43 key: key.to_owned(),
44 label: label.to_owned(),
45 format: None,
46 labels: BTreeMap::new(),
47 badge: false,
48 copyable: false,
49 span: None,
50 }
51 }
52
53 pub fn text(key: &str, label: &str) -> Self {
55 Self::new(key, label)
56 }
57
58 pub fn format(mut self, format: &str) -> Self {
62 self.format = Some(format.to_owned());
63 self
64 }
65
66 pub fn labels<V: Into<String>, L: Into<String>>(
68 mut self,
69 labels: impl IntoIterator<Item = (V, L)>,
70 ) -> Self {
71 self.labels = labels
72 .into_iter()
73 .map(|(value, label)| (value.into(), label.into()))
74 .collect();
75 self
76 }
77
78 pub fn badge(mut self) -> Self {
80 self.badge = true;
81 self
82 }
83
84 pub fn copyable(mut self) -> Self {
86 self.copyable = true;
87 self
88 }
89
90 pub fn span_full(mut self) -> Self {
92 self.span = Some("full".into());
93 self
94 }
95
96 pub fn from_column(column: &Column) -> Option<Self> {
100 let entry = Self::new(column.key(), column.label());
101 let entry = match column.kind() {
102 Kind::Custom => return None,
103 Kind::Number => entry.format("number"),
104 Kind::Money => entry.format("money"),
105 Kind::Date => entry.format("date"),
106 Kind::DateTime => entry.format("datetime"),
107 Kind::Bool => entry.format("bool"),
108 Kind::Image => entry.format("image"),
109 Kind::Color => entry.format("color"),
110 Kind::Select | Kind::Tags => entry.labels(column.options().iter().cloned()).badge(),
111 _ => entry,
112 };
113 Some(entry)
114 }
115
116 pub fn key(&self) -> &str {
118 &self.key
119 }
120
121 pub(crate) fn translate(&mut self, translate: &dyn Fn(&str) -> String) {
123 self.label = translate(&self.label);
124 for label in self.labels.values_mut() {
125 *label = translate(label);
126 }
127 }
128}