Skip to main content

ptsd/
utils.rs

1use chrono::{Datelike, Timelike};
2use prism::{Context, drawable::Drawable};
3
4pub use chrono::{DateTime, Local, Utc};
5
6// #[derive(Clone, Copy, Deserialize, Serialize, Debug)]
7// pub struct InternetConnection(pub bool);
8
9/// `Timestamp` contains the date time in an easy-to-read format.
10#[derive(Clone, Debug, PartialEq, Hash, Eq)]
11pub struct Timestamp(Option<DateTime<Utc>>);
12
13impl std::fmt::Display for Timestamp {
14    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        write!(f, "{}", self.friendly())
16    }
17}
18
19impl Default for Timestamp {
20    fn default() -> Self {
21        Timestamp::new(Some(Local::now()))
22    }
23}
24
25impl Timestamp {
26    /// Create a `Timestamp` from a local [`DateTime<Local>`].
27    pub fn new(dt: Option<DateTime<Local>>) -> Self {Timestamp(dt.map(|s| s.into()))}
28
29    /// Create a `Timestamp` with date and time set as pending (`"-"`).
30    pub fn pending() -> (String, String) {
31        ("-".to_string(), "-".to_string())
32    }
33
34    /// Tries to convert the `Timestamp` into a `DateTime<Local>`.
35    ///
36    /// Parses the stored date and time strings using the format `M/D/YY H:MM AM/PM`.
37    pub fn as_local(&self) -> Option<DateTime<Local>> {
38        self.0.map(|dt| dt.into())
39    }
40
41    /// Returns a human-readable, "direct" representation of the timestamp.
42    ///
43    /// Formats the timestamp based on how recent it is:
44    /// - **Today**: `"H:MM am/pm"`
45    /// - **Yesterday**: `"yesterday, H:MM am/pm"`
46    /// - **Same week**: day of the week (e.g., `"Monday"`)
47    /// - **Same year**: `"Month D"` (e.g., `"August 16"`)
48    /// - **Otherwise**: `"MM/DD/YY"`
49    ///
50    /// Returns `None` if the timestamp cannot be converted to a local datetime.
51    pub fn friendly(&self) -> String {
52        if let Some(dt) = self.as_local() {
53            let today = Local::now().date_naive();
54            let date = dt.date_naive();
55            let hour = dt.hour();
56            let minute = dt.minute();
57            let (hour12, am_pm) = match hour == 0 {
58                true => (12, "AM"),
59                false if hour < 12 => (hour, "AM"),
60                false if hour == 12 => (12, "PM"),
61                false => (hour - 12, "PM")
62            };
63
64            let the_time = format!("{hour12}:{minute:02} {am_pm}");
65
66            match date == today {
67                true => the_time,
68                false if date == today.pred_opt().unwrap_or(today) => format!("yesterday, {the_time}"),
69                false if date.iso_week() == today.iso_week() => format!("{}", dt.format("%A")),
70                false if date.year() == today.year() => format!("{}", dt.format("%B %-d")),
71                false => format!("{}", dt.format("%m/%d/%y")),
72            }
73        } else {"Pending".to_string()}
74    }
75
76    pub fn date(&self) -> String {
77        self.as_local().map(|dt| dt.format("%-m/%-d/%y").to_string()).unwrap_or("Pending".to_string())
78    }
79
80    pub fn time(&self) -> String {
81        self.as_local().map(|dt| dt.format("%-I:%M %p").to_string()).unwrap_or("Pending".to_string())
82    }
83    
84    pub fn date_friendly(&self) -> String {
85        if let Some(dt) = self.as_local() {
86            let today = Local::now().date_naive();
87            let date = dt.date_naive();
88
89            if date == today {return "Today".to_string();}
90            if date.iso_week() == today.iso_week() { return format!("{}", dt.format("%A")); }
91            if date.year() == today.year() { return format!("{}", dt.format("%B %-d")); }
92            format!("{}", dt.format("%m/%d/%y"))
93        } else {"Pending".to_string()}
94    }
95}
96
97// impl From<String> for PelicanError {
98//     fn from(s: String, ap: impl AppPage) -> Self {
99//         PelicanError::Err(s, ap)
100//     }
101// }
102
103#[derive(Debug, Clone)]
104pub struct TitleSubtitle {
105    pub title: String, 
106    pub subtitle: Option<String>
107}
108
109impl TitleSubtitle {
110    pub fn new(title: &str, subtitle: Option<&str>) -> Self {
111        TitleSubtitle{
112            title: title.to_string(), 
113            subtitle: subtitle.map(|s| s.to_string())
114        }
115    }
116}
117
118
119pub trait ValidationFn: FnMut(&mut Vec<Box<dyn Drawable>>) -> bool + 'static {
120    fn clone_box(&self) -> Box<dyn ValidationFn>;
121}
122
123impl<F> ValidationFn for F where F: FnMut(&mut Vec<Box<dyn Drawable>>) -> bool + Clone + 'static {
124    fn clone_box(&self) -> Box<dyn ValidationFn> { Box::new(self.clone()) }
125}
126
127impl Clone for Box<dyn ValidationFn> { fn clone(&self) -> Self { self.as_ref().clone_box() } }
128
129impl std::fmt::Debug for dyn ValidationFn {
130    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {  write!(f, "ValidationFn...") }
131}
132
133pub trait Callback: FnMut(&mut Context) + 'static {
134    fn clone_box(&self) -> Box<dyn Callback>;
135}
136
137impl PartialEq for dyn Callback{fn eq(&self, _: &Self) -> bool {true}}
138
139impl<F> Callback for F where F: FnMut(&mut Context) + Clone + 'static {
140    fn clone_box(&self) -> Box<dyn Callback> {
141        Box::new(self.clone())
142    }
143}
144
145impl Clone for Box<dyn Callback> {
146    fn clone(&self) -> Self {
147        self.as_ref().clone_box()
148    }
149}
150
151impl std::fmt::Debug for dyn Callback {
152    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
153        write!(f, "Clonable Closure")
154    }
155}