[minor] API change for Task::new

This commit is contained in:
daladim 2021-04-11 19:58:42 +02:00
parent 18e2d0a96a
commit 9d2d83e06f
4 changed files with 47 additions and 38 deletions

View file

@ -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()), 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))
}, },
}; };

View file

@ -7,6 +7,7 @@ use serde::{Deserialize, Serialize};
use url::Url; use url::Url;
use crate::resource::Resource; use crate::resource::Resource;
use crate::calendar::CalendarId;
@ -95,11 +96,10 @@ pub struct ItemId {
content: Url, content: Url,
} }
impl ItemId{ impl ItemId{
/// Generate a random ItemId. This should only be useful in tests /// Generate a random ItemId.
pub fn random() -> Self { pub fn random(parent_calendar: &CalendarId) -> Self {
let random = uuid::Uuid::new_v4().to_hyphenated().to_string(); let random = uuid::Uuid::new_v4().to_hyphenated().to_string();
let s = format!("https://server.com/{}", random); let u = parent_calendar.join(&random).unwrap(/* this cannot panic since we've just created a string that is a valid URL */);
let u = s.parse().unwrap();
Self { content:u } Self { content:u }
} }

View file

@ -2,6 +2,7 @@ use serde::{Deserialize, Serialize};
use crate::item::ItemId; use crate::item::ItemId;
use crate::item::SyncStatus; use crate::item::SyncStatus;
use crate::calendar::CalendarId;
/// A to-do task /// A to-do task
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
@ -19,8 +20,16 @@ pub struct Task {
} }
impl Task { impl Task {
/// Create a new Task /// Create a brand new Task that is not on a server yet.
pub fn new(name: String, id: ItemId, sync_status: SyncStatus, completed: bool) -> Self { /// 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 { Self {
id, id,
name, name,

View file

@ -90,7 +90,7 @@ pub fn scenarii_basic() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(&first_cal),
initial_state: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: first_cal.clone(), calendar: first_cal.clone(),
name: String::from("Task A"), name: String::from("Task A"),
@ -108,7 +108,7 @@ pub fn scenarii_basic() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(&first_cal),
initial_state: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: first_cal.clone(), calendar: first_cal.clone(),
name: String::from("Task B"), name: String::from("Task B"),
@ -122,7 +122,7 @@ pub fn scenarii_basic() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(&first_cal),
initial_state: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: first_cal.clone(), calendar: first_cal.clone(),
name: String::from("Task C"), name: String::from("Task C"),
@ -136,7 +136,7 @@ pub fn scenarii_basic() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(&first_cal),
initial_state: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: first_cal.clone(), calendar: first_cal.clone(),
name: String::from("Task D"), name: String::from("Task D"),
@ -154,7 +154,7 @@ pub fn scenarii_basic() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(&first_cal),
initial_state: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: first_cal.clone(), calendar: first_cal.clone(),
name: String::from("Task E"), name: String::from("Task E"),
@ -172,7 +172,7 @@ pub fn scenarii_basic() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(&first_cal),
initial_state: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: first_cal.clone(), calendar: first_cal.clone(),
name: String::from("Task F"), name: String::from("Task F"),
@ -191,7 +191,7 @@ pub fn scenarii_basic() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(&second_cal),
initial_state: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: second_cal.clone(), calendar: second_cal.clone(),
name: String::from("Task G"), name: String::from("Task G"),
@ -209,7 +209,7 @@ pub fn scenarii_basic() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(&second_cal),
initial_state: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: second_cal.clone(), calendar: second_cal.clone(),
name: String::from("Task H"), name: String::from("Task H"),
@ -227,7 +227,7 @@ pub fn scenarii_basic() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(&second_cal),
initial_state: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: second_cal.clone(), calendar: second_cal.clone(),
name: String::from("Task I"), name: String::from("Task I"),
@ -246,7 +246,7 @@ pub fn scenarii_basic() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(&second_cal),
initial_state: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: second_cal.clone(), calendar: second_cal.clone(),
name: String::from("Task J"), name: String::from("Task J"),
@ -260,7 +260,7 @@ pub fn scenarii_basic() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(&second_cal),
initial_state: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: second_cal.clone(), calendar: second_cal.clone(),
name: String::from("Task K"), name: String::from("Task K"),
@ -278,7 +278,7 @@ pub fn scenarii_basic() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(&second_cal),
initial_state: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: second_cal.clone(), calendar: second_cal.clone(),
name: String::from("Task L"), name: String::from("Task L"),
@ -292,7 +292,7 @@ pub fn scenarii_basic() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(&second_cal),
initial_state: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: second_cal.clone(), calendar: second_cal.clone(),
name: String::from("Task M"), name: String::from("Task M"),
@ -310,7 +310,7 @@ pub fn scenarii_basic() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(&third_cal),
initial_state: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: third_cal.clone(), calendar: third_cal.clone(),
name: String::from("Task N"), name: String::from("Task N"),
@ -328,7 +328,7 @@ pub fn scenarii_basic() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(&third_cal),
initial_state: LocatedState::BothSynced( ItemState{ initial_state: LocatedState::BothSynced( ItemState{
calendar: third_cal.clone(), calendar: third_cal.clone(),
name: String::from("Task O"), name: String::from("Task O"),
@ -344,7 +344,7 @@ pub fn scenarii_basic() -> Vec<ItemScenario> {
} }
); );
let id_p = ItemId::random(); let id_p = ItemId::random(&third_cal);
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: id_p.clone(), id: id_p.clone(),
@ -366,14 +366,14 @@ pub fn scenarii_basic() -> Vec<ItemScenario> {
} }
); );
let id_q = ItemId::random(); let id_q = ItemId::random(&third_cal);
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: id_q.clone(), id: id_q.clone(),
initial_state: LocatedState::None, initial_state: LocatedState::None,
local_changes_to_apply: Vec::new(), local_changes_to_apply: Vec::new(),
remote_changes_to_apply: vec![ChangeToApply::Create(third_cal.clone(), Item::Task( 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{ after_sync: LocatedState::BothSynced( ItemState{
calendar: third_cal.clone(), calendar: third_cal.clone(),
@ -383,13 +383,13 @@ pub fn scenarii_basic() -> Vec<ItemScenario> {
} }
); );
let id_r = ItemId::random(); let id_r = ItemId::random(&third_cal);
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: id_r.clone(), id: id_r.clone(),
initial_state: LocatedState::None, initial_state: LocatedState::None,
local_changes_to_apply: vec![ChangeToApply::Create(third_cal.clone(), Item::Task( 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(), remote_changes_to_apply: Vec::new(),
after_sync: LocatedState::BothSynced( ItemState{ after_sync: LocatedState::BothSynced( ItemState{
@ -412,7 +412,7 @@ pub fn scenarii_first_sync_to_local() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(&cal1),
initial_state: LocatedState::Remote( ItemState{ initial_state: LocatedState::Remote( ItemState{
calendar: cal1.clone(), calendar: cal1.clone(),
name: String::from("Task A1"), name: String::from("Task A1"),
@ -430,7 +430,7 @@ pub fn scenarii_first_sync_to_local() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(&cal2),
initial_state: LocatedState::Remote( ItemState{ initial_state: LocatedState::Remote( ItemState{
calendar: cal2.clone(), calendar: cal2.clone(),
name: String::from("Task A2"), name: String::from("Task A2"),
@ -448,7 +448,7 @@ pub fn scenarii_first_sync_to_local() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(&cal1),
initial_state: LocatedState::Remote( ItemState{ initial_state: LocatedState::Remote( ItemState{
calendar: cal1.clone(), calendar: cal1.clone(),
name: String::from("Task B1"), name: String::from("Task B1"),
@ -476,7 +476,7 @@ pub fn scenarii_first_sync_to_server() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(&cal3),
initial_state: LocatedState::Local( ItemState{ initial_state: LocatedState::Local( ItemState{
calendar: cal3.clone(), calendar: cal3.clone(),
name: String::from("Task A3"), name: String::from("Task A3"),
@ -494,7 +494,7 @@ pub fn scenarii_first_sync_to_server() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(&cal4),
initial_state: LocatedState::Local( ItemState{ initial_state: LocatedState::Local( ItemState{
calendar: cal4.clone(), calendar: cal4.clone(),
name: String::from("Task A4"), name: String::from("Task A4"),
@ -512,7 +512,7 @@ pub fn scenarii_first_sync_to_server() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(&cal3),
initial_state: LocatedState::Local( ItemState{ initial_state: LocatedState::Local( ItemState{
calendar: cal3.clone(), calendar: cal3.clone(),
name: String::from("Task B3"), name: String::from("Task B3"),
@ -540,7 +540,7 @@ pub fn scenarii_transient_task() -> Vec<ItemScenario> {
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: ItemId::random(), id: ItemId::random(&cal),
initial_state: LocatedState::Local( ItemState{ initial_state: LocatedState::Local( ItemState{
calendar: cal.clone(), calendar: cal.clone(),
name: String::from("A task, so that the calendar actually exists"), name: String::from("A task, so that the calendar actually exists"),
@ -556,14 +556,14 @@ pub fn scenarii_transient_task() -> Vec<ItemScenario> {
} }
); );
let id_transient = ItemId::random(); let id_transient = ItemId::random(&cal);
tasks.push( tasks.push(
ItemScenario { ItemScenario {
id: id_transient.clone(), id: id_transient.clone(),
initial_state: LocatedState::None, initial_state: LocatedState::None,
local_changes_to_apply: vec![ local_changes_to_apply: vec![
ChangeToApply::Create(cal, Item::Task( 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")), ChangeToApply::Rename(String::from("A new name")),
@ -613,11 +613,11 @@ async fn populate_test_provider(scenarii: &[ItemScenario], mock_behaviour: Arc<M
}; };
let new_item = Item::Task( let new_item = Item::Task(
Task::new( Task::new_with_parameters(
state.name.clone(), state.name.clone(),
state.completed,
item.id.clone(), item.id.clone(),
sync_status, sync_status,
state.completed,
)); ));
match required_state { match required_state {