Commands to engine p (#3426)

* hash and into converted

* keep command to engine p

* Update int.rs

Co-authored-by: JT <jonathandturner@users.noreply.github.com>
This commit is contained in:
Fernando Herrera 2021-05-15 05:11:07 +01:00 committed by GitHub
parent d79a3130b8
commit 07760b4129
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 88 additions and 141 deletions

View file

@ -2,21 +2,11 @@ use crate::prelude::*;
use nu_engine::WholeStreamCommand; use nu_engine::WholeStreamCommand;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::ShellTypeName; use nu_protocol::ShellTypeName;
use nu_protocol::{ use nu_protocol::{ColumnPath, Primitive, Signature, SyntaxShape, UntaggedValue, Value};
ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
};
use nu_source::{Tag, Tagged}; use nu_source::{Tag, Tagged};
use base64::{decode_config, encode_config}; use base64::{decode_config, encode_config};
#[derive(Deserialize)]
pub struct Arguments {
pub rest: Vec<ColumnPath>,
pub character_set: Option<Tagged<String>>,
pub encode: Tagged<bool>,
pub decode: Tagged<bool>,
}
#[derive(Clone)] #[derive(Clone)]
pub struct Base64Config { pub struct Base64Config {
pub character_set: String, pub character_set: String,
@ -64,7 +54,7 @@ impl WholeStreamCommand for SubCommand {
"base64 encode or decode a value" "base64 encode or decode a value"
} }
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
operate(args) operate(args)
} }
@ -95,29 +85,25 @@ impl WholeStreamCommand for SubCommand {
} }
} }
fn operate(args: CommandArgs) -> Result<ActionStream, ShellError> { fn operate(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name_tag = args.call_info.name_tag.clone(); let name_tag = args.call_info.name_tag.clone();
let args = args.evaluate_once()?;
let ( let encode = args.has_flag("encode");
Arguments { let decode = args.has_flag("decode");
encode, let character_set: Option<Tagged<String>> = args.get_flag("character_set")?;
decode, let column_paths: Vec<ColumnPath> = args.rest(0)?;
character_set,
rest,
},
input,
) = args.process()?;
if encode.item && decode.item { if encode && decode {
return Ok(ActionStream::one(Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"only one of --decode and --encode flags can be used", "only one of --decode and --encode flags can be used",
"conflicting flags", "conflicting flags",
name_tag, name_tag,
)))); ));
} }
// Default the action to be encoding if no flags are specified. // Default the action to be encoding if no flags are specified.
let action_type = if *decode.item() { let action_type = if decode {
ActionType::Decode ActionType::Decode
} else { } else {
ActionType::Encode ActionType::Encode
@ -134,12 +120,11 @@ fn operate(args: CommandArgs) -> Result<ActionStream, ShellError> {
action_type, action_type,
}; };
let column_paths: Vec<_> = rest; Ok(args
.input
Ok(input
.map(move |v| { .map(move |v| {
if column_paths.is_empty() { if column_paths.is_empty() {
ReturnSuccess::value(action(&v, &encoding_config, v.tag())?) action(&v, &encoding_config, v.tag())
} else { } else {
let mut ret = v; let mut ret = v;
@ -151,10 +136,10 @@ fn operate(args: CommandArgs) -> Result<ActionStream, ShellError> {
)?; )?;
} }
ReturnSuccess::value(ret) Ok(ret)
} }
}) })
.to_action_stream()) .to_input_stream())
} }
fn action( fn action(

View file

@ -1,7 +1,7 @@
use crate::prelude::*; use crate::prelude::*;
use nu_engine::WholeStreamCommand; use nu_engine::WholeStreamCommand;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue}; use nu_protocol::{Signature, SyntaxShape, UntaggedValue};
pub struct Command; pub struct Command;
@ -21,10 +21,10 @@ impl WholeStreamCommand for Command {
"Apply hash function." "Apply hash function."
} }
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
Ok(ActionStream::one(ReturnSuccess::value( Ok(OutputStream::one(
UntaggedValue::string(get_full_help(&Command, args.scope())).into_value(Tag::unknown()), UntaggedValue::string(get_full_help(&Command, args.scope())).into_value(Tag::unknown()),
))) ))
} }
} }

View file

