diff options
author | Akshay <[email protected]> | 2020-07-23 08:49:15 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2020-07-23 08:49:15 +0100 |
commit | b0b6c04a052955834f0603df79db7a0a517a9b9d (patch) | |
tree | 7b4bc347fc37601b1454562d74c5346bb3da4864 /src | |
parent | 537e4f5ebe7404031f240233cbe9807df0d580d9 (diff) |
move duplicate check to command parsing block
Diffstat (limited to 'src')
-rw-r--r-- | src/app/impl_self.rs | 25 | ||||
-rw-r--r-- | src/main.rs | 4 |
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 { | |||
33 | }; | 33 | }; |
34 | } | 34 | } |
35 | 35 | ||
36 | pub fn list_habit(&self) -> Vec<String> { | ||
37 | let habits_names = self.habits.iter().map(|x| x.name()).collect::<Vec<_>>(); | ||
38 | return habits_names; | ||
39 | } | ||
40 | |||
41 | pub fn add_habit(&mut self, h: Box<dyn HabitWrapper>) { | 36 | pub fn add_habit(&mut self, h: Box<dyn HabitWrapper>) { |
42 | if self | 37 | self.habits.push(h); |
43 | .habits | ||
44 | .iter() | ||
45 | .filter(|hab| hab.name() == h.name()) | ||
46 | .count() | ||
47 | > 0 | ||
48 | { | ||
49 | self.message.set_kind(MessageKind::Error); | ||
50 | self.message | ||
51 | .set_message(format!("Habit `{}` allready exist", h.name())) | ||
52 | } else { | ||
53 | self.habits.push(h); | ||
54 | } | ||
55 | } | 38 | } |
56 | 39 | ||
57 | pub fn list_habits(&self) -> Vec<String> { | 40 | pub fn list_habits(&self) -> Vec<String> { |
@@ -234,6 +217,12 @@ impl App { | |||
234 | match result { | 217 | match result { |
235 | Ok(c) => match c { | 218 | Ok(c) => match c { |
236 | Command::Add(name, goal, auto) => { | 219 | Command::Add(name, goal, auto) => { |
220 | if let Some(_) = self.habits.iter().find(|x| x.name() == name) { | ||
221 | self.message.set_kind(MessageKind::Error); | ||
222 | self.message | ||
223 | .set_message(format!("Habit `{}` already exist", &name)); | ||
224 | return; | ||
225 | } | ||
237 | let kind = if goal == Some(1) { "bit" } else { "count" }; | 226 | let kind = if goal == Some(1) { "bit" } else { "count" }; |
238 | if kind == "count" { | 227 | if kind == "count" { |
239 | self.add_habit(Box::new(Count::new(name, goal.unwrap_or(0), auto))); | 228 | 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() { | |||
58 | ), | 58 | ), |
59 | } | 59 | } |
60 | } else if matches.is_present("list") { | 60 | } else if matches.is_present("list") { |
61 | let app = App::load_state(); | 61 | for h in App::load_state().list_habits() { |
62 | let _habit_names = app.list_habit(); | ||
63 | for h in _habit_names { | ||
64 | println!("{}", h); | 62 | println!("{}", h); |
65 | } | 63 | } |
66 | } else { | 64 | } else { |