commit 249da5d44ed04e0b53c5a71d3934193d834791f3 from: witcher date: Sun Jan 8 09:59:48 2023 UTC Implement `Display` for some types commit - 01ba7a35eb3e4796a5d0cdf8a5d72c53d4a7c3e4 commit + 249da5d44ed04e0b53c5a71d3934193d834791f3 blob - 38241164113bfce4b50fd85ba3c75ae01d2f058f blob + d16878f8316bc82d8431fed3e3d2e7cd1d1c610d --- src/types.rs +++ src/types.rs @@ -72,6 +72,18 @@ impl<'a> Tag<'a> { } } +impl<'a> std::fmt::Display for Tag<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Project(p) => write!(f, "+{p}"), + Self::Context(c) => write!(f, "@{c}"), + Self::Due(d) => write!(f, "due:{}", d.format("%Y-%m-%d")), + Self::Rec(r) => write!(f, "rec:{r}"), + Self::KeyValue((k, v)) => write!(f, "{k}:{v}"), + } + } +} + /// The recurring tag, the contained type of [`Tag::Rec`]. /// /// This struct describes how recurring should be handled, and can be used to create another task @@ -90,6 +102,13 @@ pub struct Recurring { pub amount: TimeAmount, } +impl std::fmt::Display for Recurring { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let s = if self.strict { "+" } else { "" }; + write!(f, "{s}{}", self.amount) + } +} + /// A unit of time applicable for the [`Recurring`] tag. #[derive(Debug, Clone, PartialEq, Eq)] pub enum TimeUnit { @@ -103,6 +122,19 @@ pub enum TimeUnit { Year, } +impl std::fmt::Display for TimeUnit { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let u = match self { + Self::Day => 'd', + Self::Week => 'w', + Self::Month => 'm', + Self::Year => 'y', + }; + + write!(f, "{u}") + } +} + impl TryFrom for TimeUnit { type Error = TodoError; @@ -126,6 +158,12 @@ pub struct TimeAmount { pub unit: TimeUnit, } +impl std::fmt::Display for TimeAmount { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}{}", self.amount, self.unit) + } +} + /// The completion state of a [`Task`]. /// /// A task can either be [`Incomplete`] or [`Complete`]. Should the task be complete, there can