@ -2,16 +2,9 @@ use crate::prelude::*;
use nu_engine::WholeStreamCommand; use nu_engine::WholeStreamCommand;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::ShellTypeName; use nu_protocol::ShellTypeName;
use nu_protocol::{ use nu_protocol::{ColumnPath, Primitive, Signature, SyntaxShape, UntaggedValue, Value};
ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
};
use nu_source::Tag; use nu_source::Tag;
#[derive(Deserialize)]
pub struct Arguments {
pub rest: Vec<ColumnPath>,
}
pub struct SubCommand; pub struct SubCommand;
impl WholeStreamCommand for SubCommand { impl WholeStreamCommand for SubCommand {
@ -30,7 +23,7 @@ impl WholeStreamCommand for SubCommand {
"md5 encode a value" "md5 encode a value"
} }
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
operate(args) operate(args)
} }
@ -56,15 +49,15 @@ impl WholeStreamCommand for SubCommand {
} }
} }
fn operate(args: CommandArgs) -> Result<ActionStream, ShellError> { fn operate(args: CommandArgs) -> Result<OutputStream, ShellError> {
let (Arguments { rest }, input) = args.process()?; let args = args.evaluate_once()?;
let column_paths: Vec<ColumnPath> = args.rest(0)?;
let column_paths: Vec<_> = rest; Ok(args
.input
Ok(input
.map(move |v| { .map(move |v| {
if column_paths.is_empty() { if column_paths.is_empty() {
ReturnSuccess::value(action(&v, v.tag())?) action(&v, v.tag())
} else { } else {
let mut ret = v; let mut ret = v;
@ -75,10 +68,10 @@ fn operate(args: CommandArgs) -> Result<ActionStream, ShellError> {
)?; )?;
} }
ReturnSuccess::value(ret) Ok(ret)
} }
}) })
.to_action_stream()) .to_input_stream())
} }
fn action(input: &Value, tag: impl Into<Tag>) -> Result<Value, ShellError> { fn action(input: &Value, tag: impl Into<Tag>) -> Result<Value, ShellError> {

View file

@ -1,20 +1,11 @@
use crate::prelude::*; use crate::prelude::*;
use nu_engine::WholeStreamCommand; use nu_engine::WholeStreamCommand;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ use nu_protocol::{ColumnPath, Primitive, Signature, SyntaxShape, UntaggedValue, Value};
ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
};
use num_bigint::{BigInt, ToBigInt}; use num_bigint::{BigInt, ToBigInt};
pub struct SubCommand; pub struct SubCommand;
#[derive(Deserialize)]
pub struct Arguments {
pub rest: Vec<ColumnPath>,
pub skip: Option<Value>,
pub bytes: Option<Value>,
}
impl WholeStreamCommand for SubCommand { impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str { fn name(&self) -> &str {
"into binary" "into binary"
@ -44,7 +35,7 @@ impl WholeStreamCommand for SubCommand {
"Convert value to a binary primitive" "Convert value to a binary primitive"
} }
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
into_binary(args) into_binary(args)
} }
@ -121,20 +112,17 @@ impl WholeStreamCommand for SubCommand {
} }
} }
fn into_binary(args: CommandArgs) -> Result<ActionStream, ShellError> { fn into_binary(args: CommandArgs) -> Result<OutputStream, ShellError> {
let ( let args = args.evaluate_once()?;
Arguments { let skip: Option<Value> = args.get_flag("skip")?;
rest: column_paths, let bytes: Option<Value> = args.get_flag("bytes")?;
skip, let column_paths: Vec<ColumnPath> = args.rest(0)?;
bytes,
},
input,
) = args.process()?;
Ok(input Ok(args
.input
.map(move |v| { .map(move |v| {
if column_paths.is_empty() { if column_paths.is_empty() {
ReturnSuccess::value(action(&v, v.tag(), &skip, &bytes)?) action(&v, v.tag(), &skip, &bytes)
} else { } else {
let mut ret = v; let mut ret = v;
for path in &column_paths { for path in &column_paths {
@ -146,10 +134,10 @@ fn into_binary(args: CommandArgs) -> Result<ActionStream, ShellError> {
)?; )?;
} }
ReturnSuccess::value(ret) Ok(ret)
} }
}) })
.to_action_stream()) .to_input_stream())
} }
fn int_to_endian(n: i64) -> Vec<u8> { fn int_to_endian(n: i64) -> Vec<u8> {

View file

@ -1,7 +1,7 @@
use crate::prelude::*; use crate::prelude::*;
use nu_engine::WholeStreamCommand; use nu_engine::WholeStreamCommand;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; use nu_protocol::{Signature, UntaggedValue};
pub struct Command; pub struct Command;
@ -18,10 +18,10 @@ impl WholeStreamCommand for Command {
"Apply into function." "Apply into function."
} }
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
Ok(ActionStream::one(ReturnSuccess::value( Ok(OutputStream::one(
UntaggedValue::string(get_full_help(&Command, args.scope())).into_value(Tag::unknown()), UntaggedValue::string(get_full_help(&Command, args.scope())).into_value(Tag::unknown()),
))) ))
} }
} }

View file

@ -1,18 +1,11 @@
use crate::prelude::*; use crate::prelude::*;
use nu_engine::WholeStreamCommand; use nu_engine::WholeStreamCommand;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ use nu_protocol::{ColumnPath, Primitive, Signature, SyntaxShape, UntaggedValue, Value};
ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
};
use num_bigint::ToBigInt; use num_bigint::ToBigInt;
pub struct SubCommand; pub struct SubCommand;
#[derive(Deserialize)]
pub struct Arguments {
pub rest: Vec<ColumnPath>,
}
impl WholeStreamCommand for SubCommand { impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str { fn name(&self) -> &str {
"into int" "into int"
@ -29,7 +22,7 @@ impl WholeStreamCommand for SubCommand {
"Convert value to integer" "Convert value to integer"
} }
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
into_int(args) into_int(args)
} }
@ -85,13 +78,15 @@ impl WholeStreamCommand for SubCommand {
} }
} }
fn into_int(args: CommandArgs) -> Result<ActionStream, ShellError> { fn into_int(args: CommandArgs) -> Result<OutputStream, ShellError> {
let (Arguments { rest: column_paths }, input) = args.process()?; let args = args.evaluate_once()?;
let column_paths: Vec<ColumnPath> = args.rest(0)?;
Ok(input Ok(args
.input
.map(move |v| { .map(move |v| {
if column_paths.is_empty() { if column_paths.is_empty() {
ReturnSuccess::value(action(&v, v.tag())?) action(&v, v.tag())
} else { } else {
let mut ret = v; let mut ret = v;
for path in &column_paths { for path in &column_paths {
@ -101,10 +96,10 @@ fn into_int(args: CommandArgs) -> Result<ActionStream, ShellError> {
)?; )?;
} }
ReturnSuccess::value(ret) Ok(ret)
} }
}) })
.to_action_stream()) .to_input_stream())
} }
pub fn action(input: &Value, tag: impl Into<Tag>) -> Result<Value, ShellError> { pub fn action(input: &Value, tag: impl Into<Tag>) -> Result<Value, ShellError> {

View file

@ -1,9 +1,7 @@
use crate::prelude::*; use crate::prelude::*;
use nu_engine::WholeStreamCommand; use nu_engine::WholeStreamCommand;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ use nu_protocol::{ColumnPath, Primitive, Signature, SyntaxShape, UntaggedValue, Value};
ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
};
use nu_source::Tagged; use nu_source::Tagged;
use num_bigint::{BigInt, BigUint, ToBigInt}; use num_bigint::{BigInt, BigUint, ToBigInt};
// TODO num_format::SystemLocale once platform-specific dependencies are stable (see Cargo.toml) // TODO num_format::SystemLocale once platform-specific dependencies are stable (see Cargo.toml)
@ -14,13 +12,6 @@ use std::iter;
pub struct SubCommand; pub struct SubCommand;
#[derive(Deserialize)]
pub struct Arguments {
decimals: Option<Tagged<u64>>,
group_digits: bool,
column_paths: Vec<ColumnPath>,
}
impl WholeStreamCommand for SubCommand { impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str { fn name(&self) -> &str {
"into string" "into string"
@ -44,7 +35,7 @@ impl WholeStreamCommand for SubCommand {
"Convert value to string" "Convert value to string"
} }
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
into_string(args) into_string(args)
} }
@ -89,35 +80,33 @@ impl WholeStreamCommand for SubCommand {
} }
} }
fn into_string(args: CommandArgs) -> Result<ActionStream, ShellError> { fn into_string(args: CommandArgs) -> Result<OutputStream, ShellError> {
let (options, input) = args.extract(|params| { let args = args.evaluate_once()?;
Ok(Arguments {
decimals: params.get_flag("decimals")?,
group_digits: false,
column_paths: params.rest_args()?,
})
})?;
let digits = options.decimals.as_ref().map(|tagged| tagged.item); let decimals: Option<Tagged<u64>> = args.get_flag("decimals")?;
let group_digits = options.group_digits; let column_paths: Vec<ColumnPath> = args.rest(0)?;
Ok(input let digits = decimals.as_ref().map(|tagged| tagged.item);
let group_digits = false;
Ok(args
.input
.map(move |v| { .map(move |v| {
if options.column_paths.is_empty() { if column_paths.is_empty() {
ReturnSuccess::value(action(&v, v.tag(), digits, group_digits)?) action(&v, v.tag(), digits, group_digits)
} else { } else {
let mut ret = v; let mut ret = v;
for path in &options.column_paths { for path in &column_paths {
ret = ret.swap_data_by_column_path( ret = ret.swap_data_by_column_path(
path, path,
Box::new(move |old| action(old, old.tag(), digits, group_digits)), Box::new(move |old| action(old, old.tag(), digits, group_digits)),
)?; )?;
} }
ReturnSuccess::value(ret) Ok(ret)
} }
}) })
.to_action_stream()) .to_input_stream())
} }
pub fn action( pub fn action(

View file

@ -6,11 +6,6 @@ use nu_source::Tagged;
pub struct Command; pub struct Command;
#[derive(Deserialize)]
pub struct Arguments {
rows: Option<Tagged<usize>>,
}
impl WholeStreamCommand for Command { impl WholeStreamCommand for Command {
fn name(&self) -> &str { fn name(&self) -> &str {
"keep" "keep"
@ -28,7 +23,7 @@ impl WholeStreamCommand for Command {
"Keep the number of rows only." "Keep the number of rows only."
} }
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
keep(args) keep(args)
} }
@ -53,15 +48,17 @@ impl WholeStreamCommand for Command {
} }
} }
fn keep(args: CommandArgs) -> Result<ActionStream, ShellError> { fn keep(args: CommandArgs) -> Result<OutputStream, ShellError> {
let (Arguments { rows }, input) = args.process()?; let args = args.evaluate_once()?;
let rows: Option<Tagged<usize>> = args.opt(0)?;
let rows_desired = if let Some(quantity) = rows { let rows_desired = if let Some(quantity) = rows {
*quantity *quantity
} else { } else {
1 1
}; };
Ok(input.take(rows_desired).to_action_stream()) Ok(args.input.take(rows_desired).to_output_stream())
} }
#[cfg(test)] #[cfg(test)]

View file

@ -27,7 +27,7 @@ impl WholeStreamCommand for SubCommand {
"Keeps rows until the condition matches." "Keeps rows until the condition matches."
} }
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let ctx = Arc::new(EvaluationContext::from_args(&args)); let ctx = Arc::new(EvaluationContext::from_args(&args));
let call_info = args.evaluate_once()?; let call_info = args.evaluate_once()?;
@ -93,7 +93,7 @@ impl WholeStreamCommand for SubCommand {
!matches!(result, Ok(ref v) if v.is_true()) !matches!(result, Ok(ref v) if v.is_true())
}) })
.to_action_stream()) .to_output_stream())
} }
} }

View file

@ -26,7 +26,7 @@ impl WholeStreamCommand for SubCommand {
"Keeps rows while the condition matches." "Keeps rows while the condition matches."
} }
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let ctx = Arc::new(EvaluationContext::from_args(&args)); let ctx = Arc::new(EvaluationContext::from_args(&args));
let call_info = args.evaluate_once()?; let call_info = args.evaluate_once()?;
@ -92,7 +92,7 @@ impl WholeStreamCommand for SubCommand {
matches!(result, Ok(ref v) if v.is_true()) matches!(result, Ok(ref v) if v.is_true())
}) })
.to_action_stream()) .to_output_stream())
} }
} }