Skip to main content

serde_duration_ext/
lib.rs

1//! Serde support for Duration and chrono::Duration
2//!
3//! # Installation
4//!
5//! Add the following to your Cargo.toml:
6//!
7//! ```toml
8//! [dependencies]
9//! serde_duration_ext = "0.1.0"
10//! ```
11//!
12//! Also you can enable the `chrono` feature to support chrono::Duration
13//!
14//! ```toml
15//! [dependencies]
16//! serde_duration_ext = { version = "0.1.0", features = ["chrono"] }
17//! ```
18//!
19//! # Usage
20//!
21
22mod durationunit;
23mod timetunit;
24
25pub mod error;
26
27use std::time::Duration;
28
29pub use durationunit::*;
30use serde::{Deserialize, Deserializer};
31pub use timetunit::*;
32
33// re-export chrono if the feature is enabled
34#[cfg(feature = "chrono")]
35pub use chrono;
36
37pub fn deserialize<'de, D>(deserializer: D) -> Result<Duration, D::Error>
38where
39    D: Deserializer<'de>,
40{
41    let duration_unit = DurationUnit::deserialize(deserializer)?;
42    Ok(duration_unit.into())
43}
44
45#[cfg(feature = "chrono")]
46pub mod chrono {
47    use chrono::Duration;
48    use serde::{Deserializer, Deserialize};
49
50    use crate::DateTimeUnit;
51
52    pub fn deserialize<'de, D>(deserializer: D) -> Result<Duration, D::Error>
53    where
54        D: Deserializer<'de>,
55    {
56        let datetime_unit = DateTimeUnit::deserialize(deserializer)?;
57        Ok(datetime_unit.into())
58    }
59}