@@ -10,22 +10,22 @@ mod mode;
1010use clap:: { Arg , ArgAction , ArgMatches , Command } ;
1111use file_diff:: diff;
1212use filetime:: { set_file_times, FileTime } ;
13- use std:: error:: Error ;
14- use std:: fmt:: { Debug , Display } ;
13+ use std:: fmt:: Debug ;
1514use std:: fs:: File ;
1615use std:: fs:: { self , metadata} ;
1716use std:: path:: { Path , PathBuf , MAIN_SEPARATOR } ;
1817use std:: process;
18+ use thiserror:: Error ;
1919use uucore:: backup_control:: { self , BackupMode } ;
2020use uucore:: buf_copy:: copy_stream;
2121use uucore:: display:: Quotable ;
2222use uucore:: entries:: { grp2gid, usr2uid} ;
23- use uucore:: error:: { FromIo , UError , UIoError , UResult , UUsageError } ;
23+ use uucore:: error:: { FromIo , UError , UResult , UUsageError } ;
2424use uucore:: fs:: dir_strip_dot_for_creation;
2525use uucore:: mode:: get_umask;
2626use uucore:: perms:: { wrap_chown, Verbosity , VerbosityLevel } ;
2727use uucore:: process:: { getegid, geteuid} ;
28- use uucore:: { format_usage, help_about, help_usage, show, show_error, show_if_err, uio_error } ;
28+ use uucore:: { format_usage, help_about, help_usage, show, show_error, show_if_err} ;
2929
3030#[ cfg( unix) ]
3131use std:: os:: unix:: fs:: { FileTypeExt , MetadataExt } ;
@@ -52,22 +52,51 @@ pub struct Behavior {
5252 target_dir : Option < String > ,
5353}
5454
55- #[ derive( Debug ) ]
55+ #[ derive( Error , Debug ) ]
5656enum InstallError {
57+ #[ error( "Unimplemented feature: {0}" ) ]
5758 Unimplemented ( String ) ,
58- DirNeedsArg ( ) ,
59- CreateDirFailed ( PathBuf , std:: io:: Error ) ,
59+
60+ #[ error( "{} with -d requires at least one argument." , uucore:: util_name( ) ) ]
61+ DirNeedsArg ,
62+
63+ #[ error( "failed to create {0}" ) ]
64+ CreateDirFailed ( PathBuf , #[ source] std:: io:: Error ) ,
65+
66+ #[ error( "failed to chmod {}" , . 0 . quote( ) ) ]
6067 ChmodFailed ( PathBuf ) ,
68+
69+ #[ error( "failed to chown {}: {}" , . 0 . quote( ) , . 1 ) ]
6170 ChownFailed ( PathBuf , String ) ,
71+
72+ #[ error( "invalid target {}: No such file or directory" , . 0 . quote( ) ) ]
6273 InvalidTarget ( PathBuf ) ,
74+
75+ #[ error( "target {} is not a directory" , . 0 . quote( ) ) ]
6376 TargetDirIsntDir ( PathBuf ) ,
64- BackupFailed ( PathBuf , PathBuf , std:: io:: Error ) ,
65- InstallFailed ( PathBuf , PathBuf , std:: io:: Error ) ,
77+
78+ #[ error( "cannot backup {0} to {1}" ) ]
79+ BackupFailed ( PathBuf , PathBuf , #[ source] std:: io:: Error ) ,
80+
81+ #[ error( "cannot install {0} to {1}" ) ]
82+ InstallFailed ( PathBuf , PathBuf , #[ source] std:: io:: Error ) ,
83+
84+ #[ error( "strip program failed: {0}" ) ]
6685 StripProgramFailed ( String ) ,
67- MetadataFailed ( std:: io:: Error ) ,
86+
87+ #[ error( "metadata error" ) ]
88+ MetadataFailed ( #[ source] std:: io:: Error ) ,
89+
90+ #[ error( "invalid user: {}" , . 0 . quote( ) ) ]
6891 InvalidUser ( String ) ,
92+
93+ #[ error( "invalid group: {}" , . 0 . quote( ) ) ]
6994 InvalidGroup ( String ) ,
95+
96+ #[ error( "omitting directory {}" , . 0 . quote( ) ) ]
7097 OmittingDirectory ( PathBuf ) ,
98+
99+ #[ error( "failed to access {}: Not a directory" , . 0 . quote( ) ) ]
71100 NotADirectory ( PathBuf ) ,
72101}
73102
@@ -84,52 +113,6 @@ impl UError for InstallError {
84113 }
85114}
86115
87- impl Error for InstallError { }
88-
89- impl Display for InstallError {
90- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
91- match self {
92- Self :: Unimplemented ( opt) => write ! ( f, "Unimplemented feature: {opt}" ) ,
93- Self :: DirNeedsArg ( ) => {
94- write ! (
95- f,
96- "{} with -d requires at least one argument." ,
97- uucore:: util_name( )
98- )
99- }
100- Self :: CreateDirFailed ( dir, e) => {
101- Display :: fmt ( & uio_error ! ( e, "failed to create {}" , dir. quote( ) ) , f)
102- }
103- Self :: ChmodFailed ( file) => write ! ( f, "failed to chmod {}" , file. quote( ) ) ,
104- Self :: ChownFailed ( file, msg) => write ! ( f, "failed to chown {}: {}" , file. quote( ) , msg) ,
105- Self :: InvalidTarget ( target) => write ! (
106- f,
107- "invalid target {}: No such file or directory" ,
108- target. quote( )
109- ) ,
110- Self :: TargetDirIsntDir ( target) => {
111- write ! ( f, "target {} is not a directory" , target. quote( ) )
112- }
113- Self :: BackupFailed ( from, to, e) => Display :: fmt (
114- & uio_error ! ( e, "cannot backup {} to {}" , from. quote( ) , to. quote( ) ) ,
115- f,
116- ) ,
117- Self :: InstallFailed ( from, to, e) => Display :: fmt (
118- & uio_error ! ( e, "cannot install {} to {}" , from. quote( ) , to. quote( ) ) ,
119- f,
120- ) ,
121- Self :: StripProgramFailed ( msg) => write ! ( f, "strip program failed: {msg}" ) ,
122- Self :: MetadataFailed ( e) => Display :: fmt ( & uio_error ! ( e, "" ) , f) ,
123- Self :: InvalidUser ( user) => write ! ( f, "invalid user: {}" , user. quote( ) ) ,
124- Self :: InvalidGroup ( group) => write ! ( f, "invalid group: {}" , group. quote( ) ) ,
125- Self :: OmittingDirectory ( dir) => write ! ( f, "omitting directory {}" , dir. quote( ) ) ,
126- Self :: NotADirectory ( dir) => {
127- write ! ( f, "failed to access {}: Not a directory" , dir. quote( ) )
128- }
129- }
130- }
131- }
132-
133116#[ derive( Clone , Eq , PartialEq ) ]
134117pub enum MainFunction {
135118 /// Create directories
@@ -456,7 +439,7 @@ fn behavior(matches: &ArgMatches) -> UResult<Behavior> {
456439///
457440fn directory ( paths : & [ String ] , b : & Behavior ) -> UResult < ( ) > {
458441 if paths. is_empty ( ) {
459- Err ( InstallError :: DirNeedsArg ( ) . into ( ) )
442+ Err ( InstallError :: DirNeedsArg . into ( ) )
460443 } else {
461444 for path in paths. iter ( ) . map ( Path :: new) {
462445 // if the path already exist, don't try to create it again
0 commit comments