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::str::FromStr;
use serde::{Deserialize, Serialize};
use chrono::{Utc, DateTime};
use url::Url;
use crate::resource::Resource;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Item {
@ -68,16 +72,37 @@ impl Item {
}
}
#[derive(Clone, Debug, PartialEq, Hash, Serialize, Deserialize)]
pub struct ItemId {
content: String,
content: Url,
}
impl ItemId{
pub fn new() -> Self {
let u = uuid::Uuid::new_v4().to_hyphenated().to_string();
/// Generate a random ItemId. This is only useful in tests
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 }
}
}
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 Display for ItemId {
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;
mod item;
pub use item::Item;
pub use item::ItemId;
mod task;
pub use task::Task;
mod event;

View file

@ -20,9 +20,9 @@ pub struct Task {
impl 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 {
id: ItemId::new(),
id,
name,
last_modified,
completed: false,