55
66import chopchop .model .attributes .Quantity ;
77import chopchop .model .attributes .Tag ;
8- import chopchop .commons .util .Pair ;
98import chopchop .commons .util .Result ;
109import chopchop .commons .util .Strings ;
1110
12- import chopchop .logic .edit .EditDescriptor ;
1311import chopchop .logic .edit .EditOperationType ;
1412import chopchop .logic .edit .IngredientEditDescriptor ;
1513import chopchop .logic .edit .RecipeEditDescriptor ;
@@ -54,7 +52,10 @@ public static Result<? extends Command> parseEditCommand(CommandArguments args)
5452 })
5553 .then (item -> {
5654
57- var edits = new ArrayList <Result <EditDescriptor >>();
55+ Optional <String > editedName = Optional .empty ();
56+ var tagEdits = new ArrayList <Result <TagEditDescriptor >>();
57+ var stepEdits = new ArrayList <Result <StepEditDescriptor >>();
58+ var ingrEdits = new ArrayList <Result <IngredientEditDescriptor >>();
5859
5960 for (int i = 0 ; i < args .getAllArguments ().size (); i ++) {
6061
@@ -66,13 +67,23 @@ public static Result<? extends Command> parseEditCommand(CommandArguments args)
6667 return Result .error ("'%s' needs an edit-argument in an edit command" , argName );
6768 }
6869
69- if (argName .name ().equals (Strings .ARG_TAG .name ())) {
70+ if (argName .name ().equals (Strings .ARG_NAME .name ())) {
7071
71- edits .add (parseTagEdit (argName , argValue ));
72+ if (argValue .isEmpty ()) {
73+ return Result .error ("expected a name after '/name'" );
74+ } else if (editedName .isPresent ()) {
75+ return Result .error ("only one '/name' should be provided" );
76+ }
77+
78+ editedName = Optional .of (argValue );
79+
80+ } else if (argName .name ().equals (Strings .ARG_TAG .name ())) {
81+
82+ tagEdits .add (parseTagEdit (argName , argValue ));
7283
7384 } else if (argName .name ().equals (Strings .ARG_STEP .name ())) {
7485
75- edits .add (parseStepEdit (argName , argValue ));
86+ stepEdits .add (parseStepEdit (argName , argValue ));
7687
7788 } else if (argName .name ().equals (Strings .ARG_INGREDIENT .name ())) {
7889
@@ -93,49 +104,36 @@ public static Result<? extends Command> parseEditCommand(CommandArguments args)
93104 }
94105 }
95106
96- edits .add (parseIngredientEdit (argName , argValue , qty ));
107+ ingrEdits .add (parseIngredientEdit (argName , argValue , qty ));
97108
98109 } else {
99110 return Result .error ("'edit' command doesn't support '%s'" , argName );
100111 }
101112 }
102113
103- return Result .of (Pair .of (item , edits ));
104- })
105- .map (pair -> pair .mapSnd (x -> Result .sequence (x )))
106- .then (pair -> {
107-
108- var ingrEdits = new ArrayList <IngredientEditDescriptor >();
109- var stepEdits = new ArrayList <StepEditDescriptor >();
110- var tagEdits = new ArrayList <TagEditDescriptor >();
111-
112- if (pair .snd ().isError ()) {
113- return Result .error (pair .snd ().getError ());
114- }
115-
116- var list = pair .snd ().getValue ();
117- for (var item : list ) {
118- if (item instanceof TagEditDescriptor ) {
119- tagEdits .add ((TagEditDescriptor ) item );
120- } else if (item instanceof StepEditDescriptor ) {
121- stepEdits .add ((StepEditDescriptor ) item );
122- } else if (item instanceof IngredientEditDescriptor ) {
123- ingrEdits .add ((IngredientEditDescriptor ) item );
124- } else {
125- // unreachable
126- assert false : "invalid edit descriptor" ;
127- }
114+ // ugly AF, but i don't care at the moment.
115+ var tes = Result .sequence (tagEdits );
116+ var ses = Result .sequence (stepEdits );
117+ var ies = Result .sequence (ingrEdits );
118+
119+ if (tes .isError ()) {
120+ return Result .error (tes .getError ());
121+ } else if (ses .isError ()) {
122+ return Result .error (ses .getError ());
123+ } else if (ies .isError ()) {
124+ return Result .error (ies .getError ());
128125 }
129126
130- return Result .of (new EditCommandStub (pair .fst (),
131- new RecipeEditDescriptor (ingrEdits , stepEdits , tagEdits )));
127+ return Result .of (new EditCommandStub (item ,
128+ new RecipeEditDescriptor (editedName , ies .getValue (), ses .getValue (), tes .getValue ())
129+ ));
132130 });
133131 }
134132
135133
136134
137135
138- private static Result <EditDescriptor > parseIngredientEdit (ArgName argName , String ingredientName ,
136+ private static Result <IngredientEditDescriptor > parseIngredientEdit (ArgName argName , String ingredientName ,
139137 Optional <Quantity > qty ) {
140138
141139 var components = argName .getComponents ();
@@ -163,7 +161,7 @@ private static Result<EditDescriptor> parseIngredientEdit(ArgName argName, Strin
163161
164162
165163
166- private static Result <EditDescriptor > parseTagEdit (ArgName argName , String argValue ) {
164+ private static Result <TagEditDescriptor > parseTagEdit (ArgName argName , String argValue ) {
167165
168166 var components = argName .getComponents ();
169167
@@ -184,7 +182,7 @@ private static Result<EditDescriptor> parseTagEdit(ArgName argName, String argVa
184182 ));
185183 }
186184
187- private static Result <EditDescriptor > parseStepEdit (ArgName argName , String argValue ) {
185+ private static Result <StepEditDescriptor > parseStepEdit (ArgName argName , String argValue ) {
188186
189187 var components = argName .getComponents ();
190188
0 commit comments