Fixed to/from toml date format (#11846)

With this PR i try to resolve #11751 

# Description
I am rather new to Rust so if anything is not the way it should be
please let me know.

As described in the title I just fixed the date conversion in the to and
from toml commands as i thought it would be a good first issue. The
example of the original issue will now work as follows:

```
~> {date: 2024-02-02} | to toml
date = "2024-02-02T00:00:00+00:00"
~> "dob = 1979-05-27T07:32:00-08:00" | from toml
╭─────┬───────────────────────────╮
│ dob │ 44 years ago              |
╰─────┴───────────────────────────╯
```

The `from toml` command now returns a nushell date which is displayed as
`44 years ago` in this case.

# User-Facing Changes
none

# Tests + Formatting
all tests pass and formatting has been applied

---------

Co-authored-by: dannou812 <dannou281@gmail.com>
Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
This commit is contained in:
dannou812 2024-02-14 19:13:33 +01:00 committed by GitHub
parent 4073783d6e
commit b0600449e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 105 additions and 15 deletions

View File

@ -4,6 +4,7 @@ use nu_protocol::{
record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type,
Value,
};
use std::str::FromStr;
#[derive(Clone)]
pub struct FromToml;
@ -23,6 +24,19 @@ impl Command for FromToml {
"Parse text as .toml and create record."
}
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let span = call.head;
let (mut string_input, span, metadata) = input.collect_string_strict(span)?;
string_input.push('\n');
Ok(convert_string_to_value(string_input, span)?.into_pipeline_data_with_metadata(metadata))
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
@ -45,19 +59,6 @@ b = [1, 2]' | from toml",
},
]
}
fn run(
&self,
__engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let span = call.head;
let (mut string_input, span, metadata) = input.collect_string_strict(span)?;
string_input.push('\n');
Ok(convert_string_to_value(string_input, span)?.into_pipeline_data_with_metadata(metadata))
}
}
fn convert_toml_to_value(value: &toml::Value, span: Span) -> Value {
@ -80,7 +81,13 @@ fn convert_toml_to_value(value: &toml::Value, span: Span) -> Value {
span,
),
toml::Value::String(s) => Value::string(s.clone(), span),
toml::Value::Datetime(d) => Value::string(d.to_string(), span),
toml::Value::Datetime(d) => match chrono::DateTime::from_str(&d.to_string()) {
Ok(nushell_date) => Value::date(nushell_date, span),
// in the unlikely event that parsing goes wrong, this function still returns a valid
// nushell date (however the default one). This decision was made to make the output of
// this function uniform amongst all eventualities
Err(_) => Value::date(chrono::DateTime::default(), span),
},
}
}
@ -101,6 +108,8 @@ pub fn convert_string_to_value(string_input: String, span: Span) -> Result<Value
#[cfg(test)]
mod tests {
use super::*;
use chrono::TimeZone;
use toml::value::Datetime;
#[test]
fn test_examples() {
@ -109,6 +118,64 @@ mod tests {
test_examples(FromToml {})
}
#[test]
fn from_toml_creates_nushell_date() {
let toml_date = toml::Value::Datetime(Datetime {
date: Option::from(toml::value::Date {
year: 1980,
month: 10,
day: 12,
}),
time: Option::from(toml::value::Time {
hour: 10,
minute: 12,
second: 44,
nanosecond: 0,
}),
offset: Option::from(toml::value::Offset::Custom { minutes: 120 }),
});
let span = Span::test_data();
let reference_date = Value::date(Default::default(), Span::test_data());
let result = convert_toml_to_value(&toml_date, span);
//positive test (from toml returns a nushell date)
assert_eq!(result.get_type(), reference_date.get_type());
}
#[test]
fn from_toml_creates_correct_date() {
let toml_date = toml::Value::Datetime(Datetime {
date: Option::from(toml::value::Date {
year: 1980,
month: 10,
day: 12,
}),
time: Option::from(toml::value::Time {
hour: 10,
minute: 12,
second: 44,
nanosecond: 0,
}),
offset: Option::from(toml::value::Offset::Custom { minutes: 120 }),
});
let span = Span::test_data();
let reference_date = Value::date(
chrono::FixedOffset::east_opt(60 * 120)
.unwrap()
.with_ymd_and_hms(1980, 10, 12, 10, 12, 44)
.unwrap(),
Span::test_data(),
);
let result = convert_toml_to_value(&toml_date, span);
//positive test (from toml returns a nushell date)
assert_eq!(result, reference_date);
}
#[test]
fn string_to_toml_value_passes() {
let input_string = String::from(

View File

@ -1,3 +1,4 @@
use chrono::SecondsFormat;
use nu_protocol::ast::{Call, PathMember};
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
@ -51,7 +52,9 @@ fn helper(engine_state: &EngineState, v: &Value) -> Result<toml::Value, ShellErr
Value::Int { val, .. } => toml::Value::Integer(*val),
Value::Filesize { val, .. } => toml::Value::Integer(*val),
Value::Duration { val, .. } => toml::Value::String(val.to_string()),
Value::Date { val, .. } => toml::Value::String(val.to_string()),
Value::Date { val, .. } => {
toml::Value::String(val.to_rfc3339_opts(SecondsFormat::AutoSi, false))
}
Value::Range { .. } => toml::Value::String("<Range>".to_string()),
Value::Float { val, .. } => toml::Value::Float(*val),
Value::String { val, .. } | Value::QuotedString { val, .. } => {
@ -171,6 +174,7 @@ fn to_toml(
#[cfg(test)]
mod tests {
use super::*;
use chrono::TimeZone;
#[test]
fn test_examples() {
@ -179,6 +183,25 @@ mod tests {
test_examples(ToToml {})
}
#[test]
fn to_toml_creates_correct_date() {
let engine_state = EngineState::new();
let test_date = Value::date(
chrono::FixedOffset::east_opt(60 * 120)
.unwrap()
.with_ymd_and_hms(1980, 10, 12, 10, 12, 44)
.unwrap(),
Span::test_data(),
);
let reference_date = toml::Value::String(String::from("1980-10-12T10:12:44+02:00"));
let result = helper(&engine_state, &test_date);
assert!(result.is_ok_and(|res| res == reference_date));
}
#[test]
fn test_value_to_toml_value() {
//