taskchampion/task/
data.rs

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
use crate::{storage::TaskMap, Operation, Operations};
use chrono::Utc;
use uuid::Uuid;

/// A task.
///
/// This type presents a low-level interface consisting only of a key/value map. Interpretation of
/// fields is up to the user, and modifications both modify the [`TaskData`] and create one or
/// more [`Operation`](crate::Operation) values that can later be committed to the replica.
///
/// This interface is intended for sophisticated applications like Taskwarrior which give meaning
/// to key and values themselves. Use [`Task`](crate::Task) for a higher-level interface with
/// methods to update status, set tags, and so on.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct TaskData {
    uuid: Uuid,
    taskmap: TaskMap,
}

impl TaskData {
    /// Constructor for a TaskData representing an existing task.
    pub(crate) fn new(uuid: Uuid, taskmap: TaskMap) -> Self {
        Self { uuid, taskmap }
    }

    /// Create a new, empty task with the given UUID.
    pub fn create(uuid: Uuid, operations: &mut Operations) -> Self {
        operations.push(Operation::Create { uuid });
        Self {
            uuid,
            taskmap: TaskMap::new(),
        }
    }

    /// Get this task's UUID.
    pub fn get_uuid(&self) -> Uuid {
        self.uuid
    }

    /// Get the taskmap (used only for deprecated `Task::get_taskmap`).
    pub(in crate::task) fn get_taskmap(&self) -> &TaskMap {
        &self.taskmap
    }

    /// Get a value on this task.
    pub fn get(&self, property: impl AsRef<str>) -> Option<&str> {
        self.taskmap.get(property.as_ref()).map(|v| v.as_str())
    }

    /// Check if the given property is set.
    pub fn has(&self, property: impl AsRef<str>) -> bool {
        self.taskmap.contains_key(property.as_ref())
    }

    /// Enumerate all properties on this task, in arbitrary order.
    pub fn properties(&self) -> impl Iterator<Item = &String> {
        self.taskmap.keys()
    }

    /// Enumerate all properties and their values on this task, in arbitrary order.
    pub fn iter(&self) -> impl Iterator<Item = (&String, &String)> {
        self.taskmap.iter()
    }

    /// Set or remove a value on this task, adding an Update operation to the
    /// set of operations.
    ///
    /// Setting a value to `None` removes that value from the task.
    ///
    /// This method does not have any special handling of the `modified` property.
    pub fn update(
        &mut self,
        property: impl Into<String>,
        value: Option<String>,
        operations: &mut Operations,
    ) {
        let property = property.into();
        let old_value = self.taskmap.get(&property).cloned();
        if let Some(value) = &value {
            self.taskmap.insert(property.clone(), value.clone());
        } else {
            self.taskmap.remove(&property);
        }
        operations.push(Operation::Update {
            uuid: self.uuid,
            property,
            old_value,
            value,
            timestamp: Utc::now(),
        });
    }

