serde_nanos/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//! # Serde Nanos
//!
//! [Documentation](https://docs.rs/serde_nanos) |
//! [Github](https://github.com/caspervonb/serde_nanos) |
//! [Crate](https://crates.io/crates/serde_nanos)
//!
//! A serde wrapper that can be used to serialize timestamps and durations as
//! nanoseconds.
//!
//! It's often useful together with `serde_json` to communicate with JSON
//! protocols.
#![warn(missing_docs)]
#![warn(missing_debug_implementations)]

mod de;
mod ser;

pub use crate::de::Deserialize;
pub use crate::ser::Serialize;

use serde::Deserializer;
use serde::Serializer;

/// Serde `serialize_with` function to serialize time values as nanoseconds.
///
/// This function can be used with either of the following Serde attributes:
///
/// - `#[serde(with = "serde_nanos")]`
/// - `#[serde(serialize_with = "serde_nanos::serialize")]`
///
/// ```
/// # use serde_derive::Serialize;
/// use std::time::Duration;
/// use serde::Serialize;
///
/// #[derive(Serialize)]
/// struct Message {
///     #[serde(with = "serde_nanos")]
///     expires_in: Duration,
/// }
/// ```
pub fn serialize<T, S>(nanos: &T, serializer: S) -> Result<S::Ok, S::Error>
where
    T: ?Sized + Serialize,
    S: Serializer,
{
    Serialize::serialize(nanos, serializer)
}

/// Serde `deserialize_with` function to deserialize time values as nanoseconds.
///
/// This function can be used with either of the following Serde attributes:
///
/// - `#[serde(with = "serde_nanos")]`
/// - `#[serde(deserialize_with = "serde_nanos::deserialize")]`
///
/// ```
/// # use serde_derive::Deserialize;
/// use std::time::Duration;
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct Message {
///     #[serde(with = "serde_nanos")]
///     expires_in: Duration,
/// }
/// ```
pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
    T: Deserialize<'de>,
    D: Deserializer<'de>,
{
    Deserialize::deserialize(deserializer)
}