-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support existing types with custom conversions #751
Comments
I confirm that adding #[serde(with = "humantime_serde"] before the Duration field is sufficient to enable support for parsing durations from strings. I did it using regex in
The fix in typify would probably involve adding a new
|
To see if I understand correctly: you'd like to replace replace the type named I think the former is well handled by the "replace" options, and it sounds like that's working sufficiently for you. With regard to the latter, it sounds like you'd ideally like to have a Would an alternative solution be to define your own newtype wrapper for struct MyDuration(::std::time::Duration);
impl<'de> ::serde::Deserialize<'de> for MyDuration {
fn deserialize<D>(deserializer: D) -> ::std::result::Result<Self, D::Error>
where
D: ::serde::Deserializer<'de>,
{
let s = <String>::deserialize(deserializer)?;
todo!()
}
}
// ... |
I used It's still not ideal, as it exposes implementation details to the code. If my configuration has a field named A local type named e.g. Maybe I'm too picky, but the typify documentation made me think that there is a way to do type replacement and conversion even in the |
Thanks for clarifying. The concept is interesting. One could imagine this as part of a type replacement. We already ask for the name of the existing type and some of its trait implementations; we could ask for serde attributes to apply... or even less one apply any attributes! That said, I think we're unlikely to add this to typify--certainly not any time soon. In general, we try to keep typify as focused as we can. This might be the kind of augmentation that lives in a post-processing step--either as a regex or a more complex |
OK, understood. Maybe support for the "duration" format could be added? From my experience, duration is more common than absolute time, at least for configuration files. |
Are you thinking something like {
"type": "string",
"format": "duration"
} |
Yes. It should be deserialized from strings with units into |
I'm trying to use fields of type
std::time::Duration
and initialize them with strings like "500 ms". It doesn't appear to be possible right now.I converted my code to
build.rs
so I can explore all documented functions. Theimport_types!
documentation is unclear and insufficient. Actually, there is no example ofbuild.rs
in the rustdoc documentation, but I found an example in the typify sources.Only
with_replacement
lets me replace a generated type with the existing one (std::time::Duration
). The documentation forwith_conversion
appears to promise that behavior, but a new type is always generated. Combinations ofwith_replacement
and otherwith_*
don't seem to work.Unfortunately,
with_replacement
doesn't let me specify custom serde for the field. Look e.g. for#[serde(with = "humantime_serde")]
at https://docs.rs/humantime-serde/1.1.1/humantime_serde/ - it's specified on the field, not on the whole struct.I tried some things with
x-rust-type
without any success. In any case, I don't see it as a clean approach. It should be possible for the schema to be Rust-agnostic and still support readable strings for duration.Likewise, I don't want to use numbers for duration. It's too easy to confuse seconds with milliseconds.
Currently supported formats ("uuid", "date", "ip" etc) all support conversion from string. The problem with
std::time::Duration
is that it's standard (i.e. many std functions use it) but cannot be constructed from a string. So a different mechanism is needed.The text was updated successfully, but these errors were encountered: