aboutsummaryrefslogtreecommitdiff
path: root/src/domain/event.rs
blob: 6ad24a4ca715f1c9942cd764d6a0e61ff4996242 (plain)
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
//! Calendar event domain model.
//!
//! This module provides the core `Event` and `EventBuilder` types for representing
//! calendar events with recurrence support, metadata tracking, and validation.
//!
//! # Overview
//!
//! The `Event` struct represents a single calendar entry
//! with start and end times, optional recurrence rules, and metadata.
//! Use the `EventBuilder` to construct events with validation of required fields
//! and sensible defaults for optional ones.
//!
//! # Examples
//!
//! ```
//! # use uuid::Uuid;
//! # use chrono::Utc;
//! # let cal_id = Uuid::new_v4();
//! let event = EventBuilder::new(
//!     cal_id,
//!     "Team Meeting".to_string(),
//!     Utc::now(),
//!     Utc::now() + chrono::Duration::hours(1)
//! )?
//!     .color(5)
//!     .description(Some("Quarterly sync".to_string()))
//!     .build();
//! # Ok::<(), String>(())
//! ```

use chrono::{DateTime, TimeDelta, Utc};
use getset::Getters;
use uuid::Uuid;
use crate::domain::recurrence::RecurrenceRule;

/// A calendar event with optional recurrence, metadata, and state tracking.
///
/// `Event` represents a single calendar entry with start and end times,
/// optional recurrence rules, and metadata like color and cancellation status.
/// All mutation operations update the `updated_at` timestamp.
///
/// # Fields
///
/// * `id` - Unique identifier for the event
/// * `calendar_id` - The calendar this event belongs to
/// * `title` - Display title of the event
/// * `description` - Optional detailed description
/// * `start` - Event start time in UTC
/// * `end` - Event end time in UTC
/// * `recurring` - Optional recurrence rule for repeating events
/// * `created_at` - When the event was created
/// * `updated_at` - When the event was last modified
/// * `color` - Color identifier (0-255)
/// * `is_all_day` - Whether this is an all-day event
/// * `is_cancelled` - Whether this event is cancelled
#[derive(Clone, Debug, Getters)]
#[getset(get = "pub")]
pub struct Event {
    id: Uuid,
    calendar_id: Uuid,
    title: String,
    description: Option<String>,
    start: DateTime<Utc>,
    end: DateTime<Utc>,
    recurring: Option<RecurrenceRule>,
    created_at: DateTime<Utc>,
    updated_at: DateTime<Utc>,
    color: u8,
    is_all_day: bool,
    is_cancelled: bool
}

/// Builder for constructing `Event` instances with validation.
///
/// Provides an ergonomic way to create events with required field validation.
/// Required fields are `calendar_id`, `title`, `start`, and `end`.
/// Optional fields have sensible defaults.
///
/// # Examples
///
/// ```
/// # use uuid::Uuid;
/// # use chrono::Utc;
/// # let cal_id = Uuid::new_v4();
/// let event = EventBuilder::new(
///     cal_id,
///     "Team Meeting".to_string(),
///     Utc::now(),
///     Utc::now() + chrono::Duration::hours(1)
/// )?
///     .color(5)
///     .description(Some("Quarterly sync".to_string()))
///     .build();
/// ```
pub struct EventBuilder {
    calendar_id: Uuid,
    title: String,
    start: DateTime<Utc>,
    end: DateTime<Utc>,
    description: Option<String>,
    recurring: Option<RecurrenceRule>,
    color: u8,
    is_all_day: bool,
    is_cancelled: bool,
}

impl EventBuilder {
    /// Creates a new `EventBuilder` with required fields.
    ///
    /// # Arguments
    ///
    /// * `calendar_id` - The calendar to which this event belongs
    /// * `title` - The display title of the event (cannot be empty)
    /// * `start` - The event start time in UTC
    /// * `end` - The event end time in UTC (must be after `start`)
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - `title` is empty
    /// - `start` is not strictly before `end`
    pub fn new(
        calendar_id: Uuid,
        title: String,
        start: DateTime<Utc>,
        end: DateTime<Utc>
    ) -> Result<Self, String> {
        if title.is_empty() {
            Err("Title cannot be empty".to_string())
        } else if start >= end {
            Err("Start time must be strictly before end time".to_string())
        } else {
            Ok(Self {
                calendar_id,
                title,
                start,
                end,
                description: None,
                recurring: None,
                color: 0,
                is_all_day: false,
                is_cancelled: false
            })
        }
    }

