preparing for into subcommands (#3299)

This commit is contained in:
Darren Schroeder 2021-04-10 11:29:11 -05:00 committed by GitHub
parent 93f3ed98e1
commit a853880e07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 9 deletions

View File

@ -70,7 +70,7 @@ pub(crate) mod histogram;
pub(crate) mod history;
pub(crate) mod if_;
pub(crate) mod insert;
pub(crate) mod into_int;
pub(crate) mod into;
pub(crate) mod keep;
pub(crate) mod last;
pub(crate) mod length;
@ -171,6 +171,8 @@ pub(crate) use each::EachWindow;
pub(crate) use echo::Echo;
pub(crate) use empty::Command as Empty;
pub(crate) use if_::If;
pub(crate) use into::Into;
pub(crate) use into::IntoInt;
pub(crate) use nu::NuPlugin;
pub(crate) use update::Command as Update;
pub(crate) mod kill;
@ -212,7 +214,6 @@ pub(crate) use help::Help;
pub(crate) use histogram::Histogram;
pub(crate) use history::History;
pub(crate) use insert::Command as Insert;
pub(crate) use into_int::IntoInt;
pub(crate) use keep::{Keep, KeepUntil, KeepWhile};
pub(crate) use last::Last;
pub(crate) use length::Length;

View File

@ -121,6 +121,7 @@ pub fn create_default_context(interactive: bool) -> Result<EvaluationContext, Bo
whole_stream_command(Get),
whole_stream_command(Update),
whole_stream_command(Insert),
whole_stream_command(Into),
whole_stream_command(IntoInt),
whole_stream_command(SplitBy),
// Row manipulation

View File

@ -0,0 +1,39 @@
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue};
pub struct Command;
impl WholeStreamCommand for Command {
fn name(&self) -> &str {
"into"
}
fn signature(&self) -> Signature {
Signature::build("into")
}
fn usage(&self) -> &str {
"Apply into function."
}
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
Ok(OutputStream::one(ReturnSuccess::value(
UntaggedValue::string(get_full_help(&Command, &args.scope)).into_value(Tag::unknown()),
)))
}
}
#[cfg(test)]
mod tests {
use super::Command;
use super::ShellError;
#[test]
fn examples_work_as_expected() -> Result<(), ShellError> {
use crate::examples::test as test_examples;
test_examples(Command {})
}
}

View File

@ -4,17 +4,16 @@ use nu_errors::ShellError;
use nu_protocol::{
ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
};
use num_bigint::{BigInt, ToBigInt};
pub struct IntoInt;
pub struct SubCommand;
#[derive(Deserialize)]
pub struct IntoIntArgs {
pub struct Arguments {
pub rest: Vec<ColumnPath>,
}
impl WholeStreamCommand for IntoInt {
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"into int"
}
@ -87,7 +86,7 @@ impl WholeStreamCommand for IntoInt {
}
fn into_int(args: CommandArgs) -> Result<OutputStream, ShellError> {
let (IntoIntArgs { rest: column_paths }, input) = args.process()?;
let (Arguments { rest: column_paths }, input) = args.process()?;
Ok(input
.map(move |v| {
@ -174,13 +173,13 @@ fn int_from_string(a_string: &str, tag: &Tag) -> Result<BigInt, ShellError> {
#[cfg(test)]
mod tests {
use super::IntoInt;
use super::ShellError;
use super::SubCommand;
#[test]
fn examples_work_as_expected() -> Result<(), ShellError> {
use crate::examples::test as test_examples;
test_examples(IntoInt {})
test_examples(SubCommand {})
}
}

View File

@ -0,0 +1,5 @@
mod command;
mod int;
pub use command::Command as Into;
pub use int::SubCommand as IntoInt;