diff options
Diffstat (limited to 'src/app/impl_self.rs')
-rw-r--r-- | src/app/impl_self.rs | 25 |
1 files changed, 7 insertions, 18 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))); |