Skip to content

Commit 524247e

Browse files
afnan-mojumdersylvestre
authored andcommitted
mv: address review feedback on RenameError and tests
1 parent bff6152 commit 524247e

3 files changed

Lines changed: 268 additions & 342 deletions

File tree

‎src/uu/mv/src/error.rs‎

Lines changed: 119 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
// file that was distributed with this source code.
55

66
use std::io;
7-
use std::path::Path;
7+
use std::path::{Path, PathBuf};
88
use uucore::display::Quotable;
9-
use uucore::error::{FromIo, UError, USimpleError};
9+
use uucore::error::{FromIo, UError, USimpleError, strip_errno};
1010
use uucore::translate;
1111

1212
#[derive(Debug, thiserror::Error)]
@@ -33,63 +33,144 @@ pub enum MvError {
3333

3434
impl UError for MvError {}
3535

36-
#[derive(Debug)]
36+
#[derive(Debug, thiserror::Error)]
3737
pub enum RenameError {
3838
/// A raw I/O error from `fs::rename` (or general failure) that should be
3939
/// contextualized as "cannot move 'source' to 'target': {err}" or
4040
/// "cannot overwrite 'target': Directory not empty".
41-
Io(io::Error),
42-
/// A specific diagnostic that already includes the file path and operation,
43-
/// such as "cannot open 'src' for reading: Permission denied" or
44-
/// "error writing 'dst': No space left on device".
45-
Formatted(String),
41+
#[error("{0}")]
42+
Io(#[from] io::Error),
43+
#[error("")]
44+
PromptDeclined,
45+
#[error(
46+
"{}",
47+
translate!(
48+
"mv-error-inter-device-move-failed",
49+
"from" => .from.quote(),
50+
"to" => .to.quote(),
51+
"err" => strip_errno(.err)
52+
)
53+
)]
54+
InterDeviceMoveFailed {
55+
from: PathBuf,
56+
to: PathBuf,
57+
err: io::Error,
58+
},
59+
#[error(
60+
"{}",
61+
translate!(
62+
"mv-error-cannot-open-for-reading",
63+
"from" => .path.quote(),
64+
"err" => strip_errno(.err)
65+
)
66+
)]
67+
CannotOpenForReading { path: PathBuf, err: io::Error },
68+
#[error(
69+
"{}",
70+
translate!(
71+
"mv-error-cannot-create-regular-file",
72+
"to" => .path.quote(),
73+
"err" => strip_errno(.err)
74+
)
75+
)]
76+
CannotCreateRegularFile { path: PathBuf, err: io::Error },
77+
#[error(
78+
"{}",
79+
translate!(
80+
"mv-error-cannot-create-directory",
81+
"path" => .path.quote(),
82+
"err" => strip_errno(.err)
83+
)
84+
)]
85+
CannotCreateDirectory { path: PathBuf, err: io::Error },
86+
#[error(
87+
"{}",
88+
translate!(
89+
"mv-error-cannot-access",
90+
"path" => .path.quote(),
91+
"err" => strip_errno(.err)
92+
)
93+
)]
94+
CannotAccess { path: PathBuf, err: io::Error },
95+
#[cfg(unix)]
96+
#[error(
97+
"{}",
98+
translate!(
99+
"mv-error-error-writing",
100+
"to" => .path.quote(),
101+
"err" => strip_errno(.err)
102+
)
103+
)]
104+
ErrorWriting { path: PathBuf, err: io::Error },
105+
#[error(
106+
"{}",
107+
translate!(
108+
"mv-error-cannot-remove",
109+
"from" => .path.quote(),
110+
"err" => strip_errno(.err)
111+
)
112+
)]
113+
CannotRemove { path: PathBuf, err: io::Error },
46114
}
47115

48-
impl std::fmt::Display for RenameError {
49-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50-
match self {
51-
Self::Io(e) => write!(f, "{e}"),
52-
Self::Formatted(s) => write!(f, "{s}"),
116+
impl RenameError {
117+
pub fn inter_device_move_failed<'a>(
118+
from: &'a Path,
119+
to: &'a Path,
120+
) -> impl FnOnce(io::Error) -> Self + 'a {
121+
move |err| Self::InterDeviceMoveFailed {
122+
from: from.to_path_buf(),
123+
to: to.to_path_buf(),
124+
err,
53125
}
54126
}
55-
}
56127

57-
impl std::error::Error for RenameError {
58-
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
59-
match self {
60-
Self::Io(e) => Some(e),
61-
Self::Formatted(_) => None,
128+
pub fn cannot_open_for_reading(path: &Path) -> impl FnOnce(io::Error) -> Self + '_ {
129+
move |err| Self::CannotOpenForReading {
130+
path: path.to_path_buf(),
131+
err,
62132
}
63133
}
64-
}
65134

66-
impl From<io::Error> for RenameError {
67-
fn from(err: io::Error) -> Self {
68-
Self::Io(err)
135+
pub fn cannot_create_regular_file(path: &Path) -> impl FnOnce(io::Error) -> Self + '_ {
136+
move |err| Self::CannotCreateRegularFile {
137+
path: path.to_path_buf(),
138+
err,
139+
}
69140
}
70-
}
71141

72-
impl From<RenameError> for io::Error {
73-
fn from(err: RenameError) -> Self {
74-
match err {
75-
RenameError::Io(e) => e,
76-
RenameError::Formatted(s) => Self::other(s),
142+
pub fn cannot_create_directory(path: &Path) -> impl FnOnce(io::Error) -> Self + '_ {
143+
move |err| Self::CannotCreateDirectory {
144+
path: path.to_path_buf(),
145+
err,
77146
}
78147
}
79-
}
80148

81-
impl RenameError {
82-
pub fn is_empty(&self) -> bool {
83-
match self {
84-
Self::Io(e) => e.to_string().is_empty(),
85-
Self::Formatted(s) => s.is_empty(),
149+
pub fn cannot_access(path: &Path) -> impl FnOnce(io::Error) -> Self + '_ {
150+
move |err| Self::CannotAccess {
151+
path: path.to_path_buf(),
152+
err,
153+
}
154+
}
155+
156+
#[cfg(unix)]
157+
pub fn error_writing(path: &Path) -> impl FnOnce(io::Error) -> Self + '_ {
158+
move |err| Self::ErrorWriting {
159+
path: path.to_path_buf(),
160+
err,
161+
}
162+
}
163+
164+
pub fn cannot_remove(path: &Path) -> impl FnOnce(io::Error) -> Self + '_ {
165+
move |err| Self::CannotRemove {
166+
path: path.to_path_buf(),
167+
err,
86168
}
87169
}
88170

89171
pub fn into_uerror(self, source: &Path, target: &Path) -> Box<dyn UError> {
90172
match self {
91-
Self::Formatted(s) => USimpleError::new(1, s),
92-
Self::Io(e) if e.to_string().is_empty() => USimpleError::new(1, ""),
173+
Self::PromptDeclined => USimpleError::new(1, ""),
93174
Self::Io(e) => {
94175
if e.kind() == io::ErrorKind::DirectoryNotEmpty {
95176
e.map_err_context(|| {
@@ -110,6 +191,7 @@ impl RenameError {
110191
USimpleError::new(1, format!("{e}"))
111192
}
112193
}
194+
other => USimpleError::new(1, other.to_string()),
113195
}
114196
}
115197
}

0 commit comments

Comments
 (0)