ItemId is the Url

This commit is contained in:
daladim 2021-03-21 23:54:33 +01:00
parent cbffef8b97
commit 499f25b9b9
5 changed files with 52 additions and 20 deletions

View file

@ -1,7 +1,11 @@
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
use std::str::FromStr;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use chrono::{Utc, DateTime}; use chrono::{Utc, DateTime};
use url::Url;
use crate::resource::Resource;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Item { pub enum Item {
@ -68,16 +72,37 @@ impl Item {
} }
} }
#[derive(Clone, Debug, PartialEq, Hash, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Hash, Serialize, Deserialize)]
pub struct ItemId { pub struct ItemId {
content: String, content: Url,
} }
impl ItemId{ impl ItemId{
pub fn new() -> Self { /// Generate a random ItemId. This is only useful in tests
let u = uuid::Uuid::new_v4().to_hyphenated().to_string(); pub fn random() -> Self {
let random = uuid::Uuid::new_v4().to_hyphenated().to_string();
let s = format!("https://server.com/{}", random);
let u = s.parse().unwrap();
Self { content:u } Self { content:u }
} }
} }
impl From<Url> for ItemId {
fn from(url: Url) -> Self {
Self { content: url }
}
}
impl From<&Resource> for ItemId {
fn from(resource: &Resource) -> Self {
Self { content: resource.url().clone() }
}
}
impl FromStr for ItemId {
type Err = url::ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
s.parse()
}
}
impl Eq for ItemId {} impl Eq for ItemId {}
impl Display for ItemId { impl Display for ItemId {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {

View file

@ -14,6 +14,7 @@ pub mod calendar;
pub use calendar::cached_calendar::CachedCalendar; pub use calendar::cached_calendar::CachedCalendar;
mod item; mod item;
pub use item::Item; pub use item::Item;
pub use item::ItemId;
mod task; mod task;
pub use task::Task; pub use task::Task;
mod event; mod event;

View file

@ -20,9 +20,9 @@ pub struct Task {
impl Task { impl Task {
/// Create a new Task /// Create a new Task
pub fn new(name: String, last_modified: DateTime<Utc>) -> Self { pub fn new(name: String, id: ItemId, last_modified: DateTime<Utc>) -> Self {
Self { Self {
id: ItemId::new(), id,
name, name,
last_modified, last_modified,
completed: false, completed: false,

View file

@ -47,6 +47,11 @@ async fn show_calendars() {
for (id, calendar) in calendars.iter() { for (id, calendar) in calendars.iter() {
let cal = calendar.lock().unwrap(); let cal = calendar.lock().unwrap();
println!(" {}\t{}", cal.name(), id.as_str()); println!(" {}\t{}", cal.name(), id.as_str());
println!(" IDs:");
for id in cal.get_item_ids().await.unwrap() {
println!(" * {}", id);
}
println!(" Most recent changes:"); println!(" Most recent changes:");
for (_id, task) in cal.get_items_modified_since(None, None).await.unwrap() { for (_id, task) in cal.get_items_modified_since(None, None).await.unwrap() {
my_tasks::utils::print_task(task); my_tasks::utils::print_task(task);

View file

@ -8,6 +8,7 @@ use my_tasks::traits::{CalDavSource, SyncSlave};
use my_tasks::traits::PartialCalendar; use my_tasks::traits::PartialCalendar;
use my_tasks::cache::Cache; use my_tasks::cache::Cache;
use my_tasks::Item; use my_tasks::Item;
use my_tasks::ItemId;
use my_tasks::Task; use my_tasks::Task;
use my_tasks::calendar::cached_calendar::CachedCalendar; use my_tasks::calendar::cached_calendar::CachedCalendar;
use my_tasks::Provider; use my_tasks::Provider;
@ -52,19 +53,19 @@ async fn populate_test_provider() -> Provider<Cache, CachedCalendar, Cache, Cach
let cal_id = Url::parse("http://todo.list/cal").unwrap(); let cal_id = Url::parse("http://todo.list/cal").unwrap();
let task_a = Item::Task(Task::new("task A".into(), Utc.ymd(2000, 1, 1).and_hms(0, 0, 0))); let task_a = Item::Task(Task::new("task A".into(), ItemId::random(), Utc.ymd(2000, 1, 1).and_hms(0, 0, 0)));
let task_b = Item::Task(Task::new("task B".into(), Utc.ymd(2000, 1, 2).and_hms(0, 0, 0))); let task_b = Item::Task(Task::new("task B".into(), ItemId::random(), Utc.ymd(2000, 1, 2).and_hms(0, 0, 0)));
let task_c = Item::Task(Task::new("task C".into(), Utc.ymd(2000, 1, 3).and_hms(0, 0, 0))); let task_c = Item::Task(Task::new("task C".into(), ItemId::random(), Utc.ymd(2000, 1, 3).and_hms(0, 0, 0)));
let task_d = Item::Task(Task::new("task D".into(), Utc.ymd(2000, 1, 4).and_hms(0, 0, 0))); let task_d = Item::Task(Task::new("task D".into(), ItemId::random(), Utc.ymd(2000, 1, 4).and_hms(0, 0, 0)));
let task_e = Item::Task(Task::new("task E".into(), Utc.ymd(2000, 1, 5).and_hms(0, 0, 0))); let task_e = Item::Task(Task::new("task E".into(), ItemId::random(), Utc.ymd(2000, 1, 5).and_hms(0, 0, 0)));
let task_f = Item::Task(Task::new("task F".into(), Utc.ymd(2000, 1, 6).and_hms(0, 0, 0))); let task_f = Item::Task(Task::new("task F".into(), ItemId::random(), Utc.ymd(2000, 1, 6).and_hms(0, 0, 0)));
let task_g = Item::Task(Task::new("task G".into(), Utc.ymd(2000, 1, 7).and_hms(0, 0, 0))); let task_g = Item::Task(Task::new("task G".into(), ItemId::random(), Utc.ymd(2000, 1, 7).and_hms(0, 0, 0)));
let task_h = Item::Task(Task::new("task H".into(), Utc.ymd(2000, 1, 8).and_hms(0, 0, 0))); let task_h = Item::Task(Task::new("task H".into(), ItemId::random(), Utc.ymd(2000, 1, 8).and_hms(0, 0, 0)));
let task_i = Item::Task(Task::new("task I".into(), Utc.ymd(2000, 1, 9).and_hms(0, 0, 0))); let task_i = Item::Task(Task::new("task I".into(), ItemId::random(), Utc.ymd(2000, 1, 9).and_hms(0, 0, 0)));
let task_j = Item::Task(Task::new("task J".into(), Utc.ymd(2000, 1, 10).and_hms(0, 0, 0))); let task_j = Item::Task(Task::new("task J".into(), ItemId::random(), Utc.ymd(2000, 1, 10).and_hms(0, 0, 0)));
let task_k = Item::Task(Task::new("task K".into(), Utc.ymd(2000, 1, 11).and_hms(0, 0, 0))); let task_k = Item::Task(Task::new("task K".into(), ItemId::random(), Utc.ymd(2000, 1, 11).and_hms(0, 0, 0)));
let task_l = Item::Task(Task::new("task L".into(), Utc.ymd(2000, 1, 12).and_hms(0, 0, 0))); let task_l = Item::Task(Task::new("task L".into(), ItemId::random(), Utc.ymd(2000, 1, 12).and_hms(0, 0, 0)));
let task_m = Item::Task(Task::new("task M".into(), Utc.ymd(2000, 1, 12).and_hms(0, 0, 0))); let task_m = Item::Task(Task::new("task M".into(), ItemId::random(), Utc.ymd(2000, 1, 12).and_hms(0, 0, 0)));
let last_sync = task_m.last_modified(); let last_sync = task_m.last_modified();
local.update_last_sync(Some(last_sync)); local.update_last_sync(Some(last_sync));
@ -128,7 +129,7 @@ async fn populate_test_provider() -> Provider<Cache, CachedCalendar, Cache, Cach
cal_server.delete_item(&task_l_id).await.unwrap(); cal_server.delete_item(&task_l_id).await.unwrap();
let task_n = Item::Task(Task::new("task N (new from server)".into(), Utc::now())); let task_n = Item::Task(Task::new("task N (new from server)".into(), ItemId::random(), Utc::now()));
cal_server.add_item(task_n).await; cal_server.add_item(task_n).await;
@ -157,7 +158,7 @@ async fn populate_test_provider() -> Provider<Cache, CachedCalendar, Cache, Cach
cal_local.delete_item(&task_k_id).await.unwrap(); cal_local.delete_item(&task_k_id).await.unwrap();
cal_local.delete_item(&task_l_id).await.unwrap(); cal_local.delete_item(&task_l_id).await.unwrap();
let task_o = Item::Task(Task::new("task O (new from local)".into(), Utc::now())); let task_o = Item::Task(Task::new("task O (new from local)".into(), ItemId::random(), Utc::now()));
cal_local.add_item(task_o).await; cal_local.add_item(task_o).await;
Provider::new(server, local) Provider::new(server, local)