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
//! Special types that may require validation to ensure they don't contain invalid values.

use crate::error::{InvalidDescription, InvalidReminderTime};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::borrow::Borrow;

/// A time at which a user should be reminded that they have something to do.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Reminder {
    time: DateTime<Utc>,
}

impl Reminder {
    /// Creates a new reminder, verifying that the time is after the supplied `current_time`.
    pub fn new(
        reminder_time: DateTime<Utc>,
        current_time: DateTime<Utc>,
    ) -> Result<Reminder, InvalidReminderTime> {
        if reminder_time <= current_time {
            Err(InvalidReminderTime)
        } else {
            Ok(Reminder {
                time: reminder_time,
            })
        }
    }

    /// Gets the underlying time of the reminder.
    pub fn get_time(&self) -> DateTime<Utc> {
        self.time
    }
}

/// A description of a task to be done.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Description {
    text: String,
}

impl Description {
    /// Creates a new description, verifying that the text provided is not empty.
    pub fn new<S: Into<String>>(text: S) -> Result<Description, InvalidDescription> {
        let text = text.into();
        if text.is_empty() {
            Err(InvalidDescription)
        } else {
            Ok(Description { text })
        }
    }

    /// Provides access to the description text as a `&str`.
    pub fn as_str(&self) -> &str {
        self.text.borrow()
    }
}

impl Borrow<str> for Description {
    fn borrow(&self) -> &str {
        self.text.borrow()
    }
}