From bc699a2cc1d5f9e1699785e94e04157ad441b463 Mon Sep 17 00:00:00 2001 From: Fernando Herrera Date: Wed, 12 May 2021 03:01:49 +0100 Subject: [PATCH] date commands ported (#3410) --- .../nu-command/src/commands/date/command.rs | 8 +++--- crates/nu-command/src/commands/date/format.rs | 28 ++++++++----------- .../src/commands/date/list_timezone.rs | 12 ++++---- crates/nu-command/src/commands/date/now.rs | 6 ++-- .../nu-command/src/commands/date/to_table.rs | 10 +++---- .../src/commands/date/to_timezone.rs | 22 +++++++-------- crates/nu-command/src/commands/date/utc.rs | 2 +- 7 files changed, 40 insertions(+), 48 deletions(-) diff --git a/crates/nu-command/src/commands/date/command.rs b/crates/nu-command/src/commands/date/command.rs index 3c2193ff3..1e736eb10 100644 --- a/crates/nu-command/src/commands/date/command.rs +++ b/crates/nu-command/src/commands/date/command.rs @@ -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 date function." } - fn run_with_actions(&self, args: CommandArgs) -> Result { - Ok(ActionStream::one(ReturnSuccess::value( + fn run(&self, args: CommandArgs) -> Result { + Ok(OutputStream::one( UntaggedValue::string(get_full_help(&Command, args.scope())).into_value(Tag::unknown()), - ))) + )) } } diff --git a/crates/nu-command/src/commands/date/format.rs b/crates/nu-command/src/commands/date/format.rs index f86bc6d1f..827350b73 100644 --- a/crates/nu-command/src/commands/date/format.rs +++ b/crates/nu-command/src/commands/date/format.rs @@ -1,20 +1,12 @@ use crate::prelude::*; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{ - Dictionary, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value, -}; +use nu_protocol::{Dictionary, Primitive, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; use std::fmt::{self, write}; pub struct Date; -#[derive(Deserialize)] -pub struct FormatArgs { - format: Tagged, - table: bool, -} - impl WholeStreamCommand for Date { fn name(&self) -> &str { "date format" @@ -30,7 +22,7 @@ impl WholeStreamCommand for Date { "Format a given date using the given format string." } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { format(args) } @@ -50,11 +42,15 @@ impl WholeStreamCommand for Date { } } -pub fn format(args: CommandArgs) -> Result { +pub fn format(args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); - let (FormatArgs { format, table }, input) = args.process()?; + let args = args.evaluate_once()?; - Ok(input + let format: Tagged = args.req(0)?; + let table: Option = args.get_flag("table")?; + + Ok(args + .input .map(move |value| match value { Value { value: UntaggedValue::Primitive(Primitive::Date(dt)), @@ -70,7 +66,7 @@ pub fn format(args: CommandArgs) -> Result { &format.tag, )) } else { - let value = if table { + let value = if table.is_some() { let mut indexmap = IndexMap::new(); indexmap.insert( "formatted".to_string(), @@ -82,7 +78,7 @@ pub fn format(args: CommandArgs) -> Result { UntaggedValue::string(&output).into_value(&tag) }; - ReturnSuccess::value(value) + Ok(value) } } _ => Err(ShellError::labeled_error( @@ -91,7 +87,7 @@ pub fn format(args: CommandArgs) -> Result { &tag, )), }) - .to_action_stream()) + .to_input_stream()) } #[cfg(test)] diff --git a/crates/nu-command/src/commands/date/list_timezone.rs b/crates/nu-command/src/commands/date/list_timezone.rs index 6b19588e6..91c09866f 100644 --- a/crates/nu-command/src/commands/date/list_timezone.rs +++ b/crates/nu-command/src/commands/date/list_timezone.rs @@ -3,7 +3,7 @@ use chrono_tz::TZ_VARIANTS; use indexmap::IndexMap; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{Dictionary, ReturnSuccess, Signature, UntaggedValue}; +use nu_protocol::{Dictionary, Signature, UntaggedValue}; pub struct Date; @@ -20,7 +20,7 @@ impl WholeStreamCommand for Date { "List supported time zones." } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { list_timezone(args) } @@ -40,7 +40,7 @@ impl WholeStreamCommand for Date { } } -fn list_timezone(args: CommandArgs) -> Result { +fn list_timezone(args: CommandArgs) -> Result { let args = args.evaluate_once()?; let tag = args.call_info.name_tag.clone(); @@ -52,12 +52,10 @@ fn list_timezone(args: CommandArgs) -> Result { UntaggedValue::string(tz.name()).into_value(&tag), ); - Ok(ReturnSuccess::Value( - UntaggedValue::Row(Dictionary { entries }).into_value(&tag), - )) + Ok(UntaggedValue::Row(Dictionary { entries }).into_value(&tag)) }); - Ok(list.into_iter().to_action_stream()) + Ok(list.into_iter().to_input_stream()) } #[cfg(test)] diff --git a/crates/nu-command/src/commands/date/now.rs b/crates/nu-command/src/commands/date/now.rs index e31abbd08..063fe3f53 100644 --- a/crates/nu-command/src/commands/date/now.rs +++ b/crates/nu-command/src/commands/date/now.rs @@ -19,12 +19,12 @@ impl WholeStreamCommand for Date { "Get the current date." } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { now(args) } } -pub fn now(args: CommandArgs) -> Result { +pub fn now(args: CommandArgs) -> Result { let args = args.evaluate_once()?; let tag = args.call_info.name_tag.clone(); @@ -32,7 +32,7 @@ pub fn now(args: CommandArgs) -> Result { let value = UntaggedValue::date(now.with_timezone(now.offset())).into_value(&tag); - Ok(ActionStream::one(value)) + Ok(OutputStream::one(value)) } #[cfg(test)] diff --git a/crates/nu-command/src/commands/date/to_table.rs b/crates/nu-command/src/commands/date/to_table.rs index aa04eb618..a7c5553a7 100644 --- a/crates/nu-command/src/commands/date/to_table.rs +++ b/crates/nu-command/src/commands/date/to_table.rs @@ -3,7 +3,7 @@ use chrono::{Datelike, Timelike}; use indexmap::IndexMap; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{Dictionary, Primitive, ReturnSuccess, Signature, UntaggedValue, Value}; +use nu_protocol::{Dictionary, Primitive, Signature, UntaggedValue, Value}; pub struct Date; @@ -20,7 +20,7 @@ impl WholeStreamCommand for Date { "Print the date in a structured table." } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { to_table(args) } @@ -33,7 +33,7 @@ impl WholeStreamCommand for Date { } } -fn to_table(args: CommandArgs) -> Result { +fn to_table(args: CommandArgs) -> Result { let args = args.evaluate_once()?; let tag = args.call_info.name_tag.clone(); let input = args.input; @@ -79,7 +79,7 @@ fn to_table(args: CommandArgs) -> Result { let value = UntaggedValue::Row(Dictionary::from(indexmap)).into_value(&tag); - ReturnSuccess::value(value) + Ok(value) } _ => Err(ShellError::labeled_error( "Expected a date from pipeline", @@ -87,7 +87,7 @@ fn to_table(args: CommandArgs) -> Result { &tag, )), }) - .to_action_stream()) + .to_input_stream()) } #[cfg(test)] diff --git a/crates/nu-command/src/commands/date/to_timezone.rs b/crates/nu-command/src/commands/date/to_timezone.rs index d9f7bf4b7..b093bbda9 100644 --- a/crates/nu-command/src/commands/date/to_timezone.rs +++ b/crates/nu-command/src/commands/date/to_timezone.rs @@ -2,16 +2,11 @@ use crate::commands::date::parser::{datetime_in_timezone, ParseErrorKind}; use crate::prelude::*; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; +use nu_protocol::{Primitive, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; pub struct Date; -#[derive(Deserialize)] -struct DateToTimeZoneArgs { - timezone: Tagged, -} - impl WholeStreamCommand for Date { fn name(&self) -> &str { "date to-timezone" @@ -33,7 +28,7 @@ impl WholeStreamCommand for Date { "Use 'date list-timezone' to list all supported time zones." } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { to_timezone(args) } @@ -58,11 +53,14 @@ impl WholeStreamCommand for Date { } } -fn to_timezone(args: CommandArgs) -> Result { +fn to_timezone(args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); - let (DateToTimeZoneArgs { timezone }, input) = args.process()?; + let args = args.evaluate_once()?; - Ok(input + let timezone: Tagged = args.req(0)?; + + Ok(args + .input .map(move |value| match value { Value { value: UntaggedValue::Primitive(Primitive::Date(dt)), @@ -71,7 +69,7 @@ fn to_timezone(args: CommandArgs) -> Result { Ok(dt) => { let value = UntaggedValue::date(dt).into_value(&tag); - ReturnSuccess::value(value) + Ok(value) } Err(e) => Err(ShellError::labeled_error( error_message(e), @@ -85,7 +83,7 @@ fn to_timezone(args: CommandArgs) -> Result { &tag, )), }) - .to_action_stream()) + .to_input_stream()) } fn error_message(err: ParseErrorKind) -> &'static str { diff --git a/crates/nu-command/src/commands/date/utc.rs b/crates/nu-command/src/commands/date/utc.rs index 0c0687887..af222f59b 100644 --- a/crates/nu-command/src/commands/date/utc.rs +++ b/crates/nu-command/src/commands/date/utc.rs @@ -21,7 +21,7 @@ impl WholeStreamCommand for Date { "return the current date in utc." } - fn run_with_actions(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { utc(args) } }