kitchen-freezer/src/task.rs

68 lines
1.9 KiB
Rust
Raw Normal View History

2021-02-27 11:58:40 +01:00
use std::fmt::{Display, Formatter};
2021-02-25 00:50:49 +01:00
use chrono::{Utc, DateTime};
2021-02-24 17:38:08 +01:00
use serde::{Deserialize, Serialize};
2021-02-25 00:50:49 +01:00
// TODO: turn into this one day
// pub type TaskId = String; // This is an HTML "etag"
2021-02-27 11:58:40 +01:00
#[derive(Clone, Debug, PartialEq, Hash, Serialize, Deserialize)]
2021-02-25 00:50:49 +01:00
pub struct TaskId {
content: String,
}
impl TaskId{
pub fn new() -> Self {
let u = uuid::Uuid::new_v4().to_hyphenated().to_string();
Self { content:u }
}
}
2021-02-27 11:58:40 +01:00
impl Eq for TaskId {}
impl Display for TaskId {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "{}", self.content)
}
}
2021-02-19 07:58:53 +01:00
2021-02-18 12:02:04 +01:00
/// A to-do task
2021-02-24 17:38:08 +01:00
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
2021-02-18 12:02:04 +01:00
pub struct Task {
2021-02-19 07:58:53 +01:00
id: TaskId,
2021-02-18 12:02:04 +01:00
name: String,
2021-02-25 00:50:49 +01:00
last_modified: DateTime<Utc>,
2021-02-20 00:10:05 +01:00
completed: bool,
2021-02-18 12:02:04 +01:00
}
impl Task {
2021-02-25 00:50:49 +01:00
/// Create a new Task
pub fn new(name: String, last_modified: DateTime<Utc>) -> Self {
Self {
name, last_modified,
id: TaskId::new(),
completed: false
}
}
2021-02-21 23:45:51 +01:00
pub fn id(&self) -> &TaskId { &self.id }
2021-02-20 00:10:05 +01:00
pub fn name(&self) -> &str { &self.name }
2021-02-18 12:02:04 +01:00
pub fn completed(&self) -> bool { self.completed }
2021-02-25 00:50:49 +01:00
pub fn last_modified(&self) -> DateTime<Utc> { self.last_modified }
fn update_last_modified(&mut self) {
self.last_modified = Utc::now();
}
2021-02-18 12:02:04 +01:00
2021-02-25 00:50:49 +01:00
/// Rename a task.
/// This updates its "last modified" field
pub fn set_name(&mut self, new_name: String) {
self.update_last_modified();
self.name = new_name;
}
pub fn set_completed(&mut self, new_value: bool) {
2021-02-18 12:02:04 +01:00
// TODO: either require a reference to the DataSource, so that it is aware
// or change a flag here, and the DataSource will be able to check the flags of all its content (but then the Calendar should only give a reference/Arc, not a clone)
2021-02-25 00:50:49 +01:00
self.update_last_modified();
self.completed = new_value;
2021-02-18 12:02:04 +01:00
}
2021-02-25 00:50:49 +01:00
}