Skip to main content

ical/value/
datetime.rs

1//! # Date and time values
2//!
3//! The decoded date/time value kinds: a calendar date, a date-time, and a time.
4//!
5//! [`IcalDate`] (RFC 5545 3.3.4, `YYYYMMDD`) backs date-valued properties,
6//! [`IcalDateTime`] (RFC 5545 3.3.5, `YYYYMMDDTHHMMSS` with an optional `Z`
7//! suffix or a companion `TZID` parameter) backs `DTSTART` / `DTEND` /
8//! `DTSTAMP` and friends, and [`IcalTime`] (RFC 5545 3.3.12, `HHMMSS` with an
9//! optional `Z` suffix) backs bare time values. All three are kept as their raw
10//! text rather than decoded into calendar fields: the grammars carry
11//! timezone-dependent and reduced-precision forms whose meaning depends on
12//! companion parameters, so decoding here would risk a lossy round-trip. Callers
13//! that need calendar semantics parse the string themselves.
14
15use alloc::{borrow::Cow, string::String, vec::Vec};
16
17/// A decoded date value (`YYYYMMDD`), kept as its raw text.
18#[derive(Clone, Debug, Default, PartialEq, Eq)]
19pub struct IcalDate<'a>(pub Cow<'a, str>);
20
21impl<'a> From<&'a str> for IcalDate<'a> {
22    fn from(value: &'a str) -> Self {
23        Self(Cow::Borrowed(value))
24    }
25}
26
27impl From<String> for IcalDate<'_> {
28    fn from(value: String) -> Self {
29        Self(Cow::Owned(value))
30    }
31}
32
33impl<'a> From<Cow<'a, str>> for IcalDate<'a> {
34    fn from(value: Cow<'a, str>) -> Self {
35        Self(value)
36    }
37}
38
39/// A decoded comma-separated list of date-ish values, each kept as its raw
40/// text.
41///
42/// Backs `RDATE` and `EXDATE`, which RFC 5545 3.8.5.1 and 3.8.5.2 define as
43/// lists rather than single values. An item is a `DATE`, a `DATE-TIME` or (for
44/// `RDATE`) a `PERIOD`, whichever the property's `VALUE` parameter declares,
45/// and it is kept exactly as written, so a period item keeps its `start/end`
46/// or `start/duration` form.
47#[derive(Clone, Debug, Default, PartialEq, Eq)]
48pub struct IcalDateTimeList<'a>(pub Vec<Cow<'a, str>>);
49
50impl<'a> From<Vec<Cow<'a, str>>> for IcalDateTimeList<'a> {
51    fn from(values: Vec<Cow<'a, str>>) -> Self {
52        Self(values)
53    }
54}
55
56/// A decoded date-time value (`YYYYMMDDTHHMMSS`, optional `Z` or `TZID`), kept
57/// as its raw text.
58#[derive(Clone, Debug, Default, PartialEq, Eq)]
59pub struct IcalDateTime<'a>(pub Cow<'a, str>);
60
61impl<'a> From<&'a str> for IcalDateTime<'a> {
62    fn from(value: &'a str) -> Self {
63        Self(Cow::Borrowed(value))
64    }
65}
66
67impl From<String> for IcalDateTime<'_> {
68    fn from(value: String) -> Self {
69        Self(Cow::Owned(value))
70    }
71}
72
73impl<'a> From<Cow<'a, str>> for IcalDateTime<'a> {
74    fn from(value: Cow<'a, str>) -> Self {
75        Self(value)
76    }
77}
78
79/// A decoded time value (`HHMMSS`, optional `Z`), kept as its raw text.
80#[derive(Clone, Debug, Default, PartialEq, Eq)]
81pub struct IcalTime<'a>(pub Cow<'a, str>);
82
83impl<'a> From<&'a str> for IcalTime<'a> {
84    fn from(value: &'a str) -> Self {
85        Self(Cow::Borrowed(value))
86    }
87}
88
89impl From<String> for IcalTime<'_> {
90    fn from(value: String) -> Self {
91        Self(Cow::Owned(value))
92    }
93}
94
95impl<'a> From<Cow<'a, str>> for IcalTime<'a> {
96    fn from(value: Cow<'a, str>) -> Self {
97        Self(value)
98    }
99}