@@ -13,41 +13,41 @@ mod commitmsgfmt;
13
13
mod parser;
14
14
mod worditer;
15
15
16
- type CliResult < T > = Result < T , CliError > ;
16
+ type CliResult < ' a , T > = Result < T , CliError < ' a > > ;
17
17
18
18
#[ derive( Debug ) ]
19
- enum CliError {
19
+ enum CliError < ' a > {
20
20
ArgWidthNaN ( ParseIntError ) ,
21
21
ArgWidthOutOfBounds ( i32 ) ,
22
22
Io ( io:: Error ) ,
23
- Other ( String ) ,
23
+ Other ( std :: borrow :: Cow < ' a , str > ) ,
24
24
}
25
25
26
- impl From < io:: Error > for CliError {
27
- fn from ( err : io:: Error ) -> CliError {
26
+ impl < ' a > From < io:: Error > for CliError < ' a > {
27
+ fn from ( err : io:: Error ) -> CliError < ' a > {
28
28
CliError :: Io ( err)
29
29
}
30
30
}
31
31
32
- impl From < ParseIntError > for CliError {
33
- fn from ( err : ParseIntError ) -> CliError {
32
+ impl < ' a > From < ParseIntError > for CliError < ' a > {
33
+ fn from ( err : ParseIntError ) -> CliError < ' a > {
34
34
CliError :: ArgWidthNaN ( err)
35
35
}
36
36
}
37
37
38
- impl < ' a > From < & ' a str > for CliError {
39
- fn from ( err : & ' a str ) -> CliError {
40
- CliError :: Other ( err. to_owned ( ) )
38
+ impl < ' a > From < & ' a str > for CliError < ' a > {
39
+ fn from ( err : & ' a str ) -> CliError < ' a > {
40
+ CliError :: Other ( err. into ( ) )
41
41
}
42
42
}
43
43
44
- impl From < String > for CliError {
45
- fn from ( err : String ) -> CliError {
46
- CliError :: Other ( err)
44
+ impl < ' a > From < String > for CliError < ' a > {
45
+ fn from ( err : String ) -> CliError < ' a > {
46
+ CliError :: Other ( err. into ( ) )
47
47
}
48
48
}
49
49
50
- impl fmt:: Display for CliError {
50
+ impl < ' a > fmt:: Display for CliError < ' a > {
51
51
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
52
52
match * self {
53
53
CliError :: ArgWidthNaN ( ref err) => write ! ( f, "--width: {}" , err) ,
@@ -60,7 +60,7 @@ impl fmt::Display for CliError {
60
60
}
61
61
}
62
62
63
- impl Error for CliError {
63
+ impl < ' a > Error for CliError < ' a > {
64
64
fn source ( & self ) -> Option < & ( dyn Error + ' static ) > {
65
65
match * self {
66
66
CliError :: ArgWidthNaN ( ref err) => Some ( err) ,
@@ -77,7 +77,7 @@ pub struct Config {
77
77
}
78
78
79
79
impl Config {
80
- fn new ( m : & ArgMatches ) -> CliResult < Config > {
80
+ fn new < ' a > ( m : & ArgMatches ) -> CliResult < ' a , Config > {
81
81
use std:: str:: FromStr ;
82
82
83
83
let width = m
@@ -163,7 +163,8 @@ Some text is exempt from wrapping:
163
163
164
164
let result = read_all_bytes_from_stdin ( )
165
165
. and_then ( to_utf8)
166
- . and_then ( |text| to_stdout ( & commitmsgfmt. filter ( & text) ) ) ;
166
+ . map ( |text| commitmsgfmt. filter ( & text) )
167
+ . and_then ( to_stdout) ;
167
168
168
169
match result {
169
170
Ok ( ( ) ) => ( ) ,
@@ -178,26 +179,26 @@ Some text is exempt from wrapping:
178
179
}
179
180
}
180
181
181
- fn read_all_bytes_from_stdin ( ) -> CliResult < Vec < u8 > > {
182
+ fn read_all_bytes_from_stdin < ' a > ( ) -> CliResult < ' a , Vec < u8 > > {
182
183
let mut buf: Vec < u8 > = Vec :: new ( ) ;
183
184
let stdin = io:: stdin ( ) ;
184
185
io:: copy ( & mut stdin. lock ( ) , & mut buf) ?;
185
186
186
187
Ok ( buf)
187
188
}
188
189
189
- fn to_utf8 ( bytes : Vec < u8 > ) -> CliResult < String > {
190
+ fn to_utf8 < ' a > ( bytes : Vec < u8 > ) -> CliResult < ' a , String > {
190
191
String :: from_utf8 ( bytes) . or_else ( |e| from_iso_8859_1 ( & e. into_bytes ( ) ) )
191
192
}
192
193
193
- fn from_iso_8859_1 ( bytes : & [ u8 ] ) -> CliResult < String > {
194
+ fn from_iso_8859_1 < ' a > ( bytes : & [ u8 ] ) -> CliResult < ' a , String > {
194
195
use encoding:: types:: Encoding ;
195
196
encoding:: all:: ISO_8859_1
196
197
. decode ( bytes, encoding:: DecoderTrap :: Replace )
197
- . map_err ( |s| CliError :: Other ( s . into ( ) ) )
198
+ . map_err ( CliError :: Other )
198
199
}
199
200
200
- fn to_stdout ( msg : & str ) -> CliResult < ( ) > {
201
+ fn to_stdout < ' a > ( msg : String ) -> CliResult < ' a , ( ) > {
201
202
use std:: io:: Write ;
202
203
let stdout = io:: stdout ( ) ;
203
204
let mut stdout = stdout. lock ( ) ;
0 commit comments