Skip to content

Commit 1d2b3bf

Browse files
committed
age: Merge error cases in cli_common::file_io
1 parent bca6916 commit 1d2b3bf

1 file changed

Lines changed: 25 additions & 30 deletions

File tree

age/src/cli_common/file_io.rs

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,31 @@ use crate::{fl, util::LINE_ENDING, wfl, wlnfl};
1616
const SHORT_OUTPUT_LENGTH: usize = 20 * 80;
1717

1818
#[derive(Debug)]
19-
struct DenyBinaryOutputError;
20-
21-
impl fmt::Display for DenyBinaryOutputError {
22-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23-
wlnfl!(f, "err-deny-binary-output")?;
24-
wfl!(f, "rec-deny-binary-output")
25-
}
26-
}
27-
28-
impl std::error::Error for DenyBinaryOutputError {}
29-
30-
#[derive(Debug)]
31-
struct DetectedBinaryOutputError;
32-
33-
impl fmt::Display for DetectedBinaryOutputError {
34-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35-
wlnfl!(f, "err-detected-binary")?;
36-
wfl!(f, "rec-detected-binary")
37-
}
19+
enum FileError {
20+
DenyBinaryOutput,
21+
DenyOverwriteFile(String),
22+
DetectedBinaryOutput,
3823
}
3924

40-
impl std::error::Error for DetectedBinaryOutputError {}
41-
42-
#[derive(Debug)]
43-
struct DenyOverwriteFileError(String);
44-
45-
impl fmt::Display for DenyOverwriteFileError {
25+
impl fmt::Display for FileError {
4626
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47-
wfl!(f, "err-deny-overwrite-file", filename = self.0.as_str())
27+
match self {
28+
Self::DenyBinaryOutput => {
29+
wlnfl!(f, "err-deny-binary-output")?;
30+
wfl!(f, "rec-deny-binary-output")
31+
}
32+
Self::DenyOverwriteFile(filename) => {
33+
wfl!(f, "err-deny-overwrite-file", filename = filename.as_str())
34+
}
35+
Self::DetectedBinaryOutput => {
36+
wlnfl!(f, "err-detected-binary")?;
37+
wfl!(f, "rec-detected-binary")
38+
}
39+
}
4840
}
4941
}
5042

51-
impl std::error::Error for DenyOverwriteFileError {}
43+
impl std::error::Error for FileError {}
5244

5345
/// Wrapper around a [`File`].
5446
pub struct FileReader {
@@ -211,7 +203,7 @@ impl Write for StdoutWriter {
211203
if std::str::from_utf8(data).is_err() {
212204
return Err(io::Error::new(
213205
io::ErrorKind::InvalidInput,
214-
DetectedBinaryOutputError,
206+
FileError::DetectedBinaryOutput,
215207
));
216208
}
217209
}
@@ -359,7 +351,7 @@ impl OutputWriter {
359351
if !allow_overwrite && Path::new(&filename).exists() {
360352
return Err(io::Error::new(
361353
io::ErrorKind::AlreadyExists,
362-
DenyOverwriteFileError(filename),
354+
FileError::DenyOverwriteFile(filename),
363355
));
364356
}
365357

@@ -378,7 +370,10 @@ impl OutputWriter {
378370
} else if is_tty {
379371
if let OutputFormat::Binary = format {
380372
// If output == Some("-") then this error is skipped.
381-
return Err(io::Error::new(io::ErrorKind::Other, DenyBinaryOutputError));
373+
return Err(io::Error::new(
374+
io::ErrorKind::Other,
375+
FileError::DenyBinaryOutput,
376+
));
382377
}
383378
}
384379

0 commit comments

Comments
 (0)