@@ -3,7 +3,8 @@ pragma solidity >=0.8.13 <0.9.0;
3
3
4
4
import {VmSafe} from "forge-std/Vm.sol " ;
5
5
import {vulcan} from "./Vulcan.sol " ;
6
- import {Result, ResultType, Ok, Error} from "./Result.sol " ;
6
+ import {Result, ResultType, Ok} from "./Result.sol " ;
7
+ import {LibError, Error} from "./Error.sol " ;
7
8
import {removeSelector} from "../_utils/removeSelector.sol " ;
8
9
9
10
/// @dev Struct used to hold command parameters. Useful for creating commands that can be run
@@ -12,9 +13,7 @@ struct Command {
12
13
string [] inputs;
13
14
}
14
15
15
- struct CommandResult {
16
- Result _inner;
17
- }
16
+ type CommandResult is bytes32 ;
18
17
19
18
struct CommandOutput {
20
19
int32 exitCode;
@@ -25,6 +24,7 @@ struct CommandOutput {
25
24
26
25
library commands {
27
26
using commands for * ;
27
+ using CommandError for * ;
28
28
29
29
/// @dev Creates a new 'Command' struct with empty arguments.
30
30
/// @return cmd A new empty 'Command' struct.
@@ -161,14 +161,14 @@ library commands {
161
161
/// @dev Runs a command using the specified `Command` struct as parameters and returns the result.
162
162
/// @param self The `Command` struct that holds the parameters of the command.
163
163
/// @return The result of the command as a bytes array.
164
- function run (Command memory self ) internal returns (CommandResult memory ) {
164
+ function run (Command memory self ) internal returns (CommandResult) {
165
165
return self.inputs.run ();
166
166
}
167
167
168
168
/// @dev Runs a command with the specified `inputs` as parameters and returns the result.
169
169
/// @param inputs An array of strings representing the parameters of the command.
170
170
/// @return result The result of the command as a bytes array.
171
- function run (string [] memory inputs ) internal returns (CommandResult memory result ) {
171
+ function run (string [] memory inputs ) internal returns (CommandResult result ) {
172
172
try vulcan.hevm.tryFfi (inputs) returns (VmSafe.FfiResult memory ffiResult ) {
173
173
CommandOutput memory output;
174
174
@@ -179,89 +179,90 @@ library commands {
179
179
180
180
return Ok (output);
181
181
} catch Error (string memory message ) {
182
- return CommandError.notExecuted (message);
183
- } catch (bytes memory message ) {
184
- return CommandError.notExecuted (abi.decode (removeSelector (message), (string )));
182
+ return CommandError.NotExecuted (message).toCommandResult ();
183
+ } catch (bytes memory data ) {
184
+ string memory message = abi.decode (removeSelector (data), (string ));
185
+ return CommandError.NotExecuted (message).toCommandResult ();
185
186
}
186
187
}
187
188
188
- function run (string [1 ] memory inputs ) internal returns (CommandResult memory ) {
189
+ function run (string [1 ] memory inputs ) internal returns (CommandResult) {
189
190
return _toDynamic (inputs).run ();
190
191
}
191
192
192
- function run (string [2 ] memory inputs ) internal returns (CommandResult memory ) {
193
+ function run (string [2 ] memory inputs ) internal returns (CommandResult) {
193
194
return _toDynamic (inputs).run ();
194
195
}
195
196
196
- function run (string [3 ] memory inputs ) internal returns (CommandResult memory ) {
197
+ function run (string [3 ] memory inputs ) internal returns (CommandResult) {
197
198
return _toDynamic (inputs).run ();
198
199
}
199
200
200
- function run (string [4 ] memory inputs ) internal returns (CommandResult memory ) {
201
+ function run (string [4 ] memory inputs ) internal returns (CommandResult) {
201
202
return _toDynamic (inputs).run ();
202
203
}
203
204
204
- function run (string [5 ] memory inputs ) internal returns (CommandResult memory ) {
205
+ function run (string [5 ] memory inputs ) internal returns (CommandResult) {
205
206
return _toDynamic (inputs).run ();
206
207
}
207
208
208
- function run (string [6 ] memory inputs ) internal returns (CommandResult memory ) {
209
+ function run (string [6 ] memory inputs ) internal returns (CommandResult) {
209
210
return _toDynamic (inputs).run ();
210
211
}
211
212
212
- function run (string [7 ] memory inputs ) internal returns (CommandResult memory ) {
213
+ function run (string [7 ] memory inputs ) internal returns (CommandResult) {
213
214
return _toDynamic (inputs).run ();
214
215
}
215
216
216
- function run (string [8 ] memory inputs ) internal returns (CommandResult memory ) {
217
+ function run (string [8 ] memory inputs ) internal returns (CommandResult) {
217
218
return _toDynamic (inputs).run ();
218
219
}
219
220
220
- function run (string [9 ] memory inputs ) internal returns (CommandResult memory ) {
221
+ function run (string [9 ] memory inputs ) internal returns (CommandResult) {
221
222
return _toDynamic (inputs).run ();
222
223
}
223
224
224
- function run (string [10 ] memory inputs ) internal returns (CommandResult memory ) {
225
+ function run (string [10 ] memory inputs ) internal returns (CommandResult) {
225
226
return _toDynamic (inputs).run ();
226
227
}
227
228
228
- function run (string [11 ] memory inputs ) internal returns (CommandResult memory ) {
229
+ function run (string [11 ] memory inputs ) internal returns (CommandResult) {
229
230
return _toDynamic (inputs).run ();
230
231
}
231
232
232
- function run (string [12 ] memory inputs ) internal returns (CommandResult memory ) {
233
+ function run (string [12 ] memory inputs ) internal returns (CommandResult) {
233
234
return _toDynamic (inputs).run ();
234
235
}
235
236
236
- function run (string [13 ] memory inputs ) internal returns (CommandResult memory ) {
237
+ function run (string [13 ] memory inputs ) internal returns (CommandResult) {
237
238
return _toDynamic (inputs).run ();
238
239
}
239
240
240
- function run (string [14 ] memory inputs ) internal returns (CommandResult memory ) {
241
+ function run (string [14 ] memory inputs ) internal returns (CommandResult) {
241
242
return _toDynamic (inputs).run ();
242
243
}
243
244
244
- function run (string [15 ] memory inputs ) internal returns (CommandResult memory ) {
245
+ function run (string [15 ] memory inputs ) internal returns (CommandResult) {
245
246
return _toDynamic (inputs).run ();
246
247
}
247
248
248
- function run (string [16 ] memory inputs ) internal returns (CommandResult memory ) {
249
+ function run (string [16 ] memory inputs ) internal returns (CommandResult) {
249
250
return _toDynamic (inputs).run ();
250
251
}
251
252
252
- function run (string [17 ] memory inputs ) internal returns (CommandResult memory ) {
253
+ function run (string [17 ] memory inputs ) internal returns (CommandResult) {
253
254
return _toDynamic (inputs).run ();
254
255
}
255
256
256
- function run (string [18 ] memory inputs ) internal returns (CommandResult memory ) {
257
+ function run (string [18 ] memory inputs ) internal returns (CommandResult) {
257
258
return _toDynamic (inputs).run ();
258
259
}
259
260
260
- function run (string [19 ] memory inputs ) internal returns (CommandResult memory ) {
261
+ function run (string [19 ] memory inputs ) internal returns (CommandResult) {
261
262
return _toDynamic (inputs).run ();
262
263
}
263
264
264
- function run (string [20 ] memory inputs ) internal returns (CommandResult memory ) {
265
+ function run (string [20 ] memory inputs ) internal returns (CommandResult) {
265
266
return _toDynamic (inputs).run ();
266
267
}
267
268
@@ -405,51 +406,64 @@ library commands {
405
406
}
406
407
407
408
library CommandError {
408
- bytes32 constant NOT_EXECUTED = keccak256 ( " COMMAND_NOT_EXECUTED " ) ;
409
+ using LibError for * ;
409
410
410
- function notExecuted (string memory reason ) public pure returns (CommandResult memory ) {
411
+ function NotExecuted (string memory reason ) internal pure returns (Error ) {
411
412
string memory message = string .concat ("The command was not executed: \" " , reason, "\" " );
412
- return CommandResult (Error (NOT_EXECUTED, message).toResult ());
413
+ return NotExecuted.encodeError (message, reason);
414
+ }
415
+
416
+ function toCommandResult (Error self ) internal pure returns (CommandResult) {
417
+ return CommandResult.wrap (Result.unwrap (self.toResult ()));
413
418
}
414
419
}
415
420
416
421
library LibCommandResult {
417
- /// @dev Checks if a `CommandResult` returned an `ok` exit code.
418
- function isOk (CommandResult memory self ) internal pure returns (bool ) {
419
- return self._inner.isOk ();
422
+ function isOk (CommandResult self ) internal pure returns (bool ) {
423
+ return self.asResult ().isOk ();
424
+ }
425
+
426
+ function isError (CommandResult self ) internal pure returns (bool ) {
427
+ return self.asResult ().isError ();
420
428
}
421
429
422
- /// @dev Checks if a `CommandResult` struct is an error.
423
- function isError (CommandResult memory self ) internal pure returns (bool ) {
424
- return self._inner.isError ();
430
+ function unwrap (CommandResult self ) internal pure returns (CommandOutput memory val ) {
431
+ bytes32 _val = self.asResult ().unwrap ();
432
+ assembly {
433
+ val := _val
434
+ }
425
435
}
426
436
427
- /// @dev Returns the output of a `CommandResult` or reverts if the result was an error.
428
- function unwrap (CommandResult memory self ) internal pure returns (CommandOutput memory ) {
429
- return abi.decode (self._inner.unwrap (), (CommandOutput));
437
+ function expect (CommandResult self , string memory err ) internal pure returns (CommandOutput memory ) {
438
+ if (self.isError ()) {
439
+ revert (err);
440
+ }
441
+
442
+ return self.toValue ();
430
443
}
431
444
432
- /// @dev Returns the output of a `CommandResult` or reverts if the result was an error.
433
- /// @param customError The error message that will be used when reverting.
434
- function expect (CommandResult memory self , string memory customError )
435
- internal
436
- pure
437
- returns (CommandOutput memory )
438
- {
439
- return abi.decode (self._inner.expect (customError), (CommandOutput));
445
+ function toError (CommandResult self ) internal pure returns (Error) {
446
+ return self.asResult ().toError ();
440
447
}
441
448
442
- function toValue (CommandResult memory self ) internal pure returns (CommandOutput memory ) {
443
- return abi.decode (self._inner.toValue (), (CommandOutput));
449
+ function toValue (CommandResult self ) internal pure returns (CommandOutput memory val ) {
450
+ bytes32 _val = self.asResult ().toValue ();
451
+ assembly {
452
+ val := _val
453
+ }
444
454
}
445
455
446
- function toError (CommandResult memory self ) internal pure returns (Error memory ) {
447
- return self._inner. toError ( );
456
+ function asResult (CommandResult self ) internal pure returns (Result ) {
457
+ return Result. wrap (CommandResult. unwrap (self) );
448
458
}
449
459
}
450
460
451
- function Ok (CommandOutput memory output ) pure returns (CommandResult memory ) {
452
- return CommandResult (Ok (abi.encode (output)));
461
+ function Ok (CommandOutput memory value ) pure returns (CommandResult) {
462
+ bytes32 _value;
463
+ assembly {
464
+ _value := value
465
+ }
466
+ return CommandResult.wrap (Result.unwrap (Ok (_value)));
453
467
}
454
468
455
469
using commands for Command global ;
0 commit comments