Address clippy's warnings when porting date to engine-q

This commit is contained in:
Stefan Stanciulescu 2021-10-31 21:06:44 +01:00
parent 3437dacf0b
commit 032356bfb7
5 changed files with 33 additions and 56 deletions

View file

@ -77,18 +77,26 @@ fn format_helper(value: Value, formatter: &Spanned<String>, span: Span) -> Value
match value { match value {
Value::Date { val, span: _ } => Value::String { Value::Date { val, span: _ } => Value::String {
val: val.format(formatter.item.as_str()).to_string(), val: val.format(formatter.item.as_str()).to_string(),
span: span, span,
}, },
Value::String { val, span: _ } => { Value::String { val, span: _ } => {
let dt = parse_date_from_string(val); let dt = parse_date_from_string(val);
if dt.is_ok() { match dt {
Value::String { Ok(x) => Value::String {
val: dt.unwrap().format(formatter.item.as_str()).to_string(), val: x.format(formatter.item.as_str()).to_string(),
span: span, span,
} },
} else { Err(e) => e,
dt.unwrap_err()
} }
// if dt.is_ok() {
// Value::String {
// val: dt.unwrap().format(formatter.item.as_str()).to_string(),
// span,
// }
// } else {
// dt.unwrap_err()
// }
} }
Value::Nothing { span: _ } => { Value::Nothing { span: _ } => {
let dt = Local::now(); let dt = Local::now();
@ -97,7 +105,7 @@ fn format_helper(value: Value, formatter: &Spanned<String>, span: Span) -> Value
.with_timezone(dt.offset()) .with_timezone(dt.offset())
.format(formatter.item.as_str()) .format(formatter.item.as_str())
.to_string(), .to_string(),
span: span, span,
} }
} }
_ => unsupported_input_error(), _ => unsupported_input_error(),

View file

@ -28,19 +28,16 @@ impl Command for SubCommand {
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> { ) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let span = call.head; let span = call.head;
let tzs: Vec<Value> = TZ_VARIANTS Ok(TZ_VARIANTS
.iter() .iter()
.map(move |x| { .map(move |x| {
let cols = vec!["timezone".into()]; let cols = vec!["timezone".into()];
let vals = vec![Value::String { let vals = vec![Value::String {
val: x.name().to_string(), val: x.name().to_string(),
span: span, span,
}]; }];
Value::Record { cols, vals, span } Value::Record { cols, vals, span }
}) })
.collect();
Ok(tzs
.into_iter() .into_iter()
.into_pipeline_data(engine_state.ctrlc.clone())) .into_pipeline_data(engine_state.ctrlc.clone()))
} }

View file

@ -1,7 +1,7 @@
use chrono::Local; use chrono::Local;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Example, IntoPipelineData, PipelineData, Signature, Value}; use nu_protocol::{IntoPipelineData, PipelineData, Signature, Value};
#[derive(Clone)] #[derive(Clone)]
pub struct SubCommand; pub struct SubCommand;

View file

@ -58,40 +58,18 @@ impl Command for SubCommand {
"timezone".into(), "timezone".into(),
]; ];
let vals = vec![ let vals = vec![
Value::Int { Value::Int { val: 2020, span },
val: 2020, Value::Int { val: 4, span },
span: span, Value::Int { val: 12, span },
}, Value::Int { val: 22, span },
Value::Int { Value::Int { val: 10, span },
val: 04, Value::Int { val: 57, span },
span: span,
},
Value::Int {
val: 12,
span: span,
},
Value::Int {
val: 22,
span: span,
},
Value::Int {
val: 10,
span: span,
},
Value::Int {
val: 57,
span: span,
},
Value::String { Value::String {
val: "+02:00".to_string(), val: "+02:00".to_string(),
span: span, span,
}, },
]; ];
Some(Value::Record { Some(Value::Record { cols, vals, span })
cols: cols,
vals: vals,
span: span,
})
}, },
}, },
] ]

View file

@ -93,12 +93,9 @@ fn helper(value: Value, head: Span, timezone: &Spanned<String>) -> Value {
Value::Date { val, span: _ } => _to_timezone(val, timezone, head), Value::Date { val, span: _ } => _to_timezone(val, timezone, head),
Value::String { val, span: _ } => { Value::String { val, span: _ } => {
let time = parse_date_from_string(val); let time = parse_date_from_string(val);
match time {
if time.is_ok() { Ok(dt) => _to_timezone(dt, timezone, head),
let dt = time.unwrap(); Err(e) => e,
_to_timezone(dt, timezone, head)
} else {
time.unwrap_err()
} }
} }
@ -112,11 +109,8 @@ fn helper(value: Value, head: Span, timezone: &Spanned<String>) -> Value {
} }
fn _to_timezone(dt: DateTime<FixedOffset>, timezone: &Spanned<String>, span: Span) -> Value { fn _to_timezone(dt: DateTime<FixedOffset>, timezone: &Spanned<String>, span: Span) -> Value {
match datetime_in_timezone(&dt, &timezone.item.as_str()) { match datetime_in_timezone(&dt, timezone.item.as_str()) {
Ok(dt) => Value::Date { Ok(dt) => Value::Date { val: dt, span },
val: dt,
span: span,
},
Err(_) => Value::Error { Err(_) => Value::Error {
error: ShellError::UnsupportedInput(String::from("invalid time zone"), Span::unknown()), error: ShellError::UnsupportedInput(String::from("invalid time zone"), Span::unknown()),
}, },