Browse Source

Listing events

master
Julio Biason 5 years ago
parent
commit
8b628e3cda
  1. 1
      README.md
  2. 35
      src/eventlist/event.rs
  3. 34
      src/eventlist/eventlist.rs
  4. 5
      src/main.rs

1
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

35
src/eventlist/event.rs

@ -16,7 +16,11 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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<Utc> {
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<String> {
let now = Utc::now();
let to: DateTime<Utc> = (&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
)
}
}

34
src/eventlist/eventlist.rs

@ -29,7 +29,13 @@ static FILENAME: &str = "events.toml";
#[derive(Serialize, Deserialize, Debug)]
pub struct EventList {
events: Vec<Event>,
pub events: Vec<Event>,
}
pub struct EventListIterator<'a> {
index: usize,
max: usize,
list: &'a Vec<Event>,
}
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])
}
}
}

5
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) {

Loading…
Cancel
Save