From fd19faca532417ac93022882894cda28936e3e7d Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Tue, 12 May 2020 19:16:11 -0300 Subject: [PATCH] Order events when adding --- README.md | 4 ++-- src/eventlist/event.rs | 40 ++++++++++++++++++++++++++++++++++++++ src/eventlist/eventlist.rs | 1 + src/main.rs | 2 +- 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 967dde6..5817a89 100644 --- a/README.md +++ b/README.md @@ -24,13 +24,13 @@ tell you that you have 15 days up to it. - [x] List events - [ ] Add Events with time - [ ] List events with time -- [ ] Sort events by ETA +- [x] Sort events by ETA - [ ] Option to remove events - [ ] Move the app "db" to a fixed space - [x] Replace `dbg!` with [env_logger](https://crates.io/crates/env_logger) (reasoning: Although `dbg!` is nice and dandy, it can't be disabled, and that's bad UI) -- [ ] Remove `unwrap()`s (for example, in the argument parsing). +- [ ] Remove `unwrap()`s ## License diff --git a/src/eventlist/event.rs b/src/eventlist/event.rs index 0462205..5f86e8b 100644 --- a/src/eventlist/event.rs +++ b/src/eventlist/event.rs @@ -16,6 +16,7 @@ along with this program. If not, see . */ +use std::cmp::Ordering; use std::convert::From; use std::fmt; @@ -54,6 +55,17 @@ impl From<&EventDateType> for DateTime { } } +impl From<&EventDateType> for String { + fn from(origin: &EventDateType) -> String { + match origin { + EventDateType::AllDay(d) => format!("{}{}{}0000", d.year, d.month, d.day), + EventDateType::AtTime(d, t) => { + format!("{}{}{}{}{}", d.year, d.month, d.day, t.hour, t.min) + } + } + } +} + #[derive(Serialize, Deserialize, Debug)] pub struct Event { id: String, @@ -102,3 +114,31 @@ impl fmt::Display for Event { ) } } + +impl Eq for Event {} + +impl PartialEq for Event { + fn eq(&self, other: &Self) -> bool { + let self_str = String::from(&self.due); + let other_str = String::from(&other.due); + self_str == other_str + } +} + +impl PartialOrd for Event { + fn partial_cmp(&self, other: &Self) -> Option { + let self_str = String::from(&self.due); + let other_str = String::from(&other.due); + + Some(self_str.cmp(&other_str)) + } +} + +impl Ord for Event { + fn cmp(&self, other: &Self) -> Ordering { + let self_str = String::from(&self.due); + let other_str = String::from(&other.due); + + self_str.cmp(&other_str) + } +} diff --git a/src/eventlist/eventlist.rs b/src/eventlist/eventlist.rs index 62e0c03..5617836 100644 --- a/src/eventlist/eventlist.rs +++ b/src/eventlist/eventlist.rs @@ -56,6 +56,7 @@ impl EventList { pub fn push(&mut self, event: Event) { self.events.push(event); + self.events.sort(); } pub fn save(&self) { diff --git a/src/main.rs b/src/main.rs index af0a929..a7d391c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,7 +32,7 @@ fn main() { match command { args::Action::List => list(), args::Action::Add(description, date) => add_with_date(&description, &date), - _ => println!("Unknown command"), + _ => println!("Command not implemented yet"), } } }