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_errors::ShellError;
use nu_protocol::ShellTypeName;
use nu_protocol::{
ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
};
use nu_protocol::{ColumnPath, Primitive, Signature, SyntaxShape, UntaggedValue, Value};
use nu_source::{Tag, Tagged};
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)]
pub struct Base64Config {
pub character_set: String,
@ -46,13 +36,13 @@ impl WholeStreamCommand for SubCommand {
Some('c'),
)
.switch(
"encode",
"encode the input as base64. This is the default behavior if not specified.",
"encode",
"encode the input as base64. This is the default behavior if not specified.",
Some('e')
)
.switch(
"decode",
"decode the input from base64",
"decode",
"decode the input from base64",
Some('d'))
.rest(
SyntaxShape::ColumnPath,
@ -64,7 +54,7 @@ impl WholeStreamCommand for SubCommand {
"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)
}
@ -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 args = args.evaluate_once()?;
let (
Arguments {
encode,
decode,
character_set,
rest,
},
input,
) = args.process()?;
let encode = args.has_flag("encode");
let decode = args.has_flag("decode");
let character_set: Option<Tagged<String>> = args.get_flag("character_set")?;
let column_paths: Vec<ColumnPath> = args.rest(0)?;
if encode.item && decode.item {
return Ok(ActionStream::one(Err(ShellError::labeled_error(
if encode && decode {
return Err(ShellError::labeled_error(
"only one of --decode and --encode flags can be used",
"conflicting flags",
name_tag,
))));
));
}
// Default the action to be encoding if no flags are specified.
let action_type = if *decode.item() {
let action_type = if decode {
ActionType::Decode
} else {
ActionType::Encode
@ -134,12 +120,11 @@ fn operate(args: CommandArgs) -> Result<ActionStream, ShellError> {
action_type,
};
let column_paths: Vec<_> = rest;
Ok(input
Ok(args
.input
.map(move |v| {
if column_paths.is_empty() {
ReturnSuccess::value(action(&v, &encoding_config, v.tag())?)
action(&v, &encoding_config, v.tag())
} else {
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(
@ -182,7 +167,7 @@ fn action(
return Err(ShellError::labeled_error(
"value is not an accepted character set",
format!(
"{} is not a valid character-set.\nPlease use `help hash base64` to see a list of valid character sets.",
"{} is not a valid character-set.\nPlease use `help hash base64` to see a list of valid character sets.",
&base64_config.character_set
),
tag.into().span,

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -27,7 +27,7 @@ impl WholeStreamCommand for SubCommand {
"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 call_info = args.evaluate_once()?;
@ -93,7 +93,7 @@ impl WholeStreamCommand for SubCommand {
!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."
}
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 call_info = args.evaluate_once()?;
@ -92,7 +92,7 @@ impl WholeStreamCommand for SubCommand {
matches!(result, Ok(ref v) if v.is_true())
})
.to_action_stream())
.to_output_stream())
}
}