date commands ported (#3410)

This commit is contained in:
Fernando Herrera 2021-05-12 03:01:49 +01:00 committed by GitHub
parent 758c128147
commit bc699a2cc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 40 additions and 48 deletions

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 date function." "Apply date 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,20 +1,12 @@
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::{Dictionary, Primitive, Signature, SyntaxShape, UntaggedValue, Value};
Dictionary, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
};
use nu_source::Tagged; use nu_source::Tagged;
use std::fmt::{self, write}; use std::fmt::{self, write};
pub struct Date; pub struct Date;
#[derive(Deserialize)]
pub struct FormatArgs {
format: Tagged<String>,
table: bool,
}
impl WholeStreamCommand for Date { impl WholeStreamCommand for Date {
fn name(&self) -> &str { fn name(&self) -> &str {
"date format" "date format"
@ -30,7 +22,7 @@ impl WholeStreamCommand for Date {
"Format a given date using the given format string." "Format a given date using the given format string."
} }
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
format(args) format(args)
} }
@ -50,11 +42,15 @@ impl WholeStreamCommand for Date {
} }
} }
pub fn format(args: CommandArgs) -> Result<ActionStream, ShellError> { pub fn format(args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone(); 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<String> = args.req(0)?;
let table: Option<bool> = args.get_flag("table")?;
Ok(args
.input
.map(move |value| match value { .map(move |value| match value {
Value { Value {
value: UntaggedValue::Primitive(Primitive::Date(dt)), value: UntaggedValue::Primitive(Primitive::Date(dt)),
@ -70,7 +66,7 @@ pub fn format(args: CommandArgs) -> Result<ActionStream, ShellError> {
&format.tag, &format.tag,
)) ))
} else { } else {
let value = if table { let value = if table.is_some() {
let mut indexmap = IndexMap::new(); let mut indexmap = IndexMap::new();
indexmap.insert( indexmap.insert(
"formatted".to_string(), "formatted".to_string(),
@ -82,7 +78,7 @@ pub fn format(args: CommandArgs) -> Result<ActionStream, ShellError> {
UntaggedValue::string(&output).into_value(&tag) UntaggedValue::string(&output).into_value(&tag)
}; };
ReturnSuccess::value(value) Ok(value)
} }
} }
_ => Err(ShellError::labeled_error( _ => Err(ShellError::labeled_error(
@ -91,7 +87,7 @@ pub fn format(args: CommandArgs) -> Result<ActionStream, ShellError> {
&tag, &tag,
)), )),
}) })
.to_action_stream()) .to_input_stream())
} }
#[cfg(test)] #[cfg(test)]

View File

@ -3,7 +3,7 @@ use chrono_tz::TZ_VARIANTS;
use indexmap::IndexMap; use indexmap::IndexMap;
use nu_engine::WholeStreamCommand; use nu_engine::WholeStreamCommand;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{Dictionary, ReturnSuccess, Signature, UntaggedValue}; use nu_protocol::{Dictionary, Signature, UntaggedValue};
pub struct Date; pub struct Date;
@ -20,7 +20,7 @@ impl WholeStreamCommand for Date {
"List supported time zones." "List supported time zones."
} }
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
list_timezone(args) list_timezone(args)
} }
@ -40,7 +40,7 @@ impl WholeStreamCommand for Date {
} }
} }
fn list_timezone(args: CommandArgs) -> Result<ActionStream, ShellError> { fn list_timezone(args: CommandArgs) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once()?; let args = args.evaluate_once()?;
let tag = args.call_info.name_tag.clone(); let tag = args.call_info.name_tag.clone();
@ -52,12 +52,10 @@ fn list_timezone(args: CommandArgs) -> Result<ActionStream, ShellError> {
UntaggedValue::string(tz.name()).into_value(&tag), UntaggedValue::string(tz.name()).into_value(&tag),
); );
Ok(ReturnSuccess::Value( Ok(UntaggedValue::Row(Dictionary { entries }).into_value(&tag))
UntaggedValue::Row(Dictionary { entries }).into_value(&tag),
))
}); });
Ok(list.into_iter().to_action_stream()) Ok(list.into_iter().to_input_stream())
} }
#[cfg(test)] #[cfg(test)]

View File

@ -19,12 +19,12 @@ impl WholeStreamCommand for Date {
"Get the current date." "Get the current date."
} }
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
now(args) now(args)
} }
} }
pub fn now(args: CommandArgs) -> Result<ActionStream, ShellError> { pub fn now(args: CommandArgs) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once()?; let args = args.evaluate_once()?;
let tag = args.call_info.name_tag.clone(); let tag = args.call_info.name_tag.clone();
@ -32,7 +32,7 @@ pub fn now(args: CommandArgs) -> Result<ActionStream, ShellError> {
let value = UntaggedValue::date(now.with_timezone(now.offset())).into_value(&tag); let value = UntaggedValue::date(now.with_timezone(now.offset())).into_value(&tag);
Ok(ActionStream::one(value)) Ok(OutputStream::one(value))
} }
#[cfg(test)] #[cfg(test)]

