From 9dfe454cd8bc816522446e7e3b9f45630c886112 Mon Sep 17 00:00:00 2001 From: Guillaume Hormiere Date: Wed, 22 Jul 2020 00:38:01 +0200 Subject: Add list command for shell script purpose Usage dijo -l for printing the habit names list Add check on habit add to avoid duplicate habits --- src/app/impl_self.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'src/app') diff --git a/src/app/impl_self.rs b/src/app/impl_self.rs index 95f1871..38162ee 100644 --- a/src/app/impl_self.rs +++ b/src/app/impl_self.rs @@ -33,8 +33,27 @@ impl App { }; } + pub fn list_habit(&self) -> Vec { + let mut habits_names: Vec = vec![]; + for h in self.habits.iter() { + habits_names.push(h.name()) + } + return habits_names; + } + pub fn add_habit(&mut self, h: Box) { - self.habits.push(h); + if self + .habits + .iter() + .filter(|hab| hab.name() == h.name()) + .count() + > 0 + { + self.message + .set_message(format!("Habit `{}` allready exist", h.name())) + } else { + self.habits.push(h); + } } pub fn delete_by_name(&mut self, name: &str) { -- cgit v1.2.3 From a0c57162b2026e37220e31a39d821c2a2e31cc51 Mon Sep 17 00:00:00 2001 From: Guillaume Hormiere Date: Wed, 22 Jul 2020 22:26:19 +0200 Subject: Use immutable vector instead of mutable one and change the message kind to Error Apply code review --- src/app/impl_self.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'src/app') diff --git a/src/app/impl_self.rs b/src/app/impl_self.rs index 38162ee..8a84cb2 100644 --- a/src/app/impl_self.rs +++ b/src/app/impl_self.rs @@ -34,10 +34,7 @@ impl App { } pub fn list_habit(&self) -> Vec { - let mut habits_names: Vec = vec![]; - for h in self.habits.iter() { - habits_names.push(h.name()) - } + let habits_names = self.habits.iter().map(|x| x.name()).collect::>(); return habits_names; } @@ -49,6 +46,7 @@ impl App { .count() > 0 { + self.message.set_kind(MessageKind::Error); self.message .set_message(format!("Habit `{}` allready exist", h.name())) } else { -- cgit v1.2.3 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 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) (limited to 'src/app') 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))); -- cgit v1.2.3