1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-31 04:57:45 +00:00

uucore: clean up returning Err

This commit is contained in:
Jeong YunWon 2021-06-11 14:26:22 +09:00
parent b59c1dae59
commit fb67e54e20
4 changed files with 34 additions and 48 deletions

View file

@ -113,22 +113,14 @@ fn resolve<P: AsRef<Path>>(original: P) -> IOResult<PathBuf> {
)); ));
} }
match fs::symlink_metadata(&result) { if !fs::symlink_metadata(&result)?.file_type().is_symlink() {
Err(e) => return Err(e), break;
Ok(ref m) if !m.file_type().is_symlink() => break,
Ok(..) => {
followed += 1;
match fs::read_link(&result) {
Ok(path) => {
result.pop();
result.push(path);
}
Err(e) => {
return Err(e);
}
}
}
} }
followed += 1;
let path = fs::read_link(&result)?;
result.pop();
result.push(path);
} }
Ok(result) Ok(result)
} }
@ -193,10 +185,8 @@ pub fn canonicalize<P: AsRef<Path>>(original: P, can_mode: CanonicalizeMode) ->
} }
match resolve(&result) { match resolve(&result) {
Err(e) => match can_mode { Err(_) if can_mode == CanonicalizeMode::Missing => continue,
CanonicalizeMode::Missing => continue, Err(e) => return Err(e),
_ => return Err(e),
},
Ok(path) => { Ok(path) => {
result.pop(); result.pop();
result.push(path); result.push(path);
@ -211,15 +201,14 @@ pub fn canonicalize<P: AsRef<Path>>(original: P, can_mode: CanonicalizeMode) ->
} }
match resolve(&result) { match resolve(&result) {
Err(e) => { Err(e) if can_mode == CanonicalizeMode::Existing => {
if can_mode == CanonicalizeMode::Existing { return Err(e);
return Err(e);
}
} }
Ok(path) => { Ok(path) => {
result.pop(); result.pop();
result.push(path); result.push(path);
} }
Err(_) => (),
} }
} }
Ok(result) Ok(result)

View file

@ -89,19 +89,19 @@ fn parse_levels(mode: &str) -> (u32, usize) {
} }
fn parse_op(mode: &str, default: Option<char>) -> Result<(char, usize), String> { fn parse_op(mode: &str, default: Option<char>) -> Result<(char, usize), String> {
match mode.chars().next() { let ch = mode
Some(ch) => match ch { .chars()
'+' | '-' | '=' => Ok((ch, 1)), .next()
_ => match default { .ok_or_else(|| "unexpected end of mode".to_owned())?;
Some(ch) => Ok((ch, 0)), Ok(match ch {
None => Err(format!( '+' | '-' | '=' => (ch, 1),
"invalid operator (expected +, -, or =, but found {})", _ => {
ch let ch = default.ok_or_else(|| {
)), format!("invalid operator (expected +, -, or =, but found {})", ch)
}, })?;
}, (ch, 0)
None => Err("unexpected end of mode".to_owned()), }
} })
} }
fn parse_change(mode: &str, fperm: u32, considering_dir: bool) -> (u32, usize) { fn parse_change(mode: &str, fperm: u32, considering_dir: bool) -> (u32, usize) {

View file

@ -20,20 +20,18 @@ pub fn from_str(string: &str) -> Result<Duration, String> {
'm' | 'M' => (slice, 60), 'm' | 'M' => (slice, 60),
'h' | 'H' => (slice, 60 * 60), 'h' | 'H' => (slice, 60 * 60),
'd' | 'D' => (slice, 60 * 60 * 24), 'd' | 'D' => (slice, 60 * 60 * 24),
val => { val if !val.is_alphabetic() => (string, 1),
if !val.is_alphabetic() { _ => {
(string, 1) if string == "inf" || string == "infinity" {
} else if string == "inf" || string == "infinity" {
("inf", 1) ("inf", 1)
} else { } else {
return Err(format!("invalid time interval '{}'", string)); return Err(format!("invalid time interval '{}'", string));
} }
} }
}; };
let num = match numstr.parse::<f64>() { let num = numstr
Ok(m) => m, .parse::<f64>()
Err(e) => return Err(format!("invalid time interval '{}': {}", string, e)), .map_err(|e| format!("invalid time interval '{}': {}", string, e))?;
};
const NANOS_PER_SEC: u32 = 1_000_000_000; const NANOS_PER_SEC: u32 = 1_000_000_000;
let whole_secs = num.trunc(); let whole_secs = num.trunc();

View file

@ -85,10 +85,9 @@ impl Range {
let mut ranges: Vec<Range> = vec![]; let mut ranges: Vec<Range> = vec![];
for item in list.split(',') { for item in list.split(',') {
match FromStr::from_str(item) { let range_item = FromStr::from_str(item)
Ok(range_item) => ranges.push(range_item), .map_err(|e| format!("range '{}' was invalid: {}", item, e))?;
Err(e) => return Err(format!("range '{}' was invalid: {}", item, e)), ranges.push(range_item);
}
} }
ranges.sort(); ranges.sort();