Crate toml_datetime_compat

Source
Expand description

Adds a functionality to easily convert between toml_datetime’s and chrono’s/time’s types.

§Features

§Using serde derive macros

This crate can be used with #[serde(with="toml_datetime_compat")], but the functions deserialize and serialize can also be used on their own to (de)serialize chrono and time types.

Meaning this struct

#[derive(Deserialize, Serialize)]
struct SomeDateTimes {
    #[serde(with = "toml_datetime_compat")]
    chrono_naive_date: chrono::NaiveDate,
    #[serde(with = "toml_datetime_compat")]
    chrono_naive_time: chrono::NaiveTime,
    #[serde(with = "toml_datetime_compat")]
    chrono_naive_date_time: chrono::NaiveDateTime,
    #[serde(with = "toml_datetime_compat")]
    chrono_date_time_utc: chrono::DateTime<chrono::Utc>,
    #[serde(with = "toml_datetime_compat")]
    chrono_date_time_offset: chrono::DateTime<chrono::FixedOffset>,
    // Options work with any other supported type, too
    #[serde(with = "toml_datetime_compat", default)]
    chrono_date_time_utc_optional_present: Option<chrono::DateTime<chrono::Utc>>,
    #[serde(with = "toml_datetime_compat", default)]
    chrono_date_time_utc_optional_nonpresent: Option<chrono::DateTime<chrono::Utc>>,
    #[serde(with = "toml_datetime_compat")]
    time_date: time::Date,
    #[serde(with = "toml_datetime_compat")]
    time_time: time::Time,
    #[serde(with = "toml_datetime_compat")]
    time_primitive_date_time: time::PrimitiveDateTime,
    #[serde(with = "toml_datetime_compat")]
    time_offset_date_time: time::OffsetDateTime,
    // Options work with any other supported type, too
    #[serde(with = "toml_datetime_compat", default)]
    time_primitive_date_time_optional_present: Option<time::PrimitiveDateTime>,
    #[serde(with = "toml_datetime_compat", default)]
    time_primitive_date_time_optional_nonpresent: Option<time::PrimitiveDateTime>,
}

will (de)serialize from/to

chrono_naive_date = 1523-08-20
chrono_naive_time = 23:54:33.000011235
chrono_naive_date_time = 1523-08-20T23:54:33.000011235
chrono_date_time_utc = 1523-08-20T23:54:33.000011235Z
chrono_date_time_offset = 1523-08-20T23:54:33.000011235+04:30
chrono_date_time_utc_optional_present = 1523-08-20T23:54:33.000011235Z
time_date = 1523-08-20
time_time = 23:54:33.000011235
time_primitive_date_time = 1523-08-20T23:54:33.000011235
time_offset_date_time = 1523-08-20T23:54:33.000011235+04:30
time_primitive_date_time_optional_present = 1523-08-20T23:54:33.000011235

§Using serde_with

It is also possible to use serde_with using the TomlDateTime converter.

This is especially helpful to deserialize optional date time values (due to serde-rs/serde#723) if the existing support for Option is insufficient.

§Using FromToTomlDateTime

And by introducing a new trait FromToTomlDateTime that adds to_toml and from_toml functions to the relevant structs from chrono and time.

Structs§

TomlDateTime
Struct to allow the integration into the serde_with ecosystem

Enums§

Error
Error that can occur while transforming TomlDatetime from and to chrono and time types

Traits§

FromToTomlDateTime
Trait that allows easy conversion between TomlDatetime and chrono’s/time’s types
TomlDateTimeSerde
Used to implement serialization atop FromToTomlDateTime for TomlDatetime and various container types.

Functions§

deserialize
Function that can be used with #[serde(deserialize_with="toml_datetime_compat::deserialize")]
serialize
Function that can be used with #[serde(serialize_with="toml_datetime_compat::serialize")]