1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 12:37:49 +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) {
Err(e) => return Err(e),
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);
}
}
}
if !fs::symlink_metadata(&result)?.file_type().is_symlink() {
break;
}
followed += 1;
let path = fs::read_link(&result)?;
result.pop();
result.push(path);
}
Ok(result)
}
@ -193,10 +185,8 @@ pub fn canonicalize<P: AsRef<Path>>(original: P, can_mode: CanonicalizeMode) ->
}
match resolve(&result) {
Err(e) => match can_mode {
CanonicalizeMode::Missing => continue,
_ => return Err(e),
},
Err(_) if can_mode == CanonicalizeMode::Missing => continue,
Err(e) => return Err(e),
Ok(path) => {
result.pop();
result.push(path);
@ -211,15 +201,14 @@ pub fn canonicalize<P: AsRef<Path>>(original: P, can_mode: CanonicalizeMode) ->
}
match resolve(&result) {
Err(e) => {
if can_mode == CanonicalizeMode::Existing {
return Err(e);
}
Err(e) if can_mode == CanonicalizeMode::Existing => {
return Err(e);
}
Ok(path) => {
result.pop();
result.push(path);
}
Err(_) => (),
}
}
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> {
match mode.chars().next() {
Some(ch) => match ch {
'+' | '-' | '=' => Ok((ch, 1)),
_ => match default {
Some(ch) => Ok((ch, 0)),
None => Err(format!(
"invalid operator (expected +, -, or =, but found {})",
ch
)),
},
},
None => Err("unexpected end of mode".to_owned()),
}
let ch = mode
.chars()
.next()
.ok_or_else(|| "unexpected end of mode".to_owned())?;
Ok(match ch {
'+' | '-' | '=' => (ch, 1),
_ => {
let ch = default.ok_or_else(|| {
format!("invalid operator (expected +, -, or =, but found {})", ch)
})?;
(ch, 0)
}
})
}
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),
'h' | 'H' => (slice, 60 * 60),
'd' | 'D' => (slice, 60 * 60 * 24),
val => {
if !val.is_alphabetic() {
(string, 1)
} else if string == "inf" || string == "infinity" {
val if !val.is_alphabetic() => (string, 1),
_ => {
if string == "inf" || string == "infinity" {
("inf", 1)
} else {
return Err(format!("invalid time interval '{}'", string));
}
}
};
let num = match numstr.parse::<f64>() {
Ok(m) => m,
Err(e) => return Err(format!("invalid time interval '{}': {}", string, e)),
};
let num = numstr
.parse::<f64>()
.map_err(|e| format!("invalid time interval '{}': {}", string, e))?;
const NANOS_PER_SEC: u32 = 1_000_000_000;
let whole_secs = num.trunc();

View file

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