1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

Merge pull request #2819 from jfinkels/relpath-uresult

relpath: return UResult from uumain() function
This commit is contained in:
Sylvestre Ledru 2021-12-31 10:43:21 +01:00 committed by GitHub
commit a99ce6f3f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -11,6 +11,7 @@ use clap::{crate_version, App, Arg};
use std::env; use std::env;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use uucore::display::println_verbatim; use uucore::display::println_verbatim;
use uucore::error::{FromIo, UResult};
use uucore::fs::{canonicalize, MissingHandling, ResolveMode}; use uucore::fs::{canonicalize, MissingHandling, ResolveMode};
use uucore::InvalidEncodingHandling; use uucore::InvalidEncodingHandling;
@ -27,7 +28,8 @@ fn usage() -> String {
format!("{} [-d DIR] TO [FROM]", uucore::execution_phrase()) format!("{} [-d DIR] TO [FROM]", uucore::execution_phrase())
} }
pub fn uumain(args: impl uucore::Args) -> i32 { #[uucore_procs::gen_uumain]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let args = args let args = args
.collect_str(InvalidEncodingHandling::ConvertLossy) .collect_str(InvalidEncodingHandling::ConvertLossy)
.accept_any(); .accept_any();
@ -40,17 +42,19 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
Some(p) => Path::new(p).to_path_buf(), Some(p) => Path::new(p).to_path_buf(),
None => env::current_dir().unwrap(), None => env::current_dir().unwrap(),
}; };
let absto = canonicalize(to, MissingHandling::Normal, ResolveMode::Logical).unwrap(); let absto = canonicalize(to, MissingHandling::Normal, ResolveMode::Logical)
let absfrom = canonicalize(from, MissingHandling::Normal, ResolveMode::Logical).unwrap(); .map_err_context(String::new)?;
let absfrom = canonicalize(from, MissingHandling::Normal, ResolveMode::Logical)
.map_err_context(String::new)?;
if matches.is_present(options::DIR) { if matches.is_present(options::DIR) {
let base = Path::new(&matches.value_of(options::DIR).unwrap()).to_path_buf(); let base = Path::new(&matches.value_of(options::DIR).unwrap()).to_path_buf();
let absbase = canonicalize(base, MissingHandling::Normal, ResolveMode::Logical).unwrap(); let absbase = canonicalize(base, MissingHandling::Normal, ResolveMode::Logical)
.map_err_context(String::new)?;
if !absto.as_path().starts_with(absbase.as_path()) if !absto.as_path().starts_with(absbase.as_path())
|| !absfrom.as_path().starts_with(absbase.as_path()) || !absfrom.as_path().starts_with(absbase.as_path())
{ {
println_verbatim(absto).unwrap(); return println_verbatim(absto).map_err_context(String::new);
return 0;
} }
} }
@ -75,8 +79,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.map(|x| result.push(x.as_os_str())) .map(|x| result.push(x.as_os_str()))
.last(); .last();
println_verbatim(result).unwrap(); println_verbatim(result).map_err_context(String::new)
0
} }
pub fn uu_app() -> App<'static, 'static> { pub fn uu_app() -> App<'static, 'static> {