View File

@ -3,7 +3,7 @@ use chrono::{Datelike, Timelike};
use indexmap::IndexMap; use indexmap::IndexMap;
use nu_engine::WholeStreamCommand; use nu_engine::WholeStreamCommand;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{Dictionary, Primitive, ReturnSuccess, Signature, UntaggedValue, Value}; use nu_protocol::{Dictionary, Primitive, Signature, UntaggedValue, Value};
pub struct Date; pub struct Date;
@ -20,7 +20,7 @@ impl WholeStreamCommand for Date {
"Print the date in a structured table." "Print the date in a structured table."
} }
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
to_table(args) to_table(args)
} }
@ -33,7 +33,7 @@ impl WholeStreamCommand for Date {
} }
} }
fn to_table(args: CommandArgs) -> Result<ActionStream, ShellError> { fn to_table(args: CommandArgs) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once()?; let args = args.evaluate_once()?;
let tag = args.call_info.name_tag.clone(); let tag = args.call_info.name_tag.clone();
let input = args.input; let input = args.input;
@ -79,7 +79,7 @@ fn to_table(args: CommandArgs) -> Result<ActionStream, ShellError> {
let value = UntaggedValue::Row(Dictionary::from(indexmap)).into_value(&tag); let value = UntaggedValue::Row(Dictionary::from(indexmap)).into_value(&tag);
ReturnSuccess::value(value) Ok(value)
} }
_ => Err(ShellError::labeled_error( _ => Err(ShellError::labeled_error(
"Expected a date from pipeline", "Expected a date from pipeline",
@ -87,7 +87,7 @@ fn to_table(args: CommandArgs) -> Result<ActionStream, ShellError> {
&tag, &tag,
)), )),
}) })
.to_action_stream()) .to_input_stream())
} }
#[cfg(test)] #[cfg(test)]

View File

@ -2,16 +2,11 @@ use crate::commands::date::parser::{datetime_in_timezone, ParseErrorKind};
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::{Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_protocol::{Primitive, Signature, SyntaxShape, UntaggedValue, Value};
use nu_source::Tagged; use nu_source::Tagged;
pub struct Date; pub struct Date;
#[derive(Deserialize)]
struct DateToTimeZoneArgs {
timezone: Tagged<String>,
}
impl WholeStreamCommand for Date { impl WholeStreamCommand for Date {
fn name(&self) -> &str { fn name(&self) -> &str {
"date to-timezone" "date to-timezone"
@ -33,7 +28,7 @@ impl WholeStreamCommand for Date {
"Use 'date list-timezone' to list all supported time zones." "Use 'date list-timezone' to list all supported time zones."
} }
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
to_timezone(args) to_timezone(args)
} }
@ -58,11 +53,14 @@ impl WholeStreamCommand for Date {
} }
} }
fn to_timezone(args: CommandArgs) -> Result<ActionStream, ShellError> { fn to_timezone(args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone(); let tag = args.call_info.name_tag.clone();
let (DateToTimeZoneArgs { timezone }, input) = args.process()?; let args = args.evaluate_once()?;
Ok(input let timezone: Tagged<String> = args.req(0)?;
Ok(args
.input
.map(move |value| match value { .map(move |value| match value {
Value { Value {
value: UntaggedValue::Primitive(Primitive::Date(dt)), value: UntaggedValue::Primitive(Primitive::Date(dt)),
@ -71,7 +69,7 @@ fn to_timezone(args: CommandArgs) -> Result<ActionStream, ShellError> {
Ok(dt) => { Ok(dt) => {
let value = UntaggedValue::date(dt).into_value(&tag); let value = UntaggedValue::date(dt).into_value(&tag);
ReturnSuccess::value(value) Ok(value)
} }
Err(e) => Err(ShellError::labeled_error( Err(e) => Err(ShellError::labeled_error(
error_message(e), error_message(e),
@ -85,7 +83,7 @@ fn to_timezone(args: CommandArgs) -> Result<ActionStream, ShellError> {
&tag, &tag,
)), )),
}) })
.to_action_stream()) .to_input_stream())
} }
fn error_message(err: ParseErrorKind) -> &'static str { fn error_message(err: ParseErrorKind) -> &'static str {

View File

@ -21,7 +21,7 @@ impl WholeStreamCommand for Date {
"return the current date in utc." "return the current date in utc."
} }
fn run_with_actions(&self, args: CommandArgs) -> Result<OutputStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
utc(args) utc(args)
} }
} }