tablo_core/column/
date.rs1use 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#[derive_where(Clone, Debug)]
24pub struct DateColumn<M, T> {
25 lens: Lens<M, T>,
26 base: ColumnBase,
27 sortable: bool,
28}
29
30pub trait DateValue: Sized + Send + Sync + 'static {
35 fn calendar_date(&self, zone: &TimeZone) -> Option<jiff::civil::Date>;
38
39 fn stored_text(&self) -> String {
42 self.calendar_date(&TimeZone::UTC)
43 .map(|day| day.to_string())
44 .unwrap_or_default()
45 }
46
47 fn format_in(&self, cx: &Cx) -> String {
59 spell_day(cx, self.calendar_date(&crate::time_zone(cx)))
60 }
61}
62
63fn 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 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}