    /// Sets the event description.
    ///
    /// # Arguments
    ///
    /// * `description` - Optional detailed description of the event
    pub fn description(mut self, description: Option<String>) -> Self {
        self.description = description;
        self
    }

    /// Sets the recurrence rule for repeating events.
    ///
    /// # Arguments
    ///
    /// * `recurring` - Optional recurrence rule defining repetition pattern
    pub fn recurring(mut self, recurring: Option<RecurrenceRule>) -> Self {
        self.recurring = recurring;
        self
    }

    /// Sets the color identifier for the event.
    ///
    /// # Arguments
    ///
    /// * `color` - Color identifier in range 0-255
    pub fn color(mut self, color: u8) -> Self {
        self.color = color;
        self
    }

    /// Sets whether this is an all-day event.
    ///
    /// # Arguments
    ///
    /// * `is_all_day` - `true` if this is an all-day event, `false` otherwise
    pub fn is_all_day(mut self, is_all_day: bool) -> Self {
        self.is_all_day = is_all_day;
        self
    }

    /// Sets whether this event is cancelled.
    ///
    /// # Arguments
    ///
    /// * `is_cancelled` - `true` if this event is cancelled, `false` otherwise
    pub fn is_cancelled(mut self, is_cancelled: bool) -> Self {
        self.is_cancelled = is_cancelled;
        self
    }

    /// Builds and returns the constructed `Event`.
    ///
    /// Sets the `id`, `created_at`, and `updated_at` fields to automatically
    /// generated values. The `id` is a new UUID, and both timestamps are set
    /// to the current UTC time.
    pub fn build(self) -> Event {
        let now = Utc::now();
        Event {
            id: Uuid::new_v4(),
            calendar_id: self.calendar_id,
            title: self.title,
            description: self.description,
            start: self.start,
            end: self.end,
            recurring: self.recurring,
            created_at: now,
            updated_at: now,
            color: self.color,
            is_all_day: self.is_all_day,
            is_cancelled: self.is_cancelled,
        }
    }
}

impl Event { 
    /// Updates the event title.
    ///
    /// # Errors
    /// Returns an error if the title is empty.
    pub fn update_title(&mut self, title: String) -> Result<(), String> {
        if title.is_empty() {
            Err("Title cannot be empty".to_string())
        } else {
            self.title = title;
            self.touch();
            Ok(())
        }
    }

    /// Updates the event description.
    pub fn update_description(&mut self, description: Option<String>) {
        self.description = description;
        self.touch();
    }

    /// Updates the recurrence rule.
    pub fn update_recurring(&mut self, recurring: Option<RecurrenceRule>) {
        self.recurring = recurring;
        self.touch();
    }

    /// Reschedules the event to new start and end times.
    ///
    /// # Errors
    /// Returns an error if `start` is not before `end`.
    pub fn reschedule(
        &mut self,
        start: DateTime<Utc>,
        end: DateTime<Utc>
    ) -> Result<(), String> {
        if start >= end {
            Err("Start time must be strictly before end time".to_string())
        } else {
            self.start = start;
            self.end = end;
            self.touch();
            Ok(())
        }
    }

    /// Updates the color identifier.
    pub fn update_color(&mut self, color: u8) {
        self.color = color;
        self.touch();
    }

    /// Updates whether this is an all-day event.
    pub fn update_all_day(&mut self, is_all_day: bool) {
        self.is_all_day = is_all_day;
        self.touch();
    }

    /// Updates whether this event is cancelled.
    pub fn update_is_cancelled(&mut self, is_cancelled: bool) {
        self.is_cancelled = is_cancelled;
        self.touch();
    }

    /// Returns the duration of the event.
    pub fn get_duration(&self) -> TimeDelta {
        self.end - self.start
    }

    /// Returns true if the event has ended.
    pub fn is_past(&self) -> bool {
        self.end < Utc::now()
    }

    /// Returns true if the event is currently happening.
    pub fn is_ongoing(&self) -> bool {
        let now = Utc::now();
        self.start <= now && now < self.end
    }

    /// Returns true if the event has not yet started.
    pub fn is_future(&self) -> bool {
        self.start > Utc::now()
    }

    /// Updates the `updated_at` timestamp to the current time.
    pub fn touch(&mut self) {
        self.updated_at = Utc::now();
    }
}