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
pub mod google;
pub mod google_macros;
pub mod memory;
pub mod remote;
pub mod unixfile;

use crate::{
    cli::EntryParser,
    record::{Record, RecurringRecord},
};
use async_trait::async_trait;

#[async_trait]
pub trait DB: Send {
    async fn load(&mut self) -> Result<(), anyhow::Error>;
    async fn dump(&self) -> Result<(), anyhow::Error>;

    fn next_key(&mut self) -> u64 {
        let key = self.primary_key() + 1;
        self.set_primary_key(key);
        key
    }

    fn next_recurrence_key(&mut self) -> u64 {
        let key = self.recurrence_key() + 1;
        self.set_recurrence_key(key);
        key
    }

    fn primary_key(&self) -> u64;
    fn set_primary_key(&mut self, primary_key: u64);
    fn recurrence_key(&self) -> u64;
    fn set_recurrence_key(&mut self, primary_key: u64);

    async fn record_entry(&mut self, entry: EntryParser) -> Result<(), anyhow::Error> {
        let record = entry.to_record()?;
        let recurrence = record.recurrence();
        let mut record = record.record();
        record.set_primary_key(self.next_key());

        if let Some(mut recurrence) = recurrence {
            let key = self.next_recurrence_key();
            record.set_recurrence_key(Some(key));
            recurrence.set_recurrence_key(key);
            self.record_recurrence(recurrence).await?;
        }

        self.insert_record(record).await?;
        Ok(())
    }

    async fn delete(&mut self, primary_key: u64) -> Result<(), anyhow::Error>;
    async fn delete_recurrence(&mut self, primary_key: u64) -> Result<(), anyhow::Error>;
    async fn record(&mut self, record: Record) -> Result<(), anyhow::Error>;
    async fn record_recurrence(&mut self, record: RecurringRecord) -> Result<(), anyhow::Error>;
    async fn insert_record(&mut self, record: Record) -> Result<(), anyhow::Error>;
    async fn insert_recurrence(&mut self, record: RecurringRecord) -> Result<(), anyhow::Error>;
    async fn list_recurrence(&mut self) -> Result<Vec<RecurringRecord>, anyhow::Error>;
    async fn update_recurrence(&mut self) -> Result<(), anyhow::Error>;
    async fn list_today(&mut self, include_completed: bool) -> Result<Vec<Record>, anyhow::Error>;
    async fn list_all(&mut self, include_completed: bool) -> Result<Vec<Record>, anyhow::Error>;
    async fn events_now(
        &mut self,
        last: chrono::Duration,
        include_completed: bool,
    ) -> Result<Vec<Record>, anyhow::Error>;
    async fn complete_task(&mut self, primary_key: u64) -> Result<(), anyhow::Error>;
}

#[async_trait]
pub trait RemoteClient {
    async fn delete(&mut self, calendar_id: String, event_id: String) -> Result<(), anyhow::Error>;
    async fn delete_recurrence(
        &mut self,
        calendar_id: String,
        event_id: String,
    ) -> Result<(), anyhow::Error>;
    async fn record(
        &mut self,
        calendar_id: String,
        record: Record,
    ) -> Result<String, anyhow::Error>;
    async fn record_recurrence(
        &mut self,
        calendar_id: String,
        record: RecurringRecord,
    ) -> Result<String, anyhow::Error>;
    async fn list_recurrence(
        &mut self,
        calendar_id: String,
    ) -> Result<Vec<RecurringRecord>, anyhow::Error>;
    async fn update_recurrence(&mut self, calendar_id: String) -> Result<(), anyhow::Error>;
    async fn list_today(
        &mut self,
        calendar_id: String,
        include_completed: bool,
    ) -> Result<Vec<Record>, anyhow::Error>;
    async fn list_all(
        &mut self,
        calendar_id: String,
        include_completed: bool,
    ) -> Result<Vec<Record>, anyhow::Error>;
    async fn events_now(
        &mut self,
        calendar_id: String,
        last: chrono::Duration,
        include_completed: bool,
    ) -> Result<Vec<Record>, anyhow::Error>;
    async fn complete_task(
        &mut self,
        calendar_id: String,
        primary_key: u64,
    ) -> Result<(), anyhow::Error>;
}