Convert Shellerror::GenericError to named fields (#11230)

# Description

Replace `.to_string()` used in `GenericError` with `.into()` as
`.into()` seems more popular

Replace `Vec::new()` used in `GenericError` with `vec![]` as `vec![]`
seems more popular

(There are so, so many)
This commit is contained in:
Eric Hodel
2023-12-06 15:40:03 -08:00
committed by GitHub
parent b03f1efac4
commit a95a4505ef
160 changed files with 2975 additions and 3228 deletions

View File

@ -100,13 +100,13 @@ impl Command for Cp {
let sources: Vec<_> = match arg_glob(&src, &current_dir_path) {
Ok(files) => files.collect(),
Err(e) => {
return Err(ShellError::GenericError(
e.to_string(),
"invalid pattern".to_string(),
Some(src.span),
None,
Vec::new(),
))
return Err(ShellError::GenericError {
error: e.to_string(),
msg: "invalid pattern".into(),
span: Some(src.span),
help: None,
inner: vec![],
})
}
};
@ -115,25 +115,25 @@ impl Command for Cp {
}
if sources.len() > 1 && !destination.is_dir() {
return Err(ShellError::GenericError(
"Destination must be a directory when copying multiple files".into(),
"is not a directory".into(),
Some(dst.span),
None,
Vec::new(),
));
return Err(ShellError::GenericError {
error: "Destination must be a directory when copying multiple files".into(),
msg: "is not a directory".into(),
span: Some(dst.span),
help: None,
inner: vec![],
});
}
let any_source_is_dir = sources.iter().any(|f| matches!(f, Ok(f) if f.is_dir()));
if any_source_is_dir && !recursive {
return Err(ShellError::GenericError(
"Directories must be copied using \"--recursive\"".into(),
"resolves to a directory (not copied)".into(),
Some(src.span),
None,
Vec::new(),
));
return Err(ShellError::GenericError {
error: "Directories must be copied using \"--recursive\"".into(),
msg: "resolves to a directory (not copied)".into(),
span: Some(src.span),
help: None,
inner: vec![],
});
}
let mut result = Vec::new();
@ -170,15 +170,15 @@ impl Command for Cp {
}
let res = if src == dst {
let message = format!("src and dst identical: {:?} (not copied)", src);
let msg = format!("src and dst identical: {:?} (not copied)", src);
return Err(ShellError::GenericError(
"Copy aborted".into(),
message,
Some(span),
None,
Vec::new(),
));
return Err(ShellError::GenericError {
error: "Copy aborted".into(),
msg,
span: Some(span),
help: None,
inner: vec![],
});
} else if interactive && dst.exists() {
if progress {
interactive_copy(
@ -210,25 +210,23 @@ impl Command for Cp {
match entry.file_name() {
Some(name) => destination.join(name),
None => {
return Err(ShellError::GenericError(
"Copy aborted. Not a valid path".into(),
"not a valid path".into(),
Some(dst.span),
None,
Vec::new(),
))
return Err(ShellError::GenericError {
error: "Copy aborted. Not a valid path".into(),
msg: "not a valid path".into(),
span: Some(dst.span),
help: None,
inner: vec![],
})
}
}
};
std::fs::create_dir_all(&destination).map_err(|e| {
ShellError::GenericError(
e.to_string(),
e.to_string(),
Some(dst.span),
None,
Vec::new(),
)
std::fs::create_dir_all(&destination).map_err(|e| ShellError::GenericError {
error: e.to_string(),
msg: e.to_string(),
span: Some(dst.span),
help: None,
inner: vec![],
})?;
let not_follow_symlink = call.has_flag("no-symlink");
@ -271,14 +269,12 @@ impl Command for Cp {
}
if s.is_dir() && !d.exists() {
std::fs::create_dir_all(&d).map_err(|e| {
ShellError::GenericError(
e.to_string(),
e.to_string(),
Some(dst.span),
None,
Vec::new(),
)
std::fs::create_dir_all(&d).map_err(|e| ShellError::GenericError {
error: e.to_string(),
msg: e.to_string(),
span: Some(dst.span),
help: None,
inner: vec![],
})?;
}
if s.is_symlink() && not_follow_symlink {
@ -374,7 +370,13 @@ fn interactive_copy(
);
if let Err(e) = interaction {
Value::error(
ShellError::GenericError(e.to_string(), e.to_string(), Some(span), None, Vec::new()),
ShellError::GenericError {
error: e.to_string(),
msg: e.to_string(),
span: Some(span),
help: None,
inner: vec![],
},
span,
)
} else if !confirmed {
@ -506,15 +508,15 @@ fn copy_symlink(
let target_path = read_link(src.as_path());
let target_path = match target_path {
Ok(p) => p,
Err(err) => {
Err(e) => {
return Value::error(
ShellError::GenericError(
err.to_string(),
err.to_string(),
Some(span),
None,
vec![],
),
ShellError::GenericError {
error: e.to_string(),
msg: e.to_string(),
span: Some(span),
help: None,
inner: vec![],
},
span,
)
}
@ -542,7 +544,13 @@ fn copy_symlink(
Value::string(msg, span)
}
Err(e) => Value::error(
ShellError::GenericError(e.to_string(), e.to_string(), Some(span), None, vec![]),
ShellError::GenericError {
error: e.to_string(),
msg: e.to_string(),
span: Some(span),
help: None,
inner: vec![],
},
span,
),
}

View File

@ -158,13 +158,13 @@ impl Command for Glob {
};
if glob_pattern.item.is_empty() {
return Err(ShellError::GenericError(
"glob pattern must not be empty".to_string(),
"glob pattern is empty".to_string(),
Some(glob_pattern.span),
Some("add characters to the glob pattern".to_string()),
Vec::new(),
));
return Err(ShellError::GenericError {
error: "glob pattern must not be empty".into(),
msg: "glob pattern is empty".into(),
span: Some(glob_pattern.span),
help: Some("add characters to the glob pattern".into()),
inner: vec![],
});
}
let folder_depth = if let Some(depth) = depth {
@ -176,13 +176,13 @@ impl Command for Glob {
let (prefix, glob) = match WaxGlob::new(&glob_pattern.item) {
Ok(p) => p.partition(),
Err(e) => {
return Err(ShellError::GenericError(
"error with glob pattern".to_string(),
format!("{e}"),
Some(glob_pattern.span),
None,
Vec::new(),
))
return Err(ShellError::GenericError {
error: "error with glob pattern".into(),
msg: format!("{e}"),
span: Some(glob_pattern.span),
help: None,
inner: vec![],
})
}
};
@ -195,13 +195,13 @@ impl Command for Glob {
std::path::PathBuf::new() // user should get empty list not an error
}
Err(e) => {
return Err(ShellError::GenericError(
"error in canonicalize".to_string(),
format!("{e}"),
Some(glob_pattern.span),
None,
Vec::new(),
))
return Err(ShellError::GenericError {
error: "error in canonicalize".into(),
msg: format!("{e}"),
span: Some(glob_pattern.span),
help: None,
inner: vec![],
})
}
};
@ -216,14 +216,12 @@ impl Command for Glob {
},
)
.not(np)
.map_err(|err| {
ShellError::GenericError(
"error with glob's not pattern".to_string(),
format!("{err}"),
Some(not_pattern_span),
None,
Vec::new(),
)
.map_err(|err| ShellError::GenericError {
error: "error with glob's not pattern".into(),
msg: format!("{err}"),
span: Some(not_pattern_span),
help: None,
inner: vec![],
})?
.flatten();
let result = glob_to_value(ctrlc, glob_results, no_dirs, no_files, no_symlinks, span)?;

View File

@ -120,13 +120,13 @@ impl Command for Ls {
);
#[cfg(not(unix))]
let error_msg = String::from("Permission denied");
return Err(ShellError::GenericError(
"Permission denied".to_string(),
error_msg,
Some(p_tag),
None,
Vec::new(),
));
return Err(ShellError::GenericError {
error: "Permission denied".into(),
msg: error_msg,
span: Some(p_tag),
help: None,
inner: vec![],
});
}
if is_empty_dir(&expanded) {
return Ok(Value::list(vec![], call_span).into_pipeline_data());
@ -168,13 +168,13 @@ impl Command for Ls {
let mut paths_peek = paths.peekable();
if paths_peek.peek().is_none() {
return Err(ShellError::GenericError(
format!("No matches found for {}", &path.display().to_string()),
"Pattern, file or folder not found".to_string(),
Some(p_tag),
Some("no matches found".to_string()),
Vec::new(),
));
return Err(ShellError::GenericError {
error: format!("No matches found for {}", &path.display().to_string()),
msg: "Pattern, file or folder not found".into(),
span: Some(p_tag),
help: Some("no matches found".into()),
inner: vec![],
});
}
let mut hidden_dirs = vec![];
@ -233,14 +233,12 @@ impl Command for Ls {
} else {
Some(path.to_string_lossy().to_string())
}
.ok_or_else(|| {
ShellError::GenericError(
format!("Invalid file name: {:}", path.to_string_lossy()),
"invalid file name".into(),
Some(call_span),
None,
Vec::new(),
)
.ok_or_else(|| ShellError::GenericError {
error: format!("Invalid file name: {:}", path.to_string_lossy()),
msg: "invalid file name".into(),
span: Some(call_span),
help: None,
inner: vec![],
});
match display_name {

View File

@ -119,13 +119,13 @@ impl Command for Mktemp {
})?
}
Err(e) => {
return Err(ShellError::GenericError(
format!("{}", e),
format!("{}", e),
None,
None,
Vec::new(),
));
return Err(ShellError::GenericError {
error: format!("{}", e),
msg: format!("{}", e),
span: None,
help: None,
inner: vec![],
});
}
};
Ok(PipelineData::Value(Value::string(res, span), None))

View File

@ -96,31 +96,31 @@ impl Command for Mv {
// it's an error.
if destination.exists() && !force && !destination.is_dir() && !source.is_dir() {
return Err(ShellError::GenericError(
"Destination file already exists".into(),
return Err(ShellError::GenericError {
error: "Destination file already exists".into(),
// These messages all use to_string_lossy() because
// showing the full path reduces misinterpretation of the message.
// Also, this is preferable to {:?} because that renders Windows paths incorrectly.
format!(
msg: format!(
"Destination file '{}' already exists",
destination.to_string_lossy()
),
Some(spanned_destination.span),
Some("you can use -f, --force to force overwriting the destination".into()),
Vec::new(),
));
span: Some(spanned_destination.span),
help: Some("you can use -f, --force to force overwriting the destination".into()),
inner: vec![],
});
}
if (destination.exists() && !destination.is_dir() && sources.len() > 1)
|| (!destination.exists() && sources.len() > 1)
{
return Err(ShellError::GenericError(
"Can only move multiple sources if destination is a directory".into(),
"destination must be a directory when moving multiple sources".into(),
Some(spanned_destination.span),
None,
Vec::new(),
));
return Err(ShellError::GenericError {
error: "Can only move multiple sources if destination is a directory".into(),
msg: "destination must be a directory when moving multiple sources".into(),
span: Some(spanned_destination.span),
help: None,
inner: vec![],
});
}
// This is the case where you move a directory A to the interior of directory B, but directory B
@ -129,17 +129,17 @@ impl Command for Mv {
if let Some(name) = source.file_name() {
let dst = destination.join(name);
if dst.is_dir() {
return Err(ShellError::GenericError(
format!(
return Err(ShellError::GenericError {
error: format!(
"Can't move '{}' to '{}'",
source.to_string_lossy(),
dst.to_string_lossy()
),
format!("Directory '{}' is not empty", destination.to_string_lossy()),
Some(spanned_destination.span),
None,
Vec::new(),
));
msg: format!("Directory '{}' is not empty", destination.to_string_lossy()),
span: Some(spanned_destination.span),
help: None,
inner: vec![],
});
}
}
}
@ -149,16 +149,16 @@ impl Command for Mv {
.find(|f| matches!(f, Ok(f) if destination.starts_with(f)));
if destination.exists() && destination.is_dir() && sources.len() == 1 {
if let Some(Ok(filename)) = some_if_source_is_destination {
return Err(ShellError::GenericError(
format!(
return Err(ShellError::GenericError {
error: format!(
"Not possible to move '{}' to itself",
filename.to_string_lossy()
),
"cannot move to itself".into(),
Some(spanned_destination.span),
None,
Vec::new(),
));
msg: "cannot move to itself".into(),
span: Some(spanned_destination.span),
help: None,
inner: vec![],
});
}
}
@ -291,13 +291,13 @@ fn move_file(
format!("mv: overwrite '{}'? ", to.to_string_lossy()),
);
if let Err(e) = interaction {
return Err(ShellError::GenericError(
format!("Error during interaction: {e:}"),
"could not move".into(),
None,
None,
Vec::new(),
));
return Err(ShellError::GenericError {
error: format!("Error during interaction: {e:}"),
msg: "could not move".into(),
span: None,
help: None,
inner: vec![],
});
} else if !confirmed {
return Ok(false);
}
@ -341,13 +341,13 @@ fn move_item(from: &Path, from_span: Span, to: &Path) -> Result<(), ShellError>
}
_ => e.to_string(),
};
Err(ShellError::GenericError(
format!("Could not move {from:?} to {to:?}. Error Kind: {error_kind}"),
"could not move".into(),
Some(from_span),
None,
Vec::new(),
))
Err(ShellError::GenericError {
error: format!("Could not move {from:?} to {to:?}. Error Kind: {error_kind}"),
msg: "could not move".into(),
span: Some(from_span),
help: None,
inner: vec![],
})
}
}
})

View File

@ -125,13 +125,13 @@ impl Command for Open {
#[cfg(not(unix))]
let error_msg = String::from("Permission denied");
return Err(ShellError::GenericError(
"Permission denied".into(),
error_msg,
Some(arg_span),
None,
Vec::new(),
));
return Err(ShellError::GenericError {
error: "Permission denied".into(),
msg: error_msg,
span: Some(arg_span),
help: None,
inner: vec![],
});
} else {
#[cfg(feature = "sqlite")]
if !raw {
@ -146,13 +146,13 @@ impl Command for Open {
let file = match std::fs::File::open(path) {
Ok(file) => file,
Err(err) => {
return Err(ShellError::GenericError(
"Permission denied".into(),
err.to_string(),
Some(arg_span),
None,
Vec::new(),
));
return Err(ShellError::GenericError {
error: "Permission denied".into(),
msg: err.to_string(),
span: Some(arg_span),
help: None,
inner: vec![],
});
}
};
@ -200,13 +200,13 @@ impl Command for Open {
decl.run(engine_state, stack, &Call::new(call_span), file_contents)
};
output.push(command_output.map_err(|inner| {
ShellError::GenericError(
format!("Error while parsing as {ext}"),
format!("Could not parse '{}' with `from {}`", path.display(), ext),
Some(arg_span),
Some(format!("Check out `help from {}` or `help from` for more options or open raw data with `open --raw '{}'`", ext, path.display())),
vec![inner],
)
ShellError::GenericError{
error: format!("Error while parsing as {ext}"),
msg: format!("Could not parse '{}' with `from {}`", path.display(), ext),
span: Some(arg_span),
help: Some(format!("Check out `help from {}` or `help from` for more options or open raw data with `open --raw '{}'`", ext, path.display())),
inner: vec![inner],
}
})?);
}
None => output.push(file_contents),

View File

@ -173,47 +173,47 @@ fn rm(
if !TRASH_SUPPORTED {
if rm_always_trash {
return Err(ShellError::GenericError(
"Cannot execute `rm`; the current configuration specifies \
return Err(ShellError::GenericError {
error: "Cannot execute `rm`; the current configuration specifies \
`always_trash = true`, but the current nu executable was not \
built with feature `trash_support`."
.into(),
"trash required to be true but not supported".into(),
Some(span),
None,
Vec::new(),
));
msg: "trash required to be true but not supported".into(),
span: Some(span),
help: None,
inner: vec![],
});
} else if trash {
return Err(ShellError::GenericError(
"Cannot execute `rm` with option `--trash`; feature `trash-support` not enabled or on an unsupported platform"
return Err(ShellError::GenericError{
error: "Cannot execute `rm` with option `--trash`; feature `trash-support` not enabled or on an unsupported platform"
.into(),
"this option is only available if nu is built with the `trash-support` feature and the platform supports trash"
msg: "this option is only available if nu is built with the `trash-support` feature and the platform supports trash"
.into(),
Some(span),
None,
Vec::new(),
));
span: Some(span),
help: None,
inner: vec![],
});
}
}
if targets.is_empty() {
return Err(ShellError::GenericError(
"rm requires target paths".into(),
"needs parameter".into(),
Some(span),
None,
Vec::new(),
));
return Err(ShellError::GenericError {
error: "rm requires target paths".into(),
msg: "needs parameter".into(),
span: Some(span),
help: None,
inner: vec![],
});
}
if unique_argument_check.is_some() && !(interactive_once || interactive) {
return Err(ShellError::GenericError(
"You are trying to remove your home dir".into(),
"If you really want to remove your home dir, please use -I or -i".into(),
unique_argument_check,
None,
Vec::new(),
));
return Err(ShellError::GenericError {
error: "You are trying to remove your home dir".into(),
msg: "If you really want to remove your home dir, please use -I or -i".into(),
span: unique_argument_check,
help: None,
inner: vec![],
});
}
let targets_span = Span::new(
@ -236,13 +236,13 @@ fn rm(
if currentdir_path.to_string_lossy() == target.item
|| currentdir_path.starts_with(format!("{}{}", target.item, std::path::MAIN_SEPARATOR))
{
return Err(ShellError::GenericError(
"Cannot remove any parent directory".into(),
"cannot remove any parent directory".into(),
Some(target.span),
None,
Vec::new(),
));
return Err(ShellError::GenericError {
error: "Cannot remove any parent directory".into(),
msg: "cannot remove any parent directory".into(),
span: Some(target.span),
help: None,
inner: vec![],
});
}
let path = currentdir_path.join(&target.item);
@ -268,13 +268,13 @@ fn rm(
.or_insert_with(|| target.span);
}
Err(e) => {
return Err(ShellError::GenericError(
format!("Could not remove {:}", path.to_string_lossy()),
e.to_string(),
Some(target.span),
None,
Vec::new(),
));
return Err(ShellError::GenericError {
error: format!("Could not remove {:}", path.to_string_lossy()),
msg: e.to_string(),
span: Some(target.span),
help: None,
inner: vec![],
});
}
}
}
@ -285,25 +285,25 @@ fn rm(
}
}
Err(e) => {
return Err(ShellError::GenericError(
e.to_string(),
e.to_string(),
Some(target.span),
None,
Vec::new(),
))
return Err(ShellError::GenericError {
error: e.to_string(),
msg: e.to_string(),
span: Some(target.span),
help: None,
inner: vec![],
})
}
};
}
if all_targets.is_empty() && !force {
return Err(ShellError::GenericError(
"File(s) not found".into(),
"File(s) not found".into(),
Some(targets_span),
None,
Vec::new(),
));
return Err(ShellError::GenericError {
error: "File(s) not found".into(),
msg: "File(s) not found".into(),
span: Some(targets_span),
help: None,
inner: vec![],
});
}
if interactive_once {
@ -312,13 +312,13 @@ fn rm(
format!("rm: remove {} files? ", all_targets.len()),
);
if let Err(e) = interaction {
return Err(ShellError::GenericError(
format!("Error during interaction: {e:}"),
"could not move".into(),
None,
None,
Vec::new(),
));
return Err(ShellError::GenericError {
error: format!("Error during interaction: {e:}"),
msg: "could not move".into(),
span: None,
help: None,
inner: vec![],
});
} else if !confirmed {
return Ok(PipelineData::Empty);
}
@ -406,28 +406,28 @@ fn rm(
Value::nothing(span)
}
} else {
let msg = format!("Cannot remove {:}. try --recursive", f.to_string_lossy());
let error = format!("Cannot remove {:}. try --recursive", f.to_string_lossy());
Value::error(
ShellError::GenericError(
msg,
"cannot remove non-empty directory".into(),
Some(span),
None,
Vec::new(),
),
ShellError::GenericError {
error,
msg: "cannot remove non-empty directory".into(),
span: Some(span),
help: None,
inner: vec![],
},
span,
)
}
} else {
let msg = format!("no such file or directory: {:}", f.to_string_lossy());
let error = format!("no such file or directory: {:}", f.to_string_lossy());
Value::error(
ShellError::GenericError(
msg,
"no such file or directory".into(),
Some(span),
None,
Vec::new(),
),
ShellError::GenericError {
error,
msg: "no such file or directory".into(),
span: Some(span),
help: None,
inner: vec![],
},
span,
)
}

View File

@ -323,16 +323,16 @@ fn prepare_path(
let path = &path.item;
if !(force || append) && path.exists() {
Err(ShellError::GenericError(
"Destination file already exists".into(),
format!(
Err(ShellError::GenericError {
error: "Destination file already exists".into(),
msg: format!(
"Destination file '{}' already exists",
path.to_string_lossy()
),
Some(span),
Some("you can use -f, --force to force overwriting the destination".into()),
Vec::new(),
))
span: Some(span),
help: Some("you can use -f, --force to force overwriting the destination".into()),
inner: vec![],
})
} else {
Ok((path, span))
}
@ -347,14 +347,12 @@ fn open_file(path: &Path, span: Span, append: bool) -> Result<File, ShellError>
_ => std::fs::File::create(path),
};
file.map_err(|err| {
ShellError::GenericError(
"Permission denied".into(),
err.to_string(),
Some(span),
None,
Vec::new(),
)
file.map_err(|e| ShellError::GenericError {
error: "Permission denied".into(),
msg: e.to_string(),
span: Some(span),
help: None,
inner: vec![],
})
}
@ -380,13 +378,13 @@ fn get_files(
let stderr_file = stderr_path_and_span
.map(|(stderr_path, stderr_path_span)| {
if path == stderr_path {
Err(ShellError::GenericError(
"input and stderr input to same file".to_string(),
"can't save both input and stderr input to the same file".to_string(),
Some(stderr_path_span),
Some("you should use `o+e> file` instead".to_string()),
vec![],
))
Err(ShellError::GenericError {
error: "input and stderr input to same file".into(),
msg: "can't save both input and stderr input to the same file".into(),
span: Some(stderr_path_span),
help: Some("you should use `o+e> file` instead".into()),
inner: vec![],
})
} else {
open_file(stderr_path, stderr_path_span, append || err_append)
}

View File

@ -52,14 +52,12 @@ impl Command for Start {
if file_path.exists() {
open_path(path_no_whitespace, engine_state, stack, path.span)?;
} else if file_path.starts_with("https://") || file_path.starts_with("http://") {
let url = url::Url::parse(&path.item).map_err(|_| {
ShellError::GenericError(
format!("Cannot parse url: {}", &path.item),
"".to_string(),
Some(path.span),
Some("cannot parse".to_string()),
Vec::new(),
)
let url = url::Url::parse(&path.item).map_err(|_| ShellError::GenericError {
error: format!("Cannot parse url: {}", &path.item),
msg: "".to_string(),
span: Some(path.span),
help: Some("cannot parse".to_string()),
inner: vec![],
})?;
open_path(url.as_str(), engine_state, stack, path.span)?;
} else {
@ -73,14 +71,12 @@ impl Command for Start {
let path_with_prefix = Path::new("https://").join(&path.item);
let common_domains = ["com", "net", "org", "edu", "sh"];
if let Some(url) = path_with_prefix.to_str() {
let url = url::Url::parse(url).map_err(|_| {
ShellError::GenericError(
format!("Cannot parse url: {}", &path.item),
"".to_string(),
Some(path.span),
Some("cannot parse".to_string()),
Vec::new(),
)
let url = url::Url::parse(url).map_err(|_| ShellError::GenericError {
error: format!("Cannot parse url: {}", &path.item),
msg: "".into(),
span: Some(path.span),
help: Some("cannot parse".into()),
inner: vec![],
})?;
if let Some(domain) = url.host() {
let domain = domain.to_string();
@ -91,13 +87,13 @@ impl Command for Start {
}
}
}
return Err(ShellError::GenericError(
format!("Cannot find file or url: {}", &path.item),
"".to_string(),
Some(path.span),
Some("Use prefix https:// to disambiguate URLs from files".to_string()),
Vec::new(),
));
return Err(ShellError::GenericError {
error: format!("Cannot find file or url: {}", &path.item),
msg: "".into(),
span: Some(path.span),
help: Some("Use prefix https:// to disambiguate URLs from files".into()),
inner: vec![],
});
}
};
}

View File

@ -135,34 +135,34 @@ impl Command for UCp {
})
.collect();
if paths.is_empty() {
return Err(ShellError::GenericError(
"Missing file operand".into(),
"Missing file operand".into(),
Some(call.head),
Some("Please provide source and destination paths".into()),
Vec::new(),
));
return Err(ShellError::GenericError {
error: "Missing file operand".into(),
msg: "Missing file operand".into(),
span: Some(call.head),
help: Some("Please provide source and destination paths".into()),
inner: vec![],
});
}
if paths.len() == 1 {
return Err(ShellError::GenericError(
"Missing destination path".into(),
format!("Missing destination path operand after {}", paths[0].item),
Some(paths[0].span),
None,
Vec::new(),
));
return Err(ShellError::GenericError {
error: "Missing destination path".into(),
msg: format!("Missing destination path operand after {}", paths[0].item),
span: Some(paths[0].span),
help: None,
inner: vec![],
});
}
let target = paths.pop().expect("Should not be reached?");
let target_path = PathBuf::from(&target.item);
if target.item.ends_with(PATH_SEPARATOR) && !target_path.is_dir() {
return Err(ShellError::GenericError(
"is not a directory".into(),
"is not a directory".into(),
Some(target.span),
None,
Vec::new(),
));
return Err(ShellError::GenericError {
error: "is not a directory".into(),
msg: "is not a directory".into(),
span: Some(target.span),
help: None,
inner: vec![],
});
};
// paths now contains the sources
@ -180,13 +180,15 @@ impl Command for UCp {
match v {
Ok(path) => {
if !recursive && path.is_dir() {
return Err(ShellError::GenericError(
"could_not_copy_directory".into(),
"resolves to a directory (not copied)".into(),
Some(p.span),
Some("Directories must be copied using \"--recursive\"".into()),
Vec::new(),
));
return Err(ShellError::GenericError {
error: "could_not_copy_directory".into(),
msg: "resolves to a directory (not copied)".into(),
span: Some(p.span),
help: Some(
"Directories must be copied using \"--recursive\"".into(),
),
inner: vec![],
});
};
app_vals.push(path)
}
@ -240,13 +242,13 @@ impl Command for UCp {
// code should still be EXIT_ERR as does GNU cp
uu_cp::Error::NotAllFilesCopied => {}
_ => {
return Err(ShellError::GenericError(
format!("{}", error),
format!("{}", error),
None,
None,
Vec::new(),
))
return Err(ShellError::GenericError {
error: format!("{}", error),
msg: format!("{}", error),
span: None,
help: None,
inner: vec![],
})
}
};
// TODO: What should we do in place of set_exit_code?

View File

@ -68,13 +68,13 @@ impl Command for UMkdir {
for dir in directories {
if let Err(error) = mkdir(&dir, IS_RECURSIVE, DEFAULT_MODE, is_verbose) {
return Err(ShellError::GenericError(
format!("{}", error),
format!("{}", error),
None,
None,
Vec::new(),
));
return Err(ShellError::GenericError {
error: format!("{}", error),
msg: format!("{}", error),
span: None,
help: None,
inner: vec![],
});
}
}