renox_admin/entry.rs
1//! A record's details on its view page: what [`AdminResource::entries`]
2//! returns, drawn with the UI kit's `infolist` and `entry`.
3//!
4//! [`AdminResource::entries`]: crate::AdminResource::entries
5
6use std::collections::BTreeMap;
7
8use renox::grid::{Column, Kind};
9use serde::Serialize;
10
11/// One labelled value on a record's view page. `format` is one of the
12/// kit's `entry` formats: `date`, `datetime`, `since`, `money`, `quantity`, `number`,
13/// `bool`, `color`, `image`, `markdown`, or none for text.
14///
15/// ```
16/// use renox_admin::Entry;
17///
18/// let entries = vec![
19/// Entry::text("name", "Name").copyable(),
20/// Entry::new("status", "Status").labels([("live", "Live")]).badge(),
21/// Entry::new("price", "Price").format("money"),
22/// Entry::new("notes", "Notes").format("markdown").span_full(),
23/// ];
24/// # assert_eq!(entries.len(), 4);
25/// ```
26#[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 currency_from: Option<String>,
37 scale: u32,
38 unit_from: Option<String>,
39}
40
41impl Entry {
42 /// The record's `key`, shown as text under `label` until `format` says
43 /// otherwise.
44 pub fn new(key: &str, label: &str) -> Self {
45 Self {
46 key: key.to_owned(),
47 label: label.to_owned(),
48 format: None,
49 labels: BTreeMap::new(),
50 badge: false,
51 copyable: false,
52 span: None,
53 currency_from: None,
54 scale: 0,
55 unit_from: None,
56 }
57 }
58
59 /// The record's `key`, as text.
60 pub fn text(key: &str, label: &str) -> Self {
61 Self::new(key, label)
62 }
63
64 /// How the value is shown: `date`, `datetime`, `since`, `money` (an
65 /// amount in the smallest unit, like the grid's money columns),
66 /// `number`, `bool`, `color`, `image` or `markdown`.
67 pub fn format(mut self, format: &str) -> Self {
68 self.format = Some(format.to_owned());
69 self
70 }
71
72 /// Names for values (`("live", "Live")`), as a select's options.
73 pub fn labels<V: Into<String>, L: Into<String>>(
74 mut self,
75 labels: impl IntoIterator<Item = (V, L)>,
76 ) -> Self {
77 self.labels = labels
78 .into_iter()
79 .map(|(value, label)| (value.into(), label.into()))
80 .collect();
81 self
82 }
83
84 /// For a `money` entry: shows the amount in the currency the record
85 /// keeps in its `key` field (an ISO 4217 code such as `total_currency`),
86 /// with that currency's symbol and decimals. Without it the entry uses
87 /// `APP_CURRENCY`.
88 ///
89 /// ```
90 /// use renox_admin::Entry;
91 ///
92 /// let total = Entry::new("total", "Total")
93 /// .format("money")
94 /// .currency_from("total_currency");
95 /// # let _ = total;
96 /// ```
97 pub fn currency_from(mut self, key: &str) -> Self {
98 self.currency_from = Some(key.to_owned());
99 self
100 }
101
102 /// For a `quantity` entry: the decimals of its `Quantity<SCALE>` (at
103 /// most 9), so `2.5` shows as `2.500` with `3`.
104 ///
105 /// ```
106 /// use renox_admin::Entry;
107 ///
108 /// let stock = Entry::new("stock", "Stock").format("quantity").scale(3);
109 /// # let _ = stock;
110 /// ```
111 pub fn scale(mut self, scale: u32) -> Self {
112 self.scale = scale.min(9);
113 self
114 }
115
116 /// For a `quantity` entry: shows the quantity with the unit the record
117 /// keeps in its `key` field (such as `stock_unit`), after the value.
118 ///
119 /// ```
120 /// use renox_admin::Entry;
121 ///
122 /// let stock = Entry::new("stock", "Stock")
123 /// .format("quantity")
124 /// .scale(3)
125 /// .unit_from("stock_unit");
126 /// # let _ = stock;
127 /// ```
128 pub fn unit_from(mut self, key: &str) -> Self {
129 self.unit_from = Some(key.to_owned());
130 self
131 }
132
133 /// Shows the value as a badge.
134 pub fn badge(mut self) -> Self {
135 self.badge = true;
136 self
137 }
138
139 /// Adds a copy button.
140 pub fn copyable(mut self) -> Self {
141 self.copyable = true;
142 self
143 }
144
145 /// Takes the list's whole width.
146 pub fn span_full(mut self) -> Self {
147 self.span = Some("full".into());
148 self
149 }
150
151 /// The entry a grid column suggests: its key, its heading and a format
152 /// that fits its kind (`None` for `custom` columns, which only the page
153 /// can draw).
154 pub fn from_column(column: &Column) -> Option<Self> {
155 let entry = Self::new(column.key(), column.label());
156 let entry = match column.kind() {
157 Kind::Custom => return None,
158 Kind::Number => entry.format("number"),
159 Kind::Money => {
160 let entry = entry.format("money");
161 match column.currency_column() {
162 Some(key) => entry.currency_from(key),
163 None => entry,
164 }
165 }
166 Kind::Quantity => {
167 let entry = entry.format("quantity").scale(column.quantity_scale());
168 match column.unit_column() {
169 Some(key) => entry.unit_from(key),
170 None => entry,
171 }
172 }
173 Kind::Date => entry.format("date"),
174 Kind::DateTime => entry.format("datetime"),
175 Kind::Bool => entry.format("bool"),
176 Kind::Image => entry.format("image"),
177 Kind::Color => entry.format("color"),
178 Kind::Select | Kind::Tags => entry.labels(column.options().iter().cloned()).badge(),
179 _ => entry,
180 };
181 Some(entry)
182 }
183
184 /// The entry's key.
185 pub fn key(&self) -> &str {
186 &self.key
187 }
188
189 /// Runs the entry's label and its value names through `translate`.
190 pub(crate) fn translate(&mut self, translate: &dyn Fn(&str) -> String) {
191 self.label = translate(&self.label);
192 for label in self.labels.values_mut() {
193 *label = translate(label);
194 }
195 }
196}