Created Item (enum of either a task or en event)
This commit is contained in:
parent
e60ad19fa5
commit
571420126f
7 changed files with 257 additions and 112 deletions
106
src/calendar.rs
106
src/calendar.rs
|
@ -7,8 +7,8 @@ use url::Url;
|
|||
use serde::{Deserialize, Serialize};
|
||||
use chrono::{DateTime, Utc};
|
||||
|
||||
use crate::task::Task;
|
||||
use crate::task::TaskId;
|
||||
use crate::Item;
|
||||
use crate::item::ItemId;
|
||||
|
||||
use bitflags::bitflags;
|
||||
|
||||
|
@ -49,6 +49,24 @@ impl TryFrom<minidom::Element> for SupportedComponents {
|
|||
}
|
||||
|
||||
|
||||
/// Flags to tell which events should be retrieved
|
||||
pub enum SearchFilter {
|
||||
/// Return all items
|
||||
All,
|
||||
/// Return only tasks
|
||||
Tasks,
|
||||
// /// Return only completed tasks
|
||||
// CompletedTasks,
|
||||
// /// Return only calendar events
|
||||
// Events,
|
||||
}
|
||||
|
||||
impl Default for SearchFilter {
|
||||
fn default() -> Self {
|
||||
SearchFilter::All
|
||||
}
|
||||
}
|
||||
|
||||
/// A Caldav Calendar
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Calendar {
|
||||
|
@ -56,8 +74,8 @@ pub struct Calendar {
|
|||
url: Url,
|
||||
supported_components: SupportedComponents,
|
||||
|
||||
tasks: Vec<Task>,
|
||||
deleted_tasks: BTreeMap<DateTime<Utc>, TaskId>,
|
||||
items: Vec<Item>,
|
||||
deleted_items: BTreeMap<DateTime<Utc>, ItemId>,
|
||||
}
|
||||
|
||||
impl Calendar {
|
||||
|
@ -65,8 +83,8 @@ impl Calendar {
|
|||
pub fn new(name: String, url: Url, supported_components: SupportedComponents) -> Self {
|
||||
Self {
|
||||
name, url, supported_components,
|
||||
tasks: Vec::new(),
|
||||
deleted_tasks: BTreeMap::new(),
|
||||
items: Vec::new(),
|
||||
deleted_items: BTreeMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -85,55 +103,79 @@ impl Calendar {
|
|||
self.supported_components.contains(SupportedComponents::TODO)
|
||||
}
|
||||
|
||||
/// Add a task into this calendar
|
||||
pub fn add_task(&mut self, task: Task) {
|
||||
self.tasks.push(task);
|
||||
/// Returns whether this calDAV calendar supports calendar items
|
||||
pub fn supports_events(&self) -> bool {
|
||||
self.supported_components.contains(SupportedComponents::EVENT)
|
||||
}
|
||||
|
||||
pub fn delete_task(&mut self, task_id: &TaskId) {
|
||||
self.tasks.retain(|t| t.id() != task_id);
|
||||
self.deleted_tasks.insert(Utc::now(), task_id.clone());
|
||||
/// Add an item into this calendar
|
||||
pub fn add_item(&mut self, item: Item) {
|
||||
self.items.push(item);
|
||||
}
|
||||
|
||||
/// Returns the list of tasks that this calendar contains
|
||||
/// Pass a `completed` flag to filter only the completed (or non-completed) tasks
|
||||
pub fn get_tasks(&self, completed: Option<bool>) -> HashMap<TaskId, &Task> {
|
||||
self.get_tasks_modified_since(None, completed)
|
||||
/// Remove an item from this calendar
|
||||
pub fn delete_item(&mut self, item_id: &ItemId) {
|
||||
self.items.retain(|i| i.id() != item_id);
|
||||
self.deleted_items.insert(Utc::now(), item_id.clone());
|
||||
}
|
||||
|
||||
/// Returns the tasks that have been last-modified after `since`
|
||||
/// Pass a `completed` flag to filter only the completed (or non-completed) tasks
|
||||
pub fn get_tasks_modified_since(&self, since: Option<DateTime<Utc>>, _completed: Option<bool>) -> HashMap<TaskId, &Task> {
|
||||
/// Returns the list of items that this calendar contains
|
||||
pub fn get_items(&self) -> HashMap<ItemId, &Item> {
|
||||
self.get_items_modified_since(None, None)
|
||||
}
|
||||
/// Returns the items that have been last-modified after `since`
|
||||
pub fn get_items_modified_since(&self, since: Option<DateTime<Utc>>, filter: Option<SearchFilter>) -> HashMap<ItemId, &Item> {
|
||||
let filter = filter.unwrap_or_default();
|
||||
|
||||
let mut map = HashMap::new();
|
||||
|
||||
for task in &self.tasks {
|
||||
for item in &self.items {
|
||||
match since {
|
||||
None => (),
|
||||
Some(since) => if task.last_modified() < since {
|
||||
Some(since) => if item.last_modified() < since {
|
||||
continue;
|
||||
},
|
||||
}
|
||||
|
||||
map.insert(task.id().clone(), task);
|
||||
match filter {
|
||||
SearchFilter::Tasks => {
|
||||
if item.is_task() == false {
|
||||
continue;
|
||||
}
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
|
||||
map.insert(item.id().clone(), item);
|
||||
}
|
||||
|
||||
map
|
||||
}
|
||||
|
||||
/// Returns the tasks that have been deleted after `since`
|
||||
pub fn get_tasks_deleted_since(&self, since: DateTime<Utc>) -> Vec<TaskId> {
|
||||
self.deleted_tasks.range(since..)
|
||||
.map(|(_key, value)| value.clone())
|
||||
.collect()
|
||||
/// Returns the items that have been deleted after `since`
|
||||
pub fn get_items_deleted_since(&self, since: DateTime<Utc>) -> Vec<ItemId> {
|
||||
self.deleted_items.range(since..)
|
||||
.map(|(_key, value)| value.clone())
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Returns a particular task
|
||||
pub fn get_task_by_id_mut(&mut self, id: &TaskId) -> Option<&mut Task> {
|
||||
for task in &mut self.tasks {
|
||||
if task.id() == id {
|
||||
return Some(task);
|
||||
/// Returns a particular item
|
||||
pub fn get_item_by_id_mut(&mut self, id: &ItemId) -> Option<&mut Item> {
|
||||
for item in &mut self.items {
|
||||
if item.id() == id {
|
||||
return Some(item);
|
||||
}
|
||||
}
|
||||
return None;
|
||||
}
|
||||
|
||||
|
||||
/// Returns the list of tasks that this calendar contains
|
||||
pub fn get_tasks(&self) -> HashMap<ItemId, &Item> {
|
||||
self.get_tasks_modified_since(None)
|
||||
}
|
||||
/// Returns the tasks that have been last-modified after `since`
|
||||
pub fn get_tasks_modified_since(&self, since: Option<DateTime<Utc>>) -> HashMap<ItemId, &Item> {
|
||||
self.get_items_modified_since(since, Some(SearchFilter::Tasks))
|
||||
}
|
||||
}
|
||||
|
|
29
src/event.rs
Normal file
29
src/event.rs
Normal file
|
@ -0,0 +1,29 @@
|
|||
//! Calendar events
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use chrono::{Utc, DateTime};
|
||||
|
||||
use crate::item::ItemId;
|
||||
|
||||
/// TODO: implement Event one day.
|
||||
/// This crate currently only supports tasks, not calendar events.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Event {
|
||||
id: ItemId,
|
||||
name: String,
|
||||
last_modified: DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl Event {
|
||||
pub fn id(&self) -> &ItemId {
|
||||
&self.id
|
||||
}
|
||||
|
||||
pub fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
|
||||
pub fn last_modified(&self) -> DateTime<Utc> {
|
||||
self.last_modified
|
||||
}
|
||||
}
|
86
src/item.rs
Normal file
86
src/item.rs
Normal file
|
@ -0,0 +1,86 @@
|
|||
use std::fmt::{Display, Formatter};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use chrono::{Utc, DateTime};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub enum Item {
|
||||
Event(crate::event::Event),
|
||||
Task(crate::task::Task),
|
||||
}
|
||||
|
||||
impl Item {
|
||||
pub fn id(&self) -> &ItemId {
|
||||
match self {
|
||||
Item::Event(e) => e.id(),
|
||||
Item::Task(t) => t.id(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn name(&self) -> &str {
|
||||
match self {
|
||||
Item::Event(e) => e.name(),
|
||||
Item::Task(t) => t.name(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn last_modified(&self) -> DateTime<Utc> {
|
||||
match self {
|
||||
Item::Event(e) => e.last_modified(),
|
||||
Item::Task(t) => t.last_modified(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_event(&self) -> bool {
|
||||
match &self {
|
||||
Item::Event(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_task(&self) -> bool {
|
||||
match &self {
|
||||
Item::Task(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the inner Task
|
||||
///
|
||||
/// # Panics
|
||||
/// Panics if the inner item is not a Task
|
||||
pub fn unwrap_task_mut(&mut self) -> &mut crate::task::Task {
|
||||
match self {
|
||||
Item::Task(t) => t,
|
||||
_ => panic!("Not a task"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a reference to the inner Task
|
||||
///
|
||||
/// # Panics
|
||||
/// Panics if the inner item is not a Task
|
||||
pub fn unwrap_task(&self) -> &crate::task::Task {
|
||||
match self {
|
||||
Item::Task(t) => t,
|
||||
_ => panic!("Not a task"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Hash, Serialize, Deserialize)]
|
||||
pub struct ItemId {
|
||||
content: String,
|
||||
}
|
||||
impl ItemId{
|
||||
pub fn new() -> Self {
|
||||
let u = uuid::Uuid::new_v4().to_hyphenated().to_string();
|
||||
Self { content:u }
|
||||
}
|
||||
}
|
||||
impl Eq for ItemId {}
|
||||
impl Display for ItemId {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
|
||||
write!(f, "{}", self.content)
|
||||
}
|
||||
}
|
|
@ -12,8 +12,12 @@ pub mod traits;
|
|||
|
||||
pub mod calendar;
|
||||
pub use calendar::Calendar;
|
||||
mod item;
|
||||
pub use item::Item;
|
||||
mod task;
|
||||
pub use task::Task;
|
||||
mod event;
|
||||
pub use event::Event;
|
||||
pub mod provider;
|
||||
pub use provider::Provider;
|
||||
|
||||
|
|
|
@ -6,8 +6,8 @@ use chrono::{DateTime, Utc};
|
|||
|
||||
use crate::traits::CalDavSource;
|
||||
use crate::Calendar;
|
||||
use crate::Task;
|
||||
use crate::task::TaskId;
|
||||
use crate::Item;
|
||||
use crate::item::ItemId;
|
||||
|
||||
|
||||
pub struct Provider<S, L>
|
||||
|
@ -49,10 +49,10 @@ where
|
|||
Some(cal) => cal,
|
||||
};
|
||||
|
||||
let server_mod = cal_server.get_tasks_modified_since(Some(self.last_sync), None);
|
||||
let server_del = cal_server.get_tasks_deleted_since(self.last_sync);
|
||||
let local_mod = cal_local.get_tasks_modified_since(Some(self.last_sync), None);
|
||||
let local_del = cal_local.get_tasks_deleted_since(self.last_sync);
|
||||
let server_mod = cal_server.get_tasks_modified_since(Some(self.last_sync));
|
||||
let server_del = cal_server.get_items_deleted_since(self.last_sync);
|
||||
let local_mod = cal_local.get_tasks_modified_since(Some(self.last_sync));
|
||||
let local_del = cal_local.get_items_deleted_since(self.last_sync);
|
||||
|
||||
let mut tasks_to_add_to_local = Vec::new();
|
||||
let mut tasks_id_to_remove_from_local = Vec::new();
|
||||
|
@ -91,16 +91,16 @@ where
|
|||
}
|
||||
|
||||
|
||||
fn move_to_calendar(tasks: &mut Vec<Task>, calendar: &mut Calendar) {
|
||||
while tasks.len() > 0 {
|
||||
let task = tasks.remove(0);
|
||||
calendar.add_task(task);
|
||||
fn move_to_calendar(items: &mut Vec<Item>, calendar: &mut Calendar) {
|
||||
while items.len() > 0 {
|
||||
let item = items.remove(0);
|
||||
calendar.add_item(item);
|
||||
}
|
||||
}
|
||||
|
||||
fn remove_from_calendar(ids: &Vec<TaskId>, calendar: &mut Calendar) {
|
||||
fn remove_from_calendar(ids: &Vec<ItemId>, calendar: &mut Calendar) {
|
||||
for id in ids {
|
||||
log::info!(" Removing {:?} from local calendar", id);
|
||||
calendar.delete_task(id);
|
||||
calendar.delete_item(id);
|
||||
}
|
||||
}
|
||||
|
|
27
src/task.rs
27
src/task.rs
|
@ -1,32 +1,13 @@
|
|||
use std::fmt::{Display, Formatter};
|
||||
|
||||
use chrono::{Utc, DateTime};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
// TODO: turn into this one day
|
||||
// pub type TaskId = String; // This is an HTML "etag"
|
||||
#[derive(Clone, Debug, PartialEq, Hash, Serialize, Deserialize)]
|
||||
pub struct TaskId {
|
||||
content: String,
|
||||
}
|
||||
impl TaskId{
|
||||
pub fn new() -> Self {
|
||||
let u = uuid::Uuid::new_v4().to_hyphenated().to_string();
|
||||
Self { content:u }
|
||||
}
|
||||
}
|
||||
impl Eq for TaskId {}
|
||||
impl Display for TaskId {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
|
||||
write!(f, "{}", self.content)
|
||||
}
|
||||
}
|
||||
use crate::item::ItemId;
|
||||
|
||||
/// A to-do task
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Task {
|
||||
/// The task unique ID, that will never change
|
||||
id: TaskId,
|
||||
id: ItemId,
|
||||
|
||||
/// The last modification date of this task
|
||||
last_modified: DateTime<Utc>,
|
||||
|
@ -41,14 +22,14 @@ impl Task {
|
|||
/// Create a new Task
|
||||
pub fn new(name: String, last_modified: DateTime<Utc>) -> Self {
|
||||
Self {
|
||||
id: TaskId::new(),
|
||||
id: ItemId::new(),
|
||||
name,
|
||||
last_modified,
|
||||
completed: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn id(&self) -> &TaskId { &self.id }
|
||||
pub fn id(&self) -> &ItemId { &self.id }
|
||||
pub fn name(&self) -> &str { &self.name }
|
||||
pub fn completed(&self) -> bool { self.completed }
|
||||
pub fn last_modified(&self) -> DateTime<Utc> { self.last_modified }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue