diff --git a/crates/nu-command/src/commands/append.rs b/crates/nu-command/src/commands/append.rs index d573d557f..618600c9f 100644 --- a/crates/nu-command/src/commands/append.rs +++ b/crates/nu-command/src/commands/append.rs @@ -3,11 +3,6 @@ use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value}; -#[derive(Deserialize)] -struct Arguments { - value: Value, -} - pub struct Command; impl WholeStreamCommand for Command { @@ -28,11 +23,12 @@ impl WholeStreamCommand for Command { } fn run(&self, args: CommandArgs) -> Result { - let (Arguments { mut value }, mut input) = args.process()?; + let mut args = args.evaluate_once()?; + let mut value: Value = args.req(0)?; let mut prepend = vec![]; - if let Some(first) = input.next() { + if let Some(first) = args.input.next() { value.tag = first.tag(); prepend.push(first); } @@ -50,7 +46,7 @@ impl WholeStreamCommand for Command { Ok(prepend .into_iter() - .chain(input.into_iter().chain(vec![value])) + .chain(args.input.into_iter().chain(vec![value])) .to_output_stream()) } diff --git a/crates/nu-command/src/commands/benchmark.rs b/crates/nu-command/src/commands/benchmark.rs index 493cd239d..94005cc27 100644 --- a/crates/nu-command/src/commands/benchmark.rs +++ b/crates/nu-command/src/commands/benchmark.rs @@ -15,12 +15,11 @@ use rand::{ distributions::Alphanumeric, prelude::{thread_rng, Rng}, }; -use std::convert::TryInto; -use std::time::{Duration, Instant}; +use std::time::Instant; pub struct Benchmark; -#[derive(Deserialize, Debug)] +#[derive(Debug)] struct BenchmarkArgs { block: CapturedBlock, passthrough: Option, @@ -113,7 +112,7 @@ fn benchmark(args: CommandArgs) -> Result { { let mut indexmap = IndexMap::with_capacity(1); - let real_time = into_big_int(end_time - start_time); + let real_time = (end_time - start_time).as_nanos() as i64; indexmap.insert("real time".to_string(), real_time); benchmark_output(indexmap, output, cmd_args.passthrough, &tag, &mut context) } @@ -143,7 +142,7 @@ fn benchmark(args: CommandArgs) -> Result { } fn benchmark_output( - indexmap: IndexMap, + indexmap: IndexMap, block_output: Output, passthrough: Option, tag: T, @@ -206,13 +205,6 @@ fn add_implicit_autoview(mut block: Arc) -> Arc { block } -fn into_big_int>(time: T) -> BigInt { - time.try_into() - .unwrap_or_else(|_| Duration::new(0, 0)) - .as_nanos() - .into() -} - fn generate_random_env_value() -> String { let mut thread_rng = thread_rng(); let len = thread_rng.gen_range(1, 16 * 1024); diff --git a/crates/nu-command/src/commands/cd.rs b/crates/nu-command/src/commands/cd.rs index 3c7579ec3..d25dafca5 100644 --- a/crates/nu-command/src/commands/cd.rs +++ b/crates/nu-command/src/commands/cd.rs @@ -27,7 +27,9 @@ impl WholeStreamCommand for Cd { fn run_with_actions(&self, args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); let shell_manager = args.shell_manager(); - let (args, _): (CdArgs, _) = args.process()?; + let args = args.evaluate_once()?; + let args = CdArgs { path: args.opt(0)? }; + shell_manager.cd(args, name) } diff --git a/crates/nu-command/src/commands/cp.rs b/crates/nu-command/src/commands/cp.rs index e67d8eeeb..ba7f4e090 100644 --- a/crates/nu-command/src/commands/cp.rs +++ b/crates/nu-command/src/commands/cp.rs @@ -1,5 +1,5 @@ use crate::prelude::*; -use nu_engine::WholeStreamCommand; +use nu_engine::{shell::CopyArgs, WholeStreamCommand}; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape}; @@ -28,7 +28,13 @@ impl WholeStreamCommand for Cpy { fn run_with_actions(&self, args: CommandArgs) -> Result { let shell_manager = args.shell_manager(); let name = args.call_info.name_tag.clone(); - let (args, _) = args.process()?; + let args = args.evaluate_once()?; + + let args = CopyArgs { + src: args.req(0)?, + dst: args.req(1)?, + recursive: args.has_flag("recursive"), + }; shell_manager.cp(args, name) } diff --git a/crates/nu-command/src/commands/debug.rs b/crates/nu-command/src/commands/debug.rs index b1adf564d..3978139e4 100644 --- a/crates/nu-command/src/commands/debug.rs +++ b/crates/nu-command/src/commands/debug.rs @@ -5,11 +5,6 @@ use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; pub struct Debug; -#[derive(Deserialize)] -pub struct DebugArgs { - raw: bool, -} - impl WholeStreamCommand for Debug { fn name(&self) -> &str { "debug" @@ -29,7 +24,10 @@ impl WholeStreamCommand for Debug { } fn debug_value(args: CommandArgs) -> Result { - let (DebugArgs { raw }, input) = args.process()?; + let args = args.evaluate_once()?; + let raw = args.has_flag("raw"); + let input = args.input; + Ok(input .map(move |v| { if raw { diff --git a/crates/nu-command/src/commands/default.rs b/crates/nu-command/src/commands/default.rs index 5ed7802a7..f96993447 100644 --- a/crates/nu-command/src/commands/default.rs +++ b/crates/nu-command/src/commands/default.rs @@ -5,12 +5,6 @@ use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; use nu_value_ext::ValueExt; -#[derive(Deserialize)] -struct DefaultArgs { - column: Tagged, - value: Value, -} - pub struct Default; impl WholeStreamCommand for Default { @@ -46,7 +40,11 @@ impl WholeStreamCommand for Default { } fn default(args: CommandArgs) -> Result { - let (DefaultArgs { column, value }, input) = args.process()?; + let args = args.evaluate_once()?; + let column: Tagged = args.req(0)?; + let value: Value = args.req(1)?; + + let input = args.input; Ok(input .map(move |item| { diff --git a/crates/nu-command/src/commands/du.rs b/crates/nu-command/src/commands/du.rs index f7b12326b..d4bb1a27a 100644 --- a/crates/nu-command/src/commands/du.rs +++ b/crates/nu-command/src/commands/du.rs @@ -88,7 +88,17 @@ fn du(args: CommandArgs) -> Result { let ctrl_c = args.ctrl_c(); let ctrl_c_copy = ctrl_c.clone(); - let (args, _): (DuArgs, _) = args.process()?; + let args = args.evaluate_once()?; + + let args = DuArgs { + path: args.opt(0)?, + all: args.has_flag("all"), + deref: args.has_flag("deref"), + exclude: args.get_flag("exclude")?, + max_depth: args.get_flag("max-depth")?, + min_size: args.get_flag("min_size")?, + }; + let exclude = args.exclude.map_or(Ok(None), move |x| { Pattern::new(&x.item) .map(Option::Some) diff --git a/crates/nu-command/src/commands/empty.rs b/crates/nu-command/src/commands/empty.rs index 72e6d4704..415135793 100644 --- a/crates/nu-command/src/commands/empty.rs +++ b/crates/nu-command/src/commands/empty.rs @@ -10,11 +10,6 @@ use nu_protocol::{ use crate::utils::arguments::arguments; use nu_value_ext::{as_string, ValueExt}; -#[derive(Deserialize)] -pub struct Arguments { - rest: Vec, -} - pub struct Command; impl WholeStreamCommand for Command { @@ -83,9 +78,11 @@ fn is_empty(args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); let name_tag = Arc::new(args.call_info.name_tag.clone()); let context = Arc::new(EvaluationContext::from_args(&args)); - let (Arguments { mut rest }, input) = args.process()?; + let args = args.evaluate_once()?; + let mut rest = args.rest(0)?; let (columns, default_block): (Vec, Option>) = arguments(&mut rest)?; + let input = args.input; let default_block = Arc::new(default_block); if input.is_empty() { diff --git a/crates/nu-command/src/commands/every.rs b/crates/nu-command/src/commands/every.rs index 1e86c6067..9728c40c6 100644 --- a/crates/nu-command/src/commands/every.rs +++ b/crates/nu-command/src/commands/every.rs @@ -2,16 +2,9 @@ use crate::prelude::*; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue}; -use nu_source::Tagged; pub struct Every; -#[derive(Deserialize)] -pub struct EveryArgs { - stride: Tagged, - skip: Tagged, -} - impl WholeStreamCommand for Every { fn name(&self) -> &str { "every" @@ -63,10 +56,11 @@ impl WholeStreamCommand for Every { } fn every(args: CommandArgs) -> Result { - let (EveryArgs { stride, skip }, input) = args.process()?; + let args = args.evaluate_once()?; - let stride = stride.item; - let skip = skip.item; + let stride: u64 = args.req(0)?; + let skip: bool = args.has_flag("skip"); + let input = args.input; Ok(input .enumerate() diff --git a/crates/nu-command/src/commands/exec.rs b/crates/nu-command/src/commands/exec.rs index 110dd8ef9..20ed4f490 100644 --- a/crates/nu-command/src/commands/exec.rs +++ b/crates/nu-command/src/commands/exec.rs @@ -57,7 +57,12 @@ fn exec(args: CommandArgs) -> Result { use std::process::Command; let name = args.call_info.name_tag.clone(); - let (args, _): (ExecArgs, _) = args.process()?; + let args = args.evaluate_once()?; + + let args = ExecArgs { + command: args.req(0)?, + rest: args.rest(1)?, + }; let mut command = Command::new(args.command.item); for tagged_arg in args.rest { diff --git a/crates/nu-command/src/commands/first.rs b/crates/nu-command/src/commands/first.rs index 98fde6754..a93814faf 100644 --- a/crates/nu-command/src/commands/first.rs +++ b/crates/nu-command/src/commands/first.rs @@ -6,11 +6,6 @@ use nu_source::Tagged; pub struct First; -#[derive(Deserialize)] -pub struct FirstArgs { - rows: Option>, -} - impl WholeStreamCommand for First { fn name(&self) -> &str { "first" @@ -52,7 +47,10 @@ impl WholeStreamCommand for First { } fn first(args: CommandArgs) -> Result { - let (FirstArgs { rows }, input) = args.process()?; + let args = args.evaluate_once()?; + let rows: Option> = args.opt(0)?; + let input = args.input; + let rows_desired = if let Some(quantity) = rows { *quantity } else { diff --git a/crates/nu-command/src/commands/flatten.rs b/crates/nu-command/src/commands/flatten.rs index a8741812a..034cc1b0a 100644 --- a/crates/nu-command/src/commands/flatten.rs +++ b/crates/nu-command/src/commands/flatten.rs @@ -8,11 +8,6 @@ use nu_source::Tagged; pub struct Command; -#[derive(Deserialize)] -pub struct Arguments { - rest: Vec>, -} - impl WholeStreamCommand for Command { fn name(&self) -> &str { "flatten" @@ -53,7 +48,9 @@ impl WholeStreamCommand for Command { fn flatten(args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); - let (Arguments { rest: columns }, input) = args.process()?; + let args = args.evaluate_once()?; + let columns: Vec> = args.rest(0)?; + let input = args.input; Ok(input .map(move |item| flat_value(&columns, &item, &tag).into_iter()) diff --git a/crates/nu-command/src/commands/get.rs b/crates/nu-command/src/commands/get.rs index 58ad7c975..60573aec8 100644 --- a/crates/nu-command/src/commands/get.rs +++ b/crates/nu-command/src/commands/get.rs @@ -13,11 +13,6 @@ use nu_value_ext::get_data_by_column_path; pub struct Command; -#[derive(Deserialize)] -pub struct Arguments { - rest: Vec, -} - impl WholeStreamCommand for Command { fn name(&self) -> &str { "get" @@ -55,7 +50,10 @@ impl WholeStreamCommand for Command { } pub fn get(args: CommandArgs) -> Result { - let (Arguments { mut rest }, mut input) = args.process()?; + let args = args.evaluate_once()?; + let mut rest: Vec = args.rest(0)?; + let mut input = args.input; + let (column_paths, _) = arguments(&mut rest)?; if column_paths.is_empty() { diff --git a/crates/nu-command/src/commands/group_by_date.rs b/crates/nu-command/src/commands/group_by_date.rs index 14d9fb403..6f347837e 100644 --- a/crates/nu-command/src/commands/group_by_date.rs +++ b/crates/nu-command/src/commands/group_by_date.rs @@ -7,12 +7,6 @@ use nu_source::Tagged; pub struct GroupByDate; -#[derive(Deserialize)] -pub struct GroupByDateArgs { - column_name: Option>, - format: Option>, -} - impl WholeStreamCommand for GroupByDate { fn name(&self) -> &str { "group-by date" @@ -60,14 +54,11 @@ enum GroupByColumn { pub fn group_by_date(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); - let ( - GroupByDateArgs { - column_name, - format, - }, - input, - ) = args.process()?; - let values: Vec = input.collect(); + let args = args.evaluate_once()?; + let column_name: Option> = args.opt(0)?; + let format: Option> = args.get_flag("format")?; + + let values: Vec = args.input.collect(); if values.is_empty() { Err(ShellError::labeled_error( diff --git a/crates/nu-command/src/commands/help.rs b/crates/nu-command/src/commands/help.rs index eea5dea52..5377c1a6a 100644 --- a/crates/nu-command/src/commands/help.rs +++ b/crates/nu-command/src/commands/help.rs @@ -13,11 +13,6 @@ use nu_value_ext::ValueExt; pub struct Help; -#[derive(Deserialize)] -pub struct HelpArgs { - rest: Vec>, -} - impl WholeStreamCommand for Help { fn name(&self) -> &str { "help" @@ -39,7 +34,9 @@ impl WholeStreamCommand for Help { fn help(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); let scope = args.scope().clone(); - let (HelpArgs { rest }, ..) = args.process()?; + let args = args.evaluate_once()?; + + let rest: Vec> = args.rest(0)?; if !rest.is_empty() { if rest[0].item == "commands" { diff --git a/crates/nu-command/src/commands/history.rs b/crates/nu-command/src/commands/history.rs index 4e9f22fd2..981c45835 100644 --- a/crates/nu-command/src/commands/history.rs +++ b/crates/nu-command/src/commands/history.rs @@ -5,11 +5,6 @@ use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; use std::fs::File; use std::io::{BufRead, BufReader}; -#[derive(Deserialize)] -struct Arguments { - clear: Option, -} - pub struct History; impl WholeStreamCommand for History { @@ -33,7 +28,9 @@ impl WholeStreamCommand for History { fn history(args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); let ctx = EvaluationContext::from_args(&args); - let (Arguments { clear }, _) = args.process()?; + let args = args.evaluate_once()?; + + let clear = args.has_flag("clear"); let path = if let Some(global_cfg) = &ctx.configs.lock().global_config { nu_data::config::path::history_path_or_default(global_cfg) @@ -41,31 +38,26 @@ fn history(args: CommandArgs) -> Result { nu_data::config::path::default_history_path() }; - match clear { - Some(_) => { - // This is a NOOP, the logic to clear is handled in cli.rs - Ok(ActionStream::empty()) - } - None => { - if let Ok(file) = File::open(path) { - let reader = BufReader::new(file); - // Skips the first line, which is a Rustyline internal - let output = reader.lines().skip(1).filter_map(move |line| match line { - Ok(line) => Some(ReturnSuccess::value( - UntaggedValue::string(line).into_value(tag.clone()), - )), - Err(_) => None, - }); + if clear { + // This is a NOOP, the logic to clear is handled in cli.rs + Ok(ActionStream::empty()) + } else if let Ok(file) = File::open(path) { + let reader = BufReader::new(file); + // Skips the first line, which is a Rustyline internal + let output = reader.lines().skip(1).filter_map(move |line| match line { + Ok(line) => Some(ReturnSuccess::value( + UntaggedValue::string(line).into_value(tag.clone()), + )), + Err(_) => None, + }); - Ok(output.to_action_stream()) - } else { - Err(ShellError::labeled_error( - "Could not open history", - "history file could not be opened", - tag, - )) - } - } + Ok(output.to_action_stream()) + } else { + Err(ShellError::labeled_error( + "Could not open history", + "history file could not be opened", + tag, + )) } } diff --git a/crates/nu-command/src/commands/insert.rs b/crates/nu-command/src/commands/insert.rs index 15fbc928a..b933d6c96 100644 --- a/crates/nu-command/src/commands/insert.rs +++ b/crates/nu-command/src/commands/insert.rs @@ -10,12 +10,6 @@ use nu_value_ext::ValueExt; pub struct Command; -#[derive(Deserialize)] -pub struct Arguments { - column: ColumnPath, - value: Value, -} - impl WholeStreamCommand for Command { fn name(&self) -> &str { "insert" @@ -158,9 +152,13 @@ fn process_row( }) } -fn insert(raw_args: CommandArgs) -> Result { - let context = Arc::new(EvaluationContext::from_args(&raw_args)); - let (Arguments { column, value }, input) = raw_args.process()?; +fn insert(args: CommandArgs) -> Result { + let context = Arc::new(EvaluationContext::from_args(&args)); + let args = args.evaluate_once()?; + let column: ColumnPath = args.req(0)?; + let value: Value = args.req(1)?; + let input = args.input; + let value = Arc::new(value); let column = Arc::new(column); diff --git a/crates/nu-command/src/commands/kill.rs b/crates/nu-command/src/commands/kill.rs index 779c5060d..c1bc955d4 100644 --- a/crates/nu-command/src/commands/kill.rs +++ b/crates/nu-command/src/commands/kill.rs @@ -7,15 +7,6 @@ use std::process::{Command, Stdio}; pub struct Kill; -#[derive(Deserialize)] -pub struct KillArgs { - pub pid: Tagged, - pub rest: Vec>, - pub force: Tagged, - pub quiet: Tagged, - pub signal: Option>, -} - impl WholeStreamCommand for Kill { fn name(&self) -> &str { "kill" @@ -74,20 +65,18 @@ impl WholeStreamCommand for Kill { } fn kill(args: CommandArgs) -> Result { - let ( - KillArgs { - pid, - rest, - force, - quiet, - signal, - }, - .., - ) = args.process()?; + let args = args.evaluate_once()?; + + let pid: Tagged = args.req(0)?; + let rest: Vec> = args.rest(1)?; + let force: Option> = args.get_flag("force")?; + let quiet: bool = args.has_flag("quiet"); + let signal: Option> = args.get_flag("signal")?; + let mut cmd = if cfg!(windows) { let mut cmd = Command::new("taskkill"); - if *force { + if matches!(force, Some(Tagged { item: true, .. })) { cmd.arg("/F"); } @@ -105,14 +94,14 @@ fn kill(args: CommandArgs) -> Result { } else { let mut cmd = Command::new("kill"); - if *force { + if matches!(force, Some(Tagged { item: true, .. })) { if let Some(signal_value) = signal { return Err(ShellError::labeled_error_with_secondary( "mixing force and signal options is not supported", "signal option", signal_value.tag(), "force option", - force.tag(), + force.expect("internal error: expected value").tag(), )); } cmd.arg("-9"); @@ -128,7 +117,7 @@ fn kill(args: CommandArgs) -> Result { }; // pipe everything to null - if *quiet { + if quiet { cmd.stdin(Stdio::null()) .stdout(Stdio::null()) .stderr(Stdio::null()); diff --git a/crates/nu-command/src/commands/length.rs b/crates/nu-command/src/commands/length.rs index 732d6fff4..7f6ca491d 100644 --- a/crates/nu-command/src/commands/length.rs +++ b/crates/nu-command/src/commands/length.rs @@ -6,11 +6,6 @@ use nu_protocol::{Signature, UntaggedValue, Value}; pub struct Length; -#[derive(Deserialize)] -pub struct LengthArgs { - column: bool, -} - impl WholeStreamCommand for Length { fn name(&self) -> &str { "length" @@ -30,7 +25,9 @@ impl WholeStreamCommand for Length { fn run(&self, args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); - let (LengthArgs { column }, input) = args.process()?; + let args = args.evaluate_once()?; + let column = args.has_flag("column"); + let input = args.input; Ok(CountIterator { column, diff --git a/crates/nu-command/src/commands/let_env.rs b/crates/nu-command/src/commands/let_env.rs index 1133d6ab1..0e4ff670b 100644 --- a/crates/nu-command/src/commands/let_env.rs +++ b/crates/nu-command/src/commands/let_env.rs @@ -51,7 +51,10 @@ pub fn set_env(args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); let ctx = EvaluationContext::from_args(&args); - let (LetEnvArgs { name, rhs, .. }, _) = args.process()?; + let args = args.evaluate_once()?; + + let name: Tagged = args.req(0)?; + let rhs: CapturedBlock = args.req(2)?; let (expr, captured) = { if rhs.block.block.len() != 1 { diff --git a/crates/nu-command/src/commands/ls.rs b/crates/nu-command/src/commands/ls.rs index 76cd6612c..5ec270ade 100644 --- a/crates/nu-command/src/commands/ls.rs +++ b/crates/nu-command/src/commands/ls.rs @@ -1,5 +1,5 @@ use crate::prelude::*; -use nu_engine::WholeStreamCommand; +use nu_engine::{shell::LsArgs, WholeStreamCommand}; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape}; @@ -43,7 +43,16 @@ impl WholeStreamCommand for Ls { let name = args.call_info.name_tag.clone(); let ctrl_c = args.ctrl_c(); let shell_manager = args.shell_manager(); - let (args, _) = args.process()?; + let args = args.evaluate_once()?; + + let args = LsArgs { + path: args.opt(0)?, + all: args.has_flag("all"), + long: args.has_flag("long"), + short_names: args.has_flag("short-names"), + du: args.has_flag("du"), + }; + shell_manager.ls(args, name, ctrl_c) } diff --git a/crates/nu-command/src/commands/math/abs.rs b/crates/nu-command/src/commands/math/abs.rs index 8f1133003..5904d413f 100644 --- a/crates/nu-command/src/commands/math/abs.rs +++ b/crates/nu-command/src/commands/math/abs.rs @@ -28,7 +28,7 @@ impl WholeStreamCommand for SubCommand { UntaggedValue::decimal(val.abs()).into() } UntaggedValue::Primitive(Primitive::Duration(val)) => { - UntaggedValue::duration(val.magnitude().clone()).into() + UntaggedValue::duration(val).into() } other => abs_default(other), }); diff --git a/crates/nu-command/src/commands/merge.rs b/crates/nu-command/src/commands/merge.rs index 167ad1f7d..335b22d92 100644 --- a/crates/nu-command/src/commands/merge.rs +++ b/crates/nu-command/src/commands/merge.rs @@ -11,11 +11,6 @@ use nu_protocol::{ }; pub struct Merge; -#[derive(Deserialize)] -pub struct MergeArgs { - block: CapturedBlock, -} - impl WholeStreamCommand for Merge { fn name(&self) -> &str { "merge" @@ -46,11 +41,13 @@ impl WholeStreamCommand for Merge { } } -fn merge(raw_args: CommandArgs) -> Result { - let context = EvaluationContext::from_args(&raw_args); - let name_tag = raw_args.call_info.name_tag.clone(); - let (merge_args, input): (MergeArgs, _) = raw_args.process()?; - let block = merge_args.block; +fn merge(args: CommandArgs) -> Result { + let context = EvaluationContext::from_args(&args); + let name_tag = args.call_info.name_tag.clone(); + + let args = args.evaluate_once()?; + let block: CapturedBlock = args.req(0)?; + let input = args.input; context.scope.enter_scope(); context.scope.add_vars(&block.captured.entries); diff --git a/crates/nu-command/src/commands/mkdir.rs b/crates/nu-command/src/commands/mkdir.rs index 166ce5c7f..7a1733c23 100644 --- a/crates/nu-command/src/commands/mkdir.rs +++ b/crates/nu-command/src/commands/mkdir.rs @@ -1,5 +1,5 @@ use crate::prelude::*; -use nu_engine::WholeStreamCommand; +use nu_engine::{shell::MkdirArgs, WholeStreamCommand}; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape}; pub struct Mkdir; @@ -38,7 +38,12 @@ impl WholeStreamCommand for Mkdir { fn mkdir(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); let shell_manager = args.shell_manager(); - let (args, _) = args.process()?; + + let args = args.evaluate_once()?; + let args = MkdirArgs { + rest: args.rest(0)?, + show_created_paths: args.has_flag("show-created-paths"), + }; shell_manager.mkdir(args, name) } diff --git a/crates/nu-command/src/commands/move_/mv.rs b/crates/nu-command/src/commands/move_/mv.rs index c2dfadb18..82ba1ad14 100644 --- a/crates/nu-command/src/commands/move_/mv.rs +++ b/crates/nu-command/src/commands/move_/mv.rs @@ -1,5 +1,5 @@ use crate::prelude::*; -use nu_engine::WholeStreamCommand; +use nu_engine::{shell::MvArgs, WholeStreamCommand}; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape}; @@ -56,7 +56,12 @@ impl WholeStreamCommand for Mv { fn mv(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); let shell_manager = args.shell_manager(); - let (args, _) = args.process()?; + + let args = args.evaluate_once()?; + let args = MvArgs { + src: args.req(0)?, + dst: args.req(1)?, + }; shell_manager.mv(args, name) } diff --git a/crates/nu-command/src/commands/nth.rs b/crates/nu-command/src/commands/nth.rs index 9801aa4d8..31f9b9ee1 100644 --- a/crates/nu-command/src/commands/nth.rs +++ b/crates/nu-command/src/commands/nth.rs @@ -4,13 +4,6 @@ use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, Value}; use nu_source::Tagged; -#[derive(Deserialize)] -struct NthArgs { - row_number: Tagged, - rest: Vec>, - skip: bool, -} - pub struct Nth; impl WholeStreamCommand for Nth { @@ -59,14 +52,12 @@ impl WholeStreamCommand for Nth { } fn nth(args: CommandArgs) -> Result { - let ( - NthArgs { - row_number, - rest: and_rows, - skip, - }, - input, - ) = args.process()?; + let args = args.evaluate_once()?; + + let row_number: Tagged = args.req(0)?; + let and_rows: Vec> = args.rest(1)?; + let skip = args.has_flag("skip"); + let input = args.input; let mut rows: Vec<_> = and_rows.into_iter().map(|x| x.item as usize).collect(); rows.push(row_number.item as usize); diff --git a/crates/nu-command/src/commands/nu/plugin.rs b/crates/nu-command/src/commands/nu/plugin.rs index 1acc8fbae..668779317 100644 --- a/crates/nu-command/src/commands/nu/plugin.rs +++ b/crates/nu-command/src/commands/nu/plugin.rs @@ -48,7 +48,10 @@ impl WholeStreamCommand for SubCommand { fn run_with_actions(&self, args: CommandArgs) -> Result { let scope = args.scope().clone(); let shell_manager = args.shell_manager(); - let (Arguments { load_path }, _) = args.process()?; + + let args = args.evaluate_once()?; + + let load_path: Option> = args.get_flag("load")?; if let Some(Tagged { item: load_path, diff --git a/crates/nu-command/src/commands/open.rs b/crates/nu-command/src/commands/open.rs index ff2894273..11e19775a 100644 --- a/crates/nu-command/src/commands/open.rs +++ b/crates/nu-command/src/commands/open.rs @@ -12,13 +12,6 @@ use std::path::{Path, PathBuf}; pub struct Open; -#[derive(Deserialize)] -pub struct OpenArgs { - path: Tagged, - raw: Tagged, - encoding: Option>, -} - impl WholeStreamCommand for Open { fn name(&self) -> &str { "open" @@ -106,14 +99,10 @@ fn open(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); let ctrl_c = args.ctrl_c(); - let ( - OpenArgs { - path, - raw, - encoding, - }, - _, - ) = args.process()?; + let args = args.evaluate_once()?; + let path: Tagged = args.req(0)?; + let raw = args.has_flag("raw"); + let encoding: Option> = args.get_flag("encoding")?; if path.is_dir() { let args = nu_engine::shell::LsArgs { @@ -132,7 +121,7 @@ fn open(args: CommandArgs) -> Result { // Check if the extension has a "from *" command OR "bat" supports syntax highlighting // AND the user doesn't want the raw output // In these cases, we will collect the Stream - let ext = if raw.item { + let ext = if raw { None } else { path.extension() diff --git a/crates/nu-command/src/commands/pivot.rs b/crates/nu-command/src/commands/pivot.rs index 23c493de6..84aa6e0d3 100644 --- a/crates/nu-command/src/commands/pivot.rs +++ b/crates/nu-command/src/commands/pivot.rs @@ -52,8 +52,15 @@ impl WholeStreamCommand for Pivot { pub fn pivot(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); - let (args, input): (PivotArgs, _) = args.process()?; - let input = input.into_vec(); + //let (args, input): (PivotArgs, _) = args.process()?; + let args = args.evaluate_once()?; + let pivot_args = PivotArgs { + header_row: args.has_flag("header-row"), + ignore_titles: args.has_flag("ignore-titles"), + rest: args.rest(0)?, + }; + let input = args.input.into_vec(); + let args = pivot_args; let descs = merge_descriptors(&input); diff --git a/crates/nu-command/src/commands/prepend.rs b/crates/nu-command/src/commands/prepend.rs index d4f8eacb2..7a383034b 100644 --- a/crates/nu-command/src/commands/prepend.rs +++ b/crates/nu-command/src/commands/prepend.rs @@ -3,11 +3,6 @@ use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value}; -#[derive(Deserialize)] -struct PrependArgs { - row: Value, -} - pub struct Prepend; impl WholeStreamCommand for Prepend { @@ -46,7 +41,9 @@ impl WholeStreamCommand for Prepend { } fn prepend(args: CommandArgs) -> Result { - let (PrependArgs { row }, input) = args.process()?; + let args = args.evaluate_once()?; + let row: Value = args.req(0)?; + let input = args.input; let bos = vec![row].into_iter(); diff --git a/crates/nu-command/src/commands/reduce.rs b/crates/nu-command/src/commands/reduce.rs index 5eff6a30c..84386db9a 100644 --- a/crates/nu-command/src/commands/reduce.rs +++ b/crates/nu-command/src/commands/reduce.rs @@ -8,7 +8,6 @@ use nu_parser::ParserScope; use nu_protocol::{ hir::CapturedBlock, hir::ExternalRedirection, Signature, SyntaxShape, UntaggedValue, Value, }; -use nu_source::Tagged; use nu_stream::ActionStream; pub struct Reduce; @@ -17,7 +16,7 @@ pub struct Reduce; pub struct ReduceArgs { block: CapturedBlock, fold: Option, - numbered: Tagged, + numbered: bool, } impl WholeStreamCommand for Reduce { @@ -107,10 +106,17 @@ fn process_row( result } -fn reduce(raw_args: CommandArgs) -> Result { - let span = raw_args.call_info.name_tag.span; - let context = Arc::new(EvaluationContext::from_args(&raw_args)); - let (reduce_args, mut input): (ReduceArgs, _) = raw_args.process()?; +fn reduce(args: CommandArgs) -> Result { + let span = args.call_info.name_tag.span; + let context = Arc::new(EvaluationContext::from_args(&args)); + let args = args.evaluate_once()?; + let reduce_args = ReduceArgs { + block: args.req(0)?, + fold: args.get_flag("fold")?, + numbered: args.has_flag("numbered"), + }; + let mut input = args.input; + let block = Arc::new(reduce_args.block); let (ioffset, start) = if !input.is_empty() { match reduce_args.fold { @@ -129,7 +135,7 @@ fn reduce(raw_args: CommandArgs) -> Result { )); }; - if reduce_args.numbered.item { + if reduce_args.numbered { // process_row returns Result, so we must fold with one let initial = Ok(InputStream::one(each::make_indexed_item( ioffset - 1, diff --git a/crates/nu-command/src/commands/reject.rs b/crates/nu-command/src/commands/reject.rs index 1926aaf05..ea2618ff7 100644 --- a/crates/nu-command/src/commands/reject.rs +++ b/crates/nu-command/src/commands/reject.rs @@ -5,11 +5,6 @@ use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape}; use nu_source::Tagged; -#[derive(Deserialize)] -pub struct RejectArgs { - rest: Vec>, -} - pub struct Reject; impl WholeStreamCommand for Reject { @@ -40,7 +35,9 @@ impl WholeStreamCommand for Reject { fn reject(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); - let (RejectArgs { rest: fields }, input) = args.process()?; + let args = args.evaluate_once()?; + let fields: Vec> = args.rest(0)?; + if fields.is_empty() { return Err(ShellError::labeled_error( "Reject requires fields", @@ -51,7 +48,8 @@ fn reject(args: CommandArgs) -> Result { let fields: Vec<_> = fields.iter().map(|f| f.item.clone()).collect(); - Ok(input + Ok(args + .input .map(move |item| ReturnSuccess::value(reject_fields(&item, &fields, &item.tag))) .to_action_stream()) } diff --git a/crates/nu-command/src/commands/rename.rs b/crates/nu-command/src/commands/rename.rs index 96342ecc9..b29e2e0e0 100644 --- a/crates/nu-command/src/commands/rename.rs +++ b/crates/nu-command/src/commands/rename.rs @@ -7,12 +7,6 @@ use nu_source::Tagged; pub struct Rename; -#[derive(Deserialize)] -pub struct Arguments { - column_name: Tagged, - rest: Vec>, -} - impl WholeStreamCommand for Rename { fn name(&self) -> &str { "rename" @@ -63,7 +57,11 @@ impl WholeStreamCommand for Rename { pub fn rename(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); - let (Arguments { column_name, rest }, input) = args.process()?; + let args = args.evaluate_once()?; + let column_name: Tagged = args.req(0)?; + let rest: Vec> = args.rest(1)?; + let input = args.input; + let mut new_column_names = vec![vec![column_name]]; new_column_names.push(rest); diff --git a/crates/nu-command/src/commands/rm.rs b/crates/nu-command/src/commands/rm.rs index 28e4df9a8..762eaa5e5 100644 --- a/crates/nu-command/src/commands/rm.rs +++ b/crates/nu-command/src/commands/rm.rs @@ -65,9 +65,17 @@ impl WholeStreamCommand for Remove { fn rm(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); let shell_manager = args.shell_manager(); - let (args, _): (RemoveArgs, _) = args.process()?; + let args = args.evaluate_once()?; - if args.trash.item && args.permanent.item { + let args = RemoveArgs { + rest: args.rest(0)?, + recursive: args.has_flag("recursive"), + trash: args.has_flag("trash"), + permanent: args.has_flag("permanent"), + force: args.has_flag("force"), + }; + + if args.trash && args.permanent { return Ok(ActionStream::one(Err(ShellError::labeled_error( "only one of --permanent and --trash can be used", "conflicting flags", diff --git a/crates/nu-command/src/commands/rotate/command.rs b/crates/nu-command/src/commands/rotate/command.rs index 3dcd37305..2d28a732d 100644 --- a/crates/nu-command/src/commands/rotate/command.rs +++ b/crates/nu-command/src/commands/rotate/command.rs @@ -10,11 +10,6 @@ use nu_value_ext::ValueExt; pub struct Command; -#[derive(Deserialize)] -pub struct Arguments { - rest: Vec>, -} - impl WholeStreamCommand for Command { fn name(&self) -> &str { "rotate" @@ -38,9 +33,10 @@ impl WholeStreamCommand for Command { pub fn rotate(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); - let (Arguments { rest }, input) = args.process()?; + let args = args.evaluate_once()?; + let rest: Vec> = args.rest(0)?; + let input = args.input.into_vec(); - let input = input.into_vec(); let total_rows = input.len(); let descs = merge_descriptors(&input); let total_descriptors = descs.len(); diff --git a/crates/nu-command/src/commands/rotate/counter_clockwise.rs b/crates/nu-command/src/commands/rotate/counter_clockwise.rs index f8ffd023e..9d33e08c7 100644 --- a/crates/nu-command/src/commands/rotate/counter_clockwise.rs +++ b/crates/nu-command/src/commands/rotate/counter_clockwise.rs @@ -10,11 +10,6 @@ use nu_value_ext::ValueExt; pub struct SubCommand; -#[derive(Deserialize)] -pub struct Arguments { - rest: Vec>, -} - impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "rotate counter-clockwise" @@ -38,9 +33,10 @@ impl WholeStreamCommand for SubCommand { pub fn rotate(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); - let (Arguments { rest }, input) = args.process()?; + let args = args.evaluate_once()?; + let rest: Vec> = args.rest(0)?; - let input = input.into_vec(); + let input = args.input.into_vec(); let descs = merge_descriptors(&input); let total_rows = input.len(); diff --git a/crates/nu-command/src/commands/seq.rs b/crates/nu-command/src/commands/seq.rs index 55fcba404..51f251f73 100644 --- a/crates/nu-command/src/commands/seq.rs +++ b/crates/nu-command/src/commands/seq.rs @@ -8,14 +8,6 @@ use std::cmp; pub struct Seq; -#[derive(Deserialize)] -pub struct SeqArgs { - rest: Vec>, - separator: Option>, - terminator: Option>, - widths: Tagged, -} - impl WholeStreamCommand for Seq { fn name(&self) -> &str { "seq" @@ -108,15 +100,11 @@ impl WholeStreamCommand for Seq { fn seq(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); - let ( - SeqArgs { - rest: rest_nums, - separator, - terminator, - widths, - }, - _, - ) = args.process()?; + let args = args.evaluate_once()?; + let rest_nums: Vec> = args.rest(0)?; + let separator: Option> = args.get_flag("separator")?; + let terminator: Option> = args.get_flag("terminator")?; + let widths = args.has_flag("widths"); if rest_nums.is_empty() { return Err(ShellError::labeled_error( @@ -174,7 +162,7 @@ fn seq(args: CommandArgs) -> Result { let rest_nums: Vec = rest_nums.iter().map(|n| n.item.to_string()).collect(); - run_seq(sep, Some(term), widths.item, rest_nums) + run_seq(sep, Some(term), widths, rest_nums) } #[cfg(test)] diff --git a/crates/nu-command/src/commands/seq_dates.rs b/crates/nu-command/src/commands/seq_dates.rs index 9cf1f53b2..6165cfcba 100644 --- a/crates/nu-command/src/commands/seq_dates.rs +++ b/crates/nu-command/src/commands/seq_dates.rs @@ -3,24 +3,12 @@ use chrono::naive::NaiveDate; use chrono::{Duration, Local}; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{value::I64Ext, value::StrExt, value::StringExt, value::U64Ext}; +use nu_protocol::{value::I64Ext, value::StrExt, value::StringExt}; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; pub struct SeqDates; -#[derive(Deserialize)] -pub struct SeqDatesArgs { - separator: Option>, - output_format: Option>, - input_format: Option>, - begin_date: Option>, - end_date: Option>, - increment: Option>, - days: Option>, - reverse: Tagged, -} - impl WholeStreamCommand for SeqDates { fn name(&self) -> &str { "seq date" @@ -35,24 +23,24 @@ impl WholeStreamCommand for SeqDates { Some('s'), ) .named( - "output_format", + "output-format", SyntaxShape::String, "prints dates in this format (defaults to %Y-%m-%d)", Some('o'), ) .named( - "input_format", + "input-format", SyntaxShape::String, "give argument dates in this format (defaults to %Y-%m-%d)", Some('i'), ) .named( - "begin_date", + "begin-date", SyntaxShape::String, "beginning date range", Some('b'), ) - .named("end_date", SyntaxShape::String, "ending date", Some('e')) + .named("end-date", SyntaxShape::String, "ending date", Some('e')) .named( "increment", SyntaxShape::Int, @@ -134,19 +122,16 @@ impl WholeStreamCommand for SeqDates { fn seq_dates(args: CommandArgs) -> Result { let _name = args.call_info.name_tag.clone(); - let ( - SeqDatesArgs { - separator, - output_format, - input_format, - begin_date, - end_date, - increment, - days, - reverse, - }, - _, - ) = args.process()?; + let args = args.evaluate_once()?; + + let separator: Option> = args.get_flag("separator")?; + let output_format: Option> = args.get_flag("output-format")?; + let input_format: Option> = args.get_flag("input-format")?; + let begin_date: Option> = args.get_flag("begin-date")?; + let end_date: Option> = args.get_flag("end-date")?; + let increment: Option> = args.get_flag("increment")?; + let days: Option> = args.get_flag("days")?; + let reverse = args.has_flag("reverse"); let sep: String = match separator { Some(s) => { @@ -205,8 +190,8 @@ fn seq_dates(args: CommandArgs) -> Result { }; let mut rev = false; - if *reverse { - rev = *reverse; + if reverse { + rev = reverse; } run_seq_dates(sep, outformat, informat, begin, end, inc, day_count, rev) diff --git a/crates/nu-command/src/commands/skip/command.rs b/crates/nu-command/src/commands/skip/command.rs index 957b1d8e4..a625f426f 100644 --- a/crates/nu-command/src/commands/skip/command.rs +++ b/crates/nu-command/src/commands/skip/command.rs @@ -6,11 +6,6 @@ use nu_source::Tagged; pub struct Command; -#[derive(Deserialize)] -pub struct Arguments { - rows: Option>, -} - impl WholeStreamCommand for Command { fn name(&self) -> &str { "skip" @@ -41,7 +36,10 @@ impl WholeStreamCommand for Command { } fn skip(args: CommandArgs) -> Result { - let (Arguments { rows }, input) = args.process()?; + let args = args.evaluate_once()?; + let rows: Option> = args.opt(0)?; + let input = args.input; + let rows_desired = if let Some(quantity) = rows { *quantity } else { diff --git a/crates/nu-command/src/commands/sleep.rs b/crates/nu-command/src/commands/sleep.rs index 50ac577c0..afa525654 100644 --- a/crates/nu-command/src/commands/sleep.rs +++ b/crates/nu-command/src/commands/sleep.rs @@ -1,7 +1,7 @@ use crate::prelude::*; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{ReturnValue, Signature, SyntaxShape, UntaggedValue}; +use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; use std::{ sync::atomic::Ordering, @@ -13,12 +13,6 @@ const CTRL_C_CHECK_INTERVAL: Duration = Duration::from_millis(100); pub struct Sleep; -#[derive(Deserialize)] -pub struct SleepArgs { - pub duration: Tagged, - pub rest: Vec>, -} - impl WholeStreamCommand for Sleep { fn name(&self) -> &str { "sleep" @@ -34,23 +28,28 @@ impl WholeStreamCommand for Sleep { "Delay for a specified amount of time." } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let ctrl_c = args.ctrl_c(); - let (SleepArgs { duration, rest }, _) = args.process()?; + let args = args.evaluate_once()?; + let duration: Tagged = args.req(0)?; + let rest: Vec> = args.rest(1)?; - let total_dur = Duration::from_nanos(duration.item) - + rest - .iter() - .map(|val| Duration::from_nanos(val.item)) - .sum::(); + let total_dur = Duration::from_nanos(if duration.item > 0 { + duration.item as u64 + } else { + 0 + }) + rest + .iter() + .map(|val| Duration::from_nanos(if val.item > 0 { val.item as u64 } else { 0 })) + .sum::(); //SleepHandler::new(total_dur, ctrl_c); // this is necessary because the following 2 commands gave different results: // `echo | sleep 1sec` - nothing // `sleep 1sec` - table with 0 elements - Ok(SleepIterator::new(total_dur, ctrl_c).to_action_stream()) + Ok(SleepIterator::new(total_dur, ctrl_c).to_output_stream()) // if input.is_empty() { // Ok(OutputStream::empty()) @@ -92,7 +91,7 @@ impl SleepIterator { } impl Iterator for SleepIterator { - type Item = ReturnValue; + type Item = Value; fn next(&mut self) -> Option { let start = Instant::now(); diff --git a/crates/nu-command/src/commands/sort_by.rs b/crates/nu-command/src/commands/sort_by.rs index 2bb8fd7e0..fbbbef120 100644 --- a/crates/nu-command/src/commands/sort_by.rs +++ b/crates/nu-command/src/commands/sort_by.rs @@ -8,13 +8,6 @@ use nu_value_ext::ValueExt; pub struct SortBy; -#[derive(Deserialize)] -pub struct SortByArgs { - rest: Vec>, - insensitive: bool, - reverse: bool, -} - impl WholeStreamCommand for SortBy { fn name(&self) -> &str { "sort-by" @@ -35,7 +28,7 @@ impl WholeStreamCommand for SortBy { "Sort by the given columns, in increasing order." } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { sort_by(args) } @@ -111,18 +104,14 @@ impl WholeStreamCommand for SortBy { } } -fn sort_by(args: CommandArgs) -> Result { +fn sort_by(args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); + let mut args = args.evaluate_once()?; - let ( - SortByArgs { - rest, - insensitive, - reverse, - }, - mut input, - ) = args.process()?; - let mut vec = input.drain_vec(); + let rest = args.rest(0)?; + let insensitive = args.has_flag("insensitive"); + let reverse = args.has_flag("reverse"); + let mut vec = args.input.drain_vec(); sort(&mut vec, &rest, &tag, insensitive)?; @@ -130,7 +119,7 @@ fn sort_by(args: CommandArgs) -> Result { vec.reverse() } - Ok((vec.into_iter()).to_action_stream()) + Ok((vec.into_iter()).to_output_stream()) } pub fn sort( diff --git a/crates/nu-command/src/commands/source.rs b/crates/nu-command/src/commands/source.rs index ab2a8ee52..636490ec8 100644 --- a/crates/nu-command/src/commands/source.rs +++ b/crates/nu-command/src/commands/source.rs @@ -41,7 +41,8 @@ impl WholeStreamCommand for Source { pub fn source(args: CommandArgs) -> Result { let ctx = EvaluationContext::from_args(&args); - let (SourceArgs { filename }, _) = args.process()?; + let args = args.evaluate_once()?; + let filename: Tagged = args.req(0)?; // Note: this is a special case for setting the context from a command // In this case, if we don't set it now, we'll lose the scope that this diff --git a/crates/nu-command/src/commands/split/column.rs b/crates/nu-command/src/commands/split/column.rs index 745a196d7..f1d5bdbca 100644 --- a/crates/nu-command/src/commands/split/column.rs +++ b/crates/nu-command/src/commands/split/column.rs @@ -7,14 +7,6 @@ use nu_protocol::{ }; use nu_source::Tagged; -#[derive(Deserialize)] -struct SplitColumnArgs { - separator: Tagged, - rest: Vec>, - #[serde(rename(deserialize = "collapse-empty"))] - collapse_empty: bool, -} - pub struct SubCommand; impl WholeStreamCommand for SubCommand { @@ -26,7 +18,7 @@ impl WholeStreamCommand for SubCommand { Signature::build("split column") .required( "separator", - SyntaxShape::Any, + SyntaxShape::String, "the character that denotes what separates columns", ) .switch("collapse-empty", "remove empty columns", Some('c')) @@ -44,14 +36,11 @@ impl WholeStreamCommand for SubCommand { fn split_column(args: CommandArgs) -> Result { let name_span = args.call_info.name_tag.span; - let ( - SplitColumnArgs { - separator, - rest, - collapse_empty, - }, - input, - ) = args.process()?; + let args = args.evaluate_once()?; + let separator: Tagged = args.req(0)?; + let rest: Vec> = args.rest(1)?; + let collapse_empty = args.has_flag("collapse-empty"); + let input = args.input; Ok(input .map(move |v| { diff --git a/crates/nu-command/src/commands/split/row.rs b/crates/nu-command/src/commands/split/row.rs index 9054d2862..e5dbb287d 100644 --- a/crates/nu-command/src/commands/split/row.rs +++ b/crates/nu-command/src/commands/split/row.rs @@ -5,11 +5,6 @@ use nu_errors::ShellError; use nu_protocol::{Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue}; use nu_source::Tagged; -#[derive(Deserialize)] -struct SplitRowArgs { - separator: Tagged, -} - pub struct SubCommand; impl WholeStreamCommand for SubCommand { @@ -20,7 +15,7 @@ impl WholeStreamCommand for SubCommand { fn signature(&self) -> Signature { Signature::build("split row").required( "separator", - SyntaxShape::Any, + SyntaxShape::String, "the character that denotes what separates rows", ) } @@ -36,7 +31,11 @@ impl WholeStreamCommand for SubCommand { fn split_row(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); - let (SplitRowArgs { separator }, input) = args.process()?; + let args = args.evaluate_once()?; + + let separator: Tagged = args.req(0)?; + let input = args.input; + Ok(input .flat_map(move |v| { if let Ok(s) = v.as_string() { diff --git a/crates/nu-command/src/commands/split_by.rs b/crates/nu-command/src/commands/split_by.rs index 3ddb5caed..a1e2c64f1 100644 --- a/crates/nu-command/src/commands/split_by.rs +++ b/crates/nu-command/src/commands/split_by.rs @@ -8,11 +8,6 @@ use nu_value_ext::as_string; pub struct SplitBy; -#[derive(Deserialize)] -pub struct SplitByArgs { - column_name: Option>, -} - impl WholeStreamCommand for SplitBy { fn name(&self) -> &str { "split-by" @@ -37,8 +32,10 @@ impl WholeStreamCommand for SplitBy { pub fn split_by(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); - let (SplitByArgs { column_name }, input) = args.process()?; - let values: Vec = input.collect(); + let args = args.evaluate_once()?; + let column_name: Option> = args.opt(0)?; + + let values: Vec = args.input.collect(); if values.len() > 1 || values.is_empty() { return Err(ShellError::labeled_error( diff --git a/crates/nu-command/src/commands/termsize.rs b/crates/nu-command/src/commands/termsize.rs index ebabc8f93..29e5a8623 100644 --- a/crates/nu-command/src/commands/termsize.rs +++ b/crates/nu-command/src/commands/termsize.rs @@ -29,7 +29,10 @@ impl WholeStreamCommand for TermSize { fn run_with_actions(&self, args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); - let (TermSizeArgs { wide, tall }, _) = args.process()?; + let args = args.evaluate_once()?; + + let wide = args.has_flag("wide"); + let tall = args.has_flag("tall"); let size = term_size::dimensions(); match size { diff --git a/crates/nu-command/src/commands/touch.rs b/crates/nu-command/src/commands/touch.rs index d97491abf..d84a4cc9d 100644 --- a/crates/nu-command/src/commands/touch.rs +++ b/crates/nu-command/src/commands/touch.rs @@ -8,12 +8,6 @@ use std::path::PathBuf; pub struct Touch; -#[derive(Deserialize)] -pub struct TouchArgs { - target: Tagged, - rest: Vec>, -} - impl WholeStreamCommand for Touch { fn name(&self) -> &str { "touch" @@ -51,7 +45,9 @@ impl WholeStreamCommand for Touch { } fn touch(args: CommandArgs) -> Result { - let (TouchArgs { target, rest }, _) = args.process()?; + let args = args.evaluate_once()?; + let target: Tagged = args.req(0)?; + let rest: Vec> = args.rest(1)?; for item in vec![target].into_iter().chain(rest.into_iter()) { match OpenOptions::new().write(true).create(true).open(&item) { diff --git a/crates/nu-command/src/commands/update.rs b/crates/nu-command/src/commands/update.rs index f08a1b752..9a82a2892 100644 --- a/crates/nu-command/src/commands/update.rs +++ b/crates/nu-command/src/commands/update.rs @@ -11,12 +11,6 @@ use nu_value_ext::ValueExt; pub struct Command; -#[derive(Deserialize)] -pub struct Arguments { - field: ColumnPath, - replacement: Value, -} - impl WholeStreamCommand for Command { fn name(&self) -> &str { "update" @@ -178,10 +172,15 @@ fn process_row( }) } -fn update(raw_args: CommandArgs) -> Result { - let name_tag = Arc::new(raw_args.call_info.name_tag.clone()); - let context = Arc::new(EvaluationContext::from_args(&raw_args)); - let (Arguments { field, replacement }, input) = raw_args.process()?; +fn update(args: CommandArgs) -> Result { + let name_tag = Arc::new(args.call_info.name_tag.clone()); + let context = Arc::new(EvaluationContext::from_args(&args)); + let args = args.evaluate_once()?; + + let field: ColumnPath = args.req(0)?; + let replacement: Value = args.req(1)?; + let input = args.input; + let replacement = Arc::new(replacement); let field = Arc::new(field); diff --git a/crates/nu-command/src/commands/url_/host.rs b/crates/nu-command/src/commands/url_/host.rs index 97512ccc8..064c61c5d 100644 --- a/crates/nu-command/src/commands/url_/host.rs +++ b/crates/nu-command/src/commands/url_/host.rs @@ -1,10 +1,10 @@ use url::Url; -use super::{operate, DefaultArguments}; +use super::operate; use crate::prelude::*; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{Signature, SyntaxShape, Value}; +use nu_protocol::{ColumnPath, Signature, SyntaxShape, Value}; pub struct UrlHost; @@ -23,7 +23,10 @@ impl WholeStreamCommand for UrlHost { } fn run_with_actions(&self, args: CommandArgs) -> Result { - let (DefaultArguments { rest }, input) = args.process()?; + let args = args.evaluate_once()?; + let rest: Vec = args.rest(0)?; + let input = args.input; + Ok(operate(input, rest, &host)) } diff --git a/crates/nu-command/src/commands/url_/mod.rs b/crates/nu-command/src/commands/url_/mod.rs index de6cc8479..22c27ef9c 100644 --- a/crates/nu-command/src/commands/url_/mod.rs +++ b/crates/nu-command/src/commands/url_/mod.rs @@ -15,11 +15,6 @@ pub use path::UrlPath; pub use query::UrlQuery; pub use scheme::UrlScheme; -#[derive(Deserialize)] -struct DefaultArguments { - rest: Vec, -} - fn handle_value(action: &F, v: &Value) -> Result where F: Fn(&Url) -> &str + Send + 'static, diff --git a/crates/nu-command/src/commands/url_/path.rs b/crates/nu-command/src/commands/url_/path.rs index fc999b4e5..70c02f1ad 100644 --- a/crates/nu-command/src/commands/url_/path.rs +++ b/crates/nu-command/src/commands/url_/path.rs @@ -1,10 +1,10 @@ use url::Url; -use super::{operate, DefaultArguments}; +use super::operate; use crate::prelude::*; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{Signature, SyntaxShape, Value}; +use nu_protocol::{ColumnPath, Signature, SyntaxShape, Value}; pub struct UrlPath; @@ -23,7 +23,10 @@ impl WholeStreamCommand for UrlPath { } fn run_with_actions(&self, args: CommandArgs) -> Result { - let (DefaultArguments { rest }, input) = args.process()?; + let args = args.evaluate_once()?; + let rest: Vec = args.rest(0)?; + let input = args.input; + Ok(operate(input, rest, &Url::path)) } diff --git a/crates/nu-command/src/commands/url_/query.rs b/crates/nu-command/src/commands/url_/query.rs index 7700c2414..1675889e6 100644 --- a/crates/nu-command/src/commands/url_/query.rs +++ b/crates/nu-command/src/commands/url_/query.rs @@ -1,10 +1,10 @@ use url::Url; -use super::{operate, DefaultArguments}; +use super::operate; use crate::prelude::*; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{Signature, SyntaxShape, Value}; +use nu_protocol::{ColumnPath, Signature, SyntaxShape, Value}; pub struct UrlQuery; @@ -23,7 +23,9 @@ impl WholeStreamCommand for UrlQuery { } fn run_with_actions(&self, args: CommandArgs) -> Result { - let (DefaultArguments { rest }, input) = args.process()?; + let args = args.evaluate_once()?; + let rest: Vec = args.rest(0)?; + let input = args.input; Ok(operate(input, rest, &query)) } diff --git a/crates/nu-command/src/commands/url_/scheme.rs b/crates/nu-command/src/commands/url_/scheme.rs index 530517c2a..288901947 100644 --- a/crates/nu-command/src/commands/url_/scheme.rs +++ b/crates/nu-command/src/commands/url_/scheme.rs @@ -1,10 +1,10 @@ use url::Url; -use super::{operate, DefaultArguments}; +use super::operate; use crate::prelude::*; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{Signature, SyntaxShape, Value}; +use nu_protocol::{ColumnPath, Signature, SyntaxShape, Value}; pub struct UrlScheme; @@ -22,8 +22,9 @@ impl WholeStreamCommand for UrlScheme { } fn run_with_actions(&self, args: CommandArgs) -> Result { - let (DefaultArguments { rest }, input) = args.process()?; - Ok(operate(input, rest, &Url::scheme)) + let args = args.evaluate_once()?; + let rest: Vec = args.rest(0)?; + Ok(operate(args.input, rest, &Url::scheme)) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/where_.rs b/crates/nu-command/src/commands/where_.rs index 4da1d12f8..5e1b037ca 100644 --- a/crates/nu-command/src/commands/where_.rs +++ b/crates/nu-command/src/commands/where_.rs @@ -10,11 +10,6 @@ use nu_protocol::{ pub struct Command; -#[derive(Deserialize)] -pub struct Arguments { - block: CapturedBlock, -} - impl WholeStreamCommand for Command { fn name(&self) -> &str { "where" @@ -61,10 +56,13 @@ impl WholeStreamCommand for Command { ] } } -fn where_command(raw_args: CommandArgs) -> Result { - let context = Arc::new(EvaluationContext::from_args(&raw_args)); - let tag = raw_args.call_info.name_tag.clone(); - let (Arguments { block }, input) = raw_args.process()?; +fn where_command(args: CommandArgs) -> Result { + let context = Arc::new(EvaluationContext::from_args(&args)); + let tag = args.call_info.name_tag.clone(); + let args = args.evaluate_once()?; + + let block: CapturedBlock = args.req(0)?; + let condition = { if block.block.block.len() != 1 { return Err(ShellError::labeled_error( @@ -97,7 +95,7 @@ fn where_command(raw_args: CommandArgs) -> Result { Ok(WhereIterator { condition, context, - input, + input: args.input, block, } .to_output_stream()) diff --git a/crates/nu-command/src/commands/with_env.rs b/crates/nu-command/src/commands/with_env.rs index c2a0ebe36..401bdb0a9 100644 --- a/crates/nu-command/src/commands/with_env.rs +++ b/crates/nu-command/src/commands/with_env.rs @@ -67,10 +67,12 @@ impl WholeStreamCommand for WithEnv { } } -fn with_env(raw_args: CommandArgs) -> Result { - let external_redirection = raw_args.call_info.args.external_redirection; - let context = EvaluationContext::from_args(&raw_args); - let (WithEnvArgs { variable, block }, input) = raw_args.process()?; +fn with_env(args: CommandArgs) -> Result { + let external_redirection = args.call_info.args.external_redirection; + let context = EvaluationContext::from_args(&args); + let args = args.evaluate_once()?; + let variable: Value = args.req(0)?; + let block: CapturedBlock = args.req(1)?; let mut env = IndexMap::new(); @@ -108,7 +110,7 @@ fn with_env(raw_args: CommandArgs) -> Result { context.scope.add_env(env); context.scope.add_vars(&block.captured.entries); - let result = run_block(&block.block, &context, input, external_redirection); + let result = run_block(&block.block, &context, args.input, external_redirection); context.scope.exit_scope(); result.map(|x| x.to_action_stream()) diff --git a/crates/nu-command/src/commands/wrap.rs b/crates/nu-command/src/commands/wrap.rs index 57b3f6ff2..5f1264542 100644 --- a/crates/nu-command/src/commands/wrap.rs +++ b/crates/nu-command/src/commands/wrap.rs @@ -9,11 +9,6 @@ const DEFAULT_COLUMN_NAME: &str = "Column"; pub struct Wrap; -#[derive(Deserialize)] -struct WrapArgs { - column: Option>, -} - impl WholeStreamCommand for Wrap { fn name(&self) -> &str { "wrap" @@ -78,11 +73,13 @@ impl WholeStreamCommand for Wrap { } fn wrap(args: CommandArgs) -> Result { - let (WrapArgs { column }, input) = args.process()?; + let args = args.evaluate_once()?; + let column: Option> = args.opt(0)?; + let mut result_table = vec![]; let mut are_all_rows = true; - for value in input { + for value in args.input { match value { Value { value: UntaggedValue::Row(_), diff --git a/crates/nu-command/src/examples/double_echo.rs b/crates/nu-command/src/examples/double_echo.rs index 745d688bd..ca032592c 100644 --- a/crates/nu-command/src/examples/double_echo.rs +++ b/crates/nu-command/src/examples/double_echo.rs @@ -28,10 +28,11 @@ impl WholeStreamCommand for Command { fn run_with_actions(&self, args: CommandArgs) -> Result { let name_tag = args.call_info.name_tag.clone(); - let (Arguments { rest }, input) = args.process()?; + let args = args.evaluate_once()?; + let rest: Vec = args.rest(0)?; let mut base_value = UntaggedValue::string("Yehuda Katz in Ecuador").into_value(name_tag); - let input: Vec = input.collect(); + let input: Vec = args.input.collect(); if let Some(first) = input.get(0) { base_value = first.clone() diff --git a/crates/nu-command/src/examples/stub_generate.rs b/crates/nu-command/src/examples/stub_generate.rs index 9572dc03d..4dbe8abb3 100644 --- a/crates/nu-command/src/examples/stub_generate.rs +++ b/crates/nu-command/src/examples/stub_generate.rs @@ -3,16 +3,8 @@ use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue, Value}; use nu_source::{AnchorLocation, Tag}; use nu_stream::ActionStream; - -use serde::Deserialize; - pub struct Command; -#[derive(Deserialize)] -struct Arguments { - path: Option, -} - impl WholeStreamCommand for Command { fn name(&self) -> &str { "stub open" @@ -29,11 +21,11 @@ impl WholeStreamCommand for Command { fn run_with_actions(&self, args: CommandArgs) -> Result { let name_tag = args.call_info.name_tag.clone(); - let (Arguments { path: mocked_path }, _input) = args.process()?; + let mocked_path = args.call_info.switch_present("path"); let out = UntaggedValue::string("Yehuda Katz in Ecuador"); - if let Some(true) = mocked_path { + if mocked_path { Ok(ActionStream::one(Ok(ReturnSuccess::Value(Value { value: out, tag: Tag { diff --git a/crates/nu-data/src/base.rs b/crates/nu-data/src/base.rs index 0f020ed3f..bad390171 100644 --- a/crates/nu-data/src/base.rs +++ b/crates/nu-data/src/base.rs @@ -77,7 +77,7 @@ pub enum CompareValues { Decimals(BigDecimal, BigDecimal), String(String, String), Date(DateTime, DateTime), - DateDuration(DateTime, BigInt), + DateDuration(DateTime, i64), Booleans(bool, bool), } @@ -92,11 +92,9 @@ impl CompareValues { CompareValues::Date(left, right) => left.cmp(right), CompareValues::DateDuration(left, right) => { // FIXME: Not sure if I could do something better with the Span. - let duration = Primitive::into_chrono_duration( - Primitive::Duration(right.clone()), - Span::unknown(), - ) - .expect("Could not convert nushell Duration into chrono Duration."); + let duration = + Primitive::into_chrono_duration(Primitive::Duration(*right), Span::unknown()) + .expect("Could not convert nushell Duration into chrono Duration."); let right: DateTime = Utc::now() .checked_sub_signed(duration) .expect("Data overflow") @@ -160,7 +158,7 @@ pub fn coerce_compare_primitive( (Nothing, Nothing) => CompareValues::Booleans(true, true), (String(left), String(right)) => CompareValues::String(left.clone(), right.clone()), (Date(left), Date(right)) => CompareValues::Date(*left, *right), - (Date(left), Duration(right)) => CompareValues::DateDuration(*left, right.clone()), + (Date(left), Duration(right)) => CompareValues::DateDuration(*left, *right), (Boolean(left), Boolean(right)) => CompareValues::Booleans(*left, *right), (Boolean(left), Nothing) => CompareValues::Booleans(*left, false), (Nothing, Boolean(right)) => CompareValues::Booleans(false, *right), diff --git a/crates/nu-data/src/base/shape.rs b/crates/nu-data/src/base/shape.rs index d6c105164..a07dd3c45 100644 --- a/crates/nu-data/src/base/shape.rs +++ b/crates/nu-data/src/base/shape.rs @@ -33,7 +33,7 @@ pub enum InlineShape { GlobPattern(String), Boolean(bool), Date(DateTime), - Duration(BigInt), + Duration(i64), FilePath(PathBuf), Binary(usize), @@ -94,7 +94,7 @@ impl InlineShape { Primitive::GlobPattern(pattern) => InlineShape::GlobPattern(pattern.clone()), Primitive::Boolean(boolean) => InlineShape::Boolean(*boolean), Primitive::Date(date) => InlineShape::Date(*date), - Primitive::Duration(duration) => InlineShape::Duration(duration.clone()), + Primitive::Duration(duration) => InlineShape::Duration(*duration), Primitive::FilePath(path) => InlineShape::FilePath(path.clone()), Primitive::Binary(b) => InlineShape::Binary(b.len()), Primitive::BeginningOfStream => InlineShape::BeginningOfStream, @@ -304,10 +304,9 @@ impl PrettyDebug for FormatInlineShape { .to_owned(), ), InlineShape::Date(date) => DbgDocBldr::primitive(nu_protocol::format_date(date)), - InlineShape::Duration(duration) => DbgDocBldr::description(format_primitive( - &Primitive::Duration(duration.clone()), - None, - )), + InlineShape::Duration(duration) => { + DbgDocBldr::description(format_primitive(&Primitive::Duration(*duration), None)) + } InlineShape::FilePath(path) => DbgDocBldr::primitive(path.display()), InlineShape::Binary(length) => { DbgDocBldr::opaque(format!("", length)) diff --git a/crates/nu-data/src/value.rs b/crates/nu-data/src/value.rs index 347e2594c..70b4e051a 100644 --- a/crates/nu-data/src/value.rs +++ b/crates/nu-data/src/value.rs @@ -475,8 +475,13 @@ pub fn compute_values( if y.is_zero() { return Ok(zero_division_error()); } + let y = y.as_bigint_and_exponent(); - Ok(x / y.0) + let z = y.0.to_i64(); + match z { + Some(z) => Ok(x / z), + None => Err((left.type_name(), right.type_name())), + } } _ => Err((left.type_name(), right.type_name())), }?; diff --git a/crates/nu-engine/src/command_args.rs b/crates/nu-engine/src/command_args.rs index 06bc86bcc..cfc732545 100644 --- a/crates/nu-engine/src/command_args.rs +++ b/crates/nu-engine/src/command_args.rs @@ -1,4 +1,3 @@ -use crate::deserializer::ConfigDeserializer; use crate::env::host::Host; use crate::evaluate::scope::Scope; use crate::evaluation_context::EvaluationContext; @@ -13,7 +12,6 @@ use nu_protocol::{CallInfo, Value}; use nu_source::Tag; use nu_stream::InputStream; use parking_lot::Mutex; -use serde::Deserialize; use std::ops::Deref; use std::sync::atomic::AtomicBool; use std::sync::Arc; @@ -79,14 +77,14 @@ impl CommandArgs { Ok((f(&evaluated_args.args)?, evaluated_args.input)) } - pub fn process<'de, T: Deserialize<'de>>(self) -> Result<(T, InputStream), ShellError> { - let args = self.evaluate_once()?; - let call_info = args.call_info.clone(); + // pub fn process<'de, T: Deserialize<'de>>(self) -> Result<(T, InputStream), ShellError> { + // let args = self.evaluate_once()?; + // let call_info = args.call_info.clone(); - let mut deserializer = ConfigDeserializer::from_call_info(call_info); + // let mut deserializer = ConfigDeserializer::from_call_info(call_info); - Ok((T::deserialize(&mut deserializer)?, args.input)) - } + // Ok((T::deserialize(&mut deserializer)?, args.input)) + // } } pub struct EvaluatedCommandArgs { diff --git a/crates/nu-engine/src/deserializer.rs b/crates/nu-engine/src/deserializer.rs deleted file mode 100644 index 03a1d6951..000000000 --- a/crates/nu-engine/src/deserializer.rs +++ /dev/null @@ -1,621 +0,0 @@ -use log::trace; -use nu_errors::{CoerceInto, ShellError}; -use nu_protocol::{ - hir::CapturedBlock, CallInfo, ColumnPath, Primitive, RangeInclusion, ShellTypeName, - UntaggedValue, Value, -}; -use nu_source::Span; -use nu_source::{HasSpan, Spanned, SpannedItem, Tagged, TaggedItem}; -use nu_value_ext::ValueExt; -use serde::de; -use serde::{Deserialize, Serialize}; -use std::path::PathBuf; - -#[derive(Copy, Clone, Deserialize, Serialize)] -pub struct NumericRange { - pub from: (Option>, RangeInclusion), - pub to: (Option>, RangeInclusion), -} - -impl NumericRange { - pub fn min(self) -> u64 { - match self.from.1 { - RangeInclusion::Inclusive => self.from.0.map(|from| *from).unwrap_or(0), - RangeInclusion::Exclusive => { - self.from.0.map(|from| *from).unwrap_or(0).saturating_add(1) - } - } - } - - pub fn max(self) -> u64 { - match self.to.1 { - RangeInclusion::Inclusive => self.to.0.map(|to| *to).unwrap_or(u64::MAX), - RangeInclusion::Exclusive => self - .to - .0 - .map(|to| *to) - .unwrap_or(u64::MAX) - .saturating_sub(1), - } - } -} - -#[derive(Debug)] -pub struct DeserializerItem<'de> { - key_struct_field: Option<(String, &'de str)>, - val: Value, -} - -pub struct ConfigDeserializer<'de> { - call: CallInfo, - stack: Vec>, - saw_root: bool, - position: usize, -} - -impl<'de> ConfigDeserializer<'de> { - pub fn from_call_info(call: CallInfo) -> ConfigDeserializer<'de> { - ConfigDeserializer { - call, - stack: vec![], - saw_root: false, - position: 0, - } - } - - pub fn push_val(&mut self, val: Value) { - self.stack.push(DeserializerItem { - key_struct_field: None, - val, - }); - } - - pub fn push(&mut self, name: &'static str) -> Result<(), ShellError> { - let value: Option = if name == "rest" { - let positional = self.call.args.slice_from(self.position); - self.position += positional.len(); - Some(UntaggedValue::Table(positional).into_untagged_value()) // TODO: correct tag - } else if self.call.args.has(name) { - self.call.args.get(name).cloned() - } else { - let position = self.position; - self.position += 1; - self.call.args.nth(position).cloned() - }; - - trace!("pushing {:?}", value); - - self.stack.push(DeserializerItem { - key_struct_field: Some((name.to_string(), name)), - val: value.unwrap_or_else(|| UntaggedValue::nothing().into_value(&self.call.name_tag)), - }); - - Ok(()) - } - - pub fn top(&mut self) -> &DeserializerItem { - let value = self.stack.last(); - trace!("inspecting top value :: {:?}", value); - value.expect("Can't get top element of an empty stack") - } - - pub fn pop(&mut self) -> DeserializerItem { - let value = self.stack.pop(); - trace!("popping value :: {:?}", value); - value.expect("Can't pop an empty stack") - } -} - -use de::Visitor; - -impl<'de, 'a> de::Deserializer<'de> for &'a mut ConfigDeserializer<'de> { - type Error = ShellError; - fn deserialize_any(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - unimplemented!("deserialize_any") - } - fn deserialize_bool(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - let value = self.pop(); - trace!("Extracting {:?} for bool", value.val); - - match &value.val { - Value { - value: UntaggedValue::Primitive(Primitive::Boolean(b)), - .. - } => visitor.visit_bool(*b), - Value { - value: UntaggedValue::Primitive(Primitive::Nothing), - .. - } => visitor.visit_bool(false), - other => Err(ShellError::type_error( - "Boolean", - other.type_name().spanned(other.span()), - )), - } - } - fn deserialize_i8(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - unimplemented!("deserialize_i8") - } - fn deserialize_i16(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - unimplemented!("deserialize_i16") - } - fn deserialize_i32(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - unimplemented!("deserialize_i32") - } - fn deserialize_i64(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - unimplemented!("deserialize_i64") - } - fn deserialize_u8(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - unimplemented!("deserialize_u8") - } - fn deserialize_u16(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - unimplemented!("deserialize_u16") - } - fn deserialize_u32(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - unimplemented!("deserialize_u32") - } - fn deserialize_u64(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - unimplemented!("deserialize_u64") - } - fn deserialize_f32(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - unimplemented!("deserialize_f32") - } - fn deserialize_f64(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - unimplemented!("deserialize_f64") - } - fn deserialize_char(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - unimplemented!("deserialize_char") - } - fn deserialize_str(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - unimplemented!("deserialize_str") - } - fn deserialize_string(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - unimplemented!("deserialize_string") - } - fn deserialize_bytes(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - unimplemented!("deserialize_bytes") - } - fn deserialize_byte_buf(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - unimplemented!("deserialize_byte_buf") - } - - fn deserialize_option(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - let value = self.top(); - let name = std::any::type_name::(); - trace!("