11use crate :: app:: { App , InputField , InputMode } ;
2- use crate :: date:: { Date , DATE_FORMAT } ;
2+ use crate :: date:: { TaskDate , DATE_FORMAT } ;
33use crate :: task:: Task ;
44use anyhow:: { Context , Result } ;
55
66pub trait Command {
7- fn execute ( & mut self , app : & mut App ) -> Result < ( ) > ;
7+ fn execute ( & mut self , app : & mut App ) -> Result < ( ) > ;
88}
99
1010pub struct EnterEditModeCommand ;
1111
1212impl Command for EnterEditModeCommand {
1313 fn execute ( & mut self , app : & mut App ) -> Result < ( ) > {
14- app. input_mode = InputMode :: Editing ;
14+ app. input_mode = InputMode :: Adding ;
1515 app. input_field = InputField :: Title ;
1616 Ok ( ( ) )
1717 }
@@ -23,11 +23,12 @@ pub struct AddTaskCommand;
2323impl Command for AddTaskCommand {
2424 fn execute ( & mut self , app : & mut App ) -> Result < ( ) > {
2525 let mut t = Task :: new ( ) ;
26- t. title = app. input_title . drain ( ..) . collect ( ) ;
27- t. description = app. input_description . drain ( ..) . collect ( ) ;
2826 if !app. input_date . is_empty ( ) {
29- t. date = Date :: try_from ( app. input_date . drain ( ..) . collect :: < String > ( ) ) . context ( "Invalid date" ) ?;
27+ t. date = TaskDate :: try_from ( app. input_date . drain ( ..) . collect :: < String > ( ) )
28+ . context ( "Invalid date format, use dd-mm-yyyy" ) ?;
3029 }
30+ t. title = app. input_title . drain ( ..) . collect ( ) ;
31+ t. description = app. input_description . drain ( ..) . collect ( ) ;
3132 app. tasks_service . add_new_task ( & t) ;
3233 app. refresh_task_list ( ) ;
3334 Ok ( ( ) )
@@ -76,8 +77,11 @@ impl Command for StartEditingExistingTaskCommand {
7677 if let Some ( index) = app. task_list . state . selected ( ) {
7778 app. input_title = app. task_list . items [ index] . title . clone ( ) ;
7879 app. input_description = app. task_list . items [ index] . description . clone ( ) ;
79- app. input_date = app. task_list . items [ index] . date . clone ( ) . 0 . map (
80- |d| d. format ( DATE_FORMAT ) . to_string ( ) )
80+ app. input_date = app. task_list . items [ index]
81+ . date
82+ . clone ( )
83+ . 0
84+ . map ( |d| d. format ( DATE_FORMAT ) . to_string ( ) )
8185 . unwrap_or ( String :: new ( ) ) ;
8286 app. input_mode = InputMode :: EditingExisting ;
8387 app. input_field = InputField :: Title ;
@@ -92,16 +96,17 @@ pub struct FinishEditingExistingTaskCommand;
9296impl Command for FinishEditingExistingTaskCommand {
9397 fn execute ( & mut self , app : & mut App ) -> Result < ( ) > {
9498 if let Some ( index) = app. task_list . state . selected ( ) {
95- app. task_list . items [ index] . title = app. input_title . drain ( ..) . collect ( ) ;
96- app. task_list . items [ index] . description = app. input_description . drain ( ..) . collect ( ) ;
9799 if !app. input_date . is_empty ( ) {
98- app. task_list . items [ index] . date = Date :: try_from ( app. input_date . drain ( ..) . collect :: < String > ( ) ) ?;
100+ app. task_list . items [ index] . date =
101+ TaskDate :: try_from ( app. input_date . drain ( ..) . collect :: < String > ( ) )
102+ . context ( "Invalid date format, use dd-mm-yyyy" ) ?;
99103 } else {
100- app. task_list . items [ index] . date = Date ( None )
104+ app. task_list . items [ index] . date = TaskDate ( None )
101105 }
106+ app. task_list . items [ index] . title = app. input_title . drain ( ..) . collect ( ) ;
107+ app. task_list . items [ index] . description = app. input_description . drain ( ..) . collect ( ) ;
102108 app. tasks_service . update_task ( & app. task_list . items [ index] )
103109 }
104- app. input_mode = InputMode :: Normal ;
105110 Ok ( ( ) )
106111 }
107112}
0 commit comments