humantime_serde_option: New module in tor-basic-utils
This will be used to allow our config *builder* structs to be Deserialize.
This commit is contained in:
parent
8231e70288
commit
6193c9d974
|
@ -3098,6 +3098,8 @@ name = "tor-basic-utils"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"educe",
|
||||
"humantime-serde",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
@ -11,6 +11,12 @@ categories = ["rust-patterns"] # We must put *something* here and this will do
|
|||
repository = "https://gitlab.torproject.org/tpo/core/arti.git/"
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1.0.103", features = ["derive"], optional = true }
|
||||
humantime-serde-crate = { package = "humantime-serde", version = "1", optional = true }
|
||||
|
||||
[features]
|
||||
default = ["humantime-serde"]
|
||||
humantime-serde = ["humantime-serde-crate", "serde"]
|
||||
|
||||
[dev-dependencies]
|
||||
educe = "0.4.6"
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
//! Module to adaopt `humantime_serde` to `Option<Duration>`
|
||||
|
||||
use humantime_serde_crate::Serde as HtSerde;
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
|
||||
/// Serializes an `Option<Duration>` or `Option<SystemTime>` via the humantime crate.
|
||||
pub fn serialize<T, S>(d: &Option<T>, s: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
for<'a> HtSerde<&'a T>: Serialize,
|
||||
S: Serializer,
|
||||
{
|
||||
let nested: Option<HtSerde<&T>> = d.as_ref().map(Into::into);
|
||||
nested.serialize(s)
|
||||
}
|
||||
|
||||
/// Deserialize an `Option<Duration>` or `Option<SystemTime>` via the humantime crate.
|
||||
pub fn deserialize<'a, T, D>(d: D) -> Result<Option<T>, D::Error>
|
||||
where
|
||||
HtSerde<T>: Deserialize<'a>,
|
||||
D: Deserializer<'a>,
|
||||
{
|
||||
let got: Option<HtSerde<T>> = Deserialize::deserialize(d)?;
|
||||
Ok(got.map(HtSerde::into_inner))
|
||||
}
|
|
@ -41,6 +41,9 @@
|
|||
|
||||
use std::fmt;
|
||||
|
||||
#[cfg(feature = "humantime-serde")]
|
||||
pub mod humantime_serde_option;
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
/// Function with the signature of `Debug::fmt` that just prints `".."`
|
||||
|
|
Loading…
Reference in New Issue