open-timeline-core 0.1.2

OpenTimeline core
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// SPDX-License-Identifier: MIT

//!
//! The OpenTimeline date type
//!

use serde::{Deserialize, Deserializer, Serialize};
use std::cmp::Ordering;
use thiserror::Error;
use time::OffsetDateTime;

/// The minimum year allowed in the OpenTimeline system
pub const MIN_YEAR: i64 = -50000;

/// The maximum year allowed in the OpenTimeline system
pub const MAX_YEAR: i64 = 10000;

/// Errors that can arise in relation to a [`Date`]
#[derive(Error, Debug, Clone)]
pub enum DateError {
    /// The day number is not allowed (must be 1 <= day <= 31)
    #[error("Day `{0}` is not allowed")]
    InvalidDay(i64),

    /// The month number is not allowed (must be 1 <= day <= 12)
    #[error("Month `{0}` is not allowed")]
    InvalidMonth(i64),

    /// The day number is not allowed (must be [`MIN_YEAR`] <= day <= [`MAX_YEAR`])
    #[error("Month `{0}` is not allowed")]
    InvalidYear(i64),

    /// Invalid field initialisation.  e.g. the day has been set without the
    /// month also being set
    #[error("e.g. can't set day without setting month")]
    InvalidFields,
}

/// The OpenTimeline date type
///
/// The year field must be set but the day and month fields are optional.  If
/// the day field is set the month field must be set, but if the month field is
/// set, the day field is optional.
#[derive(Serialize, PartialEq, Eq, Clone, Copy, Debug, Hash)]
pub struct Date {
    day: Option<Day>,
    month: Option<Month>,
    year: Year,
}

/// The OpenTimeline day type
#[rustfmt::skip]
#[derive(derive_more::Display, Serialize, Eq, PartialEq, Clone, Copy, Debug, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "sqlx", derive(sqlx::Type))]
#[cfg_attr(feature = "sqlx", sqlx(transparent))]
pub struct Day(u8);

/// The OpenTimeline month type
#[rustfmt::skip]
#[derive(derive_more::Display, Serialize, Eq, PartialEq, Clone, Copy, Debug, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "sqlx", derive(sqlx::Type))]
#[cfg_attr(feature = "sqlx", sqlx(transparent))]
pub struct Month(u8);

/// The OpenTimeline year type
/// 
/// The minimum year allowed is [`MIN_YEAR`].  The maximum year allowed is
/// [`MAX_YEAR`]
#[rustfmt::skip]
#[derive(derive_more::Display, Serialize, Eq, PartialEq, Clone, Copy, Debug, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "sqlx", derive(sqlx::Type))]
#[cfg_attr(feature = "sqlx", sqlx(transparent))]
pub struct Year(i32);

impl Day {
    pub fn value(&self) -> u8 {
        self.0
    }

    pub fn current() -> Self {
        Date::today().day().unwrap()
    }
}

impl Month {
    pub fn value(&self) -> u8 {
        self.0
    }

    pub fn current() -> Self {
        Date::today().month().unwrap()
    }
}

impl Year {
    pub fn value(&self) -> i32 {
        self.0
    }

    pub fn min() -> Self {
        Year(MIN_YEAR as i32)
    }

    pub fn max() -> Self {
        Year(MAX_YEAR as i32)
    }

    pub fn current() -> Self {
        Date::today().year()
    }
}

impl TryFrom<i64> for Day {
    type Error = DateError;
    fn try_from(value: i64) -> Result<Self, Self::Error> {
        if (1..=31).contains(&value) {
            Ok(Day(value as u8))
        } else {
            Err(DateError::InvalidDay(value))
        }
    }
}

impl TryFrom<i64> for Month {
    type Error = DateError;
    fn try_from(value: i64) -> Result<Self, Self::Error> {
        if (1..=12).contains(&value) {
            Ok(Month(value as u8))
        } else {
            Err(DateError::InvalidMonth(value))
        }
    }
}

impl TryFrom<i64> for Year {
    type Error = DateError;
    fn try_from(value: i64) -> Result<Self, Self::Error> {
        if (MIN_YEAR..=MAX_YEAR).contains(&value) {
            Ok(Year(value as i32))
        } else {
            Err(DateError::InvalidYear(value))
        }
    }
}

