cp: uumain returns UResult, UError for Error
@@ -49,6 +49,7 @@ use std::path::{Path, PathBuf, StripPrefixError};
use std::str::FromStr;
use std::string::ToString;
use uucore::backup_control::{self, BackupMode};
+use uucore::error::{set_exit_code, UError, UResult};
use uucore::fs::{canonicalize, MissingHandling, ResolveMode};
use walkdir::WalkDir;
@@ -105,6 +106,12 @@ quick_error! {
}
}
+impl UError for Error {
+ fn code(&self) -> i32 {
+ 1
+ }
+}
+
/// Continue next iteration of loop if result of expression is error
macro_rules! or_continue(
($expr:expr) => (match $expr {
@@ -220,7 +227,6 @@ pub struct Options {
static ABOUT: &str = "Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.";
static LONG_HELP: &str = "";
-static EXIT_OK: i32 = 0;
static EXIT_ERR: i32 = 1;
fn usage() -> String {
@@ -446,7 +452,7 @@ pub fn uu_app() -> App<'static, 'static> {
.multiple(true))
}
-pub fn uumain(args: impl uucore::Args) -> i32 {
+pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let usage = usage();
let matches = uu_app()
.after_help(&*format!(
@@ -457,11 +463,12 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.usage(&usage[..])
.get_matches_from(args);
- let options = crash_if_err!(EXIT_ERR, Options::from_matches(&matches));
+ let options = Options::from_matches(&matches)?;
if options.overwrite == OverwriteMode::NoClobber && options.backup != BackupMode::NoBackup {
show_usage_error!("options --backup and --no-clobber are mutually exclusive");
- return 1;
+ set_exit_code(EXIT_ERR);
+ return Ok(());
}
let paths: Vec<String> = matches
@@ -469,7 +476,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.map(|v| v.map(ToString::to_string).collect())
.unwrap_or_default();
- let (sources, target) = crash_if_err!(EXIT_ERR, parse_path_args(&paths, &options));
+ let (sources, target) = parse_path_args(&paths, &options)?;
if let Err(error) = copy(&sources, &target, &options) {
match error {
@@ -479,10 +486,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
// Else we caught a fatal bubbled-up error, log it to stderr
_ => show_error!("{}", error),
};
- return EXIT_ERR;
+ set_exit_code(EXIT_ERR);
}
- EXIT_OK
+ Ok(())
}
impl ClobberMode {
@@ -1124,7 +1131,7 @@ fn copy_attribute(source: &Path, dest: &Path, attribute: &Attribute) -> CopyResu
let xattrs = xattr::list(source)?;
for attr in xattrs {
if let Some(attr_value) = xattr::get(source, attr.clone())? {
- crash_if_err!(EXIT_ERR, xattr::set(dest, attr, &attr_value[..]));
+ xattr::set(dest, attr, &attr_value[..])?;
}
}
}
