removed unwraps (#430)

This commit is contained in:
Fernando Herrera
2021-12-04 12:38:21 +00:00
committed by GitHub
parent eed22605ef
commit 8a06ea133b
24 changed files with 233 additions and 159 deletions

View File

@ -49,7 +49,7 @@ impl Command for Cp {
let interactive = call.has_flag("interactive");
let force = call.has_flag("force");
let path: PathBuf = current_dir().unwrap();
let path = current_dir()?;
let source = path.join(source.as_str());
let destination = path.join(destination.as_str());
@ -83,12 +83,36 @@ impl Command for Cp {
let prompt = format!(
"Are you shure that you want to copy {} to {}?",
file.as_ref()
.unwrap()
.map_err(|err| ShellError::LabeledError(
"Reference error".into(),
err.to_string(),
call.head
))?
.file_name()
.unwrap()
.ok_or_else(|| ShellError::LabeledError(
"File name error".into(),
"Unable to get file name".into(),
call.head
))?
.to_str()
.unwrap(),
destination.file_name().unwrap().to_str().unwrap()
.ok_or_else(|| ShellError::LabeledError(
"Unable to get str error".into(),
"Unable to convert to str file name".into(),
call.head
))?,
destination
.file_name()
.ok_or_else(|| ShellError::LabeledError(
"File name error".into(),
"Unable to get file name".into(),
call.head
))?
.to_str()
.ok_or_else(|| ShellError::LabeledError(
"Unable to get str error".into(),
"Unable to convert to str file name".into(),
call.head
))?,
);
let input = get_interactive_confirmation(prompt)?;

View File

@ -55,7 +55,13 @@ impl Command for Ls {
};
let call_span = call.head;
let glob = glob::glob(&pattern).unwrap();
let glob = glob::glob(&pattern).map_err(|err| {
nu_protocol::ShellError::LabeledError(
"Error extracting glob pattern".into(),
err.to_string(),
call.head,
)
})?;
Ok(glob
.into_iter()

View File

@ -5,7 +5,7 @@ use super::util::get_interactive_confirmation;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, PipelineData, ShellError, Signature, SyntaxShape};
use nu_protocol::{Category, PipelineData, ShellError, Signature, Spanned, SyntaxShape};
#[derive(Clone)]
pub struct Mv;
@ -45,22 +45,20 @@ impl Command for Mv {
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
// TODO: handle invalid directory or insufficient permissions when moving
let source: String = call.req(engine_state, stack, 0)?;
let spanned_source: Spanned<String> = call.req(engine_state, stack, 0)?;
let destination: String = call.req(engine_state, stack, 1)?;
let interactive = call.has_flag("interactive");
let force = call.has_flag("force");
let path: PathBuf = current_dir().unwrap();
let source = path.join(source.as_str());
let path: PathBuf = current_dir()?;
let source = path.join(spanned_source.item.as_str());
let destination = path.join(destination.as_str());
let mut sources =
glob::glob(&source.to_string_lossy()).map_or_else(|_| Vec::new(), Iterator::collect);
if sources.is_empty() {
return Err(ShellError::FileNotFound(
call.positional.first().unwrap().span,
));
return Err(ShellError::FileNotFound(spanned_source.span));
}
if interactive && !force {
@ -69,12 +67,36 @@ impl Command for Mv {
let prompt = format!(
"Are you shure that you want to move {} to {}?",
file.as_ref()
.unwrap()
.map_err(|err| ShellError::LabeledError(
"Reference error".into(),
err.to_string(),
call.head
))?
.file_name()
.unwrap()
.ok_or_else(|| ShellError::LabeledError(
"File name error".into(),
"Unable to get file name".into(),
call.head
))?
.to_str()
.unwrap(),
destination.file_name().unwrap().to_str().unwrap()
.ok_or_else(|| ShellError::LabeledError(
"Unable to get str error".into(),
"Unable to convert to str file name".into(),
call.head
))?,
destination
.file_name()
.ok_or_else(|| ShellError::LabeledError(
"File name error".into(),
"Unable to get file name".into(),
call.head
))?
.to_str()
.ok_or_else(|| ShellError::LabeledError(
"Unable to get str error".into(),
"Unable to convert to str file name".into(),
call.head
))?,
);
let input = get_interactive_confirmation(prompt)?;

View File

@ -84,23 +84,6 @@ fn rm(
"Can't use \"--trash\" with \"--permanent\"".to_string(),
call.head,
));
// let trash_span = call.get_flag_expr("trash").unwrap().span;
// let perm_span = call.get_flag_expr("permanent").unwrap().span;
// let left_message = "cannot use".to_string();
// let right_message = "with".to_string();
// let (left_span, right_span) = match trash_span.start < perm_span.start {
// true => (trash_span, perm_span),
// false => (perm_span, trash_span),
// };
// return Err(ShellError::IncompatibleParameters {
// left_message,
// left_span,
// right_message,
// right_span,
// });
}
let current_path = current_dir()?;
@ -141,7 +124,19 @@ fn rm(
for (index, file) in targets.iter().enumerate() {
let prompt: String = format!(
"Are you sure that you what to delete {}?",
file.1.file_name().unwrap().to_str().unwrap()
file.1
.file_name()
.ok_or_else(|| ShellError::LabeledError(
"File name error".into(),
"Unable to get file name".into(),
call.head
))?
.to_str()
.ok_or_else(|| ShellError::LabeledError(
"Unable to get str error".into(),
"Unable to convert to str file name".into(),
call.head
))?,
);
let input = get_interactive_confirmation(prompt)?;
@ -192,9 +187,18 @@ fn rm_helper(call: &Call, args: RmArgs) -> Vec<Value> {
#[cfg(not(feature = "trash-support"))]
{
if trash {
return vec![Value::Error {
error: ShellError::FeatureNotEnabled(call.get_flag_expr("trash").unwrap().span),
}];
let error = match call.get_flag_expr("trash").ok_or_else(|| {
ShellError::LabeledError(
"Flag not found".into(),
"trash flag not found".into(),
call.head,
)
}) {
Ok(expr) => ShellError::FeatureNotEnabled(expr.span),
Err(err) => err,
};
return vec![Value::Error { error }];
}
}