From e36f69bf3c873c8358d16d7b69f38b9298b5b98d Mon Sep 17 00:00:00 2001 From: Eric Hodel Date: Tue, 21 Nov 2023 15:30:21 -0800 Subject: [PATCH] Convert FileNotFoundCustom to named fields (#11123) # 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/eval_file.rs | 24 +++++++++---------- .../src/dataframe/eager/open.rs | 16 ++++++------- .../nu-command/src/env/config/config_reset.rs | 24 +++++++++---------- crates/nu-command/src/filesystem/cp.rs | 10 ++++++-- crates/nu-protocol/src/shell_error.rs | 6 ++++- src/ide.rs | 8 +++---- 6 files changed, 49 insertions(+), 39 deletions(-) diff --git a/crates/nu-cli/src/eval_file.rs b/crates/nu-cli/src/eval_file.rs index 94c8080a3a..88f64da6e0 100644 --- a/crates/nu-cli/src/eval_file.rs +++ b/crates/nu-cli/src/eval_file.rs @@ -35,10 +35,10 @@ pub fn evaluate_file( let working_set = StateWorkingSet::new(engine_state); report_error( &working_set, - &ShellError::FileNotFoundCustom( - format!("Could not access file '{}': {:?}", path, e.to_string()), - Span::unknown(), - ), + &ShellError::FileNotFoundCustom { + msg: format!("Could not access file '{}': {:?}", path, e.to_string()), + span: Span::unknown(), + }, ); std::process::exit(1); }); @@ -64,14 +64,14 @@ pub fn evaluate_file( let working_set = StateWorkingSet::new(engine_state); report_error( &working_set, - &ShellError::FileNotFoundCustom( - format!( + &ShellError::FileNotFoundCustom { + msg: format!( "Could not read file '{}': {:?}", file_path_str, e.to_string() ), - Span::unknown(), - ), + span: Span::unknown(), + }, ); std::process::exit(1); }); @@ -82,10 +82,10 @@ pub fn evaluate_file( let working_set = StateWorkingSet::new(engine_state); report_error( &working_set, - &ShellError::FileNotFoundCustom( - format!("The file path '{file_path_str}' does not have a parent"), - Span::unknown(), - ), + &ShellError::FileNotFoundCustom { + msg: format!("The file path '{file_path_str}' does not have a parent"), + span: Span::unknown(), + }, ); std::process::exit(1); }); diff --git a/crates/nu-cmd-dataframe/src/dataframe/eager/open.rs b/crates/nu-cmd-dataframe/src/dataframe/eager/open.rs index 4b7f3edfcf..4ea71e1537 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/eager/open.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/eager/open.rs @@ -121,15 +121,15 @@ fn command( "json" => from_json(engine_state, stack, call), "jsonl" => from_jsonl(engine_state, stack, call), "avro" => from_avro(engine_state, stack, call), - _ => Err(ShellError::FileNotFoundCustom( - format!("{msg}. Supported values: csv, tsv, parquet, ipc, arrow, json"), - blamed, - )), + _ => Err(ShellError::FileNotFoundCustom { + msg: format!("{msg}. Supported values: csv, tsv, parquet, ipc, arrow, json"), + span: blamed, + }), }, - None => Err(ShellError::FileNotFoundCustom( - "File without extension".into(), - file.span, - )), + None => Err(ShellError::FileNotFoundCustom { + msg: "File without extension".into(), + span: file.span, + }), } .map(|value| PipelineData::Value(value, None)) } diff --git a/crates/nu-command/src/env/config/config_reset.rs b/crates/nu-command/src/env/config/config_reset.rs index 76a0ea8e51..1e43e52fc3 100644 --- a/crates/nu-command/src/env/config/config_reset.rs +++ b/crates/nu-command/src/env/config/config_reset.rs @@ -72,18 +72,18 @@ impl Command for ConfigReset { Local::now().format("%F-%H-%M-%S"), )); if std::fs::rename(nu_config.clone(), backup_path).is_err() { - return Err(ShellError::FileNotFoundCustom( - "config.nu could not be backed up".into(), + return Err(ShellError::FileNotFoundCustom { + msg: "config.nu could not be backed up".into(), span, - )); + }); } } if let Ok(mut file) = std::fs::File::create(nu_config) { if writeln!(&mut file, "{config_file}").is_err() { - return Err(ShellError::FileNotFoundCustom( - "config.nu could not be written to".into(), + return Err(ShellError::FileNotFoundCustom { + msg: "config.nu could not be written to".into(), span, - )); + }); } } } @@ -95,18 +95,18 @@ impl Command for ConfigReset { let mut backup_path = config_path.clone(); backup_path.push(format!("oldenv-{}.nu", Local::now().format("%F-%H-%M-%S"),)); if std::fs::rename(env_config.clone(), backup_path).is_err() { - return Err(ShellError::FileNotFoundCustom( - "env.nu could not be backed up".into(), + return Err(ShellError::FileNotFoundCustom { + msg: "env.nu could not be backed up".into(), span, - )); + }); } } if let Ok(mut file) = std::fs::File::create(env_config) { if writeln!(&mut file, "{config_file}").is_err() { - return Err(ShellError::FileNotFoundCustom( - "env.nu could not be written to".into(), + return Err(ShellError::FileNotFoundCustom { + msg: "env.nu could not be written to".into(), span, - )); + }); } } } diff --git a/crates/nu-command/src/filesystem/cp.rs b/crates/nu-command/src/filesystem/cp.rs index 2012bc1111..c92c500f71 100644 --- a/crates/nu-command/src/filesystem/cp.rs +++ b/crates/nu-command/src/filesystem/cp.rs @@ -563,9 +563,15 @@ fn convert_io_error(error: std::io::Error, src: PathBuf, dst: PathBuf, span: Spa let shell_error = match error.kind() { ErrorKind::NotFound => { if std::path::Path::new(&dst).exists() { - ShellError::FileNotFoundCustom(message_src, span) + ShellError::FileNotFoundCustom { + msg: message_src, + span, + } } else { - ShellError::FileNotFoundCustom(message_dst, span) + ShellError::FileNotFoundCustom { + msg: message_dst, + span, + } } } ErrorKind::PermissionDenied => match std::fs::metadata(&dst) { diff --git a/crates/nu-protocol/src/shell_error.rs b/crates/nu-protocol/src/shell_error.rs index 92dce38e61..9832ae64a7 100644 --- a/crates/nu-protocol/src/shell_error.rs +++ b/crates/nu-protocol/src/shell_error.rs @@ -725,7 +725,11 @@ 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))] - FileNotFoundCustom(String, #[label("{0}")] Span), + FileNotFoundCustom { + msg: String, + #[label("{msg}")] + span: Span, + }, /// A plugin failed to load. /// diff --git a/src/ide.rs b/src/ide.rs index fb445cb961..961b3a7746 100644 --- a/src/ide.rs +++ b/src/ide.rs @@ -59,10 +59,10 @@ fn read_in_file<'a>( let working_set = StateWorkingSet::new(engine_state); report_error( &working_set, - &ShellError::FileNotFoundCustom( - format!("Could not read file '{}': {:?}", file_path, e.to_string()), - Span::unknown(), - ), + &ShellError::FileNotFoundCustom { + msg: format!("Could not read file '{}': {:?}", file_path, e.to_string()), + span: Span::unknown(), + }, ); std::process::exit(1); });