//! Differential formats for serde. // This also includes the serde implementations for all types. This doesn't need to be externally // documented, though.
// Types with guaranteed stable serde representations. Strings are avoided to allow for optimal // representations in various binary forms.
/// Consume the next item in a sequence.
macro_rules! item {
($seq:expr, $name:literal) => {
$seq.next_element()?
.ok_or_else(|| <A::Error as serde_core::de::Error>::custom(concat!("expected ", $name)))
};
}
/// The format used when serializing and deserializing a human-readable `Date`. #[cfg(feature = "parsing")] const DATE_FORMAT: StaticFormatDescription = &[
BorrowedFormatItem::Component(Component::Year(modifier::Year::default())),
BorrowedFormatItem::Literal(b"-"),
BorrowedFormatItem::Component(Component::Month(modifier::Month::default())),
BorrowedFormatItem::Literal(b"-"),
BorrowedFormatItem::Component(Component::Day(modifier::Day::default())),
];
impl Serialize for Date { #[inline] fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where
S: Serializer,
{ #[cfg(feature = "serde-human-readable")] if serializer.is_human_readable() { let Ok(s) = self.format(&DATE_FORMAT) else { return Err(S::Error::custom("failed formatting `Date`"));
}; return serializer.serialize_str(&s);
}
/// The format used when serializing and deserializing a human-readable `OffsetDateTime`. #[cfg(feature = "parsing")] const OFFSET_DATE_TIME_FORMAT: StaticFormatDescription = &[
BorrowedFormatItem::Compound(DATE_FORMAT),
BorrowedFormatItem::Literal(b" "),
BorrowedFormatItem::Compound(TIME_FORMAT),
BorrowedFormatItem::Literal(b" "),
BorrowedFormatItem::Compound(UTC_OFFSET_FORMAT),
];
impl Serialize for OffsetDateTime { #[inline] fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where
S: Serializer,
{ #[cfg(feature = "serde-human-readable")] if serializer.is_human_readable() { let Ok(s) = self.format(&OFFSET_DATE_TIME_FORMAT) else { return Err(S::Error::custom("failed formatting `OffsetDateTime`"));
}; return serializer.serialize_str(&s);
}
/// The format used when serializing and deserializing a human-readable `PrimitiveDateTime`. #[cfg(feature = "parsing")] const PRIMITIVE_DATE_TIME_FORMAT: StaticFormatDescription = &[
BorrowedFormatItem::Compound(DATE_FORMAT),
BorrowedFormatItem::Literal(b" "),
BorrowedFormatItem::Compound(TIME_FORMAT),
];
impl Serialize for PrimitiveDateTime { #[inline] fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where
S: Serializer,
{ #[cfg(feature = "serde-human-readable")] if serializer.is_human_readable() { let Ok(s) = self.format(&PRIMITIVE_DATE_TIME_FORMAT) else { return Err(S::Error::custom("failed formatting `PrimitiveDateTime`"));
}; return serializer.serialize_str(&s);
}
/// The format used when serializing and deserializing a human-readable `UtcDateTime`. #[cfg(feature = "parsing")] const UTC_DATE_TIME_FORMAT: StaticFormatDescription = PRIMITIVE_DATE_TIME_FORMAT;
impl Serialize for UtcDateTime { #[inline] fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where
S: Serializer,
{ #[cfg(feature = "serde-human-readable")] if serializer.is_human_readable() { let Ok(s) = self.format(&PRIMITIVE_DATE_TIME_FORMAT) else { return Err(S::Error::custom("failed formatting `UtcDateTime`"));
}; return serializer.serialize_str(&s);
}
/// The format used when serializing and deserializing a human-readable `Time`. #[cfg(feature = "parsing")] const TIME_FORMAT: StaticFormatDescription = &[
BorrowedFormatItem::Component(Component::Hour(modifier::Hour::default())),
BorrowedFormatItem::Literal(b":"),
BorrowedFormatItem::Component(Component::Minute(modifier::Minute::default())),
BorrowedFormatItem::Literal(b":"),
BorrowedFormatItem::Component(Component::Second(modifier::Second::default())),
BorrowedFormatItem::Literal(b"."),
BorrowedFormatItem::Component(Component::Subsecond(modifier::Subsecond::default())),
];
impl Serialize for Time { #[inline] fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where
S: Serializer,
{ #[cfg(feature = "serde-human-readable")] if serializer.is_human_readable() { let Ok(s) = self.format(&TIME_FORMAT) else { return Err(S::Error::custom("failed formatting `Time`"));
}; return serializer.serialize_str(&s);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.