From a324a50bb7b98b9ea1ceb4b0fd8a6688ded1e744 Mon Sep 17 00:00:00 2001 From: Eric Hodel Date: Tue, 21 Nov 2023 01:08:10 -0800 Subject: [PATCH] Convert FileNotFound to named fields (#11120) # Description Part of #10700 # User-Facing Changes None # Tests + Formatting - :green_circle: `toolkit fmt` - :green_circle: `toolkit clippy` - :green_circle: `toolkit test` - :green_circle: `toolkit test stdlib` # After Submitting N/A --- crates/nu-cli/src/commands/history.rs | 6 +++--- crates/nu-command/src/env/source_env.rs | 4 +++- crates/nu-command/src/filesystem/cp.rs | 2 +- crates/nu-command/src/filesystem/mv.rs | 4 +++- crates/nu-command/src/filesystem/open.rs | 2 +- crates/nu-command/src/filesystem/ucp.rs | 2 +- crates/nu-command/src/system/nu_check.rs | 4 +++- crates/nu-protocol/src/shell_error.rs | 5 ++++- 8 files changed, 19 insertions(+), 10 deletions(-) diff --git a/crates/nu-cli/src/commands/history.rs b/crates/nu-cli/src/commands/history.rs index 3021cac271..0031851883 100644 --- a/crates/nu-cli/src/commands/history.rs +++ b/crates/nu-cli/src/commands/history.rs @@ -107,7 +107,7 @@ impl Command for History { ) }) }) - .ok_or(ShellError::FileNotFound(head))? + .ok_or(ShellError::FileNotFound { span: head })? .into_pipeline_data(ctrlc)), HistoryFileFormat::Sqlite => Ok(history_reader .and_then(|h| { @@ -119,12 +119,12 @@ impl Command for History { create_history_record(idx, entry, long, head) }) }) - .ok_or(ShellError::FileNotFound(head))? + .ok_or(ShellError::FileNotFound { span: head })? .into_pipeline_data(ctrlc)), } } } else { - Err(ShellError::FileNotFound(head)) + Err(ShellError::FileNotFound { span: head }) } } diff --git a/crates/nu-command/src/env/source_env.rs b/crates/nu-command/src/env/source_env.rs index d248e031c4..22657e737f 100644 --- a/crates/nu-command/src/env/source_env.rs +++ b/crates/nu-command/src/env/source_env.rs @@ -55,7 +55,9 @@ impl Command for SourceEnv { )? { PathBuf::from(&path) } else { - return Err(ShellError::FileNotFound(source_filename.span)); + return Err(ShellError::FileNotFound { + span: source_filename.span, + }); }; if let Some(parent) = file_path.parent() { diff --git a/crates/nu-command/src/filesystem/cp.rs b/crates/nu-command/src/filesystem/cp.rs index 8eb22cdea3..2012bc1111 100644 --- a/crates/nu-command/src/filesystem/cp.rs +++ b/crates/nu-command/src/filesystem/cp.rs @@ -111,7 +111,7 @@ impl Command for Cp { }; if sources.is_empty() { - return Err(ShellError::FileNotFound(src.span)); + return Err(ShellError::FileNotFound { span: src.span }); } if sources.len() > 1 && !destination.is_dir() { diff --git a/crates/nu-command/src/filesystem/mv.rs b/crates/nu-command/src/filesystem/mv.rs index 56dbde0323..4c911a61e9 100644 --- a/crates/nu-command/src/filesystem/mv.rs +++ b/crates/nu-command/src/filesystem/mv.rs @@ -80,7 +80,9 @@ impl Command for Mv { arg_glob(&spanned_source, &path).map_or_else(|_| Vec::new(), Iterator::collect); if sources.is_empty() { - return Err(ShellError::FileNotFound(spanned_source.span)); + return Err(ShellError::FileNotFound { + span: spanned_source.span, + }); } // We have two possibilities. diff --git a/crates/nu-command/src/filesystem/open.rs b/crates/nu-command/src/filesystem/open.rs index 76964c5cde..b70ca9dca8 100644 --- a/crates/nu-command/src/filesystem/open.rs +++ b/crates/nu-command/src/filesystem/open.rs @@ -105,7 +105,7 @@ impl Command for Open { for path in nu_engine::glob_from(&path, &cwd, call_span, None) .map_err(|err| match err { - ShellError::DirectoryNotFound(span, _) => ShellError::FileNotFound(span), + ShellError::DirectoryNotFound(span, _) => ShellError::FileNotFound { span }, _ => err, })? .1 diff --git a/crates/nu-command/src/filesystem/ucp.rs b/crates/nu-command/src/filesystem/ucp.rs index a9df32d995..82ab62dfa2 100644 --- a/crates/nu-command/src/filesystem/ucp.rs +++ b/crates/nu-command/src/filesystem/ucp.rs @@ -173,7 +173,7 @@ impl Command for UCp { for p in paths { let exp_files = arg_glob(&p, &cwd)?.collect::>(); if exp_files.is_empty() { - return Err(ShellError::FileNotFound(p.span)); + return Err(ShellError::FileNotFound { span: p.span }); }; let mut app_vals: Vec = Vec::new(); for v in exp_files { diff --git a/crates/nu-command/src/system/nu_check.rs b/crates/nu-command/src/system/nu_check.rs index bbd055d23e..bcd8e34dc9 100644 --- a/crates/nu-command/src/system/nu_check.rs +++ b/crates/nu-command/src/system/nu_check.rs @@ -117,7 +117,9 @@ impl Command for NuCheck { if let Some(path) = path { path } else { - return Err(ShellError::FileNotFound(path_str.span)); + return Err(ShellError::FileNotFound { + span: path_str.span, + }); } } Err(error) => return Err(error), diff --git a/crates/nu-protocol/src/shell_error.rs b/crates/nu-protocol/src/shell_error.rs index f23762a61d..92dce38e61 100644 --- a/crates/nu-protocol/src/shell_error.rs +++ b/crates/nu-protocol/src/shell_error.rs @@ -713,7 +713,10 @@ pub enum ShellError { /// Does the file in the error message exist? Is it readable and accessible? Is the casing right? #[error("File not found")] #[diagnostic(code(nu::shell::file_not_found))] - FileNotFound(#[label("file not found")] Span), + FileNotFound { + #[label("file not found")] + span: Span, + }, /// Failed to find a file during a nushell operation. ///