toml-rs / toml

Rust TOML Parser
https://docs.rs/toml
Apache License 2.0
684 stars 103 forks source link

Add a way to conveniently parse chrono Local Dates using serde #390

Open ModProg opened 1 year ago

ModProg commented 1 year ago

Maybe I just missed something, but AFAICT there is no easy way to derive serde::Deserialize on a struct and without adding helper functions get a chrono::NaiveDate out the other side except using string values.

e.g. get from this:

a=2022-12-14

to

struct A {
  a: NaiveDate
}

For now the only way to do so seems to be to use toml::value::Datetime and afterwards ensure that it is only a local date and convert that to a NaiveDate by creating it manually.

epage commented 1 year ago

The problem is having a compatible data model. We are either blocked on serde defining one or "the ecosystem" (e.g. time and chrono) agreeing on one. Seeing as time defines several different data models for you to choose from, I don't have much confidence in this.

ModProg commented 1 year ago

Would one way be to provide a parser that can be used with serde(with = "toml::chrono::date")?

This could also easily be put behind a chrono feature flag.

ModProg commented 1 year ago

Alternatively, implementing From <toml::value::Date> for chrono::NaiveDate would allow using it with the FromInto adapter from serde_with. (Also put behind a chrono feature flag.

Though that would necessitate another crate on top of serde to use easily.

Or add a wrapper type toml::chrono::NaiveDate that implements Deserialize and allows to be converted.

epage commented 1 year ago

I would rather not have chrono or time in the public API.

ModProg commented 1 year ago

Well I guess it would be possible to create a dedicated crate for supporting this usecase, as I guess chrono would also not be in favor of adding a toml module to their crate. And changing the deserialization for chrono and time would probably be a breaking change for them. With it mostly being just to support toml I wouldn't expect them to do that.

But it seems a bit superfluous to have a toml_(serde_)date crate.

ModProg commented 1 year ago

Well I wrote said crate: https://github.com/ModProg/toml-datetime-compat