From 9d2d83e06f6bb8bbc5ff8282064a4222445f33c4 Mon Sep 17 00:00:00 2001 From: daladim Date: Sun, 11 Apr 2021 19:58:42 +0200 Subject: [PATCH] [minor] API change for Task::new --- src/ical/parser.rs | 2 +- src/item.rs | 8 +++--- src/task.rs | 13 ++++++++-- tests/scenarii.rs | 62 +++++++++++++++++++++++----------------------- 4 files changed, 47 insertions(+), 38 deletions(-) diff --git a/src/ical/parser.rs b/src/ical/parser.rs index 31aa34f..5afd315 100644 --- a/src/ical/parser.rs +++ b/src/ical/parser.rs @@ -50,7 +50,7 @@ pub fn parse(content: &str, item_id: ItemId, sync_status: SyncStatus) -> Result< None => return Err(format!("Missing name for item {}", item_id).into()), }; - Item::Task(Task::new(name, item_id, sync_status, completed)) + Item::Task(Task::new_with_parameters(name, completed, item_id, sync_status)) }, }; diff --git a/src/item.rs b/src/item.rs index 08a38df..27772ba 100644 --- a/src/item.rs +++ b/src/item.rs @@ -7,6 +7,7 @@ use serde::{Deserialize, Serialize}; use url::Url; use crate::resource::Resource; +use crate::calendar::CalendarId; @@ -95,11 +96,10 @@ pub struct ItemId { content: Url, } impl ItemId{ - /// Generate a random ItemId. This should only be useful in tests - pub fn random() -> Self { + /// Generate a random ItemId. + pub fn random(parent_calendar: &CalendarId) -> Self { let random = uuid::Uuid::new_v4().to_hyphenated().to_string(); - let s = format!("https://server.com/{}", random); - let u = s.parse().unwrap(); + let u = parent_calendar.join(&random).unwrap(/* this cannot panic since we've just created a string that is a valid URL */); Self { content:u } } diff --git a/src/task.rs b/src/task.rs index 3e5a9ef..9e0dfd3 100644 --- a/src/task.rs +++ b/src/task.rs @@ -2,6 +2,7 @@ use serde::{Deserialize, Serialize}; use crate::item::ItemId; use crate::item::SyncStatus; +use crate::calendar::CalendarId; /// A to-do task #[derive(Clone, Debug, Serialize, Deserialize)] @@ -19,8 +20,16 @@ pub struct Task { } impl Task { - /// Create a new Task - pub fn new(name: String, id: ItemId, sync_status: SyncStatus, completed: bool) -> Self { + /// Create a brand new Task that is not on a server yet. + /// This will pick a new (random) task ID. + pub fn new(name: String, completed: bool, parent_calendar_id: &CalendarId) -> Self { + let new_item_id = ItemId::random(parent_calendar_id); + let new_sync_status = SyncStatus::NotSynced; + Self::new_with_parameters(name, completed, new_item_id, new_sync_status) + } + + /// Create a new Task instance, that may be synced already + pub fn new_with_parameters(name: String, completed: bool, id: ItemId, sync_status: SyncStatus) -> Self { Self { id, name, diff --git a/tests/scenarii.rs b/tests/scenarii.rs index 0b32d34..a91912e 100644 --- a/tests/scenarii.rs +++ b/tests/scenarii.rs @@ -90,7 +90,7 @@ pub fn scenarii_basic() -> Vec { tasks.push( ItemScenario { - id: ItemId::random(), + id: ItemId::random(&first_cal), initial_state: LocatedState::BothSynced( ItemState{ calendar: first_cal.clone(), name: String::from("Task A"), @@ -108,7 +108,7 @@ pub fn scenarii_basic() -> Vec { tasks.push( ItemScenario { - id: ItemId::random(), + id: ItemId::random(&first_cal), initial_state: LocatedState::BothSynced( ItemState{ calendar: first_cal.clone(), name: String::from("Task B"), @@ -122,7 +122,7 @@ pub fn scenarii_basic() -> Vec { tasks.push( ItemScenario { - id: ItemId::random(), + id: ItemId::random(&first_cal), initial_state: LocatedState::BothSynced( ItemState{ calendar: first_cal.clone(), name: String::from("Task C"), @@ -136,7 +136,7 @@ pub fn scenarii_basic() -> Vec { tasks.push( ItemScenario { - id: ItemId::random(), + id: ItemId::random(&first_cal), initial_state: LocatedState::BothSynced( ItemState{ calendar: first_cal.clone(), name: String::from("Task D"), @@ -154,7 +154,7 @@ pub fn scenarii_basic() -> Vec { tasks.push( ItemScenario { - id: ItemId::random(), + id: ItemId::random(&first_cal), initial_state: LocatedState::BothSynced( ItemState{ calendar: first_cal.clone(), name: String::from("Task E"), @@ -172,7 +172,7 @@ pub fn scenarii_basic() -> Vec { tasks.push( ItemScenario { - id: ItemId::random(), + id: ItemId::random(&first_cal), initial_state: LocatedState::BothSynced( ItemState{ calendar: first_cal.clone(), name: String::from("Task F"), @@ -191,7 +191,7 @@ pub fn scenarii_basic() -> Vec { tasks.push( ItemScenario { - id: ItemId::random(), + id: ItemId::random(&second_cal), initial_state: LocatedState::BothSynced( ItemState{ calendar: second_cal.clone(), name: String::from("Task G"), @@ -209,7 +209,7 @@ pub fn scenarii_basic() -> Vec { tasks.push( ItemScenario { - id: ItemId::random(), + id: ItemId::random(&second_cal), initial_state: LocatedState::BothSynced( ItemState{ calendar: second_cal.clone(), name: String::from("Task H"), @@ -227,7 +227,7 @@ pub fn scenarii_basic() -> Vec { tasks.push( ItemScenario { - id: ItemId::random(), + id: ItemId::random(&second_cal), initial_state: LocatedState::BothSynced( ItemState{ calendar: second_cal.clone(), name: String::from("Task I"), @@ -246,7 +246,7 @@ pub fn scenarii_basic() -> Vec { tasks.push( ItemScenario { - id: ItemId::random(), + id: ItemId::random(&second_cal), initial_state: LocatedState::BothSynced( ItemState{ calendar: second_cal.clone(), name: String::from("Task J"), @@ -260,7 +260,7 @@ pub fn scenarii_basic() -> Vec { tasks.push( ItemScenario { - id: ItemId::random(), + id: ItemId::random(&second_cal), initial_state: LocatedState::BothSynced( ItemState{ calendar: second_cal.clone(), name: String::from("Task K"), @@ -278,7 +278,7 @@ pub fn scenarii_basic() -> Vec { tasks.push( ItemScenario { - id: ItemId::random(), + id: ItemId::random(&second_cal), initial_state: LocatedState::BothSynced( ItemState{ calendar: second_cal.clone(), name: String::from("Task L"), @@ -292,7 +292,7 @@ pub fn scenarii_basic() -> Vec { tasks.push( ItemScenario { - id: ItemId::random(), + id: ItemId::random(&second_cal), initial_state: LocatedState::BothSynced( ItemState{ calendar: second_cal.clone(), name: String::from("Task M"), @@ -310,7 +310,7 @@ pub fn scenarii_basic() -> Vec { tasks.push( ItemScenario { - id: ItemId::random(), + id: ItemId::random(&third_cal), initial_state: LocatedState::BothSynced( ItemState{ calendar: third_cal.clone(), name: String::from("Task N"), @@ -328,7 +328,7 @@ pub fn scenarii_basic() -> Vec { tasks.push( ItemScenario { - id: ItemId::random(), + id: ItemId::random(&third_cal), initial_state: LocatedState::BothSynced( ItemState{ calendar: third_cal.clone(), name: String::from("Task O"), @@ -344,7 +344,7 @@ pub fn scenarii_basic() -> Vec { } ); - let id_p = ItemId::random(); + let id_p = ItemId::random(&third_cal); tasks.push( ItemScenario { id: id_p.clone(), @@ -366,14 +366,14 @@ pub fn scenarii_basic() -> Vec { } ); - let id_q = ItemId::random(); + let id_q = ItemId::random(&third_cal); tasks.push( ItemScenario { id: id_q.clone(), initial_state: LocatedState::None, local_changes_to_apply: Vec::new(), remote_changes_to_apply: vec![ChangeToApply::Create(third_cal.clone(), Item::Task( - Task::new(String::from("Task Q, created on the server"), id_q, SyncStatus::random_synced(), false ) + Task::new_with_parameters(String::from("Task Q, created on the server"), false, id_q, SyncStatus::random_synced() ) ))], after_sync: LocatedState::BothSynced( ItemState{ calendar: third_cal.clone(), @@ -383,13 +383,13 @@ pub fn scenarii_basic() -> Vec { } ); - let id_r = ItemId::random(); + let id_r = ItemId::random(&third_cal); tasks.push( ItemScenario { id: id_r.clone(), initial_state: LocatedState::None, local_changes_to_apply: vec![ChangeToApply::Create(third_cal.clone(), Item::Task( - Task::new(String::from("Task R, created locally"), id_r, SyncStatus::NotSynced, false ) + Task::new_with_parameters(String::from("Task R, created locally"), false, id_r, SyncStatus::NotSynced ) ))], remote_changes_to_apply: Vec::new(), after_sync: LocatedState::BothSynced( ItemState{ @@ -412,7 +412,7 @@ pub fn scenarii_first_sync_to_local() -> Vec { tasks.push( ItemScenario { - id: ItemId::random(), + id: ItemId::random(&cal1), initial_state: LocatedState::Remote( ItemState{ calendar: cal1.clone(), name: String::from("Task A1"), @@ -430,7 +430,7 @@ pub fn scenarii_first_sync_to_local() -> Vec { tasks.push( ItemScenario { - id: ItemId::random(), + id: ItemId::random(&cal2), initial_state: LocatedState::Remote( ItemState{ calendar: cal2.clone(), name: String::from("Task A2"), @@ -448,7 +448,7 @@ pub fn scenarii_first_sync_to_local() -> Vec { tasks.push( ItemScenario { - id: ItemId::random(), + id: ItemId::random(&cal1), initial_state: LocatedState::Remote( ItemState{ calendar: cal1.clone(), name: String::from("Task B1"), @@ -476,7 +476,7 @@ pub fn scenarii_first_sync_to_server() -> Vec { tasks.push( ItemScenario { - id: ItemId::random(), + id: ItemId::random(&cal3), initial_state: LocatedState::Local( ItemState{ calendar: cal3.clone(), name: String::from("Task A3"), @@ -494,7 +494,7 @@ pub fn scenarii_first_sync_to_server() -> Vec { tasks.push( ItemScenario { - id: ItemId::random(), + id: ItemId::random(&cal4), initial_state: LocatedState::Local( ItemState{ calendar: cal4.clone(), name: String::from("Task A4"), @@ -512,7 +512,7 @@ pub fn scenarii_first_sync_to_server() -> Vec { tasks.push( ItemScenario { - id: ItemId::random(), + id: ItemId::random(&cal3), initial_state: LocatedState::Local( ItemState{ calendar: cal3.clone(), name: String::from("Task B3"), @@ -540,7 +540,7 @@ pub fn scenarii_transient_task() -> Vec { tasks.push( ItemScenario { - id: ItemId::random(), + id: ItemId::random(&cal), initial_state: LocatedState::Local( ItemState{ calendar: cal.clone(), name: String::from("A task, so that the calendar actually exists"), @@ -556,14 +556,14 @@ pub fn scenarii_transient_task() -> Vec { } ); - let id_transient = ItemId::random(); + let id_transient = ItemId::random(&cal); tasks.push( ItemScenario { id: id_transient.clone(), initial_state: LocatedState::None, local_changes_to_apply: vec![ ChangeToApply::Create(cal, Item::Task( - Task::new(String::from("A transient task that will be deleted before the sync"), id_transient, SyncStatus::NotSynced, false ) + Task::new_with_parameters(String::from("A transient task that will be deleted before the sync"), false, id_transient, SyncStatus::NotSynced ) )), ChangeToApply::Rename(String::from("A new name")), @@ -613,11 +613,11 @@ async fn populate_test_provider(scenarii: &[ItemScenario], mock_behaviour: Arc