@@ -3,11 +3,34 @@ use std::fmt::Display;
33use std:: fmt:: Formatter ;
44use std:: process:: { Command , ExitStatus , Stdio } ;
55
6+ use crate :: print_util:: format_command;
7+
8+ /// Contains a description of the command, for error reporting purposes.
9+ #[ derive( Clone , Debug ) ]
10+ pub struct CommandInfo {
11+ command_string : String ,
12+ }
13+
14+ impl From < & mut Command > for CommandInfo {
15+ fn from ( command : & mut Command ) -> Self {
16+ CommandInfo {
17+ command_string : format_command ( command) ,
18+ }
19+ }
20+ }
21+
22+ impl Display for CommandInfo {
23+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
24+ write ! ( f, "{}" , self . command_string)
25+ }
26+ }
27+
28+ /// Represents errors that can occur when executing system commands.
629#[ derive( Debug ) ]
730pub enum ExecuteCommandError {
8- ErrorRunning ( String ) ,
9- JobFailed ( String ) ,
10- ErrorParsing ( String ) ,
31+ ErrorRunning ( CommandInfo ) ,
32+ JobFailed ( CommandInfo ) ,
33+ ErrorParsing ( CommandInfo ) ,
1134 ErrorRunningBuildProcess ,
1235}
1336
@@ -16,14 +39,15 @@ impl Error for ExecuteCommandError {}
1639impl Display for ExecuteCommandError {
1740 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
1841 let message = match self {
19- ExecuteCommandError :: ErrorRunning ( job) => format ! (
20- "Error running {}: ensure it is installed and available in your system PATH." ,
21- job
42+ ExecuteCommandError :: ErrorRunning ( command_info) => format ! (
43+ "Error running `{command_info}`: ensure it is installed and available in your system PATH." ,
2244 ) ,
23- ExecuteCommandError :: JobFailed ( job) => {
24- format ! ( "Job {} failed." , job)
45+ ExecuteCommandError :: JobFailed ( command_info) => {
46+ format ! ( "Job failed, command: `{command_info}`" )
47+ }
48+ ExecuteCommandError :: ErrorParsing ( command_info) => {
49+ format ! ( "Error parsing output of `{command_info}`" )
2550 }
26- ExecuteCommandError :: ErrorParsing ( job) => format ! ( "Error parsing {} output" , job) ,
2751 ExecuteCommandError :: ErrorRunningBuildProcess => {
2852 "contract build process was not running" . to_string ( )
2953 }
@@ -32,25 +56,21 @@ impl Display for ExecuteCommandError {
3256 }
3357}
3458
35- pub ( crate ) fn execute_command (
36- command : & mut Command ,
37- job : & str ,
38- ) -> Result < String , ExecuteCommandError > {
59+ pub ( crate ) fn execute_command ( command : & mut Command ) -> Result < String , ExecuteCommandError > {
3960 let output = command
4061 . stderr ( Stdio :: inherit ( ) )
4162 . output ( )
42- . map_err ( |_| ExecuteCommandError :: ErrorRunning ( job . to_string ( ) ) ) ?;
63+ . map_err ( |_| ExecuteCommandError :: ErrorRunning ( command . into ( ) ) ) ?;
4364
4465 if !output. status . success ( ) {
45- return Err ( ExecuteCommandError :: JobFailed ( job . to_string ( ) ) ) ;
66+ return Err ( ExecuteCommandError :: JobFailed ( command . into ( ) ) ) ;
4667 }
4768
48- String :: from_utf8 ( output. stdout ) . map_err ( |_| ExecuteCommandError :: ErrorParsing ( job . to_string ( ) ) )
69+ String :: from_utf8 ( output. stdout ) . map_err ( |_| ExecuteCommandError :: ErrorParsing ( command . into ( ) ) )
4970}
5071
5172pub ( crate ) fn execute_spawn_command (
5273 command : & mut Command ,
53- job : & str ,
5474) -> Result < ExitStatus , ExecuteCommandError > {
5575 let response = command
5676 . spawn ( )
@@ -59,7 +79,7 @@ pub(crate) fn execute_spawn_command(
5979 . map_err ( |_| ExecuteCommandError :: ErrorRunningBuildProcess ) ?;
6080
6181 if !response. success ( ) {
62- return Err ( ExecuteCommandError :: JobFailed ( job . to_string ( ) ) ) ;
82+ return Err ( ExecuteCommandError :: JobFailed ( command . into ( ) ) ) ;
6383 }
6484
6585 Ok ( response)
0 commit comments