PRODID is parsed, stored and generated

This commit is contained in:
daladim 2021-11-05 23:31:17 +01:00
parent f7acadc3e2
commit 39867a9f15
6 changed files with 38 additions and 8 deletions

View file

@ -32,6 +32,10 @@ impl Event {
&self.name
}
pub fn ical_prod_id(&self) -> &str {
unimplemented!()
}
pub fn creation_date(&self) -> Option<&DateTime<Utc>> {
unimplemented!()
}

View file

@ -12,11 +12,7 @@ use ical::property::Property as IcalProperty;
use crate::Task;
use crate::item::Item;
use crate::task::CompletionStatus;
use crate::settings::{ORG_NAME, PRODUCT_NAME};
fn ical_product_id() -> String {
format!("-//{}//{}//EN", ORG_NAME, PRODUCT_NAME)
}
/// Create an iCal item from a `crate::item::Item`
pub fn build_from(item: &Item) -> Result<String, Box<dyn Error>> {
@ -59,7 +55,7 @@ pub fn build_from_task(task: &Task) -> Result<String, Box<dyn Error>> {
todo.push(ics_property);
}
let mut calendar = ICalendar::new("2.0", ical_product_id());
let mut calendar = ICalendar::new("2.0", task.ical_prod_id());
calendar.add_todo(todo);
Ok(calendar.to_string())
@ -89,6 +85,7 @@ fn ical_to_ics_property(prop: IcalProperty) -> IcsProperty<'static> {
mod tests {
use super::*;
use crate::Task;
use crate::settings::{ORG_NAME, PRODUCT_NAME};
#[test]
fn test_ical_from_completed_task() {

View file

@ -6,3 +6,10 @@ mod parser;
pub use parser::parse;
mod builder;
pub use builder::build_from;
use crate::settings::{ORG_NAME, PRODUCT_NAME};
pub fn default_prod_id() -> String {
format!("-//{}//{}//EN", ORG_NAME, PRODUCT_NAME)
}

View file

@ -24,6 +24,10 @@ pub fn parse(content: &str, item_id: ItemId, sync_status: SyncStatus) -> Result<
}
};
let ical_prod_id = extract_ical_prod_id(&parsed_item)
.map(|s| s.to_string())
.unwrap_or_else(|| super::default_prod_id());
let item = match assert_single_type(&parsed_item)? {
CurrentType::Event(_) => {
Item::Event(Event::new())
@ -96,7 +100,7 @@ pub fn parse(content: &str, item_id: ItemId, sync_status: SyncStatus) -> Result<
true => CompletionStatus::Completed(completion_date),
};
Item::Task(Task::new_with_parameters(name, uid, item_id, completion_status, sync_status, creation_date, last_modified, extra_parameters))
Item::Task(Task::new_with_parameters(name, uid, item_id, completion_status, sync_status, creation_date, last_modified, ical_prod_id, extra_parameters))
},
};
@ -126,6 +130,16 @@ fn parse_date_time_from_property(value: &Option<String>) -> Option<DateTime<Utc>
}
fn extract_ical_prod_id(item: &IcalCalendar) -> Option<&str> {
for prop in &item.properties {
if &prop.name == "PRODID" {
return prop.value.as_ref().map(|s| s.as_str())
}
}
None
}
enum CurrentType<'a> {
Event(&'a IcalEvent),
Todo(&'a IcalTodo),

View file

@ -38,6 +38,7 @@ impl Item {
synthetise_common_getter!(creation_date, Option<&DateTime<Utc>>);
synthetise_common_getter!(last_modified, &DateTime<Utc>);
synthetise_common_getter!(sync_status, &SyncStatus);
synthetise_common_getter!(ical_prod_id, &str);
pub fn set_sync_status(&mut self, new_status: SyncStatus) {
match self {

View file

@ -52,6 +52,10 @@ pub struct Task {
/// The display name of the task
name: String,
/// The PRODID, as defined in iCal files
ical_prod_id: String,
/// Extra parameters that have not been parsed from the iCal file (because they're not supported (yet) by this crate).
/// They are needed to serialize this item into an equivalent iCal file
extra_parameters: Vec<Property>,
@ -70,15 +74,16 @@ impl Task {
let new_completion_status = if completed {
CompletionStatus::Completed(Some(Utc::now()))
} else { CompletionStatus::Uncompleted };
let ical_prod_id = crate::ical::default_prod_id();
let extra_parameters = Vec::new();
Self::new_with_parameters(name, new_uid, new_item_id, new_completion_status, new_sync_status, new_creation_date, new_last_modified, extra_parameters)
Self::new_with_parameters(name, new_uid, new_item_id, new_completion_status, new_sync_status, new_creation_date, new_last_modified, ical_prod_id, extra_parameters)
}
/// Create a new Task instance, that may be synced on the server already
pub fn new_with_parameters(name: String, uid: String, id: ItemId,
completion_status: CompletionStatus,
sync_status: SyncStatus, creation_date: Option<DateTime<Utc>>, last_modified: DateTime<Utc>,
extra_parameters: Vec<Property>,
ical_prod_id: String, extra_parameters: Vec<Property>,
) -> Self
{
Self {
@ -89,6 +94,7 @@ impl Task {
sync_status,
creation_date,
last_modified,
ical_prod_id,
extra_parameters,
}
}
@ -97,6 +103,7 @@ impl Task {
pub fn uid(&self) -> &str { &self.uid }
pub fn name(&self) -> &str { &self.name }
pub fn completed(&self) -> bool { self.completion_status.is_completed() }
pub fn ical_prod_id(&self) -> &str { &self.ical_prod_id }
pub fn sync_status(&self) -> &SyncStatus { &self.sync_status }
pub fn last_modified(&self) -> &DateTime<Utc> { &self.last_modified }
pub fn creation_date(&self) -> Option<&DateTime<Utc>> { self.creation_date.as_ref() }