summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAkshay <nerdy@peppe.rs>2020-07-23 13:19:15 +0530
committerAkshay <nerdy@peppe.rs>2020-07-23 13:19:15 +0530
commitb0b6c04a052955834f0603df79db7a0a517a9b9d (patch)
tree7b4bc347fc37601b1454562d74c5346bb3da4864
parent537e4f5ebe7404031f240233cbe9807df0d580d9 (diff)
move duplicate check to command parsing block
-rw-r--r--src/app/impl_self.rs25
-rw-r--r--src/main.rs4
2 files changed, 8 insertions, 21 deletions
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<String> {
- let habits_names = self.habits.iter().map(|x| x.name()).collect::<Vec<_>>();
- return habits_names;
- }
-
pub fn add_habit(&mut self, h: Box<dyn HabitWrapper>) {
- 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<String> {
@@ -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 {