Skip to content

Commit f3e5a7b

Browse files
committed
Borrow strs for CLI error messages
1 parent a45c338 commit f3e5a7b

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

src/main.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,41 @@ mod commitmsgfmt;
1313
mod parser;
1414
mod worditer;
1515

16-
type CliResult<T> = Result<T, CliError>;
16+
type CliResult<'a, T> = Result<T, CliError<'a>>;
1717

1818
#[derive(Debug)]
19-
enum CliError {
19+
enum CliError<'a> {
2020
ArgWidthNaN(ParseIntError),
2121
ArgWidthOutOfBounds(i32),
2222
Io(io::Error),
23-
Other(String),
23+
Other(std::borrow::Cow<'a, str>),
2424
}
2525

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> {
2828
CliError::Io(err)
2929
}
3030
}
3131

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> {
3434
CliError::ArgWidthNaN(err)
3535
}
3636
}
3737

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())
4141
}
4242
}
4343

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())
4747
}
4848
}
4949

50-
impl fmt::Display for CliError {
50+
impl<'a> fmt::Display for CliError<'a> {
5151
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5252
match *self {
5353
CliError::ArgWidthNaN(ref err) => write!(f, "--width: {}", err),
@@ -60,7 +60,7 @@ impl fmt::Display for CliError {
6060
}
6161
}
6262

63-
impl Error for CliError {
63+
impl<'a> Error for CliError<'a> {
6464
fn source(&self) -> Option<&(dyn Error + 'static)> {
6565
match *self {
6666
CliError::ArgWidthNaN(ref err) => Some(err),
@@ -77,7 +77,7 @@ pub struct Config {
7777
}
7878

7979
impl Config {
80-
fn new(m: &ArgMatches) -> CliResult<Config> {
80+
fn new<'a>(m: &ArgMatches) -> CliResult<'a, Config> {
8181
use std::str::FromStr;
8282

8383
let width = m
@@ -163,7 +163,8 @@ Some text is exempt from wrapping:
163163

164164
let result = read_all_bytes_from_stdin()
165165
.and_then(to_utf8)
166-
.and_then(|text| to_stdout(&commitmsgfmt.filter(&text)));
166+
.map(|text| commitmsgfmt.filter(&text))
167+
.and_then(to_stdout);
167168

168169
match result {
169170
Ok(()) => (),
@@ -178,26 +179,26 @@ Some text is exempt from wrapping:
178179
}
179180
}
180181

181-
fn read_all_bytes_from_stdin() -> CliResult<Vec<u8>> {
182+
fn read_all_bytes_from_stdin<'a>() -> CliResult<'a, Vec<u8>> {
182183
let mut buf: Vec<u8> = Vec::new();
183184
let stdin = io::stdin();
184185
io::copy(&mut stdin.lock(), &mut buf)?;
185186

186187
Ok(buf)
187188
}
188189

189-
fn to_utf8(bytes: Vec<u8>) -> CliResult<String> {
190+
fn to_utf8<'a>(bytes: Vec<u8>) -> CliResult<'a, String> {
190191
String::from_utf8(bytes).or_else(|e| from_iso_8859_1(&e.into_bytes()))
191192
}
192193

193-
fn from_iso_8859_1(bytes: &[u8]) -> CliResult<String> {
194+
fn from_iso_8859_1<'a>(bytes: &[u8]) -> CliResult<'a, String> {
194195
use encoding::types::Encoding;
195196
encoding::all::ISO_8859_1
196197
.decode(bytes, encoding::DecoderTrap::Replace)
197-
.map_err(|s| CliError::Other(s.into()))
198+
.map_err(CliError::Other)
198199
}
199200

200-
fn to_stdout(msg: &str) -> CliResult<()> {
201+
fn to_stdout<'a>(msg: String) -> CliResult<'a, ()> {
201202
use std::io::Write;
202203
let stdout = io::stdout();
203204
let mut stdout = stdout.lock();

0 commit comments

Comments
 (0)