aimcal_core/
event.rs

1// SPDX-FileCopyrightText: 2025 Zexin Yuan <aim@yzx9.xyz>
2//
3// SPDX-License-Identifier: Apache-2.0
4
5use crate::DatePerhapsTime;
6use chrono::NaiveDateTime;
7use std::{fmt::Display, str::FromStr};
8
9/// Trait representing a calendar event.
10pub trait Event {
11    /// Returns the unique identifier for the event.
12    fn uid(&self) -> &str;
13    /// Returns the summary of the event.
14    fn summary(&self) -> &str;
15    /// Returns the description of the event, if available.
16    fn description(&self) -> Option<&str>;
17    /// Returns the location of the event, if available.
18    fn start(&self) -> Option<DatePerhapsTime>;
19    /// Returns the start date and time of the event, if available.
20    fn end(&self) -> Option<DatePerhapsTime>;
21    /// Returns the status of the event, if available.
22    fn status(&self) -> Option<&str>;
23}
24
25/// Represents the status of an event, which can be tentative, confirmed, or cancelled.
26#[derive(Debug, Clone, Copy)]
27pub enum EventStatus {
28    Tentative,
29    Confirmed,
30    Cancelled,
31}
32
33const STATUS_TENTATIVE: &str = "TENTATIVE";
34const STATUS_CONFIRMED: &str = "CONFIRMED";
35const STATUS_CANCELLED: &str = "CANCELLED";
36
37impl AsRef<str> for EventStatus {
38    fn as_ref(&self) -> &str {
39        match self {
40            EventStatus::Tentative => STATUS_TENTATIVE,
41            EventStatus::Confirmed => STATUS_CONFIRMED,
42            EventStatus::Cancelled => STATUS_CANCELLED,
43        }
44    }
45}
46
47impl Display for EventStatus {
48    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49        write!(f, "{}", self.as_ref())
50    }
51}
52
53impl FromStr for EventStatus {
54    type Err = ();
55
56    fn from_str(value: &str) -> Result<Self, Self::Err> {
57        match value {
58            STATUS_TENTATIVE => Ok(EventStatus::Tentative),
59            STATUS_CONFIRMED => Ok(EventStatus::Confirmed),
60            STATUS_CANCELLED => Ok(EventStatus::Cancelled),
61            _ => Err(()),
62        }
63    }
64}
65
66impl From<EventStatus> for icalendar::EventStatus {
67    fn from(status: EventStatus) -> Self {
68        match status {
69            EventStatus::Tentative => icalendar::EventStatus::Tentative,
70            EventStatus::Confirmed => icalendar::EventStatus::Confirmed,
71            EventStatus::Cancelled => icalendar::EventStatus::Cancelled,
72        }
73    }
74}
75
76impl From<icalendar::EventStatus> for EventStatus {
77    fn from(status: icalendar::EventStatus) -> Self {
78        match status {
79            icalendar::EventStatus::Tentative => EventStatus::Tentative,
80            icalendar::EventStatus::Confirmed => EventStatus::Confirmed,
81            icalendar::EventStatus::Cancelled => EventStatus::Cancelled,
82        }
83    }
84}
85
86/// Represents conditions for filtering events in a calendar.
87#[derive(Debug, Clone, Copy)]
88pub struct EventConditions {
89    /// The current time to filter events.
90    pub now: NaiveDateTime,
91}