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

View file

@ -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,