diff --git a/README.md b/README.md
index f930916..6c9a3dc 100644
--- a/README.md
+++ b/README.md
@@ -21,6 +21,7 @@ tell you that you have 15 days up to it.
## TODO
- [x] Add unique identifier for each event
+- [x] List events
- [ ] Removing events
- [ ] Replace `dbg!` with [env_logger](https://crates.io/crates/env_logger)
(reasoning: Although `dbg!` is nice and dandy, it can't be disabled, and
diff --git a/src/eventlist/event.rs b/src/eventlist/event.rs
index 1d1f736..0462205 100644
--- a/src/eventlist/event.rs
+++ b/src/eventlist/event.rs
@@ -16,7 +16,11 @@
along with this program. If not, see .
*/
+use std::convert::From;
+use std::fmt;
+
use chrono::prelude::*;
+use chrono::DateTime;
use serde_derive::Deserialize;
use serde_derive::Serialize;
use uuid::Uuid;
@@ -41,6 +45,15 @@ pub enum EventDateType {
AtTime(Date, Time),
}
+impl From<&EventDateType> for DateTime {
+ fn from(origin: &EventDateType) -> Self {
+ match origin {
+ EventDateType::AllDay(d) => Utc.ymd(d.year, d.month, d.day).and_hms(0, 0, 0),
+ _ => Utc.ymd(1970, 1, 1).and_hms(0, 0, 0),
+ }
+ }
+}
+
#[derive(Serialize, Deserialize, Debug)]
pub struct Event {
id: String,
@@ -66,4 +79,26 @@ impl Event {
panic!("Failed to parse the date");
}
}
+
+ pub fn eta(&self) -> Option {
+ let now = Utc::now();
+ let to: DateTime = (&self.due).into();
+ let eta = to - now;
+ Some(format!("{}d", eta.num_days()))
+ }
+}
+
+impl fmt::Display for Event {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(
+ f,
+ "{:8} - {:>6} - {}",
+ self.id,
+ match self.eta() {
+ Some(x) => x,
+ None => "".into(),
+ },
+ self.description
+ )
+ }
}
diff --git a/src/eventlist/eventlist.rs b/src/eventlist/eventlist.rs
index 4b0145b..62e0c03 100644
--- a/src/eventlist/eventlist.rs
+++ b/src/eventlist/eventlist.rs
@@ -29,7 +29,13 @@ static FILENAME: &str = "events.toml";
#[derive(Serialize, Deserialize, Debug)]
pub struct EventList {
- events: Vec,
+ pub events: Vec,
+}
+
+pub struct EventListIterator<'a> {
+ index: usize,
+ max: usize,
+ list: &'a Vec,
}
impl EventList {
@@ -59,3 +65,29 @@ impl EventList {
}
}
}
+
+impl<'a> IntoIterator for &'a EventList {
+ type Item = &'a Event;
+ type IntoIter = EventListIterator<'a>;
+
+ fn into_iter(self) -> Self::IntoIter {
+ EventListIterator {
+ index: 0,
+ max: self.events.len(),
+ list: &self.events,
+ }
+ }
+}
+
+impl<'a> Iterator for EventListIterator<'a> {
+ type Item = &'a Event;
+
+ fn next(&mut self) -> Option<&'a Event> {
+ if self.index >= self.max {
+ None
+ } else {
+ self.index += 1;
+ Some(&self.list[self.index - 1])
+ }
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index 8010188..55378d5 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -33,7 +33,10 @@ fn main() {
}
fn list() {
- unimplemented!()
+ let event_list = EventList::load();
+ for record in event_list.into_iter() {
+ println!("{}", record);
+ }
}
fn add_with_date(description: &str, date: &str) {