From b0b6c04a052955834f0603df79db7a0a517a9b9d Mon Sep 17 00:00:00 2001 From: Akshay Date: Thu, 23 Jul 2020 13:19:15 +0530 Subject: move duplicate check to command parsing block --- src/app/impl_self.rs | 25 +++++++------------------ src/main.rs | 4 +--- 2 files changed, 8 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/app/impl_self.rs b/src/app/impl_self.rs index 1dfe268..a806dc5 100644 --- a/src/app/impl_self.rs +++ b/src/app/impl_self.rs @@ -33,25 +33,8 @@ impl App { }; } - pub fn list_habit(&self) -> Vec { - let habits_names = self.habits.iter().map(|x| x.name()).collect::>(); - return habits_names; - } - pub fn add_habit(&mut self, h: Box) { - if self - .habits - .iter() - .filter(|hab| hab.name() == h.name()) - .count() - > 0 - { - self.message.set_kind(MessageKind::Error); - self.message - .set_message(format!("Habit `{}` allready exist", h.name())) - } else { - self.habits.push(h); - } + self.habits.push(h); } pub fn list_habits(&self) -> Vec { @@ -234,6 +217,12 @@ impl App { match result { Ok(c) => match c { Command::Add(name, goal, auto) => { + if let Some(_) = self.habits.iter().find(|x| x.name() == name) { + self.message.set_kind(MessageKind::Error); + self.message + .set_message(format!("Habit `{}` already exist", &name)); + return; + } let kind = if goal == Some(1) { "bit" } else { "count" }; if kind == "count" { self.add_habit(Box::new(Count::new(name, goal.unwrap_or(0), auto))); diff --git a/src/main.rs b/src/main.rs index 050a296..5523073 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,9 +58,7 @@ fn main() { ), } } else if matches.is_present("list") { - let app = App::load_state(); - let _habit_names = app.list_habit(); - for h in _habit_names { + for h in App::load_state().list_habits() { println!("{}", h); } } else { -- cgit v1.2.3