Skip to main content

tablo_core/column/
date.rs

1//! [`DateColumn`]: a date field rendered as a calendar day, over any [`DateValue`].
2
3use derive_where::derive_where;
4use jiff::tz::TimeZone;
5use toasty::stmt::OrderByExpr;
6use topcoat::{context::Cx, view::*};
7
8use super::{Column, ColumnBase, ColumnWidth, sort_order};
9use crate::{Lens, values::FormScalar};
10
11/// A column of a date field, rendered as a calendar day.
12///
13/// ```rust
14/// # #[derive(Debug, Clone, toasty::Model, tablo_core::PanelModel)]
15/// # struct Post { #[key] #[auto] id: uuid::Uuid, published_at: jiff::Timestamp }
16/// tablo_core::DateColumn::new(tablo_core::lens!(Post.published_at)).sortable();
17/// ```
18///
19/// The cell spells the day in the request's locale, `Oct 14, 2026` in English, a timestamp's in
20/// the request's [`time_zone`](crate::time_zone), and a `None` renders the table's dash. The export
21/// writes the stored spelling, `2026-10-14T09:30:00Z` or `2026-10-14`, as a
22/// [`TextColumn`](super::TextColumn) does; a group header follows the field, not the column.
23#[derive_where(Clone, Debug)]
24pub struct DateColumn<M, T> {
25    lens: Lens<M, T>,
26    base: ColumnBase,
27    sortable: bool,
28}
29
30/// The field types a [`DateColumn`] spells as a calendar day: `jiff::civil::Date`,
31/// `jiff::civil::DateTime`, `jiff::Timestamp` (read in the request's
32/// [`time_zone`](crate::time_zone), as a [`DateFilter`](crate::DateFilter) reads it) and an
33/// `Option` of any of them.
34pub trait DateValue: Sized + Send + Sync + 'static {
35    /// The value's calendar day in `zone`, `None` when it holds none. A civil value names its day
36    /// whatever the zone.
37    fn calendar_date(&self, zone: &TimeZone) -> Option<jiff::civil::Date>;
38
39    /// The value's stored spelling, which a column's text and export carry. The default spells
40    /// the calendar day in UTC as an ISO date.
41    fn stored_text(&self) -> String {
42        self.calendar_date(&TimeZone::UTC)
43            .map(|day| day.to_string())
44            .unwrap_or_default()
45    }
46
47    /// The value's calendar day as a [`DateColumn`] cell spells it: in the request's locale,
48    /// `Oct 14, 2026` in English, and a timestamp's in the request's
49    /// [`time_zone`](crate::time_zone). Empty when it holds none. For a page, a widget or a mail
50    /// that shows a date in the reader's words rather than as ISO text in UTC.
51    ///
52    /// ```rust,no_run
53    /// use tablo_core::DateValue;
54    /// # let cx: &topcoat::context::Cx = todo!();
55    /// let renews: jiff::Timestamp = "2026-10-14T09:30:00Z".parse().unwrap();
56    /// let line = renews.format_in(cx); // "Oct 14, 2026", "14 oct. 2026" in French
57    /// ```
58    fn format_in(&self, cx: &Cx) -> String {
59        spell_day(cx, self.calendar_date(&crate::time_zone(cx)))
60    }
61}
62
63/// `day` as the request's locale spells a calendar day, or empty for none.
64fn spell_day(cx: &Cx, day: Option<jiff::civil::Date>) -> String {
65    day.map(|day| {
66        crate::t!(
67            cx,
68            "tablo-column-date",
69            year = day.year(),
70            month = day.month(),
71            day = day.day()
72        )
73    })
74    .unwrap_or_default()
75}
76
77impl DateValue for jiff::civil::Date {
78    fn calendar_date(&self, _zone: &TimeZone) -> Option<jiff::civil::Date> {
79        Some(*self)
80    }
81
82    fn stored_text(&self) -> String {
83        FormScalar::to_form(self)
84    }
85}
86
87impl DateValue for jiff::civil::DateTime {
88    fn calendar_date(&self, _zone: &TimeZone) -> Option<jiff::civil::Date> {
89        Some(self.date())
90    }
91
92    fn stored_text(&self) -> String {
93        FormScalar::to_form(self)
94    }
95}
96
97impl DateValue for jiff::Timestamp {
98    fn calendar_date(&self, zone: &TimeZone) -> Option<jiff::civil::Date> {
99        Some(self.to_zoned(zone.clone()).date())
100    }
101
102    fn stored_text(&self) -> String {
103        FormScalar::to_form(self)
104    }
105}
106
107impl<T: DateValue> DateValue for Option<T> {
108    fn calendar_date(&self, zone: &TimeZone) -> Option<jiff::civil::Date> {
109        self.as_ref().and_then(|value| value.calendar_date(zone))
110    }
111
112    fn stored_text(&self) -> String {
113        self.as_ref()
114            .map(DateValue::stored_text)
115            .unwrap_or_default()
116    }
117}
118
119impl<M, T> DateColumn<M, T>
120where
121    M: toasty::schema::Model,
122{
123    /// Bind the column to the date field `lens` reads.
124    pub fn new(lens: Lens<M, T>) -> Self
125    where
126        T: DateValue,
127    {
128        Self {
129            base: ColumnBase::new(lens.path(), ColumnWidth::Auto),
130            lens,
131            sortable: false,
132        }
133    }
134
135    base_builders!(sortable);
136}
137
138impl<M, T> Column<M> for DateColumn<M, T>
139where
140    M: toasty::schema::Model + Send + Sync + 'static,
141    T: DateValue,
142{
143    base_column_methods!();
144
145    fn text(&self, _cx: &Cx, row: &M) -> String {
146        self.lens.read(row).stored_text()
147    }
148
149    fn cell<'a>(&self, cx: &'a Cx, row: &M) -> BoxView<'a> {
150        crate::schema::value_cell(cx, &self.lens.read(row).format_in(cx))
151    }
152
153    fn order_by(&self, descending: bool) -> Option<OrderByExpr> {
154        self.sortable
155            .then(|| sort_order(self.lens.path(), descending))
156    }
157}
158
159impl<M, T> crate::column::ColumnInternals<M> for DateColumn<M, T>
160where
161    M: toasty::schema::Model + Send + Sync + 'static,
162    T: DateValue,
163{
164    base_internals!(binding);
165}