Skip to main content

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`, `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}
37
38impl Entry {
39    /// The record's `key`, shown as text under `label` until `format` says
40    /// otherwise.
41    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    /// The record's `key`, as text.
54    pub fn text(key: &str, label: &str) -> Self {
55        Self::new(key, label)
56    }
57
58    /// How the value is shown: `date`, `datetime`, `since`, `money` (an
59    /// amount in the smallest unit, like the grid's money columns),
60    /// `number`, `bool`, `color`, `image` or `markdown`.
61    pub fn format(mut self, format: &str) -> Self {
62        self.format = Some(format.to_owned());
63        self
64    }
65
66    /// Names for values (`("live", "Live")`), as a select's options.
67    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    /// Shows the value as a badge.
79    pub fn badge(mut self) -> Self {
80        self.badge = true;
81        self
82    }
83
84    /// Adds a copy button.
85    pub fn copyable(mut self) -> Self {
86        self.copyable = true;
87        self
88    }
89
90    /// Takes the list's whole width.
91    pub fn span_full(mut self) -> Self {
92        self.span = Some("full".into());
93        self
94    }
95
96    /// The entry a grid column suggests: its key, its heading and a format
97    /// that fits its kind (`None` for `custom` columns, which only the page
98    /// can draw).
99    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    /// The entry's key.
117    pub fn key(&self) -> &str {
118        &self.key
119    }
120}