Skip to content

Commit

Permalink
feat: Add getters for basic todo and event properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
qwandor committed Apr 10, 2022
1 parent 174947a commit dad79f4
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
24 changes: 22 additions & 2 deletions src/components/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,35 @@ impl Event {
}
}

/// Defines the overall status or confirmation
/// Defines the overall status or confirmation
pub fn status(&mut self, status: EventStatus) -> &mut Self {
self.append_property(status.into());
self
}

/// Gets the overall status or confirmation.
pub fn get_status(&self) -> Option<EventStatus> {
EventStatus::from_str(self.property_value("STATUS")?)
}

//pub fn repeats<R:Repeater+?Sized>(&mut self, repeat: R) -> &mut Self {
// unimplemented!()
//}
}

// impl std::Str
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn get_properties_unset() {
let event = Event::new();
assert_eq!(event.get_status(), None);
}

#[test]
fn get_properties_set() {
let event = Event::new().status(EventStatus::Tentative).done();
assert_eq!(event.get_status(), Some(EventStatus::Tentative));
}
}
37 changes: 35 additions & 2 deletions src/components/todo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ impl Todo {
self
}

/// Gets the [`PERCENT-COMPLETE`](https://datatracker.ietf.org/doc/html/rfc5545#section-3.8.1.8) property.
///
/// Ranges between 0 - 100.
pub fn get_percent_complete(&self) -> Option<u8> {
self.property_value("PERCENT-COMPLETE")?.parse().ok()
}

/// Set the [`DUE`](https://datatracker.ietf.org/doc/html/rfc5545#section-3.8.2.3) property
///
/// See [`CalendarDateTime`] for info how are different [`chrono`] types converted automatically.
Expand All @@ -48,14 +55,40 @@ impl Todo {
self
}

/// Defines the overall status or confirmation
///
/// Defines the overall status or confirmation
pub fn status(&mut self, status: TodoStatus) -> &mut Self {
self.append_property(status.into());
self
}

/// Gets the overall status.
pub fn get_status(&self) -> Option<TodoStatus> {
TodoStatus::from_str(self.property_value("STATUS")?)
}

//pub fn repeats<R:Repeater+?Sized>(&mut self, repeat: R) -> &mut Self {
// unimplemented!()
//}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn get_properties_unset() {
let todo = Todo::new();
assert_eq!(todo.get_percent_complete(), None);
assert_eq!(todo.get_status(), None);
}

#[test]
fn get_properties_set() {
let todo = Todo::new()
.percent_complete(42)
.status(TodoStatus::NeedsAction)
.done();
assert_eq!(todo.get_percent_complete(), Some(42));
assert_eq!(todo.get_status(), Some(TodoStatus::NeedsAction));
}
}
23 changes: 23 additions & 0 deletions src/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,17 @@ pub enum EventStatus {
//Custom(&str)
}

impl EventStatus {
pub(crate) fn from_str(s: &str) -> Option<Self> {
match s {
"TENTATIVE" => Some(Self::Tentative),
"CONFIRMED" => Some(Self::Confirmed),
"CANCELLED" => Some(Self::Cancelled),
_ => None,
}
}
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
/// Encodes the status of a `Todo`
/// <https://datatracker.ietf.org/doc/html/rfc5545#section-3.8.1.11>
Expand All @@ -215,6 +226,18 @@ pub enum TodoStatus {
//Custom(&str)
}

impl TodoStatus {
pub(crate) fn from_str(s: &str) -> Option<Self> {
match s {
"NEEDS-ACTION" => Some(Self::NeedsAction),
"COMPLETED" => Some(Self::Completed),
"IN-PROCESS" => Some(Self::InProcess),
"CANCELLED" => Some(Self::Cancelled),
_ => None,
}
}
}

//pub enum JournalStatuw{
// Draft,
// Final,
Expand Down

0 comments on commit dad79f4

Please sign in to comment.