Some small improvements to du readability.

Mostly, making more use of `map` and `map_err` in `Result`. One benefit
is that at least one location had duplicated logic for how to map the
error, which is no longer the case after this commit.
This commit is contained in:
Jason Gedge 2020-03-29 13:11:59 -04:00 committed by Jason Gedge
parent 35dc7438a5
commit 2ddab3e8ce

View File

@ -89,36 +89,21 @@ impl PerItemCommand for Du {
fn du(args: DuArgs, ctx: &RunnablePerItemContext) -> Result<OutputStream, ShellError> { fn du(args: DuArgs, ctx: &RunnablePerItemContext) -> Result<OutputStream, ShellError> {
let tag = ctx.name.clone(); let tag = ctx.name.clone();
let exclude = args let exclude = args.exclude.map_or(Ok(None), move |x| {
.exclude Pattern::new(&x.item)
.clone() .map(Option::Some)
.map_or(Ok(None), move |x| match Pattern::new(&x.item) { .map_err(|e| ShellError::labeled_error(e.msg, "glob error", x.tag.clone()))
Ok(p) => Ok(Some(p)),
Err(e) => Err(ShellError::labeled_error(
e.msg,
"Glob error",
x.tag.clone(),
)),
})?; })?;
let path = args.path.clone();
let filter_files = path.is_none(); let filter_files = args.path.is_none();
let paths = match path { let paths = match args.path {
Some(p) => match glob::glob_with( Some(p) => {
p.item.to_str().expect("Why isn't this encoded properly?"), let p = p.item.to_str().expect("Why isn't this encoded properly?");
GLOB_PARAMS, glob::glob_with(p, GLOB_PARAMS)
) { }
Ok(g) => Ok(g), None => glob::glob_with("*", GLOB_PARAMS),
Err(e) => Err(ShellError::labeled_error( }
e.msg, .map_err(|e| ShellError::labeled_error(e.msg, "glob error", tag.clone()))?
"Glob error",
p.tag.clone(),
)),
},
None => match glob::glob_with("*", GLOB_PARAMS) {
Ok(g) => Ok(g),
Err(e) => Err(ShellError::labeled_error(e.msg, "Glob error", tag.clone())),
},
}?
.filter(move |p| { .filter(move |p| {
if filter_files { if filter_files {
match p { match p {
@ -130,10 +115,7 @@ fn du(args: DuArgs, ctx: &RunnablePerItemContext) -> Result<OutputStream, ShellE
true true
} }
}) })
.map(move |p| match p { .map(|v| v.map_err(glob_err_into));
Err(e) => Err(glob_err_into(e)),
Ok(s) => Ok(s),
});
let ctrl_c = ctx.ctrl_c.clone(); let ctrl_c = ctx.ctrl_c.clone();
let all = args.all; let all = args.all;
@ -324,10 +306,7 @@ impl From<DirInfo> for Value {
if !d.files.is_empty() { if !d.files.is_empty() {
let v = Value { let v = Value {
value: UntaggedValue::Table( value: UntaggedValue::Table(
d.files d.files.into_iter().map(Into::into).collect::<Vec<Value>>(),
.into_iter()
.map(move |f| f.into())
.collect::<Vec<Value>>(),
), ),
tag: d.tag.clone(), tag: d.tag.clone(),
}; };
@ -336,10 +315,7 @@ impl From<DirInfo> for Value {
if !d.dirs.is_empty() { if !d.dirs.is_empty() {
let v = Value { let v = Value {
value: UntaggedValue::Table( value: UntaggedValue::Table(
d.dirs d.dirs.into_iter().map(Into::into).collect::<Vec<Value>>(),
.into_iter()
.map(move |d| d.into())
.collect::<Vec<Value>>(),
), ),
tag: d.tag.clone(), tag: d.tag.clone(),
}; };