    /// Delete this task.
    ///
    /// Note that this is different from setting status to [`Deleted`](crate::Status::Deleted):
    /// the resulting operation removes the task from the database.
    ///
    /// Deletion may interact poorly with modifications to the same task on other replicas. For
    /// example, if a task is deleted on replica 1 and its description modified on replica 2, then
    /// after both replicas have fully synced, the resulting task will only have a `description`
    /// property.
    ///
    /// After this call, the `TaskData` value still exists but has no properties and should be
    /// dropped.
    pub fn delete(&mut self, operations: &mut Operations) {
        operations.push(Operation::Delete {
            uuid: self.uuid,
            old_task: std::mem::take(&mut self.taskmap),
        });
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use chrono::DateTime;
    use pretty_assertions::assert_eq;

    const TEST_UUID: Uuid = Uuid::from_u128(1234);

    fn make_ops(ops: &[Operation]) -> Operations {
        let mut res = Operations::new();
        for op in ops {
            res.push(op.clone());
        }
        res
    }

    /// Set all operations' timestamps to the given timestamp, to ease use of
    /// `assert_eq!`.
    pub fn set_all_timestamps(ops: &mut Operations, set_to: DateTime<Utc>) {
        for op in ops {
            if let Operation::Update { timestamp, .. } = op {
                *timestamp = set_to;
            }
        }
    }
    #[test]
    fn create() {
        let mut ops = Operations::new();
        let t = TaskData::create(TEST_UUID, &mut ops);
        assert_eq!(t.uuid, TEST_UUID);
        assert_eq!(t.get_uuid(), TEST_UUID);
        assert_eq!(t.taskmap, TaskMap::new());
        assert_eq!(ops, make_ops(&[Operation::Create { uuid: TEST_UUID }]));
    }

    #[test]
    fn get_uuid() {
        let t = TaskData::new(TEST_UUID, TaskMap::new());
        assert_eq!(t.get_uuid(), TEST_UUID);
    }

    #[test]
    fn get() {
        let t = TaskData::new(TEST_UUID, [("prop".to_string(), "val".to_string())].into());
        assert_eq!(t.get("prop"), Some("val"));
        assert_eq!(t.get("nosuch"), None)
    }

    #[test]
    fn has() {
        let t = TaskData::new(TEST_UUID, [("prop".to_string(), "val".to_string())].into());
        assert!(t.has("prop"));
        assert!(!t.has("nosuch"));
    }

    #[test]
    fn properties() {
        let t = TaskData::new(
            TEST_UUID,
            [
                ("prop1".to_string(), "val".to_string()),
                ("prop2".to_string(), "val".to_string()),
            ]
            .into(),
        );
        let mut props: Vec<_> = t.properties().collect();
        props.sort();
        assert_eq!(props, vec!["prop1", "prop2"]);
    }

    #[test]
    fn iter() {
        let t = TaskData::new(
            TEST_UUID,
            [
                ("prop1".to_string(), "val1".to_string()),
                ("prop2".to_string(), "val2".to_string()),
            ]
            .into(),
        );
        let mut props: Vec<_> = t.iter().map(|(p, v)| (p.as_str(), v.as_str())).collect();
        props.sort();
        assert_eq!(props, vec![("prop1", "val1"), ("prop2", "val2")]);
    }

    #[test]
    fn update_new_prop() {
        let mut ops = Operations::new();
        let mut t = TaskData::new(TEST_UUID, TaskMap::new());
        t.update("prop1", Some("val1".into()), &mut ops);
        let now = Utc::now();
        set_all_timestamps(&mut ops, now);
        assert_eq!(
            ops,
            make_ops(&[Operation::Update {
                uuid: TEST_UUID,
                property: "prop1".into(),
                old_value: None,
                value: Some("val1".into()),
                timestamp: now,
            }])
        );
        assert_eq!(t.get("prop1"), Some("val1"));
    }

    #[test]
    fn update_existing_prop() {
        let mut ops = Operations::new();
        let mut t = TaskData::new(TEST_UUID, [("prop1".to_string(), "val".to_string())].into());
        t.update("prop1", Some("new".into()), &mut ops);
        let now = Utc::now();
        set_all_timestamps(&mut ops, now);
        assert_eq!(
            ops,
            make_ops(&[Operation::Update {
                uuid: TEST_UUID,
                property: "prop1".into(),
                old_value: Some("val".into()),
                value: Some("new".into()),
                timestamp: now,
            }])
        );
        assert_eq!(t.get("prop1"), Some("new"));
    }

    #[test]
    fn update_remove_prop() {
        let mut ops = Operations::new();
        let mut t = TaskData::new(TEST_UUID, [("prop1".to_string(), "val".to_string())].into());
        t.update("prop1", None, &mut ops);
        let now = Utc::now();
        set_all_timestamps(&mut ops, now);
        assert_eq!(
            ops,
            make_ops(&[Operation::Update {
                uuid: TEST_UUID,
                property: "prop1".into(),
                old_value: Some("val".into()),
                value: None,
                timestamp: now,
            }])
        );
        assert_eq!(t.get("prop1"), None);
    }

    #[test]
    fn delete() {
        let mut ops = Operations::new();
        let mut t = TaskData::new(TEST_UUID, [("prop1".to_string(), "val".to_string())].into());
        t.delete(&mut ops);
        assert_eq!(
            ops,
            make_ops(&[Operation::Delete {
                uuid: TEST_UUID,
                old_task: [("prop1".to_string(), "val".to_string())].into(),
            }])
        );
    }
}