Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.

std::time::Duration is an invalid type for "1 second" #43

Open
ghost opened this issue Mar 9, 2021 · 1 comment
Open

std::time::Duration is an invalid type for "1 second" #43

ghost opened this issue Mar 9, 2021 · 1 comment

Comments

@ghost
Copy link

ghost commented Mar 9, 2021

Hi, I'm using hocon 0.4.0 and serde 1.0.124. The config file says blah = 1 second, the config struct says blah: std::time::Duration, and hocon says

Error: Error deserializing: "blah: invalid type for field \"blah\""

When I change the struct's field type from std::time::Duration to String, then the struct field successfully gets the textual value 1 second.

Am I doing something wrong please? I looked into Hocon's source code and it looks like std::time::Duration is supported.

PS. Hocon is the best config format ever, loved it since Scala times, thanks for your work.

@mockersf
Copy link
Owner

Thanks you for reporting this!

Duration formats were supported in Hocon formats, but not all the way through deserialization... They now are (in 0.5.1), with an api inspired by humantime-serde

Either with an attribute:

#[derive(Deserialize, Debug)]
struct Test {
#[serde(deserialize_with = "Serde::<Duration>::with")]
a: std::time::Duration,
}

Or with a wrapper type:

#[derive(Deserialize, Debug)]
struct Test {
a: Serde<Duration>,
}

See the doc here: https://docs.rs/hocon/0.5.1/hocon/de/wrappers/struct.Serde.html

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant