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