impl From<time::Month> for Month {
    fn from(month: time::Month) -> Self {
        Self(month.into())
    }
}

impl From<Month> for time::Month {
    fn from(month: Month) -> Self {
        Self::try_from(month.value()).unwrap()
    }
}

// TODO: add visitor so that can deserialise from strings as well?
impl<'de> Deserialize<'de> for Day {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = i64::deserialize(deserializer)?;
        Day::try_from(value).map_err(|e| serde::de::Error::custom(format!("{:?}", e)))
    }
}

// TODO: add visitor so that can deserialise from strings as well?
impl<'de> Deserialize<'de> for Month {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = i64::deserialize(deserializer)?;
        Month::try_from(value).map_err(|e| serde::de::Error::custom(format!("{:?}", e)))
    }
}

// TODO: add visitor so that can deserialise from strings as well?
impl<'de> Deserialize<'de> for Year {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = i64::deserialize(deserializer)?;
        Year::try_from(value).map_err(|e| serde::de::Error::custom(format!("{:?}", e)))
    }
}

impl Date {
    /// Today's date
    pub fn today() -> Self {
        let today = OffsetDateTime::now_utc();
        let month: Month = today.month().into();
        Self::from(
            Some(today.day().into()),
            Some(month.value().into()),
            today.year().into(),
        )
        .unwrap()
    }

    /// Create a new [`Date`] if the result will be valid
    pub fn from(day: Option<i64>, month: Option<i64>, year: i64) -> Result<Date, DateError> {
        let mut date = Date {
            day: None,
            month: None,
            year: Year(0),
        };
        date.set_year(year)?;
        date.set_month(month)?;
        date.set_day(day)?;
        Ok(date)
    }

    /// e.g. 1st Jan 2025 format
    pub fn as_long_date_format(&self) -> String {
        // Day
        let day = match self.day() {
            Some(day) => format!("{day}"),
            None => String::new(),
        };

        // Month
        let month = match self.month() {
            Some(month) => match month.value() {
                1 => "Jan",
                2 => "Feb",
                3 => "Mar",
                4 => "Apr",
                5 => "May",
                6 => "Jun",
                7 => "Jul",
                8 => "Aug",
                9 => "Sep",
                10 => "Oct",
                11 => "Nov",
                12 => "Dec",
                _ => panic!("Month value must be 1 <= x <= 12"),
            },
            None => "",
        };

        // Year
        let year = self.year();

        format!("{day} {month} {year}").trim().to_string()
    }

    /// dd/mm/yyyy format
    pub fn as_short_date_format(&self) -> String {
        // Day
        let day = match self.day() {
            Some(day) => format!("{day}"),
            None => String::from("-"),
        };

        // Month
        let month = match self.month() {
            Some(month) => format!("{month}"),
            None => String::from("-"),
        };

        // Year
        let year = format!("{}", self.year());

        // Return
        format!("{day} / {month} / {year}")
    }

    // TODO: pass in Option<Day>?
    /// Update an existing [`Date`]'s `day` if the result will be valid
    pub fn set_day(&mut self, day: Option<i64>) -> Result<(), DateError> {
        match day {
            None => self.day = None,
            Some(day) => {
                // Ensure the new Date will be valid
                let mut new_date = *self;
                new_date.day = Some(Day::try_from(day)?);
                new_date.is_valid()?;
                *self = new_date;
            }
        }
        Ok(())
    }

    // TODO: pass in Option<Month>?
    // TODO: this is wrong - can end up with a day but no month
    /// Update an existing [`Date`]'s `month` if the result will be valid
    pub fn set_month(&mut self, month: Option<i64>) -> Result<(), DateError> {
        match month {
            None => self.month = None,
            Some(month) => {
                // Ensure the new Date will be valid
                let mut new_date = *self;
                new_date.month = Some(Month::try_from(month)?);
                new_date.is_valid()?;
                *self = new_date;
            }
        }
        Ok(())
    }

