diff options
| author | tv <tv@krebsco.de> | 2026-10-26 05:24:32 +0100 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-10-26 05:24:32 +0100 |
| commit | 223834c9dabdab00fceda69b9ce989995de47efa (patch) | |
| tree | cdc8bc55750436bc81fec6c6b67b036504f313c8 /src/units.rs | |
| parent | c0ab6dbd13f26c7307b57a032da961bd6c49461c (diff) | |
remove unit suffixes from fields
Diffstat (limited to 'src/units.rs')
| -rw-r--r-- | src/units.rs | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/units.rs b/src/units.rs index e4576bc..5128b6e 100644 --- a/src/units.rs +++ b/src/units.rs @@ -162,6 +162,30 @@ pub(crate) mod duration_option { } } +/// Serde for an optional `Duration` in a request (JSON body or query string), read as +/// [`duration_option`] reads it; absent is `None`. Use with `#[serde(default)]`. +pub(crate) mod optional_duration { + use std::time::Duration; + + use serde::{Deserializer, Serializer}; + + pub(crate) fn serialize<S: Serializer>( + value: &Option<Duration>, + s: S, + ) -> Result<S::Ok, S::Error> { + match value { + Some(duration) => super::duration_option::serialize(duration, s), + None => s.serialize_none(), + } + } + + pub(crate) fn deserialize<'de, D: Deserializer<'de>>( + d: D, + ) -> Result<Option<Duration>, D::Error> { + super::duration_option::deserialize(d).map(Some) + } +} + /// Serde for a size option: a string as [`parse_size`] reads it, or a number of bytes; /// written as [`format_size`]. pub(crate) mod size_option { |
