aboutsummaryrefslogtreecommitdiffstats
path: root/src/task.rs
blob: dcfe7761f76bd4d9b28fc7758de52ee75bf7ba5b (plain) (blame)
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
use std::{
    borrow::{Borrow, Cow},
    str::FromStr,
};

use chrono::{NaiveDateTime, TimeDelta};
use sqlx::{
    encode::IsNull,
    sqlite::{SqliteArgumentValue, SqliteTypeInfo, SqliteValue},
    Decode, Sqlite, Type, TypeInfo, Value, ValueRef,
};

pub struct Task {
    pub name: String,
    pub cron: Option<Schedule>,
    pub archived: bool,
    pub description: Option<String>,
}

impl Task {
    pub fn new(
        name: String,
        cron: Option<Schedule>,
        archived: bool,
        description: Option<String>,
    ) -> Self {
        Self {
            name,
            cron,
            archived,
            description,
        }
    }
}

pub struct Schedule(cron::Schedule);

impl sqlx::Encode<'_, Sqlite> for Schedule {
    fn encode_by_ref(
        &self,
        buf: &mut <Sqlite as sqlx::Database>::ArgumentBuffer<'_>,
    ) -> Result<sqlx::encode::IsNull, sqlx::error::BoxDynError> {
        let schedule = &self.0;
        let schedule = schedule.to_string();
        buf.push(SqliteArgumentValue::Text(Cow::Owned(schedule)));

        Ok(IsNull::No)
    }
}

impl sqlx::Decode<'_, Sqlite> for Schedule {
    fn decode(
        value: <Sqlite as sqlx::Database>::ValueRef<'_>,
    ) -> Result<Self, sqlx::error::BoxDynError> {
        let schedule = Decode::<Sqlite>::decode(value)?;
        let schedule = cron::Schedule::from_str(schedule)?;
        Ok(Self(schedule))
    }
}

impl Type<Sqlite> for Schedule {
    fn type_info() -> <Sqlite as sqlx::Database>::TypeInfo {
        <&str as Type<Sqlite>>::type_info()
    }
}

pub struct Category(String);

impl Category {
    pub fn new(category: String) -> Self {
        Self(category)
    }
}

pub struct Log(NaiveDateTime);

impl Log {
    pub fn new(datetime: NaiveDateTime) -> Self {
        Self(datetime)
    }
}

pub struct Reminder(TimeDelta);

impl Reminder {
    pub fn new(time_delta: TimeDelta) -> Self {
        Self(time_delta)
    }
}