    // TODO: pass in Option<Year>?
    /// Update an existing [`Date`]'s `year` if the result will be valid
    pub fn set_year(&mut self, year: i64) -> Result<(), DateError> {
        // Ensure the new Date will be valid
        let mut new_date = *self;
        new_date.year = Year::try_from(year)?;
        new_date.is_valid()?;
        *self = new_date;

        Ok(())
    }

    /// Get the [`Date`]'s day
    pub fn day(&self) -> Option<Day> {
        self.day
    }

    /// Get the [`Date`]'s month
    pub fn month(&self) -> Option<Month> {
        self.month
    }

    /// Get the [`Date`]'s year
    pub fn year(&self) -> Year {
        self.year
    }

    /// Check if the [`Date`] is valid
    fn is_valid(&self) -> Result<(), DateError> {
        match (self.day, self.month, self.year) {
            // Valid
            (None, None, _) => Ok(()),
            (None, Some(_), _) => Ok(()),
            (Some(_), Some(_), _) => Ok(()),

            // Not valid
            _ => Err(DateError::InvalidFields),
        }
    }
}

impl PartialOrd for Date {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        match self.year.cmp(&other.year) {
            Ordering::Less => return Some(Ordering::Less),
            Ordering::Greater => return Some(Ordering::Greater),
            Ordering::Equal => (),
        };
        if let (Some(this_month), Some(other_month)) = (self.month, other.month) {
            match this_month.cmp(&other_month) {
                Ordering::Less => return Some(Ordering::Less),
                Ordering::Greater => return Some(Ordering::Greater),
                Ordering::Equal => (),
            };
        } else {
            return None;
        }
        if let (Some(this_day), Some(other_day)) = (self.day, other.day) {
            match this_day.cmp(&other_day) {
                Ordering::Less => Some(Ordering::Less),
                Ordering::Greater => Some(Ordering::Greater),
                Ordering::Equal => Some(Ordering::Equal),
            }
        } else {
            None
        }
    }
}

// Beware!
impl Ord for Date {
    fn cmp(&self, other: &Self) -> Ordering {
        let this_month = self.month().map(|m| m.value()).unwrap_or(1);
        let other_month = other.month().map(|m| m.value()).unwrap_or(1);

        let this_day = self.day().map(|d| d.value()).unwrap_or(1);
        let other_day = other.day().map(|d| d.value()).unwrap_or(1);

        (self.year, this_month, this_day).cmp(&(other.year, other_month, other_day))
    }
}

#[derive(Deserialize)]
struct RawDate {
    day: Option<i64>,
    month: Option<i64>,
    year: i64,
}

impl<'de> Deserialize<'de> for Date {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        // TODO: look into serde Visitors & doing without RawDate type
        let raw_date = RawDate::deserialize(deserializer)?;
        let date = Date::from(raw_date.day, raw_date.month, raw_date.year);
        match date {
            Ok(date) => Ok(date),
            Err(error) => Err(serde::de::Error::custom(error)),
        }
    }
}

#[cfg(test)]
mod test {
    use super::Date;

    #[test]
    fn from() {
        // Should return error
        assert!(Date::from(Some(1), None, 234).is_err());
        assert!(Date::from(None, None, 999_999).is_err());
        assert!(Date::from(None, None, -999_999).is_err());
        assert!(Date::from(Some(0), Some(0), 1234).is_err());
        assert!(Date::from(Some(32), Some(13), 1234).is_err());

        // Should be ok
        assert!(Date::from(Some(1), Some(1), 1).is_ok());
    }

    #[test]
    fn cmp() {
        // Year only
        let date_1 = Date::from(None, None, 234).unwrap();
        let date_2 = Date::from(None, None, 4321).unwrap();
        assert!(date_2 > date_1);
        assert!(date_1 < date_2);
        assert!(date_1 == date_1);
        assert!(date_1 != date_2);

        // Difference of 1 day
        let date_1 = Date::from(Some(1), Some(1), 234).unwrap();
        let date_2 = Date::from(Some(2), Some(1), 234).unwrap();
        assert!(date_2 > date_1);
    }

    #[test]
    fn today() {
        let today = Date::today();
        println!("today = {}", today.as_long_date_format());
        println!("today = {}", today.as_short_date_format());
    }
}