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
#[cfg(feature = "parsing")]
use core::marker::PhantomData;
#[cfg(feature = "formatting")]
use serde::ser::Error as _;
#[cfg(feature = "parsing")]
use serde::Deserializer;
#[cfg(feature = "formatting")]
use serde::{Serialize, Serializer};
#[cfg(feature = "parsing")]
use super::Visitor;
use crate::format_description::well_known::Rfc2822;
use crate::OffsetDateTime;
#[cfg(feature = "formatting")]
pub fn serialize<S: Serializer>(
datetime: &OffsetDateTime,
serializer: S,
) -> Result<S::Ok, S::Error> {
datetime
.format(&Rfc2822)
.map_err(S::Error::custom)?
.serialize(serializer)
}
#[cfg(feature = "parsing")]
pub fn deserialize<'a, D: Deserializer<'a>>(deserializer: D) -> Result<OffsetDateTime, D::Error> {
deserializer.deserialize_str(Visitor::<Rfc2822>(PhantomData))
}
pub mod option {
#[allow(clippy::wildcard_imports)]
use super::*;
#[cfg(feature = "formatting")]
pub fn serialize<S: Serializer>(
option: &Option<OffsetDateTime>,
serializer: S,
) -> Result<S::Ok, S::Error> {
option
.map(|odt| odt.format(&Rfc2822))
.transpose()
.map_err(S::Error::custom)?
.serialize(serializer)
}
#[cfg(feature = "parsing")]
pub fn deserialize<'a, D: Deserializer<'a>>(
deserializer: D,
) -> Result<Option<OffsetDateTime>, D::Error> {
deserializer.deserialize_option(Visitor::<Option<Rfc2822>>(PhantomData))
}
}