From 949e270e39ad209c90a99033c94f3a07d9357d13 Mon Sep 17 00:00:00 2001 From: trivernis Date: Mon, 22 May 2023 18:57:48 +0200 Subject: [PATCH] Upgrade nu version --- Cargo.toml | 12 +++---- src/context/bindings.rs | 12 +------ src/context/mod.rs | 18 +++++----- src/error.rs | 80 ++++------------------------------------- src/utils.rs | 8 ++--- tests/test_eval.rs | 7 ++++ 6 files changed, 33 insertions(+), 104 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 17cc99c..4d7973b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,12 +10,12 @@ authors = ["trivernis "] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -miette = "5.7.0" -nu-cmd-lang = "0.78.0" -nu-command = "0.78.0" -nu-engine = "0.78.0" -nu-parser = "0.78.0" -nu-protocol = "0.78.0" +miette = "5.9.0" +nu-cmd-lang = "0.80.0" +nu-command = "0.80.0" +nu-engine = "0.80.0" +nu-parser = "0.80.0" +nu-protocol = "0.80.0" paste = "1.0.12" rusty-value = { version = "0.6.0", features = ["derive"] } thiserror = "1.0.40" diff --git a/src/context/bindings.rs b/src/context/bindings.rs index 4557754..9edb398 100644 --- a/src/context/bindings.rs +++ b/src/context/bindings.rs @@ -19,7 +19,6 @@ pub fn bind_core_commands(engine_state: &mut EngineState) -> CrateResult<()> { Alias, Break, Collect, - Commandline, Const, Continue, Def, @@ -157,7 +156,7 @@ pub fn bind_filter_commands(engine_state: &mut EngineState) -> CrateResult<()> { } pub fn bind_misc_commands(engine_state: &mut EngineState) -> CrateResult<()> { - bind_commands!(engine_state, History, Tutor, HistorySession) + bind_commands!(engine_state, Tutor) } pub fn bind_path_commands(engine_state: &mut EngineState) -> CrateResult<()> { @@ -319,12 +318,8 @@ pub fn bind_platform_commands(engine_state: &mut EngineState) -> CrateResult<()> AnsiStrip, Clear, Du, - KeybindingsDefault, Input, - KeybindingsListen, - Keybindings, Kill, - KeybindingsList, Sleep, TermSize, } @@ -347,12 +342,7 @@ pub fn bind_date_commands(engine_state: &mut EngineState) -> CrateResult<()> { pub fn bind_shell_commands(engine_state: &mut EngineState) -> CrateResult<()> { bind_commands! { engine_state, - Enter, Exit, - GotoShell, - NextShell, - PrevShell, - Shells, } } diff --git a/src/context/mod.rs b/src/context/mod.rs index 77f15a9..3a98dbb 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -1,6 +1,8 @@ mod bindings; mod builder; mod command_group_config; +use std::collections::HashMap; + pub use builder::*; pub use command_group_config::CommandGroupConfig; use nu_protocol::{ @@ -58,15 +60,11 @@ impl Context { pub fn get_var>(&self, name: S) -> Option { let name = name.as_ref(); let dollar_name = format!("${name}"); - let var_id = self - .engine_state - .active_overlays(&vec![]) - .iter() - .find_map(|o| { - o.vars - .get(dollar_name.as_bytes()) - .or(o.vars.get(name.as_bytes())) - })?; + let var_id = self.engine_state.active_overlays(&vec![]).find_map(|o| { + o.vars + .get(dollar_name.as_bytes()) + .or(o.vars.get(name.as_bytes())) + })?; self.stack.get_var(*var_id, Span::new(0, 0)).ok() } @@ -99,7 +97,7 @@ impl Context { arguments: args, redirect_stdout: true, redirect_stderr: true, - parser_info: Vec::new(), + parser_info: HashMap::new(), }; let data = nu_engine::eval_call( diff --git a/src/error.rs b/src/error.rs index 177f3b3..eec9b49 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,86 +1,20 @@ use miette::Diagnostic; -use nu_parser::ParseError; -use nu_protocol::ShellError; +use nu_protocol::{ParseError, ShellError}; use thiserror::Error; pub type CrateResult = std::result::Result; -#[derive(Clone, Debug, Error)] +#[derive(Clone, Debug, Error, Diagnostic)] pub enum CrateError { #[error("Shell Error {0}")] + #[diagnostic()] NuShellError(#[from] ShellError), - #[error("Parse Error {0}")] - NuParseError(#[from] ParseError), + #[error("Parse Error {0:?}")] + #[diagnostic()] + NuParseErrors(Vec), #[error("Could not find the function {0}")] + #[diagnostic()] FunctionNotFound(String), } - -impl Diagnostic for CrateError { - fn code<'a>(&'a self) -> Option> { - match self { - CrateError::NuShellError(n) => n.code(), - CrateError::NuParseError(n) => n.code(), - Self::FunctionNotFound(_) => Some(Box::new("embed_nu::fn_not_found")), - } - } - - fn severity(&self) -> Option { - match self { - CrateError::NuShellError(n) => n.severity(), - CrateError::NuParseError(n) => n.severity(), - _ => None, - } - } - - fn help<'a>(&'a self) -> Option> { - match self { - CrateError::NuShellError(n) => n.help(), - CrateError::NuParseError(n) => n.help(), - CrateError::FunctionNotFound(_) => Some(Box::new( - "Make sure the function you want to execute is defined at this point.", - )), - } - } - - fn url<'a>(&'a self) -> Option> { - match self { - CrateError::NuShellError(n) => n.url(), - CrateError::NuParseError(n) => n.url(), - _ => None, - } - } - - fn source_code(&self) -> Option<&dyn miette::SourceCode> { - match self { - CrateError::NuShellError(n) => n.source_code(), - CrateError::NuParseError(n) => n.source_code(), - _ => None, - } - } - - fn labels(&self) -> Option + '_>> { - match self { - CrateError::NuShellError(n) => n.labels(), - CrateError::NuParseError(n) => n.labels(), - _ => None, - } - } - - fn related<'a>(&'a self) -> Option + 'a>> { - match self { - CrateError::NuShellError(n) => n.related(), - CrateError::NuParseError(n) => n.related(), - _ => None, - } - } - - fn diagnostic_source(&self) -> Option<&dyn Diagnostic> { - match self { - CrateError::NuShellError(n) => n.diagnostic_source(), - CrateError::NuParseError(n) => n.diagnostic_source(), - _ => None, - } - } -} diff --git a/src/utils.rs b/src/utils.rs index 8f9e8d8..3565d7e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -19,14 +19,14 @@ impl NewEmpty for Span { pub fn parse_nu_script(engine_state: &mut EngineState, contents: String) -> CrateResult { let mut working_set = StateWorkingSet::new(&engine_state); - let (block, err) = nu_parser::parse(&mut working_set, None, &contents.into_bytes(), false, &[]); + let block = nu_parser::parse(&mut working_set, None, &contents.into_bytes(), false); - if let Some(err) = err { - Err(CrateError::from(err)) - } else { + if working_set.parse_errors.is_empty() { let delta = working_set.render(); engine_state.merge_delta(delta)?; Ok(block) + } else { + Err(CrateError::NuParseErrors(working_set.parse_errors)) } } diff --git a/tests/test_eval.rs b/tests/test_eval.rs index 42d2d9e..fda9c56 100644 --- a/tests/test_eval.rs +++ b/tests/test_eval.rs @@ -15,6 +15,13 @@ fn it_evals_strings() { ctx.print_pipeline(pipeline).unwrap() } +#[test] +fn it_reports_parse_errors() { + let mut ctx = get_context(); + let eval_result = ctx.eval_raw(r#"let a = 1 | 2 | 3"#, PipelineData::empty()); + assert!(eval_result.is_err()); +} + #[test] fn it_returns_variables() { let mut ctx = get_context();