summaryrefslogtreecommitdiffstats
path: root/third_party/rust/time/src/serde/rfc2822.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/time/src/serde/rfc2822.rs
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/time/src/serde/rfc2822.rs')
-rw-r--r--third_party/rust/time/src/serde/rfc2822.rs72
1 files changed, 72 insertions, 0 deletions
diff --git a/third_party/rust/time/src/serde/rfc2822.rs b/third_party/rust/time/src/serde/rfc2822.rs
new file mode 100644
index 0000000000..eca90f5204
--- /dev/null
+++ b/third_party/rust/time/src/serde/rfc2822.rs
@@ -0,0 +1,72 @@
+//! Use the well-known [RFC2822 format] when serializing and deserializing an [`OffsetDateTime`].
+//!
+//! Use this module in combination with serde's [`#[with]`][with] attribute.
+//!
+//! [RFC2822 format]: https://tools.ietf.org/html/rfc2822#section-3.3
+//! [with]: https://serde.rs/field-attrs.html#with
+
+#[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;
+
+/// Serialize an [`OffsetDateTime`] using the well-known RFC2822 format.
+#[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)
+}
+
+/// Deserialize an [`OffsetDateTime`] from its RFC2822 representation.
+#[cfg(feature = "parsing")]
+pub fn deserialize<'a, D: Deserializer<'a>>(deserializer: D) -> Result<OffsetDateTime, D::Error> {
+ deserializer.deserialize_str(Visitor::<Rfc2822>(PhantomData))
+}
+
+/// Use the well-known [RFC2822 format] when serializing and deserializing an
+/// [`Option<OffsetDateTime>`].
+///
+/// Use this module in combination with serde's [`#[with]`][with] attribute.
+///
+/// [RFC2822 format]: https://tools.ietf.org/html/rfc2822#section-3.3
+/// [with]: https://serde.rs/field-attrs.html#with
+pub mod option {
+ #[allow(clippy::wildcard_imports)]
+ use super::*;
+
+ /// Serialize an [`Option<OffsetDateTime>`] using the well-known RFC2822 format.
+ #[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)
+ }
+
+ /// Deserialize an [`Option<OffsetDateTime>`] from its RFC2822 representation.
+ #[cfg(feature = "parsing")]
+ pub fn deserialize<'a, D: Deserializer<'a>>(
+ deserializer: D,
+ ) -> Result<Option<OffsetDateTime>, D::Error> {
+ deserializer.deserialize_option(Visitor::<Option<Rfc2822>>(PhantomData))
+ }
+}