diff options
Diffstat (limited to 'third_party/rust/serde_with/src')
32 files changed, 13014 insertions, 0 deletions
diff --git a/third_party/rust/serde_with/src/base64.rs b/third_party/rust/serde_with/src/base64.rs new file mode 100644 index 0000000000..e1c6490f44 --- /dev/null +++ b/third_party/rust/serde_with/src/base64.rs @@ -0,0 +1,205 @@ +//! De/Serialization of base64 encoded bytes +//! +//! This modules is only available when using the `base64` feature of the crate. +//! +//! Please check the documentation on the [`Base64`] type for details. + +use crate::{formats, DeserializeAs, SerializeAs}; +use alloc::{format, string::String, vec::Vec}; +use core::{ + convert::{TryFrom, TryInto}, + default::Default, + marker::PhantomData, +}; +use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer}; + +/// Serialize bytes with base64 +/// +/// The type serializes a sequence of bytes as a base64 string. +/// It works on any type implementing `AsRef<[u8]>` for serialization and `TryFrom<Vec<u8>>` for deserialization. +/// +/// The type allows customizing the character set and the padding behavior. +/// The `CHARSET` is a type implementing [`CharacterSet`]. +/// `PADDING` specifies if serializing should emit padding. +/// Deserialization always supports padded and unpadded formats. +/// [`formats::Padded`] emits padding and [`formats::Unpadded`] leaves it off. +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_with::serde_as; +/// use serde_with::base64::{Base64, Bcrypt, BinHex, Standard}; +/// use serde_with::formats::{Padded, Unpadded}; +/// +/// #[serde_as] +/// # #[derive(Debug, PartialEq, Eq)] +/// #[derive(Serialize, Deserialize)] +/// struct B64 { +/// // The default is the same as Standard character set with padding +/// #[serde_as(as = "Base64")] +/// default: Vec<u8>, +/// // Only change the character set, implies padding +/// #[serde_as(as = "Base64<BinHex>")] +/// charset_binhex: Vec<u8>, +/// +/// #[serde_as(as = "Base64<Standard, Padded>")] +/// explicit_padding: Vec<u8>, +/// #[serde_as(as = "Base64<Bcrypt, Unpadded>")] +/// no_padding: Vec<u8>, +/// } +/// +/// let b64 = B64 { +/// default: b"Hello World".to_vec(), +/// charset_binhex: b"Hello World".to_vec(), +/// explicit_padding: b"Hello World".to_vec(), +/// no_padding: b"Hello World".to_vec(), +/// }; +/// let json = serde_json::json!({ +/// "default": "SGVsbG8gV29ybGQ=", +/// "charset_binhex": "5'8VD'mI8epaD'3=", +/// "explicit_padding": "SGVsbG8gV29ybGQ=", +/// "no_padding": "QETqZE6eT07wZEO", +/// }); +/// +/// // Test serialization and deserialization +/// assert_eq!(json, serde_json::to_value(&b64).unwrap()); +/// assert_eq!(b64, serde_json::from_value(json).unwrap()); +/// # } +/// ``` + +// The padding might be better as `const PADDING: bool = true` +// https://blog.rust-lang.org/inside-rust/2021/09/06/Splitting-const-generics.html#featureconst_generics_default/ +#[derive(Copy, Clone, Debug, Default)] +pub struct Base64<CHARSET: CharacterSet = Standard, PADDING: formats::Format = formats::Padded>( + PhantomData<(CHARSET, PADDING)>, +); + +impl<T, CHARSET> SerializeAs<T> for Base64<CHARSET, formats::Padded> +where + T: AsRef<[u8]>, + CHARSET: CharacterSet, +{ + fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + base64_crate::encode_config(source, base64_crate::Config::new(CHARSET::charset(), true)) + .serialize(serializer) + } +} + +impl<T, CHARSET> SerializeAs<T> for Base64<CHARSET, formats::Unpadded> +where + T: AsRef<[u8]>, + CHARSET: CharacterSet, +{ + fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + base64_crate::encode_config(source, base64_crate::Config::new(CHARSET::charset(), false)) + .serialize(serializer) + } +} + +impl<'de, T, CHARSET, FORMAT> DeserializeAs<'de, T> for Base64<CHARSET, FORMAT> +where + T: TryFrom<Vec<u8>>, + CHARSET: CharacterSet, + FORMAT: formats::Format, +{ + fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error> + where + D: Deserializer<'de>, + { + String::deserialize(deserializer) + .and_then(|s| { + base64_crate::decode_config( + &*s, + base64_crate::Config::new(CHARSET::charset(), false), + ) + .map_err(Error::custom) + }) + .and_then(|vec: Vec<u8>| { + let length = vec.len(); + vec.try_into().map_err(|_e: T::Error| { + Error::custom(format!( + "Can't convert a Byte Vector of length {} to the output type.", + length + )) + }) + }) + } +} + +/// A base64 character set from [this list](base64_crate::CharacterSet). +pub trait CharacterSet { + /// Return a specific character set. + /// + /// Return one enum variant of the [`base64::CharacterSet`](base64_crate::CharacterSet) enum. + fn charset() -> base64_crate::CharacterSet; +} + +/// The standard character set (uses `+` and `/`). +/// +/// See [RFC 3548](https://tools.ietf.org/html/rfc3548#section-3). +#[derive(Copy, Clone, Debug, Default)] +pub struct Standard; +impl CharacterSet for Standard { + fn charset() -> base64_crate::CharacterSet { + base64_crate::CharacterSet::Standard + } +} + +/// The URL safe character set (uses `-` and `_`). +/// +/// See [RFC 3548](https://tools.ietf.org/html/rfc3548#section-3). +#[derive(Copy, Clone, Debug, Default)] +pub struct UrlSafe; +impl CharacterSet for UrlSafe { + fn charset() -> base64_crate::CharacterSet { + base64_crate::CharacterSet::UrlSafe + } +} + +/// The `crypt(3)` character set (uses `./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`). +/// +/// Not standardized, but folk wisdom on the net asserts that this alphabet is what crypt uses. +#[derive(Copy, Clone, Debug, Default)] +pub struct Crypt; +impl CharacterSet for Crypt { + fn charset() -> base64_crate::CharacterSet { + base64_crate::CharacterSet::Crypt + } +} + +/// The bcrypt character set (uses `./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789`). +#[derive(Copy, Clone, Debug, Default)] +pub struct Bcrypt; +impl CharacterSet for Bcrypt { + fn charset() -> base64_crate::CharacterSet { + base64_crate::CharacterSet::Bcrypt + } +} + +/// The character set used in IMAP-modified UTF-7 (uses `+` and `,`). +/// +/// See [RFC 3501](https://tools.ietf.org/html/rfc3501#section-5.1.3). +#[derive(Copy, Clone, Debug, Default)] +pub struct ImapMutf7; +impl CharacterSet for ImapMutf7 { + fn charset() -> base64_crate::CharacterSet { + base64_crate::CharacterSet::ImapMutf7 + } +} + +/// The character set used in BinHex 4.0 files. +/// +/// See [BinHex 4.0 Definition](http://files.stairways.com/other/binhex-40-specs-info.txt). +#[derive(Copy, Clone, Debug, Default)] +pub struct BinHex; +impl CharacterSet for BinHex { + fn charset() -> base64_crate::CharacterSet { + base64_crate::CharacterSet::BinHex + } +} diff --git a/third_party/rust/serde_with/src/chrono.rs b/third_party/rust/serde_with/src/chrono.rs new file mode 100644 index 0000000000..d39725464f --- /dev/null +++ b/third_party/rust/serde_with/src/chrono.rs @@ -0,0 +1,516 @@ +//! De/Serialization of [chrono][] types +//! +//! This modules is only available if using the `chrono` feature of the crate. +//! +//! [chrono]: https://docs.rs/chrono/ + +use crate::{ + de::DeserializeAs, + formats::{Flexible, Format, Strict, Strictness}, + ser::SerializeAs, + utils::duration::{DurationSigned, Sign}, + DurationMicroSeconds, DurationMicroSecondsWithFrac, DurationMilliSeconds, + DurationMilliSecondsWithFrac, DurationNanoSeconds, DurationNanoSecondsWithFrac, + DurationSeconds, DurationSecondsWithFrac, TimestampMicroSeconds, TimestampMicroSecondsWithFrac, + TimestampMilliSeconds, TimestampMilliSecondsWithFrac, TimestampNanoSeconds, + TimestampNanoSecondsWithFrac, TimestampSeconds, TimestampSecondsWithFrac, +}; +use alloc::{format, string::String, vec::Vec}; +use chrono_crate::{DateTime, Duration, Local, NaiveDateTime, TimeZone, Utc}; +use core::fmt; +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; + +/// Create a [`DateTime`] for the Unix Epoch using the [`Utc`] timezone +fn unix_epoch_utc() -> DateTime<Utc> { + DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(0, 0), Utc) +} + +/// Create a [`DateTime`] for the Unix Epoch using the [`Local`] timezone +fn unix_epoch_local() -> DateTime<Local> { + DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(0, 0), Utc).with_timezone(&Local) +} + +/// Create a [`NaiveDateTime`] for the Unix Epoch +fn unix_epoch_naive() -> NaiveDateTime { + NaiveDateTime::from_timestamp(0, 0) +} + +/// Deserialize a Unix timestamp with optional subsecond precision into a `DateTime<Utc>`. +/// +/// The `DateTime<Utc>` can be serialized from an integer, a float, or a string representing a number. +/// +/// # Examples +/// +/// ``` +/// # use chrono_crate::{DateTime, Utc}; +/// # use serde::Deserialize; +/// # +/// #[derive(Debug, Deserialize)] +/// struct S { +/// #[serde(with = "serde_with::chrono::datetime_utc_ts_seconds_from_any")] +/// date: DateTime<Utc>, +/// } +/// +/// // Deserializes integers +/// assert!(serde_json::from_str::<S>(r#"{ "date": 1478563200 }"#).is_ok()); +/// // floats +/// assert!(serde_json::from_str::<S>(r#"{ "date": 1478563200.123 }"#).is_ok()); +/// // and strings with numbers, for high-precision values +/// assert!(serde_json::from_str::<S>(r#"{ "date": "1478563200.123" }"#).is_ok()); +/// ``` +pub mod datetime_utc_ts_seconds_from_any { + use super::*; + use chrono_crate::{DateTime, NaiveDateTime, Utc}; + use serde::de::{Deserializer, Error, Unexpected, Visitor}; + + /// Deserialize a Unix timestamp with optional subsecond precision into a `DateTime<Utc>`. + pub fn deserialize<'de, D>(deserializer: D) -> Result<DateTime<Utc>, D::Error> + where + D: Deserializer<'de>, + { + struct Helper; + impl<'de> Visitor<'de> for Helper { + type Value = DateTime<Utc>; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter + .write_str("an integer, float, or string with optional subsecond precision.") + } + + fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E> + where + E: Error, + { + let ndt = NaiveDateTime::from_timestamp_opt(value, 0); + if let Some(ndt) = ndt { + Ok(DateTime::<Utc>::from_utc(ndt, Utc)) + } else { + Err(Error::custom(format!( + "a timestamp which can be represented in a DateTime but received '{}'", + value + ))) + } + } + + fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> + where + E: Error, + { + let ndt = NaiveDateTime::from_timestamp_opt(value as i64, 0); + if let Some(ndt) = ndt { + Ok(DateTime::<Utc>::from_utc(ndt, Utc)) + } else { + Err(Error::custom(format!( + "a timestamp which can be represented in a DateTime but received '{}'", + value + ))) + } + } + + fn visit_f64<E>(self, value: f64) -> Result<Self::Value, E> + where + E: Error, + { + let seconds = value.trunc() as i64; + let nsecs = (value.fract() * 1_000_000_000_f64).abs() as u32; + let ndt = NaiveDateTime::from_timestamp_opt(seconds, nsecs); + if let Some(ndt) = ndt { + Ok(DateTime::<Utc>::from_utc(ndt, Utc)) + } else { + Err(Error::custom(format!( + "a timestamp which can be represented in a DateTime but received '{}'", + value + ))) + } + } + + fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> + where + E: Error, + { + let parts: Vec<_> = value.split('.').collect(); + + match *parts.as_slice() { + [seconds] => { + if let Ok(seconds) = seconds.parse() { + let ndt = NaiveDateTime::from_timestamp_opt(seconds, 0); + if let Some(ndt) = ndt { + Ok(DateTime::<Utc>::from_utc(ndt, Utc)) + } else { + Err(Error::custom(format!( + "a timestamp which can be represented in a DateTime but received '{}'", + value + ))) + } + } else { + Err(Error::invalid_value(Unexpected::Str(value), &self)) + } + } + [seconds, subseconds] => { + if let Ok(seconds) = seconds.parse() { + let subseclen = subseconds.chars().count() as u32; + if subseclen > 9 { + return Err(Error::custom(format!( + "DateTimes only support nanosecond precision but '{}' has more than 9 digits.", + value + ))); + } + + if let Ok(mut subseconds) = subseconds.parse() { + // convert subseconds to nanoseconds (10^-9), require 9 places for nanoseconds + subseconds *= 10u32.pow(9 - subseclen); + let ndt = NaiveDateTime::from_timestamp_opt(seconds, subseconds); + if let Some(ndt) = ndt { + Ok(DateTime::<Utc>::from_utc(ndt, Utc)) + } else { + Err(Error::custom(format!( + "a timestamp which can be represented in a DateTime but received '{}'", + value + ))) + } + } else { + Err(Error::invalid_value(Unexpected::Str(value), &self)) + } + } else { + Err(Error::invalid_value(Unexpected::Str(value), &self)) + } + } + + _ => Err(Error::invalid_value(Unexpected::Str(value), &self)), + } + } + } + + deserializer.deserialize_any(Helper) + } +} + +impl SerializeAs<NaiveDateTime> for DateTime<Utc> { + fn serialize_as<S>(source: &NaiveDateTime, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + let datetime = DateTime::<Utc>::from_utc(*source, Utc); + datetime.serialize(serializer) + } +} + +impl<'de> DeserializeAs<'de, NaiveDateTime> for DateTime<Utc> { + fn deserialize_as<D>(deserializer: D) -> Result<NaiveDateTime, D::Error> + where + D: Deserializer<'de>, + { + DateTime::<Utc>::deserialize(deserializer).map(|datetime| datetime.naive_utc()) + } +} + +/// Convert a [`chrono::Duration`] into a [`DurationSigned`] +fn duration_into_duration_signed(dur: &Duration) -> DurationSigned { + match dur.to_std() { + Ok(dur) => DurationSigned::with_duration(Sign::Positive, dur), + Err(_) => { + if let Ok(dur) = (-*dur).to_std() { + DurationSigned::with_duration(Sign::Negative, dur) + } else { + panic!("A chrono Duration should be convertible to a DurationSigned") + } + } + } +} + +/// Convert a [`DurationSigned`] into a [`chrono::Duration`] +fn duration_from_duration_signed<'de, D>(dur: DurationSigned) -> Result<Duration, D::Error> +where + D: Deserializer<'de>, +{ + let mut chrono_dur = match Duration::from_std(dur.duration) { + Ok(dur) => dur, + Err(msg) => { + return Err(de::Error::custom(format!( + "Duration is outside of the representable range: {}", + msg + ))) + } + }; + if dur.sign.is_negative() { + chrono_dur = -chrono_dur; + } + Ok(chrono_dur) +} + +macro_rules! use_duration_signed_ser { + ( + $main_trait:ident $internal_trait:ident => + { + $ty:ty; $converter:ident => + $({ + $format:ty, $strictness:ty => + $($tbound:ident: $bound:ident $(,)?)* + })* + } + ) => { + $( + impl<$($tbound ,)*> SerializeAs<$ty> for $main_trait<$format, $strictness> + where + $($tbound: $bound,)* + { + fn serialize_as<S>(source: &$ty, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + let dur: DurationSigned = $converter(source); + $internal_trait::<$format, $strictness>::serialize_as( + &dur, + serializer, + ) + } + } + )* + }; + ( + $( $main_trait:ident $internal_trait:ident, )+ => $rest:tt + ) => { + $( use_duration_signed_ser!($main_trait $internal_trait => $rest); )+ + }; +} + +fn datetime_to_duration<TZ>(source: &DateTime<TZ>) -> DurationSigned +where + TZ: TimeZone, +{ + duration_into_duration_signed(&source.clone().signed_duration_since(unix_epoch_utc())) +} + +fn naive_datetime_to_duration(source: &NaiveDateTime) -> DurationSigned { + duration_into_duration_signed(&source.signed_duration_since(unix_epoch_naive())) +} + +use_duration_signed_ser!( + DurationSeconds DurationSeconds, + DurationMilliSeconds DurationMilliSeconds, + DurationMicroSeconds DurationMicroSeconds, + DurationNanoSeconds DurationNanoSeconds, + => { + Duration; duration_into_duration_signed => + {i64, STRICTNESS => STRICTNESS: Strictness} + {f64, STRICTNESS => STRICTNESS: Strictness} + {String, STRICTNESS => STRICTNESS: Strictness} + } +); +use_duration_signed_ser!( + TimestampSeconds DurationSeconds, + TimestampMilliSeconds DurationMilliSeconds, + TimestampMicroSeconds DurationMicroSeconds, + TimestampNanoSeconds DurationNanoSeconds, + => { + DateTime<TZ>; datetime_to_duration => + {i64, STRICTNESS => TZ: TimeZone, STRICTNESS: Strictness} + {f64, STRICTNESS => TZ: TimeZone, STRICTNESS: Strictness} + {String, STRICTNESS => TZ: TimeZone, STRICTNESS: Strictness} + } +); +use_duration_signed_ser!( + TimestampSeconds DurationSeconds, + TimestampMilliSeconds DurationMilliSeconds, + TimestampMicroSeconds DurationMicroSeconds, + TimestampNanoSeconds DurationNanoSeconds, + => { + NaiveDateTime; naive_datetime_to_duration => + {i64, STRICTNESS => STRICTNESS: Strictness} + {f64, STRICTNESS => STRICTNESS: Strictness} + {String, STRICTNESS => STRICTNESS: Strictness} + } +); + +// Duration/Timestamp WITH FRACTIONS +use_duration_signed_ser!( + DurationSecondsWithFrac DurationSecondsWithFrac, + DurationMilliSecondsWithFrac DurationMilliSecondsWithFrac, + DurationMicroSecondsWithFrac DurationMicroSecondsWithFrac, + DurationNanoSecondsWithFrac DurationNanoSecondsWithFrac, + => { + Duration; duration_into_duration_signed => + {f64, STRICTNESS => STRICTNESS: Strictness} + {String, STRICTNESS => STRICTNESS: Strictness} + } +); +use_duration_signed_ser!( + TimestampSecondsWithFrac DurationSecondsWithFrac, + TimestampMilliSecondsWithFrac DurationMilliSecondsWithFrac, + TimestampMicroSecondsWithFrac DurationMicroSecondsWithFrac, + TimestampNanoSecondsWithFrac DurationNanoSecondsWithFrac, + => { + DateTime<TZ>; datetime_to_duration => + {f64, STRICTNESS => TZ: TimeZone, STRICTNESS: Strictness} + {String, STRICTNESS => TZ: TimeZone, STRICTNESS: Strictness} + } +); +use_duration_signed_ser!( + TimestampSecondsWithFrac DurationSecondsWithFrac, + TimestampMilliSecondsWithFrac DurationMilliSecondsWithFrac, + TimestampMicroSecondsWithFrac DurationMicroSecondsWithFrac, + TimestampNanoSecondsWithFrac DurationNanoSecondsWithFrac, + => { + NaiveDateTime; naive_datetime_to_duration => + {f64, STRICTNESS => STRICTNESS: Strictness} + {String, STRICTNESS => STRICTNESS: Strictness} + } +); + +macro_rules! use_duration_signed_de { + ( + $main_trait:ident $internal_trait:ident => + { + $ty:ty; $converter:ident => + $({ + $format:ty, $strictness:ty => + $($tbound:ident: $bound:ident)* + })* + } + ) =>{ + $( + impl<'de, $($tbound,)*> DeserializeAs<'de, $ty> for $main_trait<$format, $strictness> + where + $($tbound: $bound,)* + { + fn deserialize_as<D>(deserializer: D) -> Result<$ty, D::Error> + where + D: Deserializer<'de>, + { + let dur: DurationSigned = $internal_trait::<$format, $strictness>::deserialize_as(deserializer)?; + $converter::<D>(dur) + } + } + )* + }; + ( + $( $main_trait:ident $internal_trait:ident, )+ => $rest:tt + ) => { + $( use_duration_signed_de!($main_trait $internal_trait => $rest); )+ + }; +} + +fn duration_to_datetime_utc<'de, D>(dur: DurationSigned) -> Result<DateTime<Utc>, D::Error> +where + D: Deserializer<'de>, +{ + Ok(unix_epoch_utc() + duration_from_duration_signed::<D>(dur)?) +} + +fn duration_to_datetime_local<'de, D>(dur: DurationSigned) -> Result<DateTime<Local>, D::Error> +where + D: Deserializer<'de>, +{ + Ok(unix_epoch_local() + duration_from_duration_signed::<D>(dur)?) +} + +fn duration_to_naive_datetime<'de, D>(dur: DurationSigned) -> Result<NaiveDateTime, D::Error> +where + D: Deserializer<'de>, +{ + Ok(unix_epoch_naive() + duration_from_duration_signed::<D>(dur)?) +} + +// No subsecond precision +use_duration_signed_de!( + DurationSeconds DurationSeconds, + DurationMilliSeconds DurationMilliSeconds, + DurationMicroSeconds DurationMicroSeconds, + DurationNanoSeconds DurationNanoSeconds, + => { + Duration; duration_from_duration_signed => + {i64, Strict =>} + {f64, Strict =>} + {String, Strict =>} + {FORMAT, Flexible => FORMAT: Format} + } +); +use_duration_signed_de!( + TimestampSeconds DurationSeconds, + TimestampMilliSeconds DurationMilliSeconds, + TimestampMicroSeconds DurationMicroSeconds, + TimestampNanoSeconds DurationNanoSeconds, + => { + DateTime<Utc>; duration_to_datetime_utc => + {i64, Strict =>} + {f64, Strict =>} + {String, Strict =>} + {FORMAT, Flexible => FORMAT: Format} + } +); +use_duration_signed_de!( + TimestampSeconds DurationSeconds, + TimestampMilliSeconds DurationMilliSeconds, + TimestampMicroSeconds DurationMicroSeconds, + TimestampNanoSeconds DurationNanoSeconds, + => { + DateTime<Local>; duration_to_datetime_local => + {i64, Strict =>} + {f64, Strict =>} + {String, Strict =>} + {FORMAT, Flexible => FORMAT: Format} + } +); +use_duration_signed_de!( + TimestampSeconds DurationSeconds, + TimestampMilliSeconds DurationMilliSeconds, + TimestampMicroSeconds DurationMicroSeconds, + TimestampNanoSeconds DurationNanoSeconds, + => { + NaiveDateTime; duration_to_naive_datetime => + {i64, Strict =>} + {f64, Strict =>} + {String, Strict =>} + {FORMAT, Flexible => FORMAT: Format} + } +); + +// Duration/Timestamp WITH FRACTIONS +use_duration_signed_de!( + DurationSecondsWithFrac DurationSecondsWithFrac, + DurationMilliSecondsWithFrac DurationMilliSecondsWithFrac, + DurationMicroSecondsWithFrac DurationMicroSecondsWithFrac, + DurationNanoSecondsWithFrac DurationNanoSecondsWithFrac, + => { + Duration; duration_from_duration_signed => + {f64, Strict =>} + {String, Strict =>} + {FORMAT, Flexible => FORMAT: Format} + } +); +use_duration_signed_de!( + TimestampSecondsWithFrac DurationSecondsWithFrac, + TimestampMilliSecondsWithFrac DurationMilliSecondsWithFrac, + TimestampMicroSecondsWithFrac DurationMicroSecondsWithFrac, + TimestampNanoSecondsWithFrac DurationNanoSecondsWithFrac, + => { + DateTime<Utc>; duration_to_datetime_utc => + {f64, Strict =>} + {String, Strict =>} + {FORMAT, Flexible => FORMAT: Format} + } +); +use_duration_signed_de!( + TimestampSecondsWithFrac DurationSecondsWithFrac, + TimestampMilliSecondsWithFrac DurationMilliSecondsWithFrac, + TimestampMicroSecondsWithFrac DurationMicroSecondsWithFrac, + TimestampNanoSecondsWithFrac DurationNanoSecondsWithFrac, + => { + DateTime<Local>; duration_to_datetime_local => + {f64, Strict =>} + {String, Strict =>} + {FORMAT, Flexible => FORMAT: Format} + } +); +use_duration_signed_de!( + TimestampSecondsWithFrac DurationSecondsWithFrac, + TimestampMilliSecondsWithFrac DurationMilliSecondsWithFrac, + TimestampMicroSecondsWithFrac DurationMicroSecondsWithFrac, + TimestampNanoSecondsWithFrac DurationNanoSecondsWithFrac, + => { + NaiveDateTime; duration_to_naive_datetime => + {f64, Strict =>} + {String, Strict =>} + {FORMAT, Flexible => FORMAT: Format} + } +); diff --git a/third_party/rust/serde_with/src/content/mod.rs b/third_party/rust/serde_with/src/content/mod.rs new file mode 100644 index 0000000000..711af475c8 --- /dev/null +++ b/third_party/rust/serde_with/src/content/mod.rs @@ -0,0 +1,5 @@ +//! Import of the unstable private `Content` type from `serde`. +//! +//! <https://github.com/serde-rs/serde/blob/55a7cedd737278a9d75a2efd038c6f38b8c38bd6/serde/src/private/ser.rs#L338-L997> + +pub(crate) mod ser; diff --git a/third_party/rust/serde_with/src/content/ser.rs b/third_party/rust/serde_with/src/content/ser.rs new file mode 100644 index 0000000000..be02595af9 --- /dev/null +++ b/third_party/rust/serde_with/src/content/ser.rs @@ -0,0 +1,623 @@ +//! Buffer for serializing data. +//! +//! This is a copy and improvement of the `serde` private type: +//! <https://github.com/serde-rs/serde/blob/55a7cedd737278a9d75a2efd038c6f38b8c38bd6/serde/src/private/ser.rs#L338-L997> +//! The code is very stable in the `serde` crate, so no maintainability problem is expected. +//! +//! Since the type is private we copy the type here. +//! `serde` is licensed as MIT+Apache2, the same as this crate. +//! +//! This version carries improvements compared to `serde`'s version. +//! The types support 128-bit integers, which is supported for all targets in Rust 1.40+. +//! The [`ContentSerializer`] can also be configured to human readable or compact representation. + +use alloc::{borrow::ToOwned, boxed::Box, string::String, vec::Vec}; +use core::marker::PhantomData; +use serde::ser::{self, Serialize, Serializer}; + +#[derive(Debug)] +pub(crate) enum Content { + Bool(bool), + + U8(u8), + U16(u16), + U32(u32), + U64(u64), + U128(u128), + + I8(i8), + I16(i16), + I32(i32), + I64(i64), + I128(i128), + + F32(f32), + F64(f64), + + Char(char), + String(String), + Bytes(Vec<u8>), + + None, + Some(Box<Content>), + + Unit, + UnitStruct(&'static str), + UnitVariant(&'static str, u32, &'static str), + NewtypeStruct(&'static str, Box<Content>), + NewtypeVariant(&'static str, u32, &'static str, Box<Content>), + + Seq(Vec<Content>), + Tuple(Vec<Content>), + TupleStruct(&'static str, Vec<Content>), + TupleVariant(&'static str, u32, &'static str, Vec<Content>), + Map(Vec<(Content, Content)>), + Struct(&'static str, Vec<(&'static str, Content)>), + StructVariant( + &'static str, + u32, + &'static str, + Vec<(&'static str, Content)>, + ), +} + +impl Serialize for Content { + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + match *self { + Content::Bool(b) => serializer.serialize_bool(b), + Content::U8(u) => serializer.serialize_u8(u), + Content::U16(u) => serializer.serialize_u16(u), + Content::U32(u) => serializer.serialize_u32(u), + Content::U64(u) => serializer.serialize_u64(u), + Content::U128(u) => serializer.serialize_u128(u), + Content::I8(i) => serializer.serialize_i8(i), + Content::I16(i) => serializer.serialize_i16(i), + Content::I32(i) => serializer.serialize_i32(i), + Content::I64(i) => serializer.serialize_i64(i), + Content::I128(i) => serializer.serialize_i128(i), + Content::F32(f) => serializer.serialize_f32(f), + Content::F64(f) => serializer.serialize_f64(f), + Content::Char(c) => serializer.serialize_char(c), + Content::String(ref s) => serializer.serialize_str(s), + Content::Bytes(ref b) => serializer.serialize_bytes(b), + Content::None => serializer.serialize_none(), + Content::Some(ref c) => serializer.serialize_some(&**c), + Content::Unit => serializer.serialize_unit(), + Content::UnitStruct(n) => serializer.serialize_unit_struct(n), + Content::UnitVariant(n, i, v) => serializer.serialize_unit_variant(n, i, v), + Content::NewtypeStruct(n, ref c) => serializer.serialize_newtype_struct(n, &**c), + Content::NewtypeVariant(n, i, v, ref c) => { + serializer.serialize_newtype_variant(n, i, v, &**c) + } + Content::Seq(ref elements) => elements.serialize(serializer), + Content::Tuple(ref elements) => { + use serde::ser::SerializeTuple; + let mut tuple = serializer.serialize_tuple(elements.len())?; + for e in elements { + tuple.serialize_element(e)?; + } + tuple.end() + } + Content::TupleStruct(n, ref fields) => { + use serde::ser::SerializeTupleStruct; + let mut ts = serializer.serialize_tuple_struct(n, fields.len())?; + for f in fields { + ts.serialize_field(f)?; + } + ts.end() + } + Content::TupleVariant(n, i, v, ref fields) => { + use serde::ser::SerializeTupleVariant; + let mut tv = serializer.serialize_tuple_variant(n, i, v, fields.len())?; + for f in fields { + tv.serialize_field(f)?; + } + tv.end() + } + Content::Map(ref entries) => { + use serde::ser::SerializeMap; + let mut map = serializer.serialize_map(Some(entries.len()))?; + for &(ref k, ref v) in entries { + map.serialize_entry(k, v)?; + } + map.end() + } + Content::Struct(n, ref fields) => { + use serde::ser::SerializeStruct; + let mut s = serializer.serialize_struct(n, fields.len())?; + for &(k, ref v) in fields { + s.serialize_field(k, v)?; + } + s.end() + } + Content::StructVariant(n, i, v, ref fields) => { + use serde::ser::SerializeStructVariant; + let mut sv = serializer.serialize_struct_variant(n, i, v, fields.len())?; + for &(k, ref v) in fields { + sv.serialize_field(k, v)?; + } + sv.end() + } + } + } +} + +pub(crate) struct ContentSerializer<E> { + is_human_readable: bool, + error: PhantomData<E>, +} + +impl<E> ContentSerializer<E> { + pub(crate) fn new(is_human_readable: bool) -> Self { + ContentSerializer { + is_human_readable, + error: PhantomData, + } + } +} + +impl<E> Default for ContentSerializer<E> { + fn default() -> Self { + Self::new(true) + } +} + +impl<E> Serializer for ContentSerializer<E> +where + E: ser::Error, +{ + type Ok = Content; + type Error = E; + + type SerializeSeq = SerializeSeq<E>; + type SerializeTuple = SerializeTuple<E>; + type SerializeTupleStruct = SerializeTupleStruct<E>; + type SerializeTupleVariant = SerializeTupleVariant<E>; + type SerializeMap = SerializeMap<E>; + type SerializeStruct = SerializeStruct<E>; + type SerializeStructVariant = SerializeStructVariant<E>; + + fn is_human_readable(&self) -> bool { + self.is_human_readable + } + + fn serialize_bool(self, v: bool) -> Result<Content, E> { + Ok(Content::Bool(v)) + } + + fn serialize_i8(self, v: i8) -> Result<Content, E> { + Ok(Content::I8(v)) + } + + fn serialize_i16(self, v: i16) -> Result<Content, E> { + Ok(Content::I16(v)) + } + + fn serialize_i32(self, v: i32) -> Result<Content, E> { + Ok(Content::I32(v)) + } + + fn serialize_i64(self, v: i64) -> Result<Content, E> { + Ok(Content::I64(v)) + } + + fn serialize_i128(self, v: i128) -> Result<Content, E> { + Ok(Content::I128(v)) + } + + fn serialize_u8(self, v: u8) -> Result<Content, E> { + Ok(Content::U8(v)) + } + + fn serialize_u16(self, v: u16) -> Result<Content, E> { + Ok(Content::U16(v)) + } + + fn serialize_u32(self, v: u32) -> Result<Content, E> { + Ok(Content::U32(v)) + } + + fn serialize_u64(self, v: u64) -> Result<Content, E> { + Ok(Content::U64(v)) + } + + fn serialize_u128(self, v: u128) -> Result<Content, E> { + Ok(Content::U128(v)) + } + + fn serialize_f32(self, v: f32) -> Result<Content, E> { + Ok(Content::F32(v)) + } + + fn serialize_f64(self, v: f64) -> Result<Content, E> { + Ok(Content::F64(v)) + } + + fn serialize_char(self, v: char) -> Result<Content, E> { + Ok(Content::Char(v)) + } + + fn serialize_str(self, value: &str) -> Result<Content, E> { + Ok(Content::String(value.to_owned())) + } + + fn serialize_bytes(self, value: &[u8]) -> Result<Content, E> { + Ok(Content::Bytes(value.to_owned())) + } + + fn serialize_none(self) -> Result<Content, E> { + Ok(Content::None) + } + + fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Content, E> + where + T: Serialize, + { + Ok(Content::Some(Box::new(value.serialize(self)?))) + } + + fn serialize_unit(self) -> Result<Content, E> { + Ok(Content::Unit) + } + + fn serialize_unit_struct(self, name: &'static str) -> Result<Content, E> { + Ok(Content::UnitStruct(name)) + } + + fn serialize_unit_variant( + self, + name: &'static str, + variant_index: u32, + variant: &'static str, + ) -> Result<Content, E> { + Ok(Content::UnitVariant(name, variant_index, variant)) + } + + fn serialize_newtype_struct<T: ?Sized>( + self, + name: &'static str, + value: &T, + ) -> Result<Content, E> + where + T: Serialize, + { + Ok(Content::NewtypeStruct( + name, + Box::new(value.serialize(self)?), + )) + } + + fn serialize_newtype_variant<T: ?Sized>( + self, + name: &'static str, + variant_index: u32, + variant: &'static str, + value: &T, + ) -> Result<Content, E> + where + T: Serialize, + { + Ok(Content::NewtypeVariant( + name, + variant_index, + variant, + Box::new(value.serialize(self)?), + )) + } + + fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, E> { + Ok(SerializeSeq { + is_human_readable: self.is_human_readable, + elements: Vec::with_capacity(len.unwrap_or(0)), + error: PhantomData, + }) + } + + fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, E> { + Ok(SerializeTuple { + is_human_readable: self.is_human_readable, + elements: Vec::with_capacity(len), + error: PhantomData, + }) + } + + fn serialize_tuple_struct( + self, + name: &'static str, + len: usize, + ) -> Result<Self::SerializeTupleStruct, E> { + Ok(SerializeTupleStruct { + is_human_readable: self.is_human_readable, + name, + fields: Vec::with_capacity(len), + error: PhantomData, + }) + } + + fn serialize_tuple_variant( + self, + name: &'static str, + variant_index: u32, + variant: &'static str, + len: usize, + ) -> Result<Self::SerializeTupleVariant, E> { + Ok(SerializeTupleVariant { + is_human_readable: self.is_human_readable, + name, + variant_index, + variant, + fields: Vec::with_capacity(len), + error: PhantomData, + }) + } + + fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, E> { + Ok(SerializeMap { + is_human_readable: self.is_human_readable, + entries: Vec::with_capacity(len.unwrap_or(0)), + key: None, + error: PhantomData, + }) + } + + fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeStruct, E> { + Ok(SerializeStruct { + is_human_readable: self.is_human_readable, + name, + fields: Vec::with_capacity(len), + error: PhantomData, + }) + } + + fn serialize_struct_variant( + self, + name: &'static str, + variant_index: u32, + variant: &'static str, + len: usize, + ) -> Result<Self::SerializeStructVariant, E> { + Ok(SerializeStructVariant { + is_human_readable: self.is_human_readable, + name, + variant_index, + variant, + fields: Vec::with_capacity(len), + error: PhantomData, + }) + } +} + +pub(crate) struct SerializeSeq<E> { + is_human_readable: bool, + elements: Vec<Content>, + error: PhantomData<E>, +} + +impl<E> ser::SerializeSeq for SerializeSeq<E> +where + E: ser::Error, +{ + type Ok = Content; + type Error = E; + + fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), E> + where + T: Serialize, + { + let value = value.serialize(ContentSerializer::<E>::new(self.is_human_readable))?; + self.elements.push(value); + Ok(()) + } + + fn end(self) -> Result<Content, E> { + Ok(Content::Seq(self.elements)) + } +} + +pub(crate) struct SerializeTuple<E> { + is_human_readable: bool, + elements: Vec<Content>, + error: PhantomData<E>, +} + +impl<E> ser::SerializeTuple for SerializeTuple<E> +where + E: ser::Error, +{ + type Ok = Content; + type Error = E; + + fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), E> + where + T: Serialize, + { + let value = value.serialize(ContentSerializer::<E>::new(self.is_human_readable))?; + self.elements.push(value); + Ok(()) + } + + fn end(self) -> Result<Content, E> { + Ok(Content::Tuple(self.elements)) + } +} + +pub(crate) struct SerializeTupleStruct<E> { + is_human_readable: bool, + name: &'static str, + fields: Vec<Content>, + error: PhantomData<E>, +} + +impl<E> ser::SerializeTupleStruct for SerializeTupleStruct<E> +where + E: ser::Error, +{ + type Ok = Content; + type Error = E; + + fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), E> + where + T: Serialize, + { + let value = value.serialize(ContentSerializer::<E>::new(self.is_human_readable))?; + self.fields.push(value); + Ok(()) + } + + fn end(self) -> Result<Content, E> { + Ok(Content::TupleStruct(self.name, self.fields)) + } +} + +pub(crate) struct SerializeTupleVariant<E> { + is_human_readable: bool, + name: &'static str, + variant_index: u32, + variant: &'static str, + fields: Vec<Content>, + error: PhantomData<E>, +} + +impl<E> ser::SerializeTupleVariant for SerializeTupleVariant<E> +where + E: ser::Error, +{ + type Ok = Content; + type Error = E; + + fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), E> + where + T: Serialize, + { + let value = value.serialize(ContentSerializer::<E>::new(self.is_human_readable))?; + self.fields.push(value); + Ok(()) + } + + fn end(self) -> Result<Content, E> { + Ok(Content::TupleVariant( + self.name, + self.variant_index, + self.variant, + self.fields, + )) + } +} + +pub(crate) struct SerializeMap<E> { + is_human_readable: bool, + entries: Vec<(Content, Content)>, + key: Option<Content>, + error: PhantomData<E>, +} + +impl<E> ser::SerializeMap for SerializeMap<E> +where + E: ser::Error, +{ + type Ok = Content; + type Error = E; + + fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), E> + where + T: Serialize, + { + let key = key.serialize(ContentSerializer::<E>::new(self.is_human_readable))?; + self.key = Some(key); + Ok(()) + } + + fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), E> + where + T: Serialize, + { + let key = self + .key + .take() + .expect("serialize_value called before serialize_key"); + let value = value.serialize(ContentSerializer::<E>::new(self.is_human_readable))?; + self.entries.push((key, value)); + Ok(()) + } + + fn end(self) -> Result<Content, E> { + Ok(Content::Map(self.entries)) + } + + fn serialize_entry<K: ?Sized, V: ?Sized>(&mut self, key: &K, value: &V) -> Result<(), E> + where + K: Serialize, + V: Serialize, + { + let key = key.serialize(ContentSerializer::<E>::new(self.is_human_readable))?; + let value = value.serialize(ContentSerializer::<E>::new(self.is_human_readable))?; + self.entries.push((key, value)); + Ok(()) + } +} + +pub(crate) struct SerializeStruct<E> { + is_human_readable: bool, + name: &'static str, + fields: Vec<(&'static str, Content)>, + error: PhantomData<E>, +} + +impl<E> ser::SerializeStruct for SerializeStruct<E> +where + E: ser::Error, +{ + type Ok = Content; + type Error = E; + + fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), E> + where + T: Serialize, + { + let value = value.serialize(ContentSerializer::<E>::new(self.is_human_readable))?; + self.fields.push((key, value)); + Ok(()) + } + + fn end(self) -> Result<Content, E> { + Ok(Content::Struct(self.name, self.fields)) + } +} + +pub(crate) struct SerializeStructVariant<E> { + is_human_readable: bool, + name: &'static str, + variant_index: u32, + variant: &'static str, + fields: Vec<(&'static str, Content)>, + error: PhantomData<E>, +} + +impl<E> ser::SerializeStructVariant for SerializeStructVariant<E> +where + E: ser::Error, +{ + type Ok = Content; + type Error = E; + + fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), E> + where + T: Serialize, + { + let value = value.serialize(ContentSerializer::<E>::new(self.is_human_readable))?; + self.fields.push((key, value)); + Ok(()) + } + + fn end(self) -> Result<Content, E> { + Ok(Content::StructVariant( + self.name, + self.variant_index, + self.variant, + self.fields, + )) + } +} diff --git a/third_party/rust/serde_with/src/de/const_arrays.rs b/third_party/rust/serde_with/src/de/const_arrays.rs new file mode 100644 index 0000000000..63f6d662d6 --- /dev/null +++ b/third_party/rust/serde_with/src/de/const_arrays.rs @@ -0,0 +1,338 @@ +use super::*; +use crate::utils::{MapIter, SeqIter}; +use alloc::{borrow::Cow, boxed::Box, collections::BTreeMap, string::String, vec::Vec}; +use core::{convert::TryInto, fmt, mem::MaybeUninit}; +use serde::de::*; +use std::collections::HashMap; + +// TODO this should probably be moved into the utils module when const generics are available for MSRV + +/// # Safety +/// The code follow exactly the pattern of initializing an array element-by-element from the standard library. +/// <https://doc.rust-lang.org/nightly/std/mem/union.MaybeUninit.html#initializing-an-array-element-by-element> +fn array_from_iterator<I, T, E, const N: usize>( + mut iter: I, + expected: &dyn Expected, +) -> Result<[T; N], E> +where + I: Iterator<Item = Result<T, E>>, + E: Error, +{ + fn drop_array_elems<T, const N: usize>(num: usize, mut arr: [MaybeUninit<T>; N]) { + arr[..num].iter_mut().for_each(|elem| { + // TODO This would be better with assume_init_drop nightly function + // https://github.com/rust-lang/rust/issues/63567 + unsafe { core::ptr::drop_in_place(elem.as_mut_ptr()) }; + }); + } + + // Create an uninitialized array of `MaybeUninit`. The `assume_init` is + // safe because the type we are claiming to have initialized here is a + // bunch of `MaybeUninit`s, which do not require initialization. + // + // TODO could be simplified with nightly maybe_uninit_uninit_array feature + // https://doc.rust-lang.org/nightly/std/mem/union.MaybeUninit.html#method.uninit_array + let mut arr: [MaybeUninit<T>; N] = unsafe { MaybeUninit::uninit().assume_init() }; + + // Dropping a `MaybeUninit` does nothing. Thus using raw pointer + // assignment instead of `ptr::write` does not cause the old + // uninitialized value to be dropped. Also if there is a panic during + // this loop, we have a memory leak, but there is no memory safety + // issue. + for (idx, elem) in arr[..].iter_mut().enumerate() { + *elem = match iter.next() { + Some(Ok(value)) => MaybeUninit::new(value), + Some(Err(err)) => { + drop_array_elems(idx, arr); + return Err(err); + } + None => { + drop_array_elems(idx, arr); + return Err(Error::invalid_length(idx, expected)); + } + }; + } + + // Everything is initialized. Transmute the array to the + // initialized type. + // A normal transmute is not possible because of: + // https://github.com/rust-lang/rust/issues/61956 + Ok(unsafe { core::mem::transmute_copy::<_, [T; N]>(&arr) }) +} + +impl<'de, T, As, const N: usize> DeserializeAs<'de, [T; N]> for [As; N] +where + As: DeserializeAs<'de, T>, +{ + fn deserialize_as<D>(deserializer: D) -> Result<[T; N], D::Error> + where + D: Deserializer<'de>, + { + struct ArrayVisitor<T, const M: usize>(PhantomData<T>); + + impl<'de, T, As, const M: usize> Visitor<'de> for ArrayVisitor<DeserializeAsWrap<T, As>, M> + where + As: DeserializeAs<'de, T>, + { + type Value = [T; M]; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_fmt(format_args!("an array of size {}", M)) + } + + fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error> + where + A: SeqAccess<'de>, + { + array_from_iterator( + SeqIter::new(seq).map(|res: Result<DeserializeAsWrap<T, As>, A::Error>| { + res.map(|t| t.into_inner()) + }), + &self, + ) + } + } + + deserializer.deserialize_tuple(N, ArrayVisitor::<DeserializeAsWrap<T, As>, N>(PhantomData)) + } +} + +macro_rules! tuple_seq_as_map_impl_intern { + ($tyorig:ty, $ty:ident <KAs, VAs>) => { + #[allow(clippy::implicit_hasher)] + impl<'de, K, KAs, V, VAs, const N: usize> DeserializeAs<'de, $tyorig> for $ty<KAs, VAs> + where + KAs: DeserializeAs<'de, K>, + VAs: DeserializeAs<'de, V>, + { + fn deserialize_as<D>(deserializer: D) -> Result<$tyorig, D::Error> + where + D: Deserializer<'de>, + { + struct MapVisitor<K, KAs, V, VAs, const M: usize> { + marker: PhantomData<(K, KAs, V, VAs)>, + } + + impl<'de, K, KAs, V, VAs, const M: usize> Visitor<'de> for MapVisitor<K, KAs, V, VAs, M> + where + KAs: DeserializeAs<'de, K>, + VAs: DeserializeAs<'de, V>, + { + type Value = [(K, V); M]; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_fmt(format_args!("a map of length {}", M)) + } + + fn visit_map<A>(self, access: A) -> Result<Self::Value, A::Error> + where + A: MapAccess<'de>, + { + array_from_iterator(MapIter::new(access).map( + |res: Result<(DeserializeAsWrap<K, KAs>, DeserializeAsWrap<V, VAs>), A::Error>| { + res.map(|(k, v)| (k.into_inner(), v.into_inner())) + } + ), &self) + } + } + + let visitor = MapVisitor::<K, KAs, V, VAs, N> { + marker: PhantomData, + }; + deserializer.deserialize_map(visitor) + } + } + } +} +tuple_seq_as_map_impl_intern!([(K, V); N], BTreeMap<KAs, VAs>); +tuple_seq_as_map_impl_intern!([(K, V); N], HashMap<KAs, VAs>); + +impl<'de, const N: usize> DeserializeAs<'de, [u8; N]> for Bytes { + fn deserialize_as<D>(deserializer: D) -> Result<[u8; N], D::Error> + where + D: Deserializer<'de>, + { + struct ArrayVisitor<const M: usize>; + + impl<'de, const M: usize> Visitor<'de> for ArrayVisitor<M> { + type Value = [u8; M]; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_fmt(format_args!("an byte array of size {}", M)) + } + + fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error> + where + A: SeqAccess<'de>, + { + array_from_iterator(SeqIter::new(seq), &self) + } + + fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> + where + E: Error, + { + v.try_into() + .map_err(|_| Error::invalid_length(v.len(), &self)) + } + + fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> + where + E: Error, + { + v.as_bytes() + .try_into() + .map_err(|_| Error::invalid_length(v.len(), &self)) + } + } + + deserializer.deserialize_bytes(ArrayVisitor::<N>) + } +} + +impl<'de, const N: usize> DeserializeAs<'de, &'de [u8; N]> for Bytes { + fn deserialize_as<D>(deserializer: D) -> Result<&'de [u8; N], D::Error> + where + D: Deserializer<'de>, + { + struct ArrayVisitor<const M: usize>; + + impl<'de, const M: usize> Visitor<'de> for ArrayVisitor<M> { + type Value = &'de [u8; M]; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_fmt(format_args!("a borrowed byte array of size {}", M)) + } + + fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E> + where + E: Error, + { + v.try_into() + .map_err(|_| Error::invalid_length(v.len(), &self)) + } + + fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E> + where + E: Error, + { + v.as_bytes() + .try_into() + .map_err(|_| Error::invalid_length(v.len(), &self)) + } + } + + deserializer.deserialize_bytes(ArrayVisitor::<N>) + } +} + +impl<'de, const N: usize> DeserializeAs<'de, Cow<'de, [u8; N]>> for Bytes { + fn deserialize_as<D>(deserializer: D) -> Result<Cow<'de, [u8; N]>, D::Error> + where + D: Deserializer<'de>, + { + struct CowVisitor<const M: usize>; + + impl<'de, const M: usize> Visitor<'de> for CowVisitor<M> { + type Value = Cow<'de, [u8; M]>; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a byte array") + } + + fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E> + where + E: Error, + { + Ok(Cow::Borrowed( + v.try_into() + .map_err(|_| Error::invalid_length(v.len(), &self))?, + )) + } + + fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E> + where + E: Error, + { + Ok(Cow::Borrowed( + v.as_bytes() + .try_into() + .map_err(|_| Error::invalid_length(v.len(), &self))?, + )) + } + + fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> + where + E: Error, + { + Ok(Cow::Owned( + v.to_vec() + .try_into() + .map_err(|_| Error::invalid_length(v.len(), &self))?, + )) + } + + fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> + where + E: Error, + { + Ok(Cow::Owned( + v.as_bytes() + .to_vec() + .try_into() + .map_err(|_| Error::invalid_length(v.len(), &self))?, + )) + } + + fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E> + where + E: Error, + { + let len = v.len(); + Ok(Cow::Owned( + v.try_into() + .map_err(|_| Error::invalid_length(len, &self))?, + )) + } + + fn visit_string<E>(self, v: String) -> Result<Self::Value, E> + where + E: Error, + { + let len = v.len(); + Ok(Cow::Owned( + v.into_bytes() + .try_into() + .map_err(|_| Error::invalid_length(len, &self))?, + )) + } + + fn visit_seq<V>(self, seq: V) -> Result<Self::Value, V::Error> + where + V: SeqAccess<'de>, + { + Ok(Cow::Owned(array_from_iterator(SeqIter::new(seq), &self)?)) + } + } + + deserializer.deserialize_bytes(CowVisitor) + } +} + +impl<'de, const N: usize> DeserializeAs<'de, Box<[u8; N]>> for Bytes { + fn deserialize_as<D>(deserializer: D) -> Result<Box<[u8; N]>, D::Error> + where + D: Deserializer<'de>, + { + Bytes::deserialize_as(deserializer).map(Box::new) + } +} + +impl<'de, const N: usize> DeserializeAs<'de, Cow<'de, [u8; N]>> for BorrowCow { + fn deserialize_as<D>(deserializer: D) -> Result<Cow<'de, [u8; N]>, D::Error> + where + D: Deserializer<'de>, + { + Bytes::deserialize_as(deserializer) + } +} diff --git a/third_party/rust/serde_with/src/de/impls.rs b/third_party/rust/serde_with/src/de/impls.rs new file mode 100644 index 0000000000..fc960c3d07 --- /dev/null +++ b/third_party/rust/serde_with/src/de/impls.rs @@ -0,0 +1,1508 @@ +use super::*; +use crate::{ + formats::{Flexible, Format, Strict}, + rust::StringWithSeparator, + utils, + utils::duration::DurationSigned, +}; +use alloc::{ + borrow::{Cow, ToOwned}, + boxed::Box, + collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque}, + rc::{Rc, Weak as RcWeak}, + string::String, + sync::{Arc, Weak as ArcWeak}, + vec::Vec, +}; +use core::{ + cell::{Cell, RefCell}, + convert::TryInto, + fmt::{self, Display}, + hash::{BuildHasher, Hash}, + iter::FromIterator, + str::FromStr, + time::Duration, +}; +#[cfg(feature = "indexmap")] +use indexmap_crate::{IndexMap, IndexSet}; +use serde::de::*; +use std::{ + collections::{HashMap, HashSet}, + sync::{Mutex, RwLock}, + time::SystemTime, +}; + +/////////////////////////////////////////////////////////////////////////////// +// region: Simple Wrapper types (e.g., Box, Option) + +impl<'de, T, U> DeserializeAs<'de, Box<T>> for Box<U> +where + U: DeserializeAs<'de, T>, +{ + fn deserialize_as<D>(deserializer: D) -> Result<Box<T>, D::Error> + where + D: Deserializer<'de>, + { + Ok(Box::new( + DeserializeAsWrap::<T, U>::deserialize(deserializer)?.into_inner(), + )) + } +} + +impl<'de, T, U> DeserializeAs<'de, Option<T>> for Option<U> +where + U: DeserializeAs<'de, T>, +{ + fn deserialize_as<D>(deserializer: D) -> Result<Option<T>, D::Error> + where + D: Deserializer<'de>, + { + struct OptionVisitor<T, U>(PhantomData<(T, U)>); + + impl<'de, T, U> Visitor<'de> for OptionVisitor<T, U> + where + U: DeserializeAs<'de, T>, + { + type Value = Option<T>; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("option") + } + + #[inline] + fn visit_unit<E>(self) -> Result<Self::Value, E> + where + E: Error, + { + Ok(None) + } + + #[inline] + fn visit_none<E>(self) -> Result<Self::Value, E> + where + E: Error, + { + Ok(None) + } + + #[inline] + fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error> + where + D: Deserializer<'de>, + { + U::deserialize_as(deserializer).map(Some) + } + } + + deserializer.deserialize_option(OptionVisitor::<T, U>(PhantomData)) + } +} + +impl<'de, T, U> DeserializeAs<'de, Rc<T>> for Rc<U> +where + U: DeserializeAs<'de, T>, +{ + fn deserialize_as<D>(deserializer: D) -> Result<Rc<T>, D::Error> + where + D: Deserializer<'de>, + { + Ok(Rc::new( + DeserializeAsWrap::<T, U>::deserialize(deserializer)?.into_inner(), + )) + } +} + +impl<'de, T, U> DeserializeAs<'de, RcWeak<T>> for RcWeak<U> +where + U: DeserializeAs<'de, T>, +{ + fn deserialize_as<D>(deserializer: D) -> Result<RcWeak<T>, D::Error> + where + D: Deserializer<'de>, + { + DeserializeAsWrap::<Option<Rc<T>>, Option<Rc<U>>>::deserialize(deserializer)?; + Ok(RcWeak::new()) + } +} + +impl<'de, T, U> DeserializeAs<'de, Arc<T>> for Arc<U> +where + U: DeserializeAs<'de, T>, +{ + fn deserialize_as<D>(deserializer: D) -> Result<Arc<T>, D::Error> + where + D: Deserializer<'de>, + { + Ok(Arc::new( + DeserializeAsWrap::<T, U>::deserialize(deserializer)?.into_inner(), + )) + } +} + +impl<'de, T, U> DeserializeAs<'de, ArcWeak<T>> for ArcWeak<U> +where + U: DeserializeAs<'de, T>, +{ + fn deserialize_as<D>(deserializer: D) -> Result<ArcWeak<T>, D::Error> + where + D: Deserializer<'de>, + { + DeserializeAsWrap::<Option<Arc<T>>, Option<Arc<U>>>::deserialize(deserializer)?; + Ok(ArcWeak::new()) + } +} + +impl<'de, T, U> DeserializeAs<'de, Cell<T>> for Cell<U> +where + U: DeserializeAs<'de, T>, +{ + fn deserialize_as<D>(deserializer: D) -> Result<Cell<T>, D::Error> + where + D: Deserializer<'de>, + { + Ok(Cell::new( + DeserializeAsWrap::<T, U>::deserialize(deserializer)?.into_inner(), + )) + } +} + +impl<'de, T, U> DeserializeAs<'de, RefCell<T>> for RefCell<U> +where + U: DeserializeAs<'de, T>, +{ + fn deserialize_as<D>(deserializer: D) -> Result<RefCell<T>, D::Error> + where + D: Deserializer<'de>, + { + Ok(RefCell::new( + DeserializeAsWrap::<T, U>::deserialize(deserializer)?.into_inner(), + )) + } +} + +impl<'de, T, U> DeserializeAs<'de, Mutex<T>> for Mutex<U> +where + U: DeserializeAs<'de, T>, +{ + fn deserialize_as<D>(deserializer: D) -> Result<Mutex<T>, D::Error> + where + D: Deserializer<'de>, + { + Ok(Mutex::new( + DeserializeAsWrap::<T, U>::deserialize(deserializer)?.into_inner(), + )) + } +} + +impl<'de, T, U> DeserializeAs<'de, RwLock<T>> for RwLock<U> +where + U: DeserializeAs<'de, T>, +{ + fn deserialize_as<D>(deserializer: D) -> Result<RwLock<T>, D::Error> + where + D: Deserializer<'de>, + { + Ok(RwLock::new( + DeserializeAsWrap::<T, U>::deserialize(deserializer)?.into_inner(), + )) + } +} + +impl<'de, T, TAs, E, EAs> DeserializeAs<'de, Result<T, E>> for Result<TAs, EAs> +where + TAs: DeserializeAs<'de, T>, + EAs: DeserializeAs<'de, E>, +{ + fn deserialize_as<D>(deserializer: D) -> Result<Result<T, E>, D::Error> + where + D: Deserializer<'de>, + { + Ok( + match Result::<DeserializeAsWrap<T, TAs>, DeserializeAsWrap<E, EAs>>::deserialize( + deserializer, + )? { + Ok(value) => Ok(value.into_inner()), + Err(err) => Err(err.into_inner()), + }, + ) + } +} + +// endregion +/////////////////////////////////////////////////////////////////////////////// +// region: Collection Types (e.g., Maps, Sets, Vec) + +macro_rules! seq_impl { + ( + $ty:ident < T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)* )* >, + $access:ident, + $with_capacity:expr, + $append:ident + ) => { + // Fix for clippy regression in macros on stable + // The bug no longer exists on nightly + // https://github.com/rust-lang/rust-clippy/issues/7768 + #[allow(clippy::semicolon_if_nothing_returned)] + impl<'de, T, U $(, $typaram)*> DeserializeAs<'de, $ty<T $(, $typaram)*>> for $ty<U $(, $typaram)*> + where + U: DeserializeAs<'de, T>, + $(T: $tbound1 $(+ $tbound2)*,)* + $($typaram: $bound1 $(+ $bound2)*),* + { + fn deserialize_as<D>(deserializer: D) -> Result<$ty<T $(, $typaram)*>, D::Error> + where + D: Deserializer<'de>, + { + struct SeqVisitor<T, U $(, $typaram)*> { + marker: PhantomData<(T, U $(, $typaram)*)>, + } + + impl<'de, T, U $(, $typaram)*> Visitor<'de> for SeqVisitor<T, U $(, $typaram)*> + where + U: DeserializeAs<'de, T>, + $(T: $tbound1 $(+ $tbound2)*,)* + $($typaram: $bound1 $(+ $bound2)*),* + { + type Value = $ty<T $(, $typaram)*>; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a sequence") + } + + fn visit_seq<A>(self, mut $access: A) -> Result<Self::Value, A::Error> + where + A: SeqAccess<'de>, + { + let mut values = $with_capacity; + + while let Some(value) = $access + .next_element()? + .map(|v: DeserializeAsWrap<T, U>| v.into_inner()) + { + values.$append(value); + } + + Ok(values.into()) + } + } + + let visitor = SeqVisitor::<T, U $(, $typaram)*> { + marker: PhantomData, + }; + deserializer.deserialize_seq(visitor) + } + } + }; +} + +type BoxedSlice<T> = Box<[T]>; +seq_impl!( + BinaryHeap<T: Ord>, + seq, + BinaryHeap::with_capacity(utils::size_hint_cautious(seq.size_hint())), + push +); +seq_impl!( + BoxedSlice<T>, + seq, + Vec::with_capacity(utils::size_hint_cautious(seq.size_hint())), + push +); +seq_impl!(BTreeSet<T: Ord>, seq, BTreeSet::new(), insert); +seq_impl!( + HashSet<T: Eq + Hash, S: BuildHasher + Default>, + seq, + HashSet::with_capacity_and_hasher(utils::size_hint_cautious(seq.size_hint()), S::default()), + insert +); +seq_impl!(LinkedList<T>, seq, LinkedList::new(), push_back); +seq_impl!( + Vec<T>, + seq, + Vec::with_capacity(utils::size_hint_cautious(seq.size_hint())), + push +); +seq_impl!( + VecDeque<T>, + seq, + VecDeque::with_capacity(utils::size_hint_cautious(seq.size_hint())), + push_back +); +#[cfg(feature = "indexmap")] +seq_impl!( + IndexSet<T: Eq + Hash, S: BuildHasher + Default>, + seq, + IndexSet::with_capacity_and_hasher(utils::size_hint_cautious(seq.size_hint()), S::default()), + insert +); + +macro_rules! map_impl { + ( + $ty:ident < K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)* >, + // We need an external name, such that we can use it in the `with_capacity` expression + $access:ident, + $with_capacity:expr + ) => { + // Fix for clippy regression in macros on stable + // The bug no longer exists on nightly + // https://github.com/rust-lang/rust-clippy/issues/7768 + #[allow(clippy::semicolon_if_nothing_returned)] + impl<'de, K, V, KU, VU $(, $typaram)*> DeserializeAs<'de, $ty<K, V $(, $typaram)*>> for $ty<KU, VU $(, $typaram)*> + where + KU: DeserializeAs<'de, K>, + VU: DeserializeAs<'de, V>, + $(K: $kbound1 $(+ $kbound2)*,)* + $($typaram: $bound1 $(+ $bound2)*),* + { + fn deserialize_as<D>(deserializer: D) -> Result<$ty<K, V $(, $typaram)*>, D::Error> + where + D: Deserializer<'de>, + { + struct MapVisitor<K, V, KU, VU $(, $typaram)*> { + marker: PhantomData<$ty<K, V $(, $typaram)*>>, + marker2: PhantomData<$ty<KU, VU $(, $typaram)*>>, + } + + impl<'de, K, V, KU, VU $(, $typaram)*> Visitor<'de> for MapVisitor<K, V, KU, VU $(, $typaram)*> + where + KU: DeserializeAs<'de, K>, + VU: DeserializeAs<'de, V>, + $(K: $kbound1 $(+ $kbound2)*,)* + $($typaram: $bound1 $(+ $bound2)*),* + { + type Value = $ty<K, V $(, $typaram)*>; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a map") + } + + #[inline] + fn visit_map<A>(self, mut $access: A) -> Result<Self::Value, A::Error> + where + A: MapAccess<'de>, + { + let mut values = $with_capacity; + + while let Some((key, value)) = ($access.next_entry())?.map(|(k, v): (DeserializeAsWrap::<K, KU>, DeserializeAsWrap::<V, VU>)| (k.into_inner(), v.into_inner())) { + values.insert(key, value); + } + + Ok(values) + } + } + + let visitor = MapVisitor::<K, V, KU, VU $(, $typaram)*> { marker: PhantomData, marker2: PhantomData }; + deserializer.deserialize_map(visitor) + } + } + } +} + +map_impl!( + BTreeMap<K: Ord, V>, + map, + BTreeMap::new()); +map_impl!( + HashMap<K: Eq + Hash, V, S: BuildHasher + Default>, + map, + HashMap::with_capacity_and_hasher(utils::size_hint_cautious(map.size_hint()), S::default())); +#[cfg(feature = "indexmap")] +map_impl!( + IndexMap<K: Eq + Hash, V, S: BuildHasher + Default>, + map, + IndexMap::with_capacity_and_hasher(utils::size_hint_cautious(map.size_hint()), S::default())); + +macro_rules! tuple_impl { + ($len:literal $($n:tt $t:ident $tas:ident)+) => { + impl<'de, $($t, $tas,)+> DeserializeAs<'de, ($($t,)+)> for ($($tas,)+) + where + $($tas: DeserializeAs<'de, $t>,)+ + { + fn deserialize_as<D>(deserializer: D) -> Result<($($t,)+), D::Error> + where + D: Deserializer<'de>, + { + struct TupleVisitor<$($t,)+>(PhantomData<($($t,)+)>); + + impl<'de, $($t, $tas,)+> Visitor<'de> + for TupleVisitor<$(DeserializeAsWrap<$t, $tas>,)+> + where + $($tas: DeserializeAs<'de, $t>,)+ + { + type Value = ($($t,)+); + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str(concat!("a tuple of size ", $len)) + } + + #[allow(non_snake_case)] + fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> + where + A: SeqAccess<'de>, + { + $( + let $t: DeserializeAsWrap<$t, $tas> = match seq.next_element()? { + Some(value) => value, + None => return Err(Error::invalid_length($n, &self)), + }; + )+ + + Ok(($($t.into_inner(),)+)) + } + } + + deserializer.deserialize_tuple( + $len, + TupleVisitor::<$(DeserializeAsWrap<$t, $tas>,)+>(PhantomData), + ) + } + } + }; +} + +tuple_impl!(1 0 T0 As0); +tuple_impl!(2 0 T0 As0 1 T1 As1); +tuple_impl!(3 0 T0 As0 1 T1 As1 2 T2 As2); +tuple_impl!(4 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3); +tuple_impl!(5 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4); +tuple_impl!(6 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5); +tuple_impl!(7 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6); +tuple_impl!(8 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7); +tuple_impl!(9 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7 8 T8 As8); +tuple_impl!(10 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7 8 T8 As8 9 T9 As9); +tuple_impl!(11 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7 8 T8 As8 9 T9 As9 10 T10 As10); +tuple_impl!(12 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7 8 T8 As8 9 T9 As9 10 T10 As10 11 T11 As11); +tuple_impl!(13 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7 8 T8 As8 9 T9 As9 10 T10 As10 11 T11 As11 12 T12 As12); +tuple_impl!(14 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7 8 T8 As8 9 T9 As9 10 T10 As10 11 T11 As11 12 T12 As12 13 T13 As13); +tuple_impl!(15 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7 8 T8 As8 9 T9 As9 10 T10 As10 11 T11 As11 12 T12 As12 13 T13 As13 14 T14 As14); +tuple_impl!(16 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7 8 T8 As8 9 T9 As9 10 T10 As10 11 T11 As11 12 T12 As12 13 T13 As13 14 T14 As14 15 T15 As15); + +macro_rules! map_as_tuple_seq { + ($ty:ident < K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V>) => { + impl<'de, K, KAs, V, VAs> DeserializeAs<'de, $ty<K, V>> for Vec<(KAs, VAs)> + where + KAs: DeserializeAs<'de, K>, + VAs: DeserializeAs<'de, V>, + $(K: $kbound1 $(+ $kbound2)*,)* + { + fn deserialize_as<D>(deserializer: D) -> Result<$ty<K, V>, D::Error> + where + D: Deserializer<'de>, + { + struct SeqVisitor<K, KAs, V, VAs> { + marker: PhantomData<(K, KAs, V, VAs)>, + } + + impl<'de, K, KAs, V, VAs> Visitor<'de> for SeqVisitor<K, KAs, V, VAs> + where + KAs: DeserializeAs<'de, K>, + VAs: DeserializeAs<'de, V>, + $(K: $kbound1 $(+ $kbound2)*,)* + { + type Value = $ty<K, V>; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a sequence") + } + + #[inline] + fn visit_seq<A>(self, access: A) -> Result<Self::Value, A::Error> + where + A: SeqAccess<'de>, + { + let iter = utils::SeqIter::new(access); + iter.map(|res| { + res.map( + |(k, v): (DeserializeAsWrap<K, KAs>, DeserializeAsWrap<V, VAs>)| { + (k.into_inner(), v.into_inner()) + }, + ) + }) + .collect() + } + } + + let visitor = SeqVisitor::<K, KAs, V, VAs> { + marker: PhantomData, + }; + deserializer.deserialize_seq(visitor) + } + } + }; +} +map_as_tuple_seq!(BTreeMap<K: Ord, V>); +map_as_tuple_seq!(HashMap<K: Eq + Hash, V>); +#[cfg(feature = "indexmap")] +map_as_tuple_seq!(IndexMap<K: Eq + Hash, V>); + +// endregion +/////////////////////////////////////////////////////////////////////////////// +// region: Conversion types which cause different serialization behavior + +impl<'de, T: Deserialize<'de>> DeserializeAs<'de, T> for Same { + fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error> + where + D: Deserializer<'de>, + { + T::deserialize(deserializer) + } +} + +impl<'de, T> DeserializeAs<'de, T> for DisplayFromStr +where + T: FromStr, + T::Err: Display, +{ + fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error> + where + D: Deserializer<'de>, + { + crate::rust::display_fromstr::deserialize(deserializer) + } +} + +impl<'de, T, U> DeserializeAs<'de, Vec<T>> for VecSkipError<U> +where + U: DeserializeAs<'de, T>, +{ + fn deserialize_as<D>(deserializer: D) -> Result<Vec<T>, D::Error> + where + D: Deserializer<'de>, + { + #[derive(serde::Deserialize)] + #[serde( + untagged, + bound(deserialize = "DeserializeAsWrap<T, TAs>: Deserialize<'de>") + )] + enum GoodOrError<'a, T, TAs> + where + TAs: DeserializeAs<'a, T>, + { + Good(DeserializeAsWrap<T, TAs>), + // This consumes one "item" when `T` errors while deserializing. + // This is necessary to make this work, when instead of having a direct value + // like integer or string, the deserializer sees a list or map. + Error(IgnoredAny), + #[serde(skip)] + _JustAMarkerForTheLifetime(PhantomData<&'a u32>), + } + + struct SeqVisitor<T, U> { + marker: PhantomData<T>, + marker2: PhantomData<U>, + } + + impl<'de, T, U> Visitor<'de> for SeqVisitor<T, U> + where + U: DeserializeAs<'de, T>, + { + type Value = Vec<T>; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a sequence") + } + + fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> + where + A: SeqAccess<'de>, + { + let mut values = Vec::with_capacity(seq.size_hint().unwrap_or_default()); + + while let Some(value) = seq.next_element()? { + if let GoodOrError::<T, U>::Good(value) = value { + values.push(value.into_inner()); + } + } + Ok(values) + } + } + + let visitor = SeqVisitor::<T, U> { + marker: PhantomData, + marker2: PhantomData, + }; + deserializer.deserialize_seq(visitor) + } +} + +impl<'de, Str> DeserializeAs<'de, Option<Str>> for NoneAsEmptyString +where + Str: FromStr, + Str::Err: Display, +{ + fn deserialize_as<D>(deserializer: D) -> Result<Option<Str>, D::Error> + where + D: Deserializer<'de>, + { + crate::rust::string_empty_as_none::deserialize(deserializer) + } +} + +macro_rules! tuple_seq_as_map_impl_intern { + ($tyorig:ident < (K $(: $($kbound:ident $(+)?)+)?, V $(: $($vbound:ident $(+)?)+)?)>, $ty:ident <KAs, VAs>) => { + #[allow(clippy::implicit_hasher)] + impl<'de, K, KAs, V, VAs> DeserializeAs<'de, $tyorig < (K, V) >> for $ty<KAs, VAs> + where + KAs: DeserializeAs<'de, K>, + VAs: DeserializeAs<'de, V>, + K: $($($kbound +)*)*, + V: $($($vbound +)*)*, + { + fn deserialize_as<D>(deserializer: D) -> Result<$tyorig < (K, V) >, D::Error> + where + D: Deserializer<'de>, + { + struct MapVisitor<K, KAs, V, VAs> { + marker: PhantomData<(K, KAs, V, VAs)>, + } + + impl<'de, K, KAs, V, VAs> Visitor<'de> for MapVisitor<K, KAs, V, VAs> + where + KAs: DeserializeAs<'de, K>, + VAs: DeserializeAs<'de, V>, + K: $($($kbound +)*)*, + V: $($($vbound +)*)*, + { + type Value = $tyorig < (K, V) >; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a map") + } + + #[inline] + fn visit_map<A>(self, access: A) -> Result<Self::Value, A::Error> + where + A: MapAccess<'de>, + { + let iter = utils::MapIter::new(access); + iter.map(|res| { + res.map( + |(k, v): (DeserializeAsWrap<K, KAs>, DeserializeAsWrap<V, VAs>)| { + (k.into_inner(), v.into_inner()) + }, + ) + }) + .collect() + } + } + + let visitor = MapVisitor::<K, KAs, V, VAs> { + marker: PhantomData, + }; + deserializer.deserialize_map(visitor) + } + } + } +} +macro_rules! tuple_seq_as_map_impl { + ($($tyorig:ident < (K $(: $($kbound:ident $(+)?)+)?, V $(: $($vbound:ident $(+)?)+)?)> $(,)?)+) => {$( + tuple_seq_as_map_impl_intern!($tyorig < (K $(: $($kbound +)+)?, V $(: $($vbound +)+)?) >, BTreeMap<KAs, VAs>); + tuple_seq_as_map_impl_intern!($tyorig < (K $(: $($kbound +)+)?, V $(: $($vbound +)+)?) >, HashMap<KAs, VAs>); + )+} +} + +tuple_seq_as_map_impl! { + BinaryHeap<(K: Ord, V: Ord)>, + BTreeSet<(K: Ord, V: Ord)>, + LinkedList<(K, V)>, + Vec<(K, V)>, + VecDeque<(K, V)>, +} +tuple_seq_as_map_impl!(HashSet<(K: Eq + Hash, V: Eq + Hash)>); +#[cfg(feature = "indexmap")] +tuple_seq_as_map_impl!(IndexSet<(K: Eq + Hash, V: Eq + Hash)>); + +macro_rules! tuple_seq_as_map_option_impl { + ($($ty:ident $(,)?)+) => {$( + #[allow(clippy::implicit_hasher)] + impl<'de, K, KAs, V, VAs> DeserializeAs<'de, Option<(K, V)>> for $ty<KAs, VAs> + where + KAs: DeserializeAs<'de, K>, + VAs: DeserializeAs<'de, V>, + { + fn deserialize_as<D>(deserializer: D) -> Result<Option<(K, V)>, D::Error> + where + D: Deserializer<'de>, + { + struct MapVisitor<K, KAs, V, VAs> { + marker: PhantomData<(K, KAs, V, VAs)>, + } + + impl<'de, K, KAs, V, VAs> Visitor<'de> for MapVisitor<K, KAs, V, VAs> + where + KAs: DeserializeAs<'de, K>, + VAs: DeserializeAs<'de, V>, + { + type Value = Option<(K, V)>; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a map of size 1") + } + + #[inline] + fn visit_map<A>(self, access: A) -> Result<Self::Value, A::Error> + where + A: MapAccess<'de>, + { + let iter = utils::MapIter::new(access); + iter.map(|res| { + res.map( + |(k, v): (DeserializeAsWrap<K, KAs>, DeserializeAsWrap<V, VAs>)| { + (k.into_inner(), v.into_inner()) + }, + ) + }) + .next() + .transpose() + } + } + + let visitor = MapVisitor::<K, KAs, V, VAs> { + marker: PhantomData, + }; + deserializer.deserialize_map(visitor) + } + } + )+} +} +tuple_seq_as_map_option_impl!(BTreeMap); +tuple_seq_as_map_option_impl!(HashMap); + +impl<'de, T, TAs> DeserializeAs<'de, T> for DefaultOnError<TAs> +where + TAs: DeserializeAs<'de, T>, + T: Default, +{ + fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error> + where + D: Deserializer<'de>, + { + #[derive(serde::Deserialize)] + #[serde( + untagged, + bound(deserialize = "DeserializeAsWrap<T, TAs>: Deserialize<'de>") + )] + enum GoodOrError<'a, T, TAs> + where + TAs: DeserializeAs<'a, T>, + { + Good(DeserializeAsWrap<T, TAs>), + // This consumes one "item" when `T` errors while deserializing. + // This is necessary to make this work, when instead of having a direct value + // like integer or string, the deserializer sees a list or map. + Error(IgnoredAny), + #[serde(skip)] + _JustAMarkerForTheLifetime(PhantomData<&'a u32>), + } + + Ok(match Deserialize::deserialize(deserializer) { + Ok(GoodOrError::<T, TAs>::Good(res)) => res.into_inner(), + _ => Default::default(), + }) + } +} + +impl<'de> DeserializeAs<'de, Vec<u8>> for BytesOrString { + fn deserialize_as<D>(deserializer: D) -> Result<Vec<u8>, D::Error> + where + D: Deserializer<'de>, + { + crate::rust::bytes_or_string::deserialize(deserializer) + } +} + +impl<'de, SEPARATOR, I, T> DeserializeAs<'de, I> for StringWithSeparator<SEPARATOR, T> +where + SEPARATOR: Separator, + I: FromIterator<T>, + T: FromStr, + T::Err: Display, +{ + fn deserialize_as<D>(deserializer: D) -> Result<I, D::Error> + where + D: Deserializer<'de>, + { + let s = String::deserialize(deserializer)?; + if s.is_empty() { + Ok(None.into_iter().collect()) + } else { + s.split(SEPARATOR::separator()) + .map(FromStr::from_str) + .collect::<Result<_, _>>() + .map_err(Error::custom) + } + } +} + +macro_rules! use_signed_duration { + ( + $main_trait:ident $internal_trait:ident => + { + $ty:ty; $converter:ident => + $({ + $format:ty, $strictness:ty => + $($tbound:ident: $bound:ident $(,)?)* + })* + } + ) => { + $( + impl<'de, $($tbound,)*> DeserializeAs<'de, $ty> for $main_trait<$format, $strictness> + where + $($tbound: $bound,)* + { + fn deserialize_as<D>(deserializer: D) -> Result<$ty, D::Error> + where + D: Deserializer<'de>, + { + let dur: DurationSigned = $internal_trait::<$format, $strictness>::deserialize_as(deserializer)?; + dur.$converter::<D>() + } + } + )* + }; + ( + $( $main_trait:ident $internal_trait:ident, )+ => $rest:tt + ) => { + $( use_signed_duration!($main_trait $internal_trait => $rest); )+ + }; +} + +use_signed_duration!( + DurationSeconds DurationSeconds, + DurationMilliSeconds DurationMilliSeconds, + DurationMicroSeconds DurationMicroSeconds, + DurationNanoSeconds DurationNanoSeconds, + => { + Duration; to_std_duration => + {u64, Strict =>} + {f64, Strict =>} + {String, Strict =>} + {FORMAT, Flexible => FORMAT: Format} + } +); +use_signed_duration!( + DurationSecondsWithFrac DurationSecondsWithFrac, + DurationMilliSecondsWithFrac DurationMilliSecondsWithFrac, + DurationMicroSecondsWithFrac DurationMicroSecondsWithFrac, + DurationNanoSecondsWithFrac DurationNanoSecondsWithFrac, + => { + Duration; to_std_duration => + {f64, Strict =>} + {String, Strict =>} + {FORMAT, Flexible => FORMAT: Format} + } +); + +use_signed_duration!( + TimestampSeconds DurationSeconds, + TimestampMilliSeconds DurationMilliSeconds, + TimestampMicroSeconds DurationMicroSeconds, + TimestampNanoSeconds DurationNanoSeconds, + => { + SystemTime; to_system_time => + {i64, Strict =>} + {f64, Strict =>} + {String, Strict =>} + {FORMAT, Flexible => FORMAT: Format} + } +); +use_signed_duration!( + TimestampSecondsWithFrac DurationSecondsWithFrac, + TimestampMilliSecondsWithFrac DurationMilliSecondsWithFrac, + TimestampMicroSecondsWithFrac DurationMicroSecondsWithFrac, + TimestampNanoSecondsWithFrac DurationNanoSecondsWithFrac, + => { + SystemTime; to_system_time => + {f64, Strict =>} + {String, Strict =>} + {FORMAT, Flexible => FORMAT: Format} + } +); + +impl<'de, T, U> DeserializeAs<'de, T> for DefaultOnNull<U> +where + U: DeserializeAs<'de, T>, + T: Default, +{ + fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error> + where + D: Deserializer<'de>, + { + Ok(Option::<U>::deserialize_as(deserializer)?.unwrap_or_default()) + } +} + +impl<'de> DeserializeAs<'de, &'de [u8]> for Bytes { + fn deserialize_as<D>(deserializer: D) -> Result<&'de [u8], D::Error> + where + D: Deserializer<'de>, + { + <&'de [u8]>::deserialize(deserializer) + } +} + +// serde_bytes implementation for ByteBuf +// https://github.com/serde-rs/bytes/blob/cbae606b9dc225fc094b031cc84eac9493da2058/src/bytebuf.rs#L196 +// +// Implements: +// * visit_seq +// * visit_bytes +// * visit_byte_buf +// * visit_str +// * visit_string +impl<'de> DeserializeAs<'de, Vec<u8>> for Bytes { + fn deserialize_as<D>(deserializer: D) -> Result<Vec<u8>, D::Error> + where + D: Deserializer<'de>, + { + struct VecVisitor; + + impl<'de> Visitor<'de> for VecVisitor { + type Value = Vec<u8>; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a byte array") + } + + fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error> + where + A: SeqAccess<'de>, + { + utils::SeqIter::new(seq).collect::<Result<_, _>>() + } + + fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> + where + E: Error, + { + Ok(v.to_vec()) + } + + fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E> + where + E: Error, + { + Ok(v) + } + + fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> + where + E: Error, + { + Ok(v.as_bytes().to_vec()) + } + + fn visit_string<E>(self, v: String) -> Result<Self::Value, E> + where + E: Error, + { + Ok(v.into_bytes()) + } + } + + deserializer.deserialize_byte_buf(VecVisitor) + } +} + +impl<'de> DeserializeAs<'de, Box<[u8]>> for Bytes { + fn deserialize_as<D>(deserializer: D) -> Result<Box<[u8]>, D::Error> + where + D: Deserializer<'de>, + { + <Bytes as DeserializeAs<'de, Vec<u8>>>::deserialize_as(deserializer) + .map(|vec| vec.into_boxed_slice()) + } +} + +// serde_bytes implementation for Cow<'a, [u8]> +// https://github.com/serde-rs/bytes/blob/cbae606b9dc225fc094b031cc84eac9493da2058/src/de.rs#L77 +// +// Implements: +// * visit_borrowed_bytes +// * visit_borrowed_str +// * visit_bytes +// * visit_str +// * visit_byte_buf +// * visit_string +// * visit_seq +impl<'de> DeserializeAs<'de, Cow<'de, [u8]>> for Bytes { + fn deserialize_as<D>(deserializer: D) -> Result<Cow<'de, [u8]>, D::Error> + where + D: Deserializer<'de>, + { + struct CowVisitor; + + impl<'de> Visitor<'de> for CowVisitor { + type Value = Cow<'de, [u8]>; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a byte array") + } + + fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E> + where + E: Error, + { + Ok(Cow::Borrowed(v)) + } + + fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E> + where + E: Error, + { + Ok(Cow::Borrowed(v.as_bytes())) + } + + fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> + where + E: Error, + { + Ok(Cow::Owned(v.to_vec())) + } + + fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> + where + E: Error, + { + Ok(Cow::Owned(v.as_bytes().to_vec())) + } + + fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E> + where + E: Error, + { + Ok(Cow::Owned(v)) + } + + fn visit_string<E>(self, v: String) -> Result<Self::Value, E> + where + E: Error, + { + Ok(Cow::Owned(v.into_bytes())) + } + + fn visit_seq<V>(self, seq: V) -> Result<Self::Value, V::Error> + where + V: SeqAccess<'de>, + { + Ok(Cow::Owned( + utils::SeqIter::new(seq).collect::<Result<_, _>>()?, + )) + } + } + + deserializer.deserialize_bytes(CowVisitor) + } +} + +impl<'de, T, U, FORMAT> DeserializeAs<'de, Vec<T>> for OneOrMany<U, FORMAT> +where + U: DeserializeAs<'de, T>, + FORMAT: Format, +{ + fn deserialize_as<D>(deserializer: D) -> Result<Vec<T>, D::Error> + where + D: Deserializer<'de>, + { + #[derive(serde::Deserialize)] + #[serde( + untagged, + bound(deserialize = r#"DeserializeAsWrap<T, U>: Deserialize<'de>, + DeserializeAsWrap<Vec<T>, Vec<U>>: Deserialize<'de>"#), + expecting = "a list or single element" + )] + enum Helper<'a, T, U> + where + U: DeserializeAs<'a, T>, + { + One(DeserializeAsWrap<T, U>), + Many(DeserializeAsWrap<Vec<T>, Vec<U>>), + #[serde(skip)] + _JustAMarkerForTheLifetime(PhantomData<&'a u32>), + } + + let h: Helper<'de, T, U> = Deserialize::deserialize(deserializer)?; + match h { + Helper::One(one) => Ok(alloc::vec![one.into_inner()]), + Helper::Many(many) => Ok(many.into_inner()), + Helper::_JustAMarkerForTheLifetime(_) => unreachable!(), + } + } +} + +impl<'de, T, TAs1> DeserializeAs<'de, T> for PickFirst<(TAs1,)> +where + TAs1: DeserializeAs<'de, T>, +{ + fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error> + where + D: Deserializer<'de>, + { + Ok(DeserializeAsWrap::<T, TAs1>::deserialize(deserializer)?.into_inner()) + } +} + +impl<'de, T, TAs1, TAs2> DeserializeAs<'de, T> for PickFirst<(TAs1, TAs2)> +where + TAs1: DeserializeAs<'de, T>, + TAs2: DeserializeAs<'de, T>, +{ + fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error> + where + D: Deserializer<'de>, + { + #[derive(serde::Deserialize)] + #[serde( + untagged, + bound(deserialize = r#" + DeserializeAsWrap<T, TAs1>: Deserialize<'de>, + DeserializeAsWrap<T, TAs2>: Deserialize<'de>, + "#), + expecting = "PickFirst could not deserialize data" + )] + enum Helper<'a, T, TAs1, TAs2> + where + TAs1: DeserializeAs<'a, T>, + TAs2: DeserializeAs<'a, T>, + { + First(DeserializeAsWrap<T, TAs1>), + Second(DeserializeAsWrap<T, TAs2>), + #[serde(skip)] + _JustAMarkerForTheLifetime(PhantomData<&'a u32>), + } + + let h: Helper<'de, T, TAs1, TAs2> = Deserialize::deserialize(deserializer)?; + match h { + Helper::First(first) => Ok(first.into_inner()), + Helper::Second(second) => Ok(second.into_inner()), + Helper::_JustAMarkerForTheLifetime(_) => unreachable!(), + } + } +} + +impl<'de, T, TAs1, TAs2, TAs3> DeserializeAs<'de, T> for PickFirst<(TAs1, TAs2, TAs3)> +where + TAs1: DeserializeAs<'de, T>, + TAs2: DeserializeAs<'de, T>, + TAs3: DeserializeAs<'de, T>, +{ + fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error> + where + D: Deserializer<'de>, + { + #[derive(serde::Deserialize)] + #[serde( + untagged, + bound(deserialize = r#" + DeserializeAsWrap<T, TAs1>: Deserialize<'de>, + DeserializeAsWrap<T, TAs2>: Deserialize<'de>, + DeserializeAsWrap<T, TAs3>: Deserialize<'de>, + "#), + expecting = "PickFirst could not deserialize data" + )] + enum Helper<'a, T, TAs1, TAs2, TAs3> + where + TAs1: DeserializeAs<'a, T>, + TAs2: DeserializeAs<'a, T>, + TAs3: DeserializeAs<'a, T>, + { + First(DeserializeAsWrap<T, TAs1>), + Second(DeserializeAsWrap<T, TAs2>), + Third(DeserializeAsWrap<T, TAs3>), + #[serde(skip)] + _JustAMarkerForTheLifetime(PhantomData<&'a u32>), + } + + let h: Helper<'de, T, TAs1, TAs2, TAs3> = Deserialize::deserialize(deserializer)?; + match h { + Helper::First(first) => Ok(first.into_inner()), + Helper::Second(second) => Ok(second.into_inner()), + Helper::Third(third) => Ok(third.into_inner()), + Helper::_JustAMarkerForTheLifetime(_) => unreachable!(), + } + } +} + +impl<'de, T, TAs1, TAs2, TAs3, TAs4> DeserializeAs<'de, T> for PickFirst<(TAs1, TAs2, TAs3, TAs4)> +where + TAs1: DeserializeAs<'de, T>, + TAs2: DeserializeAs<'de, T>, + TAs3: DeserializeAs<'de, T>, + TAs4: DeserializeAs<'de, T>, +{ + fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error> + where + D: Deserializer<'de>, + { + #[derive(serde::Deserialize)] + #[serde( + untagged, + bound(deserialize = r#" + DeserializeAsWrap<T, TAs1>: Deserialize<'de>, + DeserializeAsWrap<T, TAs2>: Deserialize<'de>, + DeserializeAsWrap<T, TAs3>: Deserialize<'de>, + DeserializeAsWrap<T, TAs4>: Deserialize<'de>, + "#), + expecting = "PickFirst could not deserialize data" + )] + enum Helper<'a, T, TAs1, TAs2, TAs3, TAs4> + where + TAs1: DeserializeAs<'a, T>, + TAs2: DeserializeAs<'a, T>, + TAs3: DeserializeAs<'a, T>, + TAs4: DeserializeAs<'a, T>, + { + First(DeserializeAsWrap<T, TAs1>), + Second(DeserializeAsWrap<T, TAs2>), + Third(DeserializeAsWrap<T, TAs3>), + Forth(DeserializeAsWrap<T, TAs4>), + #[serde(skip)] + _JustAMarkerForTheLifetime(PhantomData<&'a u32>), + } + + let h: Helper<'de, T, TAs1, TAs2, TAs3, TAs4> = Deserialize::deserialize(deserializer)?; + match h { + Helper::First(first) => Ok(first.into_inner()), + Helper::Second(second) => Ok(second.into_inner()), + Helper::Third(third) => Ok(third.into_inner()), + Helper::Forth(forth) => Ok(forth.into_inner()), + Helper::_JustAMarkerForTheLifetime(_) => unreachable!(), + } + } +} + +impl<'de, T, U> DeserializeAs<'de, T> for FromInto<U> +where + U: Into<T>, + U: Deserialize<'de>, +{ + fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error> + where + D: Deserializer<'de>, + { + Ok(U::deserialize(deserializer)?.into()) + } +} + +impl<'de, T, U> DeserializeAs<'de, T> for TryFromInto<U> +where + U: TryInto<T>, + <U as TryInto<T>>::Error: Display, + U: Deserialize<'de>, +{ + fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error> + where + D: Deserializer<'de>, + { + U::deserialize(deserializer)? + .try_into() + .map_err(Error::custom) + } +} + +impl<'de> DeserializeAs<'de, Cow<'de, str>> for BorrowCow { + fn deserialize_as<D>(deserializer: D) -> Result<Cow<'de, str>, D::Error> + where + D: Deserializer<'de>, + { + struct CowVisitor; + + impl<'de> Visitor<'de> for CowVisitor { + type Value = Cow<'de, str>; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("an optionally borrowed string") + } + + fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E> + where + E: Error, + { + Ok(Cow::Borrowed(v)) + } + + fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> + where + E: Error, + { + Ok(Cow::Owned(v.to_owned())) + } + + fn visit_string<E>(self, v: String) -> Result<Self::Value, E> + where + E: Error, + { + Ok(Cow::Owned(v)) + } + } + + deserializer.deserialize_str(CowVisitor) + } +} + +impl<'de> DeserializeAs<'de, Cow<'de, [u8]>> for BorrowCow { + fn deserialize_as<D>(deserializer: D) -> Result<Cow<'de, [u8]>, D::Error> + where + D: Deserializer<'de>, + { + Bytes::deserialize_as(deserializer) + } +} + +impl<'de> DeserializeAs<'de, bool> for BoolFromInt<Strict> { + fn deserialize_as<D>(deserializer: D) -> Result<bool, D::Error> + where + D: Deserializer<'de>, + { + struct U8Visitor; + impl<'de> Visitor<'de> for U8Visitor { + type Value = bool; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("an integer 0 or 1") + } + + fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E> + where + E: Error, + { + match v { + 0 => Ok(false), + 1 => Ok(true), + unexp => Err(Error::invalid_value( + Unexpected::Unsigned(unexp as u64), + &"0 or 1", + )), + } + } + + fn visit_i8<E>(self, v: i8) -> Result<Self::Value, E> + where + E: Error, + { + match v { + 0 => Ok(false), + 1 => Ok(true), + unexp => Err(Error::invalid_value( + Unexpected::Signed(unexp as i64), + &"0 or 1", + )), + } + } + + fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E> + where + E: Error, + { + match v { + 0 => Ok(false), + 1 => Ok(true), + unexp => Err(Error::invalid_value(Unexpected::Unsigned(unexp), &"0 or 1")), + } + } + + fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E> + where + E: Error, + { + match v { + 0 => Ok(false), + 1 => Ok(true), + unexp => Err(Error::invalid_value(Unexpected::Signed(unexp), &"0 or 1")), + } + } + + fn visit_u128<E>(self, v: u128) -> Result<Self::Value, E> + where + E: Error, + { + match v { + 0 => Ok(false), + 1 => Ok(true), + unexp => Err(Error::invalid_value( + Unexpected::Unsigned(unexp as u64), + &"0 or 1", + )), + } + } + + fn visit_i128<E>(self, v: i128) -> Result<Self::Value, E> + where + E: Error, + { + match v { + 0 => Ok(false), + 1 => Ok(true), + unexp => Err(Error::invalid_value( + Unexpected::Unsigned(unexp as u64), + &"0 or 1", + )), + } + } + } + + deserializer.deserialize_u8(U8Visitor) + } +} + +impl<'de> DeserializeAs<'de, bool> for BoolFromInt<Flexible> { + fn deserialize_as<D>(deserializer: D) -> Result<bool, D::Error> + where + D: Deserializer<'de>, + { + struct U8Visitor; + impl<'de> Visitor<'de> for U8Visitor { + type Value = bool; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("an integer") + } + + fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E> + where + E: Error, + { + Ok(v != 0) + } + + fn visit_i8<E>(self, v: i8) -> Result<Self::Value, E> + where + E: Error, + { + Ok(v != 0) + } + + fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E> + where + E: Error, + { + Ok(v != 0) + } + + fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E> + where + E: Error, + { + Ok(v != 0) + } + + fn visit_u128<E>(self, v: u128) -> Result<Self::Value, E> + where + E: Error, + { + Ok(v != 0) + } + + fn visit_i128<E>(self, v: i128) -> Result<Self::Value, E> + where + E: Error, + { + Ok(v != 0) + } + } + + deserializer.deserialize_u8(U8Visitor) + } +} + +// endregion diff --git a/third_party/rust/serde_with/src/de/legacy_arrays.rs b/third_party/rust/serde_with/src/de/legacy_arrays.rs new file mode 100644 index 0000000000..550e444a2f --- /dev/null +++ b/third_party/rust/serde_with/src/de/legacy_arrays.rs @@ -0,0 +1,85 @@ +use super::*; +use core::fmt; +use serde::de::*; + +macro_rules! array_impl { + ($len:literal $($idx:tt)*) => { + impl<'de, T, As> DeserializeAs<'de, [T; $len]> for [As; $len] + where + As: DeserializeAs<'de, T>, + { + fn deserialize_as<D>(deserializer: D) -> Result<[T; $len], D::Error> + where + D: Deserializer<'de>, + { + struct ArrayVisitor<T>(PhantomData<T>); + + impl<'de, T, As> Visitor<'de> + for ArrayVisitor<DeserializeAsWrap<T, As>> + where + As: DeserializeAs<'de, T>, + { + type Value = [T; $len]; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str(concat!("an array of size ", $len)) + } + + #[allow(non_snake_case)] + // Because of 0-size arrays + #[allow(unused_variables, unused_mut)] + fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> + where + A: SeqAccess<'de>, + { + Ok([$( + match seq.next_element::<DeserializeAsWrap<T, As>>()? { + Some(value) => value.into_inner(), + None => return Err(Error::invalid_length($idx, &self)), + }, + )*]) + } + } + + deserializer.deserialize_tuple( + $len, + ArrayVisitor::<DeserializeAsWrap<T, As>>(PhantomData), + ) + } + } + }; +} + +array_impl!(0); +array_impl!(1 0); +array_impl!(2 0 1); +array_impl!(3 0 1 2); +array_impl!(4 0 1 2 3); +array_impl!(5 0 1 2 3 4); +array_impl!(6 0 1 2 3 4 5); +array_impl!(7 0 1 2 3 4 5 6); +array_impl!(8 0 1 2 3 4 5 6 7); +array_impl!(9 0 1 2 3 4 5 6 7 8); +array_impl!(10 0 1 2 3 4 5 6 7 8 9); +array_impl!(11 0 1 2 3 4 5 6 7 8 9 10); +array_impl!(12 0 1 2 3 4 5 6 7 8 9 10 11); +array_impl!(13 0 1 2 3 4 5 6 7 8 9 10 11 12); +array_impl!(14 0 1 2 3 4 5 6 7 8 9 10 11 12 13); +array_impl!(15 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14); +array_impl!(16 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15); +array_impl!(17 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16); +array_impl!(18 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17); +array_impl!(19 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18); +array_impl!(20 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19); +array_impl!(21 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20); +array_impl!(22 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21); +array_impl!(23 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22); +array_impl!(24 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23); +array_impl!(25 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24); +array_impl!(26 0 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); +array_impl!(27 0 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); +array_impl!(28 0 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); +array_impl!(29 0 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); +array_impl!(30 0 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); +array_impl!(31 0 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); +array_impl!(32 0 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); diff --git a/third_party/rust/serde_with/src/de/mod.rs b/third_party/rust/serde_with/src/de/mod.rs new file mode 100644 index 0000000000..518b4f2606 --- /dev/null +++ b/third_party/rust/serde_with/src/de/mod.rs @@ -0,0 +1,143 @@ +//! Module for [`DeserializeAs`][] implementations +//! +//! The module contains the [`DeserializeAs`][] trait and helper code. +//! Additionally, it contains implementations of [`DeserializeAs`][] for types defined in the Rust Standard Library or this crate. +//! +//! You can find more details on how to implement this trait for your types in the documentation of the [`DeserializeAs`][] trait and details about the usage in the [user guide][]. +//! +//! [user guide]: crate::guide + +mod const_arrays; +mod impls; + +use super::*; + +/// A **data structure** that can be deserialized from any data format supported by Serde, analogue to [`Deserialize`]. +/// +/// The trait is analogue to the [`serde::Deserialize`][`Deserialize`] trait, with the same meaning of input and output arguments. +/// It can and should the implemented using the same code structure as the [`Deserialize`] trait. +/// As such, the same advice for [implementing `Deserialize`][impl-deserialize] applies here. +/// +/// # Differences to [`Deserialize`] +/// +/// The trait is only required for container-like types or types implementing specific conversion functions. +/// Container-like types are [`Vec`], [`BTreeMap`], but also [`Option`] and [`Box`]. +/// Conversion types deserialize into a different Rust type. +/// For example, [`DisplayFromStr`] uses the [`FromStr`] trait after deserializing a string and [`DurationSeconds`] creates a [`Duration`] from either String or integer values. +/// +/// This code shows how to implement [`Deserialize`] for [`Box`]: +/// +/// ```rust,ignore +/// impl<'de, T: Deserialize<'de>> Deserialize<'de> for Box<T> { +/// fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> +/// where +/// D: Deserializer<'de>, +/// { +/// Ok(Box::new(Deserialize::deserialize(deserializer)?)) +/// } +/// } +/// ``` +/// +/// and this code shows how to do the same using [`DeserializeAs`][]: +/// +/// ```rust,ignore +/// impl<'de, T, U> DeserializeAs<'de, Box<T>> for Box<U> +/// where +/// U: DeserializeAs<'de, T>, +/// { +/// fn deserialize_as<D>(deserializer: D) -> Result<Box<T>, D::Error> +/// where +/// D: Deserializer<'de>, +/// { +/// Ok(Box::new( +/// DeserializeAsWrap::<T, U>::deserialize(deserializer)?.into_inner(), +/// )) +/// } +/// } +/// ``` +/// +/// It uses two type parameters, `T` and `U` instead of only one and performs the deserialization step using the `DeserializeAsWrap` type. +/// The `T` type is the on the Rust side after deserialization, whereas the `U` type determines how the value will be deserialized. +/// These two changes are usually enough to make a container type implement [`DeserializeAs`][]. +/// +/// +/// [`DeserializeAsWrap`] is a piece of glue code which turns [`DeserializeAs`] into a serde compatible datatype, by converting all calls to `deserialize` into `deserialize_as`. +/// This allows us to implement [`DeserializeAs`] such that it can be applied recursively throughout the whole data structure. +/// This is mostly important for container types, such as `Vec` or `BTreeMap`. +/// In a `BTreeMap` this allows us to specify two different serialization behaviors, one for key and one for value, using the [`DeserializeAs`] trait. +/// +/// ## Implementing a converter Type +/// +/// This shows a simplified implementation for [`DisplayFromStr`]. +/// +/// ```rust +/// # #[cfg(all(feature = "macros"))] { +/// # use serde::Deserialize; +/// # use serde::de::Error; +/// # use serde_with::DeserializeAs; +/// # use std::str::FromStr; +/// # use std::fmt::Display; +/// struct DisplayFromStr; +/// +/// impl<'de, T> DeserializeAs<'de, T> for DisplayFromStr +/// where +/// T: FromStr, +/// T::Err: Display, +/// { +/// fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error> +/// where +/// D: serde::Deserializer<'de>, +/// { +/// let s = String::deserialize(deserializer).map_err(Error::custom)?; +/// s.parse().map_err(Error::custom) +/// } +/// } +/// # +/// # #[serde_with::serde_as] +/// # #[derive(serde::Deserialize)] +/// # struct S (#[serde_as(as = "DisplayFromStr")] bool); +/// # +/// # assert_eq!(false, serde_json::from_str::<S>(r#""false""#).unwrap().0); +/// # } +/// ``` +/// [`Box`]: std::boxed::Box +/// [`BTreeMap`]: std::collections::BTreeMap +/// [`Duration`]: std::time::Duration +/// [`FromStr`]: std::str::FromStr +/// [`Vec`]: std::vec::Vec +/// [impl-deserialize]: https://serde.rs/impl-deserialize.html +pub trait DeserializeAs<'de, T>: Sized { + /// Deserialize this value from the given Serde deserializer. + fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error> + where + D: Deserializer<'de>; +} + +/// Helper type to implement [`DeserializeAs`] for container-like types. +#[derive(Debug)] +pub struct DeserializeAsWrap<T, U> { + value: T, + marker: PhantomData<U>, +} + +impl<T, U> DeserializeAsWrap<T, U> { + /// Return the inner value of type `T`. + pub fn into_inner(self) -> T { + self.value + } +} + +impl<'de, T, U> Deserialize<'de> for DeserializeAsWrap<T, U> +where + U: DeserializeAs<'de, T>, +{ + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> + where + D: Deserializer<'de>, + { + U::deserialize_as(deserializer).map(|value| Self { + value, + marker: PhantomData, + }) + } +} diff --git a/third_party/rust/serde_with/src/duplicate_key_impls/error_on_duplicate.rs b/third_party/rust/serde_with/src/duplicate_key_impls/error_on_duplicate.rs new file mode 100644 index 0000000000..9370f5fd51 --- /dev/null +++ b/third_party/rust/serde_with/src/duplicate_key_impls/error_on_duplicate.rs @@ -0,0 +1,127 @@ +use alloc::collections::{BTreeMap, BTreeSet}; +use core::hash::{BuildHasher, Hash}; +#[cfg(feature = "indexmap")] +use indexmap_crate::{IndexMap, IndexSet}; +use std::collections::{HashMap, HashSet}; + +pub trait PreventDuplicateInsertsSet<T> { + fn new(size_hint: Option<usize>) -> Self; + + /// Return true if the insert was successful and the value did not exist in the set + fn insert(&mut self, value: T) -> bool; +} + +pub trait PreventDuplicateInsertsMap<K, V> { + fn new(size_hint: Option<usize>) -> Self; + + /// Return true if the insert was successful and the key did not exist in the map + fn insert(&mut self, key: K, value: V) -> bool; +} + +impl<T, S> PreventDuplicateInsertsSet<T> for HashSet<T, S> +where + T: Eq + Hash, + S: BuildHasher + Default, +{ + #[inline] + fn new(size_hint: Option<usize>) -> Self { + match size_hint { + Some(size) => Self::with_capacity_and_hasher(size, S::default()), + None => Self::with_hasher(S::default()), + } + } + + #[inline] + fn insert(&mut self, value: T) -> bool { + self.insert(value) + } +} + +#[cfg(feature = "indexmap")] +impl<T, S> PreventDuplicateInsertsSet<T> for IndexSet<T, S> +where + T: Eq + Hash, + S: BuildHasher + Default, +{ + #[inline] + fn new(size_hint: Option<usize>) -> Self { + match size_hint { + Some(size) => Self::with_capacity_and_hasher(size, S::default()), + None => Self::with_hasher(S::default()), + } + } + + #[inline] + fn insert(&mut self, value: T) -> bool { + self.insert(value) + } +} + +impl<T> PreventDuplicateInsertsSet<T> for BTreeSet<T> +where + T: Ord, +{ + #[inline] + fn new(_size_hint: Option<usize>) -> Self { + Self::new() + } + + #[inline] + fn insert(&mut self, value: T) -> bool { + self.insert(value) + } +} + +impl<K, V, S> PreventDuplicateInsertsMap<K, V> for HashMap<K, V, S> +where + K: Eq + Hash, + S: BuildHasher + Default, +{ + #[inline] + fn new(size_hint: Option<usize>) -> Self { + match size_hint { + Some(size) => Self::with_capacity_and_hasher(size, S::default()), + None => Self::with_hasher(S::default()), + } + } + + #[inline] + fn insert(&mut self, key: K, value: V) -> bool { + self.insert(key, value).is_none() + } +} + +#[cfg(feature = "indexmap")] +impl<K, V, S> PreventDuplicateInsertsMap<K, V> for IndexMap<K, V, S> +where + K: Eq + Hash, + S: BuildHasher + Default, +{ + #[inline] + fn new(size_hint: Option<usize>) -> Self { + match size_hint { + Some(size) => Self::with_capacity_and_hasher(size, S::default()), + None => Self::with_hasher(S::default()), + } + } + + #[inline] + fn insert(&mut self, key: K, value: V) -> bool { + self.insert(key, value).is_none() + } +} + +impl<K, V> PreventDuplicateInsertsMap<K, V> for BTreeMap<K, V> +where + K: Ord, +{ + #[inline] + fn new(_size_hint: Option<usize>) -> Self { + Self::new() + } + + #[inline] + fn insert(&mut self, key: K, value: V) -> bool { + self.insert(key, value).is_none() + } +} diff --git a/third_party/rust/serde_with/src/duplicate_key_impls/first_value_wins.rs b/third_party/rust/serde_with/src/duplicate_key_impls/first_value_wins.rs new file mode 100644 index 0000000000..faf922b8c2 --- /dev/null +++ b/third_party/rust/serde_with/src/duplicate_key_impls/first_value_wins.rs @@ -0,0 +1,136 @@ +use alloc::collections::{BTreeMap, BTreeSet}; +use core::hash::{BuildHasher, Hash}; +#[cfg(feature = "indexmap")] +use indexmap_crate::IndexMap; +use std::collections::{HashMap, HashSet}; + +#[deprecated = "This is serde's default behavior."] +pub trait DuplicateInsertsFirstWinsSet<T> { + fn new(size_hint: Option<usize>) -> Self; + + /// Insert the value into the set, if there is not already an existing value + fn insert(&mut self, value: T); +} + +pub trait DuplicateInsertsFirstWinsMap<K, V> { + fn new(size_hint: Option<usize>) -> Self; + + /// Insert the value into the map, if there is not already an existing value + fn insert(&mut self, key: K, value: V); +} + +#[allow(deprecated)] +impl<T, S> DuplicateInsertsFirstWinsSet<T> for HashSet<T, S> +where + T: Eq + Hash, + S: BuildHasher + Default, +{ + #[inline] + fn new(size_hint: Option<usize>) -> Self { + match size_hint { + Some(size) => Self::with_capacity_and_hasher(size, S::default()), + None => Self::with_hasher(S::default()), + } + } + + #[inline] + fn insert(&mut self, value: T) { + // Hashset already fulfils the contract and always keeps the first value + self.insert(value); + } +} + +#[allow(deprecated)] +impl<T> DuplicateInsertsFirstWinsSet<T> for BTreeSet<T> +where + T: Ord, +{ + #[inline] + fn new(_size_hint: Option<usize>) -> Self { + Self::new() + } + + #[inline] + fn insert(&mut self, value: T) { + // BTreeSet already fulfils the contract and always keeps the first value + self.insert(value); + } +} + +impl<K, V, S> DuplicateInsertsFirstWinsMap<K, V> for HashMap<K, V, S> +where + K: Eq + Hash, + S: BuildHasher + Default, +{ + #[inline] + fn new(size_hint: Option<usize>) -> Self { + match size_hint { + Some(size) => Self::with_capacity_and_hasher(size, S::default()), + None => Self::with_hasher(S::default()), + } + } + + #[inline] + fn insert(&mut self, key: K, value: V) { + use std::collections::hash_map::Entry; + + match self.entry(key) { + // we want to keep the first value, so do nothing + Entry::Occupied(_) => {} + Entry::Vacant(vacant) => { + vacant.insert(value); + } + } + } +} + +#[cfg(feature = "indexmap")] +impl<K, V, S> DuplicateInsertsFirstWinsMap<K, V> for IndexMap<K, V, S> +where + K: Eq + Hash, + S: BuildHasher + Default, +{ + #[inline] + fn new(size_hint: Option<usize>) -> Self { + match size_hint { + Some(size) => Self::with_capacity_and_hasher(size, S::default()), + None => Self::with_hasher(S::default()), + } + } + + #[inline] + fn insert(&mut self, key: K, value: V) { + use indexmap_crate::map::Entry; + + match self.entry(key) { + // we want to keep the first value, so do nothing + Entry::Occupied(_) => {} + Entry::Vacant(vacant) => { + vacant.insert(value); + } + } + } +} + +impl<K, V> DuplicateInsertsFirstWinsMap<K, V> for BTreeMap<K, V> +where + K: Ord, +{ + #[inline] + fn new(_size_hint: Option<usize>) -> Self { + Self::new() + } + + #[inline] + fn insert(&mut self, key: K, value: V) { + use alloc::collections::btree_map::Entry; + + match self.entry(key) { + // we want to keep the first value, so do nothing + Entry::Occupied(_) => {} + Entry::Vacant(vacant) => { + vacant.insert(value); + } + } + } +} diff --git a/third_party/rust/serde_with/src/duplicate_key_impls/last_value_wins.rs b/third_party/rust/serde_with/src/duplicate_key_impls/last_value_wins.rs new file mode 100644 index 0000000000..3eeeaac079 --- /dev/null +++ b/third_party/rust/serde_with/src/duplicate_key_impls/last_value_wins.rs @@ -0,0 +1,69 @@ +use alloc::collections::BTreeSet; +use core::hash::{BuildHasher, Hash}; +#[cfg(feature = "indexmap")] +use indexmap_crate::IndexSet; +use std::collections::HashSet; + +pub trait DuplicateInsertsLastWinsSet<T> { + fn new(size_hint: Option<usize>) -> Self; + + /// Insert or replace the existing value + fn replace(&mut self, value: T); +} + +impl<T, S> DuplicateInsertsLastWinsSet<T> for HashSet<T, S> +where + T: Eq + Hash, + S: BuildHasher + Default, +{ + #[inline] + fn new(size_hint: Option<usize>) -> Self { + match size_hint { + Some(size) => Self::with_capacity_and_hasher(size, S::default()), + None => Self::with_hasher(S::default()), + } + } + + #[inline] + fn replace(&mut self, value: T) { + // Hashset already fulfils the contract + self.replace(value); + } +} + +#[cfg(feature = "indexmap")] +impl<T, S> DuplicateInsertsLastWinsSet<T> for IndexSet<T, S> +where + T: Eq + Hash, + S: BuildHasher + Default, +{ + #[inline] + fn new(size_hint: Option<usize>) -> Self { + match size_hint { + Some(size) => Self::with_capacity_and_hasher(size, S::default()), + None => Self::with_hasher(S::default()), + } + } + + #[inline] + fn replace(&mut self, value: T) { + // Hashset already fulfils the contract + self.replace(value); + } +} + +impl<T> DuplicateInsertsLastWinsSet<T> for BTreeSet<T> +where + T: Ord, +{ + #[inline] + fn new(_size_hint: Option<usize>) -> Self { + Self::new() + } + + #[inline] + fn replace(&mut self, value: T) { + // BTreeSet already fulfils the contract + self.replace(value); + } +} diff --git a/third_party/rust/serde_with/src/duplicate_key_impls/mod.rs b/third_party/rust/serde_with/src/duplicate_key_impls/mod.rs new file mode 100644 index 0000000000..6080812232 --- /dev/null +++ b/third_party/rust/serde_with/src/duplicate_key_impls/mod.rs @@ -0,0 +1,10 @@ +mod error_on_duplicate; +mod first_value_wins; +mod last_value_wins; + +#[allow(deprecated)] +pub use self::{ + error_on_duplicate::{PreventDuplicateInsertsMap, PreventDuplicateInsertsSet}, + first_value_wins::{DuplicateInsertsFirstWinsMap, DuplicateInsertsFirstWinsSet}, + last_value_wins::DuplicateInsertsLastWinsSet, +}; diff --git a/third_party/rust/serde_with/src/enum_map.rs b/third_party/rust/serde_with/src/enum_map.rs new file mode 100644 index 0000000000..ef65f8985f --- /dev/null +++ b/third_party/rust/serde_with/src/enum_map.rs @@ -0,0 +1,888 @@ +use crate::{ + content::ser::{Content, ContentSerializer}, + DeserializeAs, SerializeAs, +}; +use alloc::{string::ToString, vec::Vec}; +use core::{fmt, marker::PhantomData}; +use serde::{ + de::{DeserializeSeed, EnumAccess, Error, MapAccess, SeqAccess, VariantAccess, Visitor}, + ser, + ser::{Impossible, SerializeMap, SerializeSeq, SerializeStructVariant, SerializeTupleVariant}, + Deserialize, Deserializer, Serialize, Serializer, +}; + +/// Represent a list of enum values as a map. +/// +/// This **only works** if the enum uses the default *externally tagged* representation. +/// Other enum representations are not supported. +/// +/// serde data formats often represent *externally tagged* enums as maps with a single key. +/// The key is the enum variant name, and the value is the variant value. +/// Sometimes a map with multiple keys should be treated like a list of enum values. +/// +/// # Examples +/// +/// ## JSON Map with multiple keys +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// use serde_with::EnumMap; +/// +/// # #[derive(Debug, Clone, PartialEq, Eq)] +/// #[derive(Serialize, Deserialize)] +/// enum EnumValue { +/// Int(i32), +/// String(String), +/// Unit, +/// Tuple(i32, String, bool), +/// Struct { +/// a: i32, +/// b: String, +/// c: bool, +/// }, +/// } +/// +/// #[serde_with::serde_as] +/// # #[derive(Debug, Clone, PartialEq, Eq)] +/// #[derive(Serialize, Deserialize)] +/// struct VecEnumValues ( +/// #[serde_as(as = "EnumMap")] +/// Vec<EnumValue>, +/// ); +/// +/// // --- +/// +/// // This will serialize this list of values +/// let values = VecEnumValues(vec![ +/// EnumValue::Int(123), +/// EnumValue::String("FooBar".to_string()), +/// EnumValue::Int(456), +/// EnumValue::String("XXX".to_string()), +/// EnumValue::Unit, +/// EnumValue::Tuple(1, "Middle".to_string(), false), +/// EnumValue::Struct { +/// a: 666, +/// b: "BBB".to_string(), +/// c: true, +/// }, +/// ]); +/// +/// // into this JSON map +/// // Duplicate keys are emitted for identical enum variants. +/// let expected = +/// r#"{ +/// "Int": 123, +/// "String": "FooBar", +/// "Int": 456, +/// "String": "XXX", +/// "Unit": null, +/// "Tuple": [ +/// 1, +/// "Middle", +/// false +/// ], +/// "Struct": { +/// "a": 666, +/// "b": "BBB", +/// "c": true +/// } +/// }"#; +/// +/// // Both serialization and deserialization work flawlessly. +/// let serialized = serde_json::to_string_pretty(&values).unwrap(); +/// assert_eq!(expected, serialized); +/// let deserialized: VecEnumValues = serde_json::from_str(&serialized).unwrap(); +/// assert_eq!(values, deserialized); +/// # } +/// ``` +/// +/// ## XML structure with varying keys +/// +/// With `serde_xml_rs` tuple and struct variants are not supported since they fail to roundtrip. +/// The enum may have such variants as long as they are not serialized or deserialized. +/// +/// ``` +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// use serde_with::EnumMap; +/// +/// # #[derive(Debug, Clone, PartialEq, Eq)] +/// #[derive(Serialize, Deserialize)] +/// enum EnumValue { +/// Int(i32), +/// String(String), +/// Unit, +/// } +/// +/// #[serde_with::serde_as] +/// # #[derive(Debug, Clone, PartialEq, Eq)] +/// #[derive(Serialize, Deserialize)] +/// struct VecEnumValues { +/// #[serde_as(as = "EnumMap")] +/// vec: Vec<EnumValue>, +/// } +/// +/// // --- +/// +/// // This will serialize this list of values +/// let values = VecEnumValues { +/// vec: vec![ +/// EnumValue::Int(123), +/// EnumValue::String("FooBar".to_string()), +/// EnumValue::Int(456), +/// EnumValue::String("XXX".to_string()), +/// EnumValue::Unit, +/// ], +/// }; +/// +/// // into this XML document +/// // Duplicate keys are emitted for identical enum variants. +/// let expected = r#" +/// <VecEnumValues> +/// <vec> +/// <Int>123</Int> +/// <String>FooBar</String> +/// <Int>456</Int> +/// <String>XXX</String> +/// <Unit></Unit> +/// </vec> +/// </VecEnumValues>"# +/// // Remove whitespace +/// .replace(' ', "") +/// .replace('\n', ""); +/// +/// // Both serialization and deserialization work flawlessly. +/// let serialized = serde_xml_rs::to_string(&values).unwrap(); +/// assert_eq!(expected, serialized); +/// let deserialized: VecEnumValues = serde_xml_rs::from_str(&serialized).unwrap(); +/// assert_eq!(values, deserialized); +/// # } +/// ``` +#[derive(Debug, Copy, Clone)] +pub struct EnumMap; + +impl<T> SerializeAs<Vec<T>> for EnumMap +where + T: Serialize, +{ + fn serialize_as<S>(source: &Vec<T>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + source.serialize(SeqAsMapSerializer(serializer)) + } +} + +impl<'de, T> DeserializeAs<'de, Vec<T>> for EnumMap +where + T: Deserialize<'de>, +{ + fn deserialize_as<D>(deserializer: D) -> Result<Vec<T>, D::Error> + where + D: Deserializer<'de>, + { + struct EnumMapVisitor<T>(PhantomData<T>); + + impl<'de, T> Visitor<'de> for EnumMapVisitor<T> + where + T: Deserialize<'de>, + { + type Value = Vec<T>; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(formatter, "a map or enum values") + } + + fn visit_map<A: MapAccess<'de>>(self, map: A) -> Result<Self::Value, A::Error> { + Vec::deserialize(SeqDeserializer(map)) + } + } + + deserializer.deserialize_map(EnumMapVisitor(PhantomData)) + } +} + +static END_OF_MAP_IDENTIFIER: &str = "__PRIVATE_END_OF_MAP_MARKER__"; + +// Serialization code below here + +/// Convert a sequence to a map during serialization. +/// +/// Only `serialize_seq` is implemented and forwarded to `serialize_map` on the inner `Serializer`. +/// The elements are serialized with [`SerializeSeqElement`]. +struct SeqAsMapSerializer<S>(S); + +impl<S> Serializer for SeqAsMapSerializer<S> +where + S: Serializer, +{ + type Ok = S::Ok; + type Error = S::Error; + + type SerializeSeq = SerializeSeqElement<S::SerializeMap>; + type SerializeTuple = Impossible<S::Ok, S::Error>; + type SerializeTupleStruct = Impossible<S::Ok, S::Error>; + type SerializeTupleVariant = Impossible<S::Ok, S::Error>; + type SerializeMap = Impossible<S::Ok, S::Error>; + type SerializeStruct = Impossible<S::Ok, S::Error>; + type SerializeStructVariant = Impossible<S::Ok, S::Error>; + + fn serialize_bool(self, _v: bool) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_i8(self, _v: i8) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_i16(self, _v: i16) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_i32(self, _v: i32) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_i64(self, _v: i64) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_i128(self, _v: i128) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_u8(self, _v: u8) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_u16(self, _v: u16) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_u32(self, _v: u32) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_u64(self, _v: u64) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_u128(self, _v: u128) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_f32(self, _v: f32) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_f64(self, _v: f64) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_char(self, _v: char) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_str(self, _v: &str) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_none(self) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_some<T: ?Sized>(self, _value: &T) -> Result<Self::Ok, Self::Error> + where + T: Serialize, + { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_unit(self) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_unit_variant( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + ) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_newtype_struct<T: ?Sized>( + self, + _name: &'static str, + _value: &T, + ) -> Result<Self::Ok, Self::Error> + where + T: Serialize, + { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_newtype_variant<T: ?Sized>( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + _value: &T, + ) -> Result<Self::Ok, Self::Error> + where + T: Serialize, + { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { + let is_human_readable = self.0.is_human_readable(); + self.0 + .serialize_map(len) + .map(|delegate| SerializeSeqElement { + delegate, + is_human_readable, + }) + } + + fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_tuple_struct( + self, + _name: &'static str, + _len: usize, + ) -> Result<Self::SerializeTupleStruct, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_tuple_variant( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + _len: usize, + ) -> Result<Self::SerializeTupleVariant, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_struct( + self, + _name: &'static str, + _len: usize, + ) -> Result<Self::SerializeStruct, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_struct_variant( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + _len: usize, + ) -> Result<Self::SerializeStructVariant, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } +} + +/// Serialize a single element but turn the sequence into a map logic. +/// +/// It uses [`SerializeEnumAsMapElement`] for the map element serialization. +/// +/// The [`Serializer`] implementation handles all the `serialize_*_variant` functions and defers to [`SerializeVariant`] for the more complicated tuple and struct variants. +struct SerializeSeqElement<M> { + delegate: M, + is_human_readable: bool, +} + +impl<M> SerializeSeq for SerializeSeqElement<M> +where + M: SerializeMap, +{ + type Ok = M::Ok; + type Error = M::Error; + + fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error> + where + T: Serialize, + { + value.serialize(EnumAsMapElementSerializer { + delegate: &mut self.delegate, + is_human_readable: self.is_human_readable, + })?; + Ok(()) + } + + fn end(self) -> Result<Self::Ok, Self::Error> { + self.delegate.end() + } +} + +struct EnumAsMapElementSerializer<'a, M> { + delegate: &'a mut M, + is_human_readable: bool, +} + +impl<'a, M> Serializer for EnumAsMapElementSerializer<'a, M> +where + M: SerializeMap, +{ + type Ok = (); + type Error = M::Error; + + type SerializeSeq = Impossible<Self::Ok, Self::Error>; + type SerializeTuple = Impossible<Self::Ok, Self::Error>; + type SerializeTupleStruct = Impossible<Self::Ok, Self::Error>; + type SerializeTupleVariant = SerializeVariant<'a, M>; + type SerializeMap = Impossible<Self::Ok, Self::Error>; + type SerializeStruct = Impossible<Self::Ok, Self::Error>; + type SerializeStructVariant = SerializeVariant<'a, M>; + + fn serialize_bool(self, _v: bool) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_i8(self, _v: i8) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_i16(self, _v: i16) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_i32(self, _v: i32) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_i64(self, _v: i64) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_i128(self, _v: i128) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_u8(self, _v: u8) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_u16(self, _v: u16) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_u32(self, _v: u32) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_u64(self, _v: u64) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_u128(self, _v: u128) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_f32(self, _v: f32) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_f64(self, _v: f64) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_char(self, _v: char) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_str(self, _v: &str) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_none(self) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_some<T: ?Sized>(self, _value: &T) -> Result<Self::Ok, Self::Error> + where + T: Serialize, + { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_unit(self) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_unit_variant( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + ) -> Result<Self::Ok, Self::Error> { + self.delegate.serialize_entry(variant, &())?; + Ok(()) + } + + fn serialize_newtype_struct<T: ?Sized>( + self, + _name: &'static str, + _value: &T, + ) -> Result<Self::Ok, Self::Error> + where + T: Serialize, + { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_newtype_variant<T: ?Sized>( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + value: &T, + ) -> Result<Self::Ok, Self::Error> + where + T: Serialize, + { + self.delegate.serialize_entry(variant, value)?; + Ok(()) + } + + fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_tuple_struct( + self, + _name: &'static str, + _len: usize, + ) -> Result<Self::SerializeTupleStruct, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_tuple_variant( + self, + name: &'static str, + _variant_index: u32, + variant: &'static str, + len: usize, + ) -> Result<Self::SerializeTupleVariant, Self::Error> { + Ok(SerializeVariant { + delegate: self.delegate, + is_human_readable: self.is_human_readable, + variant, + content: Content::TupleStruct(name, Vec::with_capacity(len)), + }) + } + + fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_struct( + self, + _name: &'static str, + _len: usize, + ) -> Result<Self::SerializeStruct, Self::Error> { + Err(ser::Error::custom("wrong type for EnumMap")) + } + + fn serialize_struct_variant( + self, + name: &'static str, + _variant_index: u32, + variant: &'static str, + len: usize, + ) -> Result<Self::SerializeStructVariant, Self::Error> { + Ok(SerializeVariant { + delegate: self.delegate, + is_human_readable: self.is_human_readable, + variant, + content: Content::Struct(name, Vec::with_capacity(len)), + }) + } +} + +/// Serialize a struct or tuple variant enum as a map element +/// +/// [`SerializeStructVariant`] serializes a struct variant, and [`SerializeTupleVariant`] a tuple variant. +struct SerializeVariant<'a, M> { + delegate: &'a mut M, + is_human_readable: bool, + variant: &'static str, + content: Content, +} + +impl<'a, M> SerializeStructVariant for SerializeVariant<'a, M> +where + M: SerializeMap, +{ + type Ok = (); + + type Error = M::Error; + + fn serialize_field<T: ?Sized>( + &mut self, + key: &'static str, + value: &T, + ) -> Result<(), Self::Error> + where + T: Serialize, + { + // Serialize to a Content type first + let value: Content = value.serialize(ContentSerializer::new(self.is_human_readable))?; + if let Content::Struct(_name, fields) = &mut self.content { + fields.push((key, value)); + } + Ok(()) + } + + fn end(self) -> Result<Self::Ok, Self::Error> { + self.delegate.serialize_entry(&self.variant, &self.content) + } +} + +impl<'a, M> SerializeTupleVariant for SerializeVariant<'a, M> +where + M: SerializeMap, +{ + type Ok = (); + + type Error = M::Error; + + fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error> + where + T: Serialize, + { + // Serialize to a Content type first + let value: Content = value.serialize(ContentSerializer::new(self.is_human_readable))?; + if let Content::TupleStruct(_name, fields) = &mut self.content { + fields.push(value); + } + Ok(()) + } + + fn end(self) -> Result<Self::Ok, Self::Error> { + self.delegate.serialize_entry(&self.variant, &self.content) + } +} + +// Below is deserialization code + +/// Deserialize the sequence of enum instances. +/// +/// The main [`Deserializer`] implementation handles the outer sequence (e.g., `Vec`), while the [`SeqAccess`] implementation is responsible for the inner elements. +struct SeqDeserializer<M>(M); + +impl<'de, M> Deserializer<'de> for SeqDeserializer<M> +where + M: MapAccess<'de>, +{ + type Error = M::Error; + + fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error> + where + V: Visitor<'de>, + { + visitor.visit_seq(self) + } + + fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> + where + V: Visitor<'de>, + { + self.deserialize_seq(visitor) + } + + serde::forward_to_deserialize_any! { + bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string + bytes byte_buf option unit unit_struct newtype_struct tuple + tuple_struct map struct enum identifier ignored_any + } +} + +impl<'de, M> SeqAccess<'de> for SeqDeserializer<M> +where + M: MapAccess<'de>, +{ + type Error = M::Error; + + fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> + where + T: DeserializeSeed<'de>, + { + match seed.deserialize(EnumDeserializer(&mut self.0)) { + Ok(value) => Ok(Some(value)), + Err(err) => { + // Unfortunately we loose the optional aspect of MapAccess, so we need to special case an error value to mark the end of the map. + if err.to_string().contains(END_OF_MAP_IDENTIFIER) { + Ok(None) + } else { + Err(err) + } + } + } + } + + fn size_hint(&self) -> Option<usize> { + self.0.size_hint() + } +} + +/// Deserialize an enum from a map element +/// +/// The [`Deserializer`] implementation is the starting point, which first calls the [`EnumAccess`] methods. +/// The [`EnumAccess`] is used to deserialize the enum variant type of the enum. +/// The [`VariantAccess`] is used to deserialize the value part of the enum. +struct EnumDeserializer<M>(M); + +impl<'de, M> Deserializer<'de> for EnumDeserializer<M> +where + M: MapAccess<'de>, +{ + type Error = M::Error; + + fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> + where + V: Visitor<'de>, + { + self.deserialize_enum("", &[], visitor) + } + + fn deserialize_enum<V>( + self, + _name: &'static str, + _variants: &'static [&'static str], + visitor: V, + ) -> Result<V::Value, Self::Error> + where + V: Visitor<'de>, + { + visitor.visit_enum(self) + } + + serde::forward_to_deserialize_any! { + bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string + bytes byte_buf option unit unit_struct newtype_struct seq tuple + tuple_struct map struct identifier ignored_any + } +} + +impl<'de, M> EnumAccess<'de> for EnumDeserializer<M> +where + M: MapAccess<'de>, +{ + type Error = M::Error; + type Variant = Self; + + fn variant_seed<T>(mut self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> + where + T: DeserializeSeed<'de>, + { + match self.0.next_key_seed(seed)? { + Some(key) => Ok((key, self)), + + // Unfortunately we loose the optional aspect of MapAccess, so we need to special case an error value to mark the end of the map. + None => Err(Error::custom(END_OF_MAP_IDENTIFIER)), + } + } +} + +impl<'de, M> VariantAccess<'de> for EnumDeserializer<M> +where + M: MapAccess<'de>, +{ + type Error = M::Error; + + fn unit_variant(mut self) -> Result<(), Self::Error> { + self.0.next_value() + } + + fn newtype_variant_seed<T>(mut self, seed: T) -> Result<T::Value, Self::Error> + where + T: DeserializeSeed<'de>, + { + self.0.next_value_seed(seed) + } + + fn tuple_variant<V>(mut self, len: usize, visitor: V) -> Result<V::Value, Self::Error> + where + V: Visitor<'de>, + { + self.0.next_value_seed(SeedTupleVariant { len, visitor }) + } + + fn struct_variant<V>( + mut self, + _fields: &'static [&'static str], + visitor: V, + ) -> Result<V::Value, Self::Error> + where + V: Visitor<'de>, + { + self.0.next_value_seed(SeedStructVariant { visitor }) + } +} + +struct SeedTupleVariant<V> { + len: usize, + visitor: V, +} + +impl<'de, V> DeserializeSeed<'de> for SeedTupleVariant<V> +where + V: Visitor<'de>, +{ + type Value = V::Value; + + fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> + where + D: Deserializer<'de>, + { + deserializer.deserialize_tuple(self.len, self.visitor) + } +} + +struct SeedStructVariant<V> { + visitor: V, +} + +impl<'de, V> DeserializeSeed<'de> for SeedStructVariant<V> +where + V: Visitor<'de>, +{ + type Value = V::Value; + + fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> + where + D: Deserializer<'de>, + { + deserializer.deserialize_map(self.visitor) + } +} diff --git a/third_party/rust/serde_with/src/flatten_maybe.rs b/third_party/rust/serde_with/src/flatten_maybe.rs new file mode 100644 index 0000000000..e012812d5a --- /dev/null +++ b/third_party/rust/serde_with/src/flatten_maybe.rs @@ -0,0 +1,86 @@ +/// Support deserializing from flattened and non-flattened representation +/// +/// When working with different serialization formats, sometimes it is more idiomatic to flatten +/// fields, while other formats prefer nesting. Using `#[serde(flatten)]` only the flattened form +/// is supported. +/// +/// This helper creates a function, which support deserializing from either the flattened or the +/// nested form. It gives an error, when both forms are provided. The `flatten` attribute is +/// required on the field such that the helper works. The serialization format will always be +/// flattened. +/// +/// # Examples +/// +/// ```rust +/// # use serde::Deserialize; +/// # +/// // Setup the types +/// #[derive(Deserialize, Debug)] +/// struct S { +/// #[serde(flatten, deserialize_with = "deserialize_t")] +/// t: T, +/// } +/// +/// #[derive(Deserialize, Debug)] +/// struct T { +/// i: i32, +/// } +/// +/// // The macro creates custom deserialization code. +/// // You need to specify a function name and the field name of the flattened field. +/// serde_with::flattened_maybe!(deserialize_t, "t"); +/// +/// # fn main() { +/// // Supports both flattened +/// let j = r#" {"i":1} "#; +/// assert!(serde_json::from_str::<S>(j).is_ok()); +/// +/// // and non-flattened versions. +/// let j = r#" {"t":{"i":1}} "#; +/// assert!(serde_json::from_str::<S>(j).is_ok()); +/// +/// // Ensure that the value is given +/// let j = r#" {} "#; +/// assert!(serde_json::from_str::<S>(j).is_err()); +/// +/// // and only occurs once, not multiple times. +/// let j = r#" {"i":1,"t":{"i":1}} "#; +/// assert!(serde_json::from_str::<S>(j).is_err()); +/// # } +/// ``` +#[macro_export] +macro_rules! flattened_maybe { + ($fn:ident, $field:literal) => { + fn $fn<'de, T, D>(deserializer: D) -> ::std::result::Result<T, D::Error> + where + T: $crate::serde::Deserialize<'de>, + D: $crate::serde::Deserializer<'de>, + { + use ::std::{ + option::Option::{self, None, Some}, + result::Result::{self, Err, Ok}, + }; + use $crate::serde; + + #[derive($crate::serde::Deserialize)] + #[serde(crate = "serde")] + pub struct Both<T> { + #[serde(flatten)] + flat: Option<T>, + #[serde(rename = $field)] + not_flat: Option<T>, + } + + let both: Both<T> = $crate::serde::Deserialize::deserialize(deserializer)?; + match (both.flat, both.not_flat) { + (Some(t), None) | (None, Some(t)) => Ok(t), + (None, None) => Err($crate::serde::de::Error::missing_field($field)), + (Some(_), Some(_)) => Err($crate::serde::de::Error::custom(concat!( + "`", + $field, + "` is both flattened and not" + ))), + } + } + }; +} diff --git a/third_party/rust/serde_with/src/formats.rs b/third_party/rust/serde_with/src/formats.rs new file mode 100644 index 0000000000..2d7aaa6965 --- /dev/null +++ b/third_party/rust/serde_with/src/formats.rs @@ -0,0 +1,96 @@ +//! Specify the format and how lenient the deserialization is + +use alloc::string::String; + +/// Specify how to serialize/deserialize a type +/// +/// The format specifier allows to configure how a value is serialized/deserialized. +/// For example, you can serialize a timestamp as an integer using the UNIX epoch, as a string containing an integer, or as a string using ISO 8601. +/// This [`Format`] traits allows more flexibility in configuring the format without the need to create a new type for each case. +pub trait Format {} + +macro_rules! impl_format { + ($(#[$attr:meta] $t:ty)*) => { + $( + #[$attr] + impl Format for $t {} + )* + }; +} +macro_rules! create_format { + ($(#[$attr:meta] $t:ident)*) => { + $( + #[$attr] + #[derive(Copy, Clone, Debug, Default)] + pub struct $t; + impl_format!(#[$attr] $t); + )* + }; +} +impl_format!( + /// Serialize into an i8 + i8 + /// Serialize into a u8 + u8 + /// Serialize into an i16 + i16 + /// Serialize into a u16 + u16 + /// Serialize into an i32 + i32 + /// Serialize into a u32 + u32 + /// Serialize into an i64 + i64 + /// Serialize into a u64 + u64 + + /// Serialize into a f32 + f32 + /// Serialize into a f64 + f64 + + /// Serialize into a bool + bool + + /// Serialize into a String + String +); +serde::serde_if_integer128!(impl_format!( + /// Serialize into an i128 + i128 + /// Serialize into a u128 + u128 +);); + +create_format!( + /// Use uppercase characters + Uppercase + /// Use lowercase characters + Lowercase + + /// Use in combination with [`OneOrMany`](crate::OneOrMany). Emit single element for lists of size 1. + PreferOne + /// Use in combination with [`OneOrMany`](crate::OneOrMany). Always emit the list form. + PreferMany + + /// Emit padding during serialization. + Padded + /// Do not emit padding during serialization. + Unpadded +); + +/// Specify how lenient the deserialization process should be +/// +/// Formats which make use of this trait should specify how it affects the deserialization behavior. +pub trait Strictness {} + +/// Use strict deserialization behavior, see [`Strictness`]. +#[derive(Copy, Clone, Debug, Default)] +pub struct Strict; +impl Strictness for Strict {} + +/// Use a flexible deserialization behavior, see [`Strictness`]. +#[derive(Copy, Clone, Debug, Default)] +pub struct Flexible; +impl Strictness for Flexible {} diff --git a/third_party/rust/serde_with/src/guide.md b/third_party/rust/serde_with/src/guide.md new file mode 100644 index 0000000000..9eb80dd915 --- /dev/null +++ b/third_party/rust/serde_with/src/guide.md @@ -0,0 +1,144 @@ +# `serde_with` User Guide + +This crate provides helper functions to extend and change how [`serde`] serializes different data types. +For example, you can serialize [a map as a sequence of tuples][crate::guide::serde_as#maps-to-vec-of-tuples], serialize [using the `Display` and `FromStr` traits][`DisplayFromStr`], or serialize [an empty `String` like `None`][NoneAsEmptyString]. +`serde_with` covers types from the Rust Standard Library and some common crates like [`chrono`][serde_with_chrono]. + +[**A list of all supported transformations is available on this page.**](crate::guide::serde_as_transformations) + +The crate offers four types of functionality. + +## 1. A more flexible and composable replacement for the with annotation, called `serde_as` *(v1.5.0+)* + +This is an alternative to [serde's with-annotation][with-annotation], which adds flexibility and composability to the scheme. +The main downside is that it work with fewer types than [with-annotations][with-annotation]. +However, all types from the Rust Standard Library should be supported in all combinations and any missing entry is a bug. + +The `serde_as` scheme is based on two new traits: [`SerializeAs`] and [`DeserializeAs`]. +[Check out the detailed page about `serde_as` and the available features.](crate::guide::serde_as) + +### Example + +```rust +# use serde::{Deserialize, Serialize}; +# use serde_with::{serde_as, DisplayFromStr}; +# use std::collections::HashMap; +# use std::net::Ipv4Addr; +# +#[serde_as] +# #[derive(Debug, PartialEq, Eq)] +#[derive(Deserialize, Serialize)] +struct Data { + // Type does not implement Serialize or Deserialize + #[serde_as(as = "DisplayFromStr")] + address: Ipv4Addr, + // Treat the Vec like a map with duplicates + // Convert u32 into a String and keep the String the same type + #[serde_as(as = "HashMap<DisplayFromStr, _>")] + vec_as_map: Vec<(u32, String)>, +} + +let data = Data { + address: Ipv4Addr::new(192, 168, 0, 1), + vec_as_map: vec![ + (123, "Hello".into()), + (456, "World".into()), + (123, "Hello".into()), + ], +}; + +let json = r#"{ + "address": "192.168.0.1", + "vec_as_map": { + "123": "Hello", + "456": "World", + "123": "Hello" + } +}"#; + +// Test Serialization +assert_eq!(json, serde_json::to_string_pretty(&data).unwrap()); +// Test Deserialization +assert_eq!(data, serde_json::from_str(json).unwrap()); +``` + +## 2. Integration with serde's with-annotation + +[serde's with-annotation][with-annotation] allows specifying a different serialization or deserialization function for a field. +It is useful to adapt the serialization of existing types to the requirements of a protocol. +Most modules in this crate can be used together with the with-annotation. + +The annotation approach has one big drawback, in that it is very inflexible. +It allows specifying arbitrary serialization code, but the code has to perform the correct transformations. +It is not possible to combine multiple of those functions. +One common use case for this is the serialization of collections like `Vec`. +If you have a field of type `T`, you can apply the with-annotation, but if you have a field of type `Vec<T>`, there is no way to re-use the same functions for the with-annotation. +This inflexibility is fixed in the `serde_as` scheme presented above. + +The example shows a similar setup as in the `serde_as` example above, but using the with-annotation. + +### Example + +```rust +# use serde::{Deserialize, Serialize}; +# use std::net::Ipv4Addr; +# +# #[derive(Debug, PartialEq, Eq)] +#[derive(Deserialize, Serialize)] +struct Data { + // Type does not implement Serialize or Deserialize + #[serde(with = "serde_with::rust::display_fromstr")] + address: Ipv4Addr, + // Treat the Vec like a map with duplicates + #[serde(with = "serde_with::rust::tuple_list_as_map")] + vec_as_map: Vec<(String, u32)>, +} + +let data = Data { + address: Ipv4Addr::new(192, 168, 0, 1), + vec_as_map: vec![ + ("Hello".into(), 123), + ("World".into(), 456), + ("Hello".into(), 123), + ], +}; + +let json = r#"{ + "address": "192.168.0.1", + "vec_as_map": { + "Hello": 123, + "World": 456, + "Hello": 123 + } +}"#; + +// Test Serialization +assert_eq!(json, serde_json::to_string_pretty(&data).unwrap()); +// Test Deserialization +assert_eq!(data, serde_json::from_str(json).unwrap()); +``` + +## 3. proc-macros to make it easier to use both above parts + +The proc-macros are an optional addition and improve the user experience for common tasks. +We have already seen how the `serde_as` attribute is used to define the serialization instructions. + +The proc-macro attributes are defined in the [`serde_with_macros`] crate and re-exported from the root of this crate. +The proc-macros are optional, but enabled by default. +For further details, please refer to the documentation of each proc-macro. + +## 4. Derive macros to implement `Deserialize` and `Serialize` + +The derive macros work similar to the serde provided ones, but they do implement other de/serialization schemes. +For example, the derives [`DeserializeFromStr`] and [`SerializeDisplay`] require that the type also implement [`FromStr`] and [`Display`] and de/serializes from/to a string instead of the usual way of iterating over all fields. + +## Migrating from the with-annotations to `serde_as` + +Each old style module explains how it can be converted to `serde_as`. +Not all modules have such a description since not all are migrated and some are hard to implement in the `serde_as` system. + +[`Display`]: std::fmt::Display +[`FromStr`]: std::str::FromStr +[`serde_with_macros`]: serde_with_macros +[serde_with_chrono]: crate::chrono +[with-annotation]: https://serde.rs/field-attrs.html#with diff --git a/third_party/rust/serde_with/src/guide/feature_flags.md b/third_party/rust/serde_with/src/guide/feature_flags.md new file mode 100644 index 0000000000..9e80177ad2 --- /dev/null +++ b/third_party/rust/serde_with/src/guide/feature_flags.md @@ -0,0 +1,62 @@ +# Available Feature Flags + +This crate has the following features which can be enabled. +Each entry will explain the feature in more detail. + +1. [`base64`](#base64) +2. [`chrono`](#chrono) +3. [`guide`](#guide) +4. [`hex`](#hex) +5. [`indexmap`](#indexmap) +6. [`json`](#json) +7. [`macros`](#macros) +8. [`time_0_3`](#time_0_3) + +## `base64` + +The `base64` feature enables serializing data in base64 format. + +This pulls in `base64` as a dependency. + +## `chrono` + +The `chrono` feature enables integration of `chrono` specific conversions. +This includes support for the timestamp and duration types. + +This pulls in `chrono` as a dependency. + +## `guide` + +The `guide` feature enables inclusion of this user guide. +The feature only changes the rustdoc output and enables no other effects. + +## `hex` + +The `hex` feature enables serializing data in hex format. + +This pulls in `hex` as a dependency. + +## `indexmap` + +The `indexmap` feature enables implementations of `indexmap` specific checks. +This includes support for checking duplicate keys + +## `json` + +The `json` features enables JSON conversions from the `json` module. + +This pulls in `serde_json` as a dependency. + +## `macros` + +The `macros` features enables all helper macros and derives. +It is enabled by default, since the macros provide a usability benefit, especially for `serde_as`. + +This pulls in `serde_with_macros` as a dependency. + +## `time_0_3` + +The `time_0_3` enables integration of `time` v0.3 specific conversions. +This includes support for the timestamp and duration types. + +This pulls in `time` v0.3 as a dependency. diff --git a/third_party/rust/serde_with/src/guide/serde_as.md b/third_party/rust/serde_with/src/guide/serde_as.md new file mode 100644 index 0000000000..76275f0047 --- /dev/null +++ b/third_party/rust/serde_with/src/guide/serde_as.md @@ -0,0 +1,332 @@ +# `serde_as` Annotation + +This is an alternative to serde's with-annotation. +It is more flexible and composable, but work with fewer types. + +The scheme is based on two new traits, [`SerializeAs`] and [`DeserializeAs`], which need to be implemented by all types which want to be compatible with `serde_as`. +The proc-macro attribute [`#[serde_as]`][crate::serde_as] exists as a usability boost for users. +The basic design of `serde_as` was developed by [@markazmierczak](https://github.com/markazmierczak). + +This page contains some general advice on the usage of `serde_as` and on implementing the necessary traits. +[**A list of all supported transformations enabled by `serde_as` is available on this page.**](crate::guide::serde_as_transformations) + +1. [Switching from serde's with to `serde_as`](#switching-from-serdes-with-to-serde_as) + 1. [Deserializing Optional Fields](#deserializing-optional-fields) + 2. [Gating `serde_as` on Features](#gating-serde_as-on-features) +2. [Implementing `SerializeAs` / `DeserializeAs`](#implementing-serializeas--deserializeas) + 1. [Using `#[serde_as]` on types without `SerializeAs` and `Serialize` implementations](#using-serde_as-on-types-without-serializeas-and-serialize-implementations) + 2. [Using `#[serde_as]` with serde's remote derives](#using-serde_as-with-serdes-remote-derives) +3. [Re-exporting `serde_as`](#re-exporting-serde_as) + +## Switching from serde's with to `serde_as` + +For the user, the main difference is that instead of + +```rust,ignore +#[serde(with = "...")] +``` + +you now have to write + +```rust,ignore +#[serde_as(as = "...")] +``` + +and place the `#[serde_as]` attribute *before* the `#[derive]` attribute. +You still need the `#[derive(Serialize, Deserialize)]` on the struct/enum. + +All together, this looks like: + +```rust +use serde::{Deserialize, Serialize}; +use serde_with::{serde_as, DisplayFromStr}; + +#[serde_as] +#[derive(Serialize, Deserialize)] +struct A { + #[serde_as(as = "DisplayFromStr")] + mime: mime::Mime, +} +``` + +The main advantage is that you can compose `serde_as` stuff, which is impossible with the with-annotation. +For example, the `mime` field from above could be nested in one or more data structures: + +```rust +# use std::collections::BTreeMap; +# use serde::{Deserialize, Serialize}; +# use serde_with::{serde_as, DisplayFromStr}; +# +#[serde_as] +#[derive(Serialize, Deserialize)] +struct A { + #[serde_as(as = "Option<BTreeMap<_, Vec<DisplayFromStr>>>")] + mime: Option<BTreeMap<String, Vec<mime::Mime>>>, +} +``` + +### Deserializing Optional Fields + +During deserialization, serde treats fields of `Option<T>` as optional and does not require them to be present. +This breaks when adding either the `serde_as` annotation or serde's `with` annotation. +The default behavior can be restored by adding serde's `default` attribute. + +```rust +# use serde::{Deserialize, Serialize}; +# use serde_with::{serde_as, DisplayFromStr}; +# +#[serde_as] +#[derive(Serialize, Deserialize)] +struct A { + #[serde_as(as = "Option<DisplayFromStr>")] + // Allows deserialization without providing a value for `val` + #[serde(default)] + val: Option<u32>, +} +``` + +In the future, this behavior might change and `default` would be applied on `Option<T>` fields. +You can add your feedback at [serde_with#185]. + +### Gating `serde_as` on Features + +Gating `serde_as` behind optional features is currently not supported. +More details can be found in the corresponding issue [serde_with#355]. + +```rust,ignore +#[cfg_attr(feature="serde" ,serde_as)] +#[cfg_attr(feature="serde", derive(Serialize, Deserialize))] +struct StructC { + #[cfg_attr(feature="serde" ,serde_as(as = "Vec<(_, _)>"))] + map: HashMap<(i32,i32), i32>, +} +``` + +The `serde_as` proc-macro attribute will not recognize the `serde_as` attribute on the field and will not perform the necessary translation steps. +The problem can be avoided by forcing Rust to evaluate all cfg-expressions before running `serde_as`. +This is possible with the `#[cfg_eval]` attribute, which is considered for stabilization ([rust#82679], [rust#87221]). + +As a workaround, it is possible to remove the `serde_as` proc-macro attribute and perform the transformation manually. +The transformation steps are listed in the [`serde_as`] documentations. +For the example above, this means to replace the field attribute with: + +```rust,ignore +use serde_with::{As, Same}; + +#[cfg_attr(feature="serde", serde(with = "As::<Vec<(Same, Same)>>"))] +map: HashMap<(i32,i32), i32>, +``` + +[rust#82679]: https://github.com/rust-lang/rust/issues/82679 +[rust#87221]: https://github.com/rust-lang/rust/pull/87221 +[serde_with#355]: https://github.com/jonasbb/serde_with/issues/355 + +## Implementing `SerializeAs` / `DeserializeAs` + +You can support [`SerializeAs`] / [`DeserializeAs`] on your own types too. +Most "leaf" types do not need to implement these traits, since they are supported implicitly. +"Leaf" type refers to types which directly serialize like plain data types. +[`SerializeAs`] / [`DeserializeAs`] is very important for collection types, like `Vec` or `BTreeMap`, since they need special handling for the key/value de/serialization such that the conversions can be done on the key/values. +You also find them implemented on the conversion types, such as the [`DisplayFromStr`] type. +These make up the bulk of this crate and allow you to perform all the nice conversions to [hex strings], the [bytes to string converter], or [duration to UNIX epoch]. + +In many cases, conversion is only required from one serializable type to another one, without requiring the full power of the `Serialize` or `Deserialize` traits. +In these cases, the [`serde_conv!`] macro conveniently allows defining conversion types without the boilerplate. +The documentation of [`serde_conv!`] contains more details how to use it. + +The trait documentations for [`SerializeAs`] and [`DeserializeAs`] describe in details how to implement them for container types like `Box` or `Vec` and other types. + +### Using `#[serde_as]` on types without `SerializeAs` and `Serialize` implementations + +The `SerializeAs` and `DeserializeAs` traits can easily be used together with types from other crates without running into orphan rule problems. +This is a distinct advantage of the `serde_as` system. +For this example we assume we have a type `RemoteType` from a dependency which does not implement `Serialize` nor `SerializeAs`. +We assume we have a module containing a `serialize` and a `deserialize` function, which can be used in the `#[serde(with = "MODULE")]` annotation. +You find an example in the [official serde documentation](https://serde.rs/custom-date-format.html). + +Our goal is to serialize this `Data` struct. +Right now, we do not have anything we can use to replace `???` with, since `_` only works if `RemoteType` would implement `Serialize`, which it does not. + +```rust +# #[cfg(FALSE)] { +#[serde_as] +#[derive(serde::Serialize)] +struct Data { + #[serde_as(as = "Vec<???>")] + vec: Vec<RemoteType>, +} +# } +``` + +We need to create a new type for which we can implement `SerializeAs`, to replace the `???`. +The `SerializeAs` implementation is **always** written for a local type. +This allows it to seamlessly work with types from dependencies without running into orphan rule problems. + +```rust +# #[cfg(FALSE)] { +struct LocalType; + +impl SerializeAs<RemoteType> for LocalType { + fn serialize_as<S>(value: &RemoteType, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + MODULE::serialize(value, serializer) + } +} + +impl<'de> DeserializeAs<'de, RemoteType> for LocalType { + fn deserialize_as<D>(deserializer: D) -> Result<RemoteType, D::Error> + where + D: Deserializer<'de>, + { + MODULE::deserialize(deserializer) + } +} +# } +``` + +This is how the final implementation looks like. +We assumed we have a module `MODULE` with a `serialize` function already, which we use here to provide the implementation. +As can be seen, this is mostly boilerplate, since the most part is encapsulated in `$module::serialize`. +The final `Data` struct will now look like: + +```rust +# #[cfg(FALSE)] { +#[serde_as] +#[derive(serde::Serialize)] +struct Data { + #[serde_as(as = "Vec<LocalType>")] + vec: Vec<RemoteType>, +} +# } +``` + +### Using `#[serde_as]` with serde's remote derives + +A special case of the above section is using it on remote derives. +This is a special functionality of serde, where it derives the de/serialization code for a type from another crate if all fields are `pub`. +You can find all the details in the [official serde documentation](https://serde.rs/remote-derive.html). + +```rust +# #[cfg(FALSE)] { +// Pretend that this is somebody else's crate, not a module. +mod other_crate { + // Neither Serde nor the other crate provides Serialize and Deserialize + // impls for this struct. + pub struct Duration { + pub secs: i64, + pub nanos: i32, + } +} + +//////////////////////////////////////////////////////////////////////////////// + +use other_crate::Duration; + +// Serde calls this the definition of the remote type. It is just a copy of the +// remote data structure. The `remote` attribute gives the path to the actual +// type we intend to derive code for. +#[derive(serde::Serialize, serde::Deserialize)] +#[serde(remote = "Duration")] +struct DurationDef { + secs: i64, + nanos: i32, +} +# } +``` + +Our goal is now to use `Duration` within `serde_as`. +We use the existing `DurationDef` type and its `serialize` and `deserialize` functions. +We can write this implementation. +The implementation for `DeserializeAs` works analogue. + +```rust +# #[cfg(FALSE)] { +impl SerializeAs<Duration> for DurationDef { + fn serialize_as<S>(value: &Duration, serializer: S) -> Result<S::Ok, S::Error> + where + S: serde::Serializer, + { + DurationDef::serialize(value, serializer) + } +} +# } +``` + +This now allows us to use `Duration` for serialization. + +```rust +# #[cfg(FALSE)] { +use other_crate::Duration; + +#[serde_as] +#[derive(serde::Serialize)] +struct Data { + #[serde_as(as = "Vec<DurationDef>")] + vec: Vec<Duration>, +} +# } +``` + +## Re-exporting `serde_as` + +If `serde_as` is being used in a context where the `serde_with` crate is not available from the root +path, but is re-exported at some other path, the `crate = "..."` attribute argument should be used +to specify its path. This may be the case if `serde_as` is being used in a procedural macro - +otherwise, users of that macro would need to add `serde_with` to their own Cargo manifest. + +The `crate` argument will generally be used in conjunction with [`serde`'s own `crate` argument]. + +For example, a type definition may be defined in a procedural macro: + +```rust,ignore +// some_other_lib_derive/src/lib.rs + +use proc_macro::TokenStream; +use quote::quote; + +#[proc_macro] +pub fn define_some_type(_item: TokenStream) -> TokenStream { + let def = quote! { + #[serde(crate = "::some_other_lib::serde")] + #[::some_other_lib::serde_with::serde_as(crate = "::some_other_lib::serde_with")] + #[derive(::some_other_lib::serde::Deserialize)] + struct Data { + #[serde_as(as = "_")] + a: u32, + } + }; + + TokenStream::from(def) +} +``` + +This can be re-exported through a library which also re-exports `serde` and `serde_with`: + +```rust,ignore +// some_other_lib/src/lib.rs + +pub use serde; +pub use serde_with; +pub use some_other_lib_derive::define_some_type; +``` + +The procedural macro can be used by other crates without any additional imports: + +```rust,ignore +// consuming_crate/src/main.rs + +some_other_lib::define_some_type!(); +``` + +[`DeserializeAs`]: crate::DeserializeAs +[`DisplayFromStr`]: crate::DisplayFromStr +[`serde_as`]: crate::serde_as +[`serde_conv!`]: crate::serde_conv! +[`serde`'s own `crate` argument]: https://serde.rs/container-attrs.html#crate +[`SerializeAs`]: crate::SerializeAs +[bytes to string converter]: crate::BytesOrString +[duration to UNIX epoch]: crate::DurationSeconds +[hex strings]: crate::hex::Hex +[serde_with#185]: https://github.com/jonasbb/serde_with/issues/185 diff --git a/third_party/rust/serde_with/src/guide/serde_as_transformations.md b/third_party/rust/serde_with/src/guide/serde_as_transformations.md new file mode 100644 index 0000000000..9be67e154b --- /dev/null +++ b/third_party/rust/serde_with/src/guide/serde_as_transformations.md @@ -0,0 +1,518 @@ +# De/Serialize Transformations Available + +This page lists the transformations implemented in this crate and supported by `serde_as`. + +1. [Base64 encode bytes](#base64-encode-bytes) +2. [Big Array support](#big-array-support) +3. [`bool` from integer](#bool-from-integer) +4. [Borrow from the input for `Cow` type](#borrow-from-the-input-for-cow-type) +5. [`Bytes` with more efficiency](#bytes-with-more-efficiency) +6. [Convert to an intermediate type using `Into`](#convert-to-an-intermediate-type-using-into) +7. [Convert to an intermediate type using `TryInto`](#convert-to-an-intermediate-type-using-tryinto) +8. [`Default` from `null`](#default-from-null) +9. [De/Serialize into `Vec`, ignoring errors](#deserialize-into-vec-ignoring-errors) +10. [De/Serialize with `FromStr` and `Display`](#deserialize-with-fromstr-and-display) +11. [`Duration` as seconds](#duration-as-seconds) +12. [Hex encode bytes](#hex-encode-bytes) +13. [Ignore deserialization errors](#ignore-deserialization-errors) +14. [`Maps` to `Vec` of enums](#maps-to-vec-of-enums) +15. [`Maps` to `Vec` of tuples](#maps-to-vec-of-tuples) +16. [`NaiveDateTime` like UTC timestamp](#naivedatetime-like-utc-timestamp) +17. [`None` as empty `String`](#none-as-empty-string) +18. [One or many elements into `Vec`](#one-or-many-elements-into-vec) +19. [Pick first successful deserialization](#pick-first-successful-deserialization) +20. [Timestamps as seconds since UNIX epoch](#timestamps-as-seconds-since-unix-epoch) +21. [Value into JSON String](#value-into-json-string) +22. [`Vec` of tuples to `Maps`](#vec-of-tuples-to-maps) +23. [Well-known time formats for `OffsetDateTime`](#well-known-time-formats-for-offsetdatetime) + +## Base64 encode bytes + +[`Base64`] + +Requires the `base64` feature. +The character set and padding behavior can be configured. + +```ignore +// Rust +#[serde_as(as = "serde_with::base64::Base64")] +value: Vec<u8>, +#[serde_as(as = "Base64<Bcrypt, Unpadded>")] +bcrypt_unpadded: Vec<u8>, + +// JSON +"value": "SGVsbG8gV29ybGQ=", +"bcrypt_unpadded": "QETqZE6eT07wZEO", +``` + +## Big Array support + +Support for arrays of arbitrary size. + +```ignore +// Rust +#[serde_as(as = "[[_; 64]; 33]")] +value: [[u8; 64]; 33], + +// JSON +"value": [[0,0,0,0,0,...], [0,0,0,...], ...], +``` + +## `bool` from integer + +Deserialize an integer and convert it into a `bool`. +[`BoolFromInt<Strict>`] (default) deserializes 0 to `false` and `1` to `true`, other numbers are errors. +[`BoolFromInt<Flexible>`] deserializes any non-zero as `true`. +Serialization only emits 0/1. + +```ignore +// Rust +#[serde_as(as = "BoolFromInt")] // BoolFromInt<Strict> +b: bool, + +// JSON +"b": 1, +``` + +## Borrow from the input for `Cow` type + +The types `Cow<'_, str>`, `Cow<'_, [u8]>`, or `Cow<'_, [u8; N]>` can borrow from the input, avoiding extra copies. + +```ignore +// Rust +#[serde_as(as = "BorrowCow")] +value: Cow<'a, str>, + +// JSON +"value": "foobar", +``` + +## `Bytes` with more efficiency + +[`Bytes`] + +More efficient serialization for byte slices and similar. + +```ignore +// Rust +#[serde_as(as = "Bytes")] +value: Vec<u8>, + +// JSON +"value": [0, 1, 2, 3, ...], +``` + +## Convert to an intermediate type using `Into` + +[`FromInto`] + +```ignore +// Rust +#[serde_as(as = "FromInto<(u8, u8, u8)>")] +value: Rgb, + +impl From<(u8, u8, u8)> for Rgb { ... } +impl From<Rgb> for (u8, u8, u8) { ... } + +// JSON +"value": [128, 64, 32], +``` + +## Convert to an intermediate type using `TryInto` + +[`TryFromInto`] + +```ignore +// Rust +#[serde_as(as = "TryFromInto<i8>")] +value: u8, + +// JSON +"value": 127, +``` + +## `Default` from `null` + +[`DefaultOnNull`] + +```ignore +// Rust +#[serde_as(as = "DefaultOnNull")] +value: u32, +#[serde_as(as = "DefaultOnNull<DisplayFromStr>")] +value2: u32, + +// JSON +"value": 123, +"value2": "999", + +// Deserializes null into the Default value, i.e., +null => 0 +``` + +## De/Serialize into `Vec`, ignoring errors + +[`VecSkipError`] + +For formats with heterogenous-typed sequences, we can collect only the deserializable elements. +This is also useful for unknown enum variants. + +```ignore +#[derive(serde::Deserialize)] +enum Color { + Red, + Green, + Blue, +} + +// JSON +"colors": ["Blue", "Yellow", "Green"], + +// Rust +#[serde_as(as = "VecSkipError<_>")] +colors: Vec<Color>, + +// => vec![Blue, Green] +``` + +## De/Serialize with `FromStr` and `Display` + +Useful if a type implements `FromStr` / `Display` but not `Deserialize` / `Serialize`. + +[`DisplayFromStr`] + +```ignore +// Rust +#[serde_as(as = "serde_with::DisplayFromStr")] +value: u128, +#[serde_as(as = "serde_with::DisplayFromStr")] +mime: mime::Mime, + +// JSON +"value": "340282366920938463463374607431768211455", +"mime": "text/*", +``` + +## `Duration` as seconds + +[`DurationSeconds`] + +```ignore +// Rust +#[serde_as(as = "serde_with::DurationSeconds<u64>")] +value: Duration, + +// JSON +"value": 86400, +``` + +[`DurationSecondsWithFrac`] supports subsecond precision: + +```ignore +// Rust +#[serde_as(as = "serde_with::DurationSecondsWithFrac<f64>")] +value: Duration, + +// JSON +"value": 1.234, +``` + +Different serialization formats are possible: + +```ignore +// Rust +#[serde_as(as = "serde_with::DurationSecondsWithFrac<String>")] +value: Duration, + +// JSON +"value": "1.234", +``` + +The same conversions are also implemented for [`chrono::Duration`] with the `chrono` feature. + +The same conversions are also implemented for [`time::Duration`] with the `time_0_3` feature. + +## Hex encode bytes + +[`Hex`] + +Requires the `hex` feature. +The hex string can use upper- and lowercase characters. + +```ignore +// Rust +#[serde_as(as = "serde_with::hex::Hex")] +lowercase: Vec<u8>, +#[serde_as(as = "serde_with::hex::Hex<serde_with::formats::Uppercase>")] +uppercase: Vec<u8>, + +// JSON +"lowercase": "deadbeef", +"uppercase": "DEADBEEF", +``` + +## Ignore deserialization errors + +Check the documentation for [`DefaultOnError`]. + +## `Maps` to `Vec` of enums + +[`EnumMap`] + +Combine multiple enum values into a single map. +The key is the enum variant name, and the value is the variant value. +This only works with [*externally tagged*] enums, the default enum representation. +Other forms cannot be supported. + +```ignore +enum EnumValue { + Int(i32), + String(String), + Unit, + Tuple(i32, String), + Struct { + a: i32, + b: String, + }, +} + +// Rust +struct VecEnumValues ( + #[serde_as(as = "EnumMap")] + Vec<EnumValue>, +); + +VecEnumValues(vec![ + EnumValue::Int(123), + EnumValue::String("Foo".to_string()), + EnumValue::Unit, + EnumValue::Tuple(1, "Bar".to_string()), + EnumValue::Struct { + a: 666, + b: "Baz".to_string(), + }, +]) + +// JSON +{ + "Int": 123, + "String": "Foo", + "Unit": null, + "Tuple": [ + 1, + "Bar", + ], + "Struct": { + "a": 666, + "b": "Baz", + } +} +``` + +[*externally tagged*]: https://serde.rs/enum-representations.html#externally-tagged + +## `Maps` to `Vec` of tuples + +```ignore +// Rust +#[serde_as(as = "Vec<(_, _)>")] +value: HashMap<String, u32>, // also works with BTreeMap + +// JSON +"value": [ + ["hello", 1], + ["world", 2] +], +``` + +The [inverse operation](#vec-of-tuples-to-maps) is also available. + +## `NaiveDateTime` like UTC timestamp + +Requires the `chrono` feature. + +```ignore +// Rust +#[serde_as(as = "chrono::DateTime<chrono::Utc>")] +value: chrono::NaiveDateTime, + +// JSON +"value": "1994-11-05T08:15:30Z", + ^ Pretend DateTime is UTC +``` + +## `None` as empty `String` + +[`NoneAsEmptyString`] + +```ignore +// Rust +#[serde_as(as = "serde_with::NoneAsEmptyString")] +value: Option<String>, + +// JSON +"value": "", // converts to None + +"value": "Hello World!", // converts to Some +``` + +## One or many elements into `Vec` + +[`OneOrMany`] + +```ignore +// Rust +#[serde_as(as = "serde_with::OneOrMany<_>")] +value: Vec<String>, + +// JSON +"value": "", // Deserializes single elements + +"value": ["Hello", "World!"], // or lists of many +``` + +## Pick first successful deserialization + +[`PickFirst`] + +```ignore +// Rust +#[serde_as(as = "serde_with::PickFirst<(_, serde_with::DisplayFromStr)>")] +value: u32, + +// JSON +// serialize into +"value": 666, +// deserialize from either +"value": 666, +"value": "666", +``` + +## Timestamps as seconds since UNIX epoch + +[`TimestampSeconds`] + +```ignore +// Rust +#[serde_as(as = "serde_with::TimestampSeconds<i64>")] +value: SystemTime, + +// JSON +"value": 86400, +``` + +[`TimestampSecondsWithFrac`] supports subsecond precision: + +```ignore +// Rust +#[serde_as(as = "serde_with::TimestampSecondsWithFrac<f64>")] +value: SystemTime, + +// JSON +"value": 1.234, +``` + +Different serialization formats are possible: + +```ignore +// Rust +#[serde_as(as = "serde_with::TimestampSecondsWithFrac<String>")] +value: SystemTime, + +// JSON +"value": "1.234", +``` + +The same conversions are also implemented for [`chrono::DateTime<Utc>`], [`chrono::DateTime<Local>`], and [`chrono::NaiveDateTime`] with the `chrono` feature. + +The conversions are availble for [`time::OffsetDateTime`] and [`time::PrimitiveDateTime`] with the `time_0_3` feature enabled. + +## Value into JSON String + +Some JSON APIs are weird and return a JSON encoded string in a JSON response + +[`JsonString`] + +Requires the `json` feature. + +```ignore +// Rust +#[derive(Deserialize, Serialize)] +struct OtherStruct { + value: usize, +} + +#[serde_as(as = "serde_with::json::JsonString")] +value: OtherStruct, + +// JSON +"value": "{\"value\":5}", +``` + +## `Vec` of tuples to `Maps` + +```ignore +// Rust +#[serde_as(as = "HashMap<_, _>")] // also works with BTreeMap +value: Vec<(String, u32)>, + +// JSON +"value": { + "hello": 1, + "world": 2 +}, +``` + +This operation is also available for other sequence types. +This includes `BinaryHeap<(K, V)>`, `BTreeSet<(K, V)>`, `HashSet<(K, V)>`, `LinkedList<(K, V)>`, `VecDeque<(K, V)>`, `Option<(K, V)>` and `[(K, V); N]` for all sizes of N. + +The [inverse operation](#maps-to-vec-of-tuples) is also available. + +## Well-known time formats for `OffsetDateTime` + +[`time::OffsetDateTime`] can be serialized in string format in different well-known formats. +Two formats are supported, [`time::format_description::well_known::Rfc2822`] and [`time::format_description::well_known::Rfc3339`]. + +```ignore +// Rust +#[serde_as(as = "time::format_description::well_known::Rfc2822")] +rfc_2822: OffsetDateTime, +#[serde_as(as = "time::format_description::well_known::Rfc3339")] +rfc_3339: OffsetDateTime, + +// JSON +"rfc_2822": "Fri, 21 Nov 1997 09:55:06 -0600", +"rfc_3339": "1997-11-21T09:55:06-06:00", +``` + +These conversions are availble with the `time_0_3` feature flag. + +[`Base64`]: crate::base64::Base64 +[`BoolFromInt<Flexible>`]: crate::BoolFromInt +[`BoolFromInt<Strict>`]: crate::BoolFromInt +[`Bytes`]: crate::Bytes +[`chrono::DateTime<Local>`]: chrono_crate::DateTime +[`chrono::DateTime<Utc>`]: chrono_crate::DateTime +[`chrono::Duration`]: https://docs.rs/chrono/latest/chrono/struct.Duration.html +[`chrono::NaiveDateTime`]: chrono_crate::NaiveDateTime +[`DefaultOnError`]: crate::DefaultOnError +[`DefaultOnNull`]: crate::DefaultOnNull +[`DisplayFromStr`]: crate::DisplayFromStr +[`DurationSeconds`]: crate::DurationSeconds +[`DurationSecondsWithFrac`]: crate::DurationSecondsWithFrac +[`EnumMap`]: crate::EnumMap +[`FromInto`]: crate::FromInto +[`Hex`]: crate::hex::Hex +[`JsonString`]: crate::json::JsonString +[`NoneAsEmptyString`]: crate::NoneAsEmptyString +[`OneOrMany`]: crate::OneOrMany +[`PickFirst`]: crate::PickFirst +[`time::Duration`]: time_0_3::Duration +[`time::format_description::well_known::Rfc2822`]: time_0_3::format_description::well_known::Rfc2822 +[`time::format_description::well_known::Rfc3339`]: time_0_3::format_description::well_known::Rfc3339 +[`time::OffsetDateTime`]: time_0_3::OffsetDateTime +[`time::PrimitiveDateTime`]: time_0_3::PrimitiveDateTime +[`TimestampSeconds`]: crate::TimestampSeconds +[`TimestampSecondsWithFrac`]: crate::TimestampSecondsWithFrac +[`TryFromInto`]: crate::TryFromInto +[`VecSkipError`]: crate::VecSkipError diff --git a/third_party/rust/serde_with/src/hex.rs b/third_party/rust/serde_with/src/hex.rs new file mode 100644 index 0000000000..1937e35394 --- /dev/null +++ b/third_party/rust/serde_with/src/hex.rs @@ -0,0 +1,155 @@ +//! De/Serialization of hexadecimal encoded bytes +//! +//! This modules is only available when using the `hex` feature of the crate. +//! +//! Please check the documentation on the [`Hex`] type for details. + +use crate::{ + de::DeserializeAs, + formats::{Format, Lowercase, Uppercase}, + ser::SerializeAs, +}; +use alloc::{borrow::Cow, format, vec::Vec}; +use core::{ + convert::{TryFrom, TryInto}, + marker::PhantomData, +}; +use serde::{de::Error, Deserialize, Deserializer, Serializer}; + +/// Serialize bytes as a hex string +/// +/// The type serializes a sequence of bytes as a hexadecimal string. +/// It works on any type implementing `AsRef<[u8]>` for serialization and `TryFrom<Vec<u8>>` for deserialization. +/// +/// The format type parameter specifies if the hex string should use lower- or uppercase characters. +/// Valid options are the types [`Lowercase`] and [`Uppercase`]. +/// Deserialization always supports lower- and uppercase characters, even mixed in one string. +/// +/// # Example +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_json::json; +/// # use serde_with::serde_as; +/// # +/// #[serde_as] +/// # #[derive(Debug, PartialEq, Eq)] +/// #[derive(Deserialize, Serialize)] +/// struct BytesLowercase( +/// // Equivalent to serde_with::hex::Hex<serde_with::formats::Lowercase> +/// #[serde_as(as = "serde_with::hex::Hex")] +/// Vec<u8> +/// ); +/// +/// #[serde_as] +/// # #[derive(Debug, PartialEq, Eq)] +/// #[derive(Deserialize, Serialize)] +/// struct BytesUppercase( +/// #[serde_as(as = "serde_with::hex::Hex<serde_with::formats::Uppercase>")] +/// Vec<u8> +/// ); +/// +/// let b = b"Hello World!"; +/// +/// // Hex with lowercase letters +/// assert_eq!( +/// json!("48656c6c6f20576f726c6421"), +/// serde_json::to_value(BytesLowercase(b.to_vec())).unwrap() +/// ); +/// // Hex with uppercase letters +/// assert_eq!( +/// json!("48656C6C6F20576F726C6421"), +/// serde_json::to_value(BytesUppercase(b.to_vec())).unwrap() +/// ); +/// +/// // Serialization always work from lower- and uppercase characters, even mixed case. +/// assert_eq!( +/// BytesLowercase(vec![0x00, 0xaa, 0xbc, 0x99, 0xff]), +/// serde_json::from_value(json!("00aAbc99FF")).unwrap() +/// ); +/// assert_eq!( +/// BytesUppercase(vec![0x00, 0xaa, 0xbc, 0x99, 0xff]), +/// serde_json::from_value(json!("00aAbc99FF")).unwrap() +/// ); +/// +/// #[serde_as] +/// # #[derive(Debug, PartialEq, Eq)] +/// #[derive(Deserialize, Serialize)] +/// struct ByteArray( +/// // Equivalent to serde_with::hex::Hex<serde_with::formats::Lowercase> +/// #[serde_as(as = "serde_with::hex::Hex")] +/// [u8; 12] +/// ); +/// +/// let b = b"Hello World!"; +/// +/// assert_eq!( +/// json!("48656c6c6f20576f726c6421"), +/// serde_json::to_value(ByteArray(b.clone())).unwrap() +/// ); +/// +/// // Serialization always work from lower- and uppercase characters, even mixed case. +/// assert_eq!( +/// ByteArray([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0xaa, 0xbc, 0x99, 0xff]), +/// serde_json::from_value(json!("0011223344556677aAbc99FF")).unwrap() +/// ); +/// +/// // Remember that the conversion may fail. (The following errors are specific to fixed-size arrays) +/// let error_result: Result<ByteArray, _> = serde_json::from_value(json!("42")); // Too short +/// error_result.unwrap_err(); +/// +/// let error_result: Result<ByteArray, _> = +/// serde_json::from_value(json!("000000000000000000000000000000")); // Too long +/// error_result.unwrap_err(); +/// # } +/// ``` +#[derive(Copy, Clone, Debug, Default)] +pub struct Hex<FORMAT: Format = Lowercase>(PhantomData<FORMAT>); + +impl<T> SerializeAs<T> for Hex<Lowercase> +where + T: AsRef<[u8]>, +{ + fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.serialize_str(&hex::encode(source)) + } +} + +impl<T> SerializeAs<T> for Hex<Uppercase> +where + T: AsRef<[u8]>, +{ + fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.serialize_str(&hex::encode_upper(source)) + } +} + +impl<'de, T, FORMAT> DeserializeAs<'de, T> for Hex<FORMAT> +where + T: TryFrom<Vec<u8>>, + FORMAT: Format, +{ + fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error> + where + D: Deserializer<'de>, + { + <Cow<'de, str> as Deserialize<'de>>::deserialize(deserializer) + .and_then(|s| hex::decode(&*s).map_err(Error::custom)) + .and_then(|vec: Vec<u8>| { + let length = vec.len(); + vec.try_into().map_err(|_e: T::Error| { + Error::custom(format!( + "Can't convert a Byte Vector of length {} to the output type.", + length + )) + }) + }) + } +} diff --git a/third_party/rust/serde_with/src/json.rs b/third_party/rust/serde_with/src/json.rs new file mode 100644 index 0000000000..699d31c8d2 --- /dev/null +++ b/third_party/rust/serde_with/src/json.rs @@ -0,0 +1,150 @@ +//! De/Serialization of JSON +//! +//! This modules is only available when using the `json` feature of the crate. + +use crate::{de::DeserializeAs, ser::SerializeAs}; +use serde::{de::DeserializeOwned, Deserializer, Serialize, Serializer}; + +/// Serialize value as string containing JSON +/// +/// The same functionality is also available as [`serde_with::json::JsonString`][crate::json::JsonString] compatible with the `serde_as`-annotation. +/// +/// # Examples +/// +/// ``` +/// # use serde::{Deserialize, Serialize}; +/// # +/// #[derive(Deserialize, Serialize)] +/// struct A { +/// #[serde(with = "serde_with::json::nested")] +/// other_struct: B, +/// } +/// #[derive(Deserialize, Serialize)] +/// struct B { +/// value: usize, +/// } +/// +/// let v: A = serde_json::from_str(r#"{"other_struct":"{\"value\":5}"}"#).unwrap(); +/// assert_eq!(5, v.other_struct.value); +/// +/// let x = A { +/// other_struct: B { value: 10 }, +/// }; +/// assert_eq!( +/// r#"{"other_struct":"{\"value\":10}"}"#, +/// serde_json::to_string(&x).unwrap() +/// ); +/// ``` +pub mod nested { + use core::{fmt, marker::PhantomData}; + use serde::{ + de::{DeserializeOwned, Deserializer, Error, Visitor}, + ser::{self, Serialize, Serializer}, + }; + + /// Deserialize value from a string which is valid JSON + pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error> + where + D: Deserializer<'de>, + T: DeserializeOwned, + { + #[derive(Default)] + struct Helper<S: DeserializeOwned>(PhantomData<S>); + + impl<'de, S> Visitor<'de> for Helper<S> + where + S: DeserializeOwned, + { + type Value = S; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(formatter, "valid json object") + } + + fn visit_str<E>(self, value: &str) -> Result<S, E> + where + E: Error, + { + serde_json::from_str(value).map_err(Error::custom) + } + } + + deserializer.deserialize_str(Helper(PhantomData)) + } + + /// Serialize value as string containing JSON + /// + /// # Errors + /// + /// Serialization can fail if `T`'s implementation of `Serialize` decides to + /// fail, or if `T` contains a map with non-string keys. + pub fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error> + where + T: Serialize, + S: Serializer, + { + let s = serde_json::to_string(value).map_err(ser::Error::custom)?; + serializer.serialize_str(&*s) + } +} + +/// Serialize value as string containing JSON +/// +/// The same functionality is also available as [`serde_with::json::nested`][crate::json::nested] compatible with serde's with-annotation. +/// +/// # Examples +/// +/// ``` +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_with::{serde_as, json::JsonString}; +/// # +/// #[serde_as] +/// #[derive(Deserialize, Serialize)] +/// struct A { +/// #[serde_as(as = "JsonString")] +/// other_struct: B, +/// } +/// #[derive(Deserialize, Serialize)] +/// struct B { +/// value: usize, +/// } +/// +/// let v: A = serde_json::from_str(r#"{"other_struct":"{\"value\":5}"}"#).unwrap(); +/// assert_eq!(5, v.other_struct.value); +/// +/// let x = A { +/// other_struct: B { value: 10 }, +/// }; +/// assert_eq!( +/// r#"{"other_struct":"{\"value\":10}"}"#, +/// serde_json::to_string(&x).unwrap() +/// ); +/// # } +/// ``` +#[derive(Copy, Clone, Debug, Default)] +pub struct JsonString; + +impl<T> SerializeAs<T> for JsonString +where + T: Serialize, +{ + fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + crate::json::nested::serialize(source, serializer) + } +} + +impl<'de, T> DeserializeAs<'de, T> for JsonString +where + T: DeserializeOwned, +{ + fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error> + where + D: Deserializer<'de>, + { + crate::json::nested::deserialize(deserializer) + } +} diff --git a/third_party/rust/serde_with/src/lib.rs b/third_party/rust/serde_with/src/lib.rs new file mode 100644 index 0000000000..6285a6f695 --- /dev/null +++ b/third_party/rust/serde_with/src/lib.rs @@ -0,0 +1,2034 @@ +#![warn( + clippy::semicolon_if_nothing_returned, + missing_copy_implementations, + // missing_crate_level_docs, not available in MSRV + missing_debug_implementations, + missing_docs, + rust_2018_idioms, + trivial_casts, + trivial_numeric_casts, + unused_extern_crates, + unused_import_braces, + unused_qualifications, + variant_size_differences +)] +#![doc(test(attr(forbid(unsafe_code))))] +#![doc(test(attr(deny( + missing_copy_implementations, + missing_debug_implementations, + trivial_casts, + trivial_numeric_casts, + unused_extern_crates, + unused_import_braces, + unused_qualifications, +))))] +#![doc(test(attr(warn(rust_2018_idioms))))] +// Not needed for 2018 edition and conflicts with `rust_2018_idioms` +#![doc(test(no_crate_inject))] +#![doc(html_root_url = "https://docs.rs/serde_with/1.14.0")] +#![cfg_attr(docsrs, feature(doc_cfg))] +#![allow( + // clippy is broken and shows wrong warnings + // clippy on stable does not know yet about the lint name + unknown_lints, + // https://github.com/rust-lang/rust-clippy/issues/8560 + clippy::only_used_in_recursion, + // https://github.com/rust-lang/rust-clippy/issues/8867 + clippy::derive_partial_eq_without_eq, +)] +#![no_std] + +//! [![crates.io badge](https://img.shields.io/crates/v/serde_with.svg)](https://crates.io/crates/serde_with/) +//! [![Build Status](https://github.com/jonasbb/serde_with/workflows/Rust%20CI/badge.svg)](https://github.com/jonasbb/serde_with) +//! [![codecov](https://codecov.io/gh/jonasbb/serde_with/branch/master/graph/badge.svg)](https://codecov.io/gh/jonasbb/serde_with) +//! [![CII Best Practices](https://bestpractices.coreinfrastructure.org/projects/4322/badge)](https://bestpractices.coreinfrastructure.org/projects/4322) +//! [![Binder](https://img.shields.io/badge/Try%20on%20-binder-579ACA.svg?logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFkAAABZCAMAAABi1XidAAAB8lBMVEX///9XmsrmZYH1olJXmsr1olJXmsrmZYH1olJXmsr1olJXmsrmZYH1olL1olJXmsr1olJXmsrmZYH1olL1olJXmsrmZYH1olJXmsr1olL1olJXmsrmZYH1olL1olJXmsrmZYH1olL1olL0nFf1olJXmsrmZYH1olJXmsq8dZb1olJXmsrmZYH1olJXmspXmspXmsr1olL1olJXmsrmZYH1olJXmsr1olL1olJXmsrmZYH1olL1olLeaIVXmsrmZYH1olL1olL1olJXmsrmZYH1olLna31Xmsr1olJXmsr1olJXmsrmZYH1olLqoVr1olJXmsr1olJXmsrmZYH1olL1olKkfaPobXvviGabgadXmsqThKuofKHmZ4Dobnr1olJXmsr1olJXmspXmsr1olJXmsrfZ4TuhWn1olL1olJXmsqBi7X1olJXmspZmslbmMhbmsdemsVfl8ZgmsNim8Jpk8F0m7R4m7F5nLB6jbh7jbiDirOEibOGnKaMhq+PnaCVg6qWg6qegKaff6WhnpKofKGtnomxeZy3noG6dZi+n3vCcpPDcpPGn3bLb4/Mb47UbIrVa4rYoGjdaIbeaIXhoWHmZYHobXvpcHjqdHXreHLroVrsfG/uhGnuh2bwj2Hxk17yl1vzmljzm1j0nlX1olL3AJXWAAAAbXRSTlMAEBAQHx8gICAuLjAwMDw9PUBAQEpQUFBXV1hgYGBkcHBwcXl8gICAgoiIkJCQlJicnJ2goKCmqK+wsLC4usDAwMjP0NDQ1NbW3Nzg4ODi5+3v8PDw8/T09PX29vb39/f5+fr7+/z8/Pz9/v7+zczCxgAABC5JREFUeAHN1ul3k0UUBvCb1CTVpmpaitAGSLSpSuKCLWpbTKNJFGlcSMAFF63iUmRccNG6gLbuxkXU66JAUef/9LSpmXnyLr3T5AO/rzl5zj137p136BISy44fKJXuGN/d19PUfYeO67Znqtf2KH33Id1psXoFdW30sPZ1sMvs2D060AHqws4FHeJojLZqnw53cmfvg+XR8mC0OEjuxrXEkX5ydeVJLVIlV0e10PXk5k7dYeHu7Cj1j+49uKg7uLU61tGLw1lq27ugQYlclHC4bgv7VQ+TAyj5Zc/UjsPvs1sd5cWryWObtvWT2EPa4rtnWW3JkpjggEpbOsPr7F7EyNewtpBIslA7p43HCsnwooXTEc3UmPmCNn5lrqTJxy6nRmcavGZVt/3Da2pD5NHvsOHJCrdc1G2r3DITpU7yic7w/7Rxnjc0kt5GC4djiv2Sz3Fb2iEZg41/ddsFDoyuYrIkmFehz0HR2thPgQqMyQYb2OtB0WxsZ3BeG3+wpRb1vzl2UYBog8FfGhttFKjtAclnZYrRo9ryG9uG/FZQU4AEg8ZE9LjGMzTmqKXPLnlWVnIlQQTvxJf8ip7VgjZjyVPrjw1te5otM7RmP7xm+sK2Gv9I8Gi++BRbEkR9EBw8zRUcKxwp73xkaLiqQb+kGduJTNHG72zcW9LoJgqQxpP3/Tj//c3yB0tqzaml05/+orHLksVO+95kX7/7qgJvnjlrfr2Ggsyx0eoy9uPzN5SPd86aXggOsEKW2Prz7du3VID3/tzs/sSRs2w7ovVHKtjrX2pd7ZMlTxAYfBAL9jiDwfLkq55Tm7ifhMlTGPyCAs7RFRhn47JnlcB9RM5T97ASuZXIcVNuUDIndpDbdsfrqsOppeXl5Y+XVKdjFCTh+zGaVuj0d9zy05PPK3QzBamxdwtTCrzyg/2Rvf2EstUjordGwa/kx9mSJLr8mLLtCW8HHGJc2R5hS219IiF6PnTusOqcMl57gm0Z8kanKMAQg0qSyuZfn7zItsbGyO9QlnxY0eCuD1XL2ys/MsrQhltE7Ug0uFOzufJFE2PxBo/YAx8XPPdDwWN0MrDRYIZF0mSMKCNHgaIVFoBbNoLJ7tEQDKxGF0kcLQimojCZopv0OkNOyWCCg9XMVAi7ARJzQdM2QUh0gmBozjc3Skg6dSBRqDGYSUOu66Zg+I2fNZs/M3/f/Grl/XnyF1Gw3VKCez0PN5IUfFLqvgUN4C0qNqYs5YhPL+aVZYDE4IpUk57oSFnJm4FyCqqOE0jhY2SMyLFoo56zyo6becOS5UVDdj7Vih0zp+tcMhwRpBeLyqtIjlJKAIZSbI8SGSF3k0pA3mR5tHuwPFoa7N7reoq2bqCsAk1HqCu5uvI1n6JuRXI+S1Mco54YmYTwcn6Aeic+kssXi8XpXC4V3t7/ADuTNKaQJdScAAAAAElFTkSuQmCC)](https://mybinder.org/v2/gist/jonasbb/18b9aece4c17f617b1c2b3946d29eeb0/HEAD?filepath=serde-with-demo.ipynb) +//! +//! --- +//! +//! This crate provides custom de/serialization helpers to use in combination with [serde's with-annotation][with-annotation] and with the improved [`serde_as`][as-annotation]-annotation. +//! Some common use cases are: +//! +//! * De/Serializing a type using the `Display` and `FromStr` traits, e.g., for `u8`, `url::Url`, or `mime::Mime`. +//! Check [`DisplayFromStr`][] or [`serde_with::rust::display_fromstr`][display_fromstr] for details. +//! * Support for arrays larger than 32 elements or using const generics. +//! With `serde_as` large arrays are supported, even if they are nested in other types. +//! `[bool; 64]`, `Option<[u8; M]>`, and `Box<[[u8; 64]; N]>` are all supported, as [this examples shows](#large-and-const-generic-arrays). +//! * Skip serializing all empty `Option` types with [`#[skip_serializing_none]`][skip_serializing_none]. +//! * Apply a prefix to each field name of a struct, without changing the de/serialize implementations of the struct using [`with_prefix!`][]. +//! * Deserialize a comma separated list like `#hash,#tags,#are,#great` into a `Vec<String>`. +//! Check the documentation for [`serde_with::rust::StringWithSeparator::<CommaSeparator>`][StringWithSeparator]. +//! +//! ## Getting Help +//! +//! **Check out the [user guide][user guide] to find out more tips and tricks about this crate.** +//! +//! For further help using this crate you can [open a new discussion](https://github.com/jonasbb/serde_with/discussions/new) or ask on [users.rust-lang.org](https://users.rust-lang.org/). +//! For bugs, please open a [new issue](https://github.com/jonasbb/serde_with/issues/new) on GitHub. +//! +//! # Use `serde_with` in your Project +//! +//! Add this to your `Cargo.toml`: +//! +//! ```toml +//! [dependencies.serde_with] +//! version = "1.14.0" +//! features = [ "..." ] +//! ``` +//! +//! The crate contains different features for integration with other common crates. +//! Check the [feature flags][] section for information about all available features. +//! +//! # Examples +//! +//! Annotate your struct or enum to enable the custom de/serializer. +//! The `#[serde_as]` attribute must be place *before* the `#[derive]`. +//! +//! ## `DisplayFromStr` +//! +//! ```rust +//! # #[cfg(feature = "macros")] +//! # use serde::{Deserialize, Serialize}; +//! # #[cfg(feature = "macros")] +//! # use serde_with::{serde_as, DisplayFromStr}; +//! # #[cfg(feature = "macros")] +//! #[serde_as] +//! # #[derive(Debug, Eq, PartialEq)] +//! #[derive(Deserialize, Serialize)] +//! struct Foo { +//! // Serialize with Display, deserialize with FromStr +//! #[serde_as(as = "DisplayFromStr")] +//! bar: u8, +//! } +//! +//! # #[cfg(all(feature = "macros", feature = "json"))] { +//! // This will serialize +//! # let foo = +//! Foo {bar: 12} +//! # ; +//! +//! // into this JSON +//! # let json = r#" +//! {"bar": "12"} +//! # "#; +//! # assert_eq!(json.replace(" ", "").replace("\n", ""), serde_json::to_string(&foo).unwrap()); +//! # assert_eq!(foo, serde_json::from_str(&json).unwrap()); +//! # } +//! ``` +//! +//! ## Large and const-generic arrays +//! +//! serde does not support arrays with more than 32 elements or using const-generics. +//! The `serde_as` attribute allows circumventing this restriction, even for nested types and nested arrays. +//! +//! ```rust +//! # #[cfg(FALSE)] { +//! # #[cfg(feature = "macros")] +//! # use serde::{Deserialize, Serialize}; +//! # #[cfg(feature = "macros")] +//! # use serde_with::serde_as; +//! # #[cfg(feature = "macros")] +//! #[serde_as] +//! # #[derive(Debug, Eq, PartialEq)] +//! #[derive(Deserialize, Serialize)] +//! struct Arrays<const N: usize, const M: usize> { +//! #[serde_as(as = "[_; N]")] +//! constgeneric: [bool; N], +//! +//! #[serde_as(as = "Box<[[_; 64]; N]>")] +//! nested: Box<[[u8; 64]; N]>, +//! +//! #[serde_as(as = "Option<[_; M]>")] +//! optional: Option<[u8; M]>, +//! } +//! +//! # #[cfg(all(feature = "macros", feature = "json"))] { +//! // This allows us to serialize a struct like this +//! let arrays: Arrays<100, 128> = Arrays { +//! constgeneric: [true; 100], +//! nested: Box::new([[111; 64]; 100]), +//! optional: Some([222; 128]) +//! }; +//! assert!(serde_json::to_string(&arrays).is_ok()); +//! # } +//! # } +//! ``` +//! +//! ## `skip_serializing_none` +//! +//! This situation often occurs with JSON, but other formats also support optional fields. +//! If many fields are optional, putting the annotations on the structs can become tedious. +//! The `#[skip_serializing_none]` attribute must be place *before* the `#[derive]`. +//! +//! ```rust +//! # #[cfg(feature = "macros")] +//! # use serde::{Deserialize, Serialize}; +//! # #[cfg(feature = "macros")] +//! # use serde_with::skip_serializing_none; +//! # #[cfg(feature = "macros")] +//! #[skip_serializing_none] +//! # #[derive(Debug, Eq, PartialEq)] +//! #[derive(Deserialize, Serialize)] +//! struct Foo { +//! a: Option<usize>, +//! b: Option<usize>, +//! c: Option<usize>, +//! d: Option<usize>, +//! e: Option<usize>, +//! f: Option<usize>, +//! g: Option<usize>, +//! } +//! +//! # #[cfg(all(feature = "macros", feature = "json"))] { +//! // This will serialize +//! # let foo = +//! Foo {a: None, b: None, c: None, d: Some(4), e: None, f: None, g: Some(7)} +//! # ; +//! +//! // into this JSON +//! # let json = r#" +//! {"d": 4, "g": 7} +//! # "#; +//! # assert_eq!(json.replace(" ", "").replace("\n", ""), serde_json::to_string(&foo).unwrap()); +//! # assert_eq!(foo, serde_json::from_str(&json).unwrap()); +//! # } +//! ``` +//! +//! ## Advanced `serde_as` usage +//! +//! This example is mainly supposed to highlight the flexibility of the `serde_as`-annotation compared to [serde's with-annotation][with-annotation]. +//! More details about `serde_as` can be found in the [user guide][]. +//! +//! ```rust +//! # #[cfg(all(feature = "macros", feature = "hex"))] +//! # use { +//! # serde::{Deserialize, Serialize}, +//! # serde_with::{serde_as, DisplayFromStr, DurationSeconds, hex::Hex}, +//! # std::time::Duration, +//! # std::collections::BTreeMap, +//! # }; +//! # #[cfg(all(feature = "macros", feature = "hex"))] +//! #[serde_as] +//! # #[derive(Debug, Eq, PartialEq)] +//! #[derive(Deserialize, Serialize)] +//! struct Foo { +//! // Serialize them into a list of number as seconds +//! #[serde_as(as = "Vec<DurationSeconds>")] +//! durations: Vec<Duration>, +//! // We can treat a Vec like a map with duplicates. +//! // JSON only allows string keys, so convert i32 to strings +//! // The bytes will be hex encoded +//! #[serde_as(as = "BTreeMap<DisplayFromStr, Hex>")] +//! bytes: Vec<(i32, Vec<u8>)>, +//! } +//! +//! # #[cfg(all(feature = "macros", feature = "json", feature = "hex"))] { +//! // This will serialize +//! # let foo = +//! Foo { +//! durations: vec![Duration::new(5, 0), Duration::new(3600, 0), Duration::new(0, 0)], +//! bytes: vec![ +//! (1, vec![0, 1, 2]), +//! (-100, vec![100, 200, 255]), +//! (1, vec![0, 111, 222]), +//! ], +//! } +//! # ; +//! +//! // into this JSON +//! # let json = r#" +//! { +//! "durations": [5, 3600, 0], +//! "bytes": { +//! "1": "000102", +//! "-100": "64c8ff", +//! "1": "006fde" +//! } +//! } +//! # "#; +//! # assert_eq!(json.replace(" ", "").replace("\n", ""), serde_json::to_string(&foo).unwrap()); +//! # assert_eq!(foo, serde_json::from_str(&json).unwrap()); +//! # } +//! ``` +//! +//! [`DisplayFromStr`]: https://docs.rs/serde_with/1.14.0/serde_with/struct.DisplayFromStr.html +//! [`with_prefix!`]: https://docs.rs/serde_with/1.14.0/serde_with/macro.with_prefix.html +//! [display_fromstr]: https://docs.rs/serde_with/1.14.0/serde_with/rust/display_fromstr/index.html +//! [feature flags]: https://docs.rs/serde_with/1.14.0/serde_with/guide/feature_flags/index.html +//! [skip_serializing_none]: https://docs.rs/serde_with/1.14.0/serde_with/attr.skip_serializing_none.html +//! [StringWithSeparator]: https://docs.rs/serde_with/1.14.0/serde_with/rust/struct.StringWithSeparator.html +//! [user guide]: https://docs.rs/serde_with/1.14.0/serde_with/guide/index.html +//! [with-annotation]: https://serde.rs/field-attrs.html#with +//! [as-annotation]: https://docs.rs/serde_with/1.14.0/serde_with/guide/serde_as/index.html + +extern crate alloc; +#[doc(hidden)] +pub extern crate serde; +extern crate std; + +#[cfg(feature = "base64")] +#[cfg_attr(docsrs, doc(cfg(feature = "base64")))] +pub mod base64; +#[cfg(feature = "chrono")] +#[cfg_attr(docsrs, doc(cfg(feature = "chrono")))] +pub mod chrono; +mod content; +pub mod de; +mod duplicate_key_impls; +mod enum_map; +mod flatten_maybe; +pub mod formats; +#[cfg(feature = "hex")] +#[cfg_attr(docsrs, doc(cfg(feature = "hex")))] +pub mod hex; +#[cfg(feature = "json")] +#[cfg_attr(docsrs, doc(cfg(feature = "json")))] +pub mod json; +pub mod rust; +pub mod ser; +mod serde_conv; +#[cfg(feature = "time_0_3")] +#[cfg_attr(docsrs, doc(cfg(feature = "time_0_3")))] +pub mod time_0_3; +mod utils; +#[doc(hidden)] +pub mod with_prefix; + +// Taken from shepmaster/snafu +// Originally licensed as MIT+Apache 2 +// https://github.com/shepmaster/snafu/blob/fd37d79d4531ed1d3eebffad0d658928eb860cfe/src/lib.rs#L121-L165 +#[cfg(feature = "guide")] +#[allow(unused_macro_rules)] +macro_rules! generate_guide { + (pub mod $name:ident; $($rest:tt)*) => { + generate_guide!(@gen ".", pub mod $name { } $($rest)*); + }; + (pub mod $name:ident { $($children:tt)* } $($rest:tt)*) => { + generate_guide!(@gen ".", pub mod $name { $($children)* } $($rest)*); + }; + (@gen $prefix:expr, ) => {}; + (@gen $prefix:expr, pub mod $name:ident; $($rest:tt)*) => { + generate_guide!(@gen $prefix, pub mod $name { } $($rest)*); + }; + (@gen $prefix:expr, @code pub mod $name:ident; $($rest:tt)*) => { + pub mod $name; + generate_guide!(@gen $prefix, $($rest)*); + }; + (@gen $prefix:expr, pub mod $name:ident { $($children:tt)* } $($rest:tt)*) => { + doc_comment::doc_comment! { + include_str!(concat!($prefix, "/", stringify!($name), ".md")), + pub mod $name { + generate_guide!(@gen concat!($prefix, "/", stringify!($name)), $($children)*); + } + } + generate_guide!(@gen $prefix, $($rest)*); + }; +} + +#[cfg(feature = "guide")] +generate_guide! { + pub mod guide { + pub mod feature_flags; + pub mod serde_as; + pub mod serde_as_transformations; + } +} + +#[doc(inline)] +pub use crate::{ + de::DeserializeAs, enum_map::EnumMap, rust::StringWithSeparator, ser::SerializeAs, +}; +use core::marker::PhantomData; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; +// Re-Export all proc_macros, as these should be seen as part of the serde_with crate +#[cfg(feature = "macros")] +#[cfg_attr(docsrs, doc(cfg(feature = "macros")))] +#[doc(inline)] +pub use serde_with_macros::*; + +/// Separator for string-based collection de/serialization +pub trait Separator { + /// Return the string delimiting two elements in the string-based collection + fn separator() -> &'static str; +} + +/// Predefined separator using a single space +#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)] +pub struct SpaceSeparator; + +impl Separator for SpaceSeparator { + #[inline] + fn separator() -> &'static str { + " " + } +} + +/// Predefined separator using a single comma +#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)] +pub struct CommaSeparator; + +impl Separator for CommaSeparator { + #[inline] + fn separator() -> &'static str { + "," + } +} + +/// Adapter to convert from `serde_as` to the serde traits. +/// +/// The `As` type adapter allows using types which implement [`DeserializeAs`] or [`SerializeAs`] in place of serde's with-annotation. +/// The with-annotation allows running custom code when de/serializing, however it is quite inflexible. +/// The traits [`DeserializeAs`]/[`SerializeAs`] are more flexible, as they allow composition and nesting of types to create more complex de/serialization behavior. +/// However, they are not directly compatible with serde, as they are not provided by serde. +/// The `As` type adapter makes them compatible, by forwarding the function calls to `serialize`/`deserialize` to the corresponding functions `serialize_as` and `deserialize_as`. +/// +/// It is not required to use this type directly. +/// Instead, it is highly encouraged to use the [`#[serde_as]`][serde_as] attribute since it includes further usability improvements. +/// If the use of the use of the proc-macro is not acceptable, then `As` can be used directly with serde. +/// +/// ```rust +/// # use serde::{Deserialize, Serialize}; +/// # use serde_with::{As, DisplayFromStr}; +/// # +/// #[derive(Deserialize, Serialize)] +/// # struct S { +/// // Serialize numbers as sequence of strings, using Display and FromStr +/// #[serde(with = "As::<Vec<DisplayFromStr>>")] +/// field: Vec<u8>, +/// # } +/// ``` +/// If the normal `Deserialize`/`Serialize` traits should be used, the placeholder type [`Same`] can be used. +/// It implements [`DeserializeAs`][]/[`SerializeAs`][], when the underlying type implements `Deserialize`/`Serialize`. +/// +/// ```rust +/// # use serde::{Deserialize, Serialize}; +/// # use serde_with::{As, DisplayFromStr, Same}; +/// # use std::collections::BTreeMap; +/// # +/// #[derive(Deserialize, Serialize)] +/// # struct S { +/// // Serialize map, turn keys into strings but keep type of value +/// #[serde(with = "As::<BTreeMap<DisplayFromStr, Same>>")] +/// field: BTreeMap<u8, i32>, +/// # } +/// ``` +/// +/// [serde_as]: https://docs.rs/serde_with/1.14.0/serde_with/attr.serde_as.html +#[derive(Copy, Clone, Debug, Default)] +pub struct As<T: ?Sized>(PhantomData<T>); + +impl<T: ?Sized> As<T> { + /// Serialize type `T` using [`SerializeAs`][] + /// + /// The function signature is compatible with [serde's with-annotation][with-annotation]. + /// + /// [with-annotation]: https://serde.rs/field-attrs.html#with + pub fn serialize<S, I>(value: &I, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + T: SerializeAs<I>, + I: ?Sized, + { + T::serialize_as(value, serializer) + } + + /// Deserialize type `T` using [`DeserializeAs`][] + /// + /// The function signature is compatible with [serde's with-annotation][with-annotation]. + /// + /// [with-annotation]: https://serde.rs/field-attrs.html#with + pub fn deserialize<'de, D, I>(deserializer: D) -> Result<I, D::Error> + where + T: DeserializeAs<'de, I>, + D: Deserializer<'de>, + { + T::deserialize_as(deserializer) + } +} + +/// Adapter to convert from `serde_as` to the serde traits. +/// +/// This is the counter-type to [`As`][]. +/// It can be used whenever a type implementing [`DeserializeAs`][]/[`SerializeAs`][] is required but the normal `Deserialize`/`Serialize` traits should be used. +/// Check [`As`] for an example. +#[derive(Copy, Clone, Debug, Default)] +pub struct Same; + +/// De/Serialize using [`Display`] and [`FromStr`] implementation +/// +/// This allows deserializing a string as a number. +/// It can be very useful for serialization formats like JSON, which do not support integer +/// numbers and have to resort to strings to represent them. +/// +/// Another use case is types with [`Display`] and [`FromStr`] implementations, but without serde +/// support, which can be found in some crates. +/// +/// If you control the type you want to de/serialize, you can instead use the two derive macros, [`SerializeDisplay`] and [`DeserializeFromStr`]. +/// They properly implement the traits [`Serialize`] and [`Deserialize`] such that user of the type no longer have to use the `serde_as` system. +/// +/// The same functionality is also available as [`serde_with::rust::display_fromstr`][crate::rust::display_fromstr] compatible with serde's with-annotation. +/// +/// # Examples +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_json::json; +/// # use serde_with::{serde_as, DisplayFromStr}; +/// # +/// #[serde_as] +/// #[derive(Deserialize, Serialize)] +/// struct A { +/// #[serde_as(as = "DisplayFromStr")] +/// mime: mime::Mime, +/// #[serde_as(as = "DisplayFromStr")] +/// number: u32, +/// } +/// +/// let v: A = serde_json::from_value(json!({ +/// "mime": "text/plain", +/// "number": "159", +/// })).unwrap(); +/// assert_eq!(mime::TEXT_PLAIN, v.mime); +/// assert_eq!(159, v.number); +/// +/// let x = A { +/// mime: mime::STAR_STAR, +/// number: 777, +/// }; +/// assert_eq!(json!({ "mime": "*/*", "number": "777" }), serde_json::to_value(&x).unwrap()); +/// # } +/// ``` +/// +/// [`Display`]: std::fmt::Display +/// [`FromStr`]: std::str::FromStr +#[derive(Copy, Clone, Debug, Default)] +pub struct DisplayFromStr; + +/// De/Serialize a [`Option<String>`] type while transforming the empty string to [`None`] +/// +/// Convert an [`Option<T>`] from/to string using [`FromStr`] and [`AsRef<str>`] implementations. +/// An empty string is deserialized as [`None`] and a [`None`] vice versa. +/// +/// The same functionality is also available as [`serde_with::rust::string_empty_as_none`][crate::rust::string_empty_as_none] compatible with serde's with-annotation. +/// +/// # Examples +/// +/// ``` +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_json::json; +/// # use serde_with::{serde_as, NoneAsEmptyString}; +/// # +/// #[serde_as] +/// #[derive(Deserialize, Serialize)] +/// struct A { +/// #[serde_as(as = "NoneAsEmptyString")] +/// tags: Option<String>, +/// } +/// +/// let v: A = serde_json::from_value(json!({ "tags": "" })).unwrap(); +/// assert_eq!(None, v.tags); +/// +/// let v: A = serde_json::from_value(json!({ "tags": "Hi" })).unwrap(); +/// assert_eq!(Some("Hi".to_string()), v.tags); +/// +/// let x = A { +/// tags: Some("This is text".to_string()), +/// }; +/// assert_eq!(json!({ "tags": "This is text" }), serde_json::to_value(&x).unwrap()); +/// +/// let x = A { +/// tags: None, +/// }; +/// assert_eq!(json!({ "tags": "" }), serde_json::to_value(&x).unwrap()); +/// # } +/// ``` +/// +/// [`FromStr`]: std::str::FromStr +#[derive(Copy, Clone, Debug, Default)] +pub struct NoneAsEmptyString; + +/// Deserialize value and return [`Default`] on error +/// +/// The main use case is ignoring error while deserializing. +/// Instead of erroring, it simply deserializes the [`Default`] variant of the type. +/// It is not possible to find the error location, i.e., which field had a deserialization error, with this method. +/// During serialization this wrapper does nothing. +/// The serialization behavior of the underlying type is preserved. +/// The type must implement [`Default`] for this conversion to work. +/// +/// The same functionality is also available as [`serde_with::rust::default_on_error`][crate::rust::default_on_error] compatible with serde's with-annotation. +/// +/// # Examples +/// +/// ``` +/// # #[cfg(feature = "macros")] { +/// # use serde::Deserialize; +/// # use serde_with::{serde_as, DefaultOnError}; +/// # +/// #[serde_as] +/// #[derive(Deserialize, Debug)] +/// struct A { +/// #[serde_as(deserialize_as = "DefaultOnError")] +/// value: u32, +/// } +/// +/// let a: A = serde_json::from_str(r#"{"value": 123}"#).unwrap(); +/// assert_eq!(123, a.value); +/// +/// // null is of invalid type +/// let a: A = serde_json::from_str(r#"{"value": null}"#).unwrap(); +/// assert_eq!(0, a.value); +/// +/// // String is of invalid type +/// let a: A = serde_json::from_str(r#"{"value": "123"}"#).unwrap(); +/// assert_eq!(0, a.value); +/// +/// // Map is of invalid type +/// let a: A = dbg!(serde_json::from_str(r#"{"value": {}}"#)).unwrap(); +/// assert_eq!(0, a.value); +/// +/// // Missing entries still cause errors +/// assert!(serde_json::from_str::<A>(r#"{ }"#).is_err()); +/// # } +/// ``` +/// +/// Deserializing missing values can be supported by adding the `default` field attribute: +/// +/// ``` +/// # #[cfg(feature = "macros")] { +/// # use serde::Deserialize; +/// # use serde_with::{serde_as, DefaultOnError}; +/// # +/// #[serde_as] +/// #[derive(Deserialize)] +/// struct B { +/// #[serde_as(deserialize_as = "DefaultOnError")] +/// #[serde(default)] +/// value: u32, +/// } +/// +/// let b: B = serde_json::from_str(r#"{ }"#).unwrap(); +/// assert_eq!(0, b.value); +/// # } +/// ``` +/// +/// `DefaultOnError` can be combined with other conversion methods. +/// In this example, we deserialize a `Vec`, each element is deserialized from a string. +/// If the string does not parse as a number, then we get the default value of 0. +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_json::json; +/// # use serde_with::{serde_as, DefaultOnError, DisplayFromStr}; +/// # +/// #[serde_as] +/// #[derive(Serialize, Deserialize)] +/// struct C { +/// #[serde_as(as = "Vec<DefaultOnError<DisplayFromStr>>")] +/// value: Vec<u32>, +/// }; +/// +/// let c: C = serde_json::from_value(json!({ +/// "value": ["1", "2", "a3", "", {}, "6"] +/// })).unwrap(); +/// assert_eq!(vec![1, 2, 0, 0, 0, 6], c.value); +/// # } +/// ``` +#[derive(Copy, Clone, Debug, Default)] +pub struct DefaultOnError<T = Same>(PhantomData<T>); + +/// Deserialize [`Default`] from `null` values +/// +/// Instead of erroring on `null` values, it simply deserializes the [`Default`] variant of the type. +/// During serialization this wrapper does nothing. +/// The serialization behavior of the underlying type is preserved. +/// The type must implement [`Default`] for this conversion to work. +/// +/// The same functionality is also available as [`serde_with::rust::default_on_null`][crate::rust::default_on_null] compatible with serde's with-annotation. +/// +/// # Examples +/// +/// ``` +/// # #[cfg(feature = "macros")] { +/// # use serde::Deserialize; +/// # use serde_with::{serde_as, DefaultOnNull}; +/// # +/// #[serde_as] +/// #[derive(Deserialize, Debug)] +/// struct A { +/// #[serde_as(deserialize_as = "DefaultOnNull")] +/// value: u32, +/// } +/// +/// let a: A = serde_json::from_str(r#"{"value": 123}"#).unwrap(); +/// assert_eq!(123, a.value); +/// +/// // null values are deserialized into the default, here 0 +/// let a: A = serde_json::from_str(r#"{"value": null}"#).unwrap(); +/// assert_eq!(0, a.value); +/// # } +/// ``` +/// +/// `DefaultOnNull` can be combined with other conversion methods. +/// In this example, we deserialize a `Vec`, each element is deserialized from a string. +/// If we encounter null, then we get the default value of 0. +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_json::json; +/// # use serde_with::{serde_as, DefaultOnNull, DisplayFromStr}; +/// # +/// #[serde_as] +/// #[derive(Serialize, Deserialize)] +/// struct C { +/// #[serde_as(as = "Vec<DefaultOnNull<DisplayFromStr>>")] +/// value: Vec<u32>, +/// }; +/// +/// let c: C = serde_json::from_value(json!({ +/// "value": ["1", "2", null, null, "5"] +/// })).unwrap(); +/// assert_eq!(vec![1, 2, 0, 0, 5], c.value); +/// # } +/// ``` +#[derive(Copy, Clone, Debug, Default)] +pub struct DefaultOnNull<T = Same>(PhantomData<T>); + +/// Deserialize from bytes or string +/// +/// Any Rust [`String`] can be converted into bytes, i.e., `Vec<u8>`. +/// Accepting both as formats while deserializing can be helpful while interacting with language +/// which have a looser definition of string than Rust. +/// +/// # Example +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_json::json; +/// # use serde_with::{serde_as, BytesOrString}; +/// # +/// #[serde_as] +/// #[derive(Deserialize, Serialize)] +/// struct A { +/// #[serde_as(as = "BytesOrString")] +/// bytes_or_string: Vec<u8>, +/// } +/// +/// // Here we deserialize from a byte array ... +/// let j = json!({ +/// "bytes_or_string": [ +/// 0, +/// 1, +/// 2, +/// 3 +/// ] +/// }); +/// +/// let a: A = serde_json::from_value(j.clone()).unwrap(); +/// assert_eq!(vec![0, 1, 2, 3], a.bytes_or_string); +/// +/// // and serialization works too. +/// assert_eq!(j, serde_json::to_value(&a).unwrap()); +/// +/// // But we also support deserializing from a String +/// let j = json!({ +/// "bytes_or_string": "✨Works!" +/// }); +/// +/// let a: A = serde_json::from_value(j).unwrap(); +/// assert_eq!("✨Works!".as_bytes(), &*a.bytes_or_string); +/// # } +/// ``` +/// [`String`]: std::string::String +#[derive(Copy, Clone, Debug, Default)] +pub struct BytesOrString; + +/// De/Serialize Durations as number of seconds. +/// +/// De/serialize durations as number of seconds with subsecond precision. +/// Subsecond precision is *only* supported for [`DurationSecondsWithFrac`], but not for [`DurationSeconds`]. +/// You can configure the serialization format between integers, floats, and stringified numbers with the `FORMAT` specifier and configure the deserialization with the `STRICTNESS` specifier. +/// +/// The `STRICTNESS` specifier can either be [`formats::Strict`] or [`formats::Flexible`] and defaults to [`formats::Strict`]. +/// [`formats::Strict`] means that deserialization only supports the type given in `FORMAT`, e.g., if `FORMAT` is `u64` deserialization from a `f64` will error. +/// [`formats::Flexible`] means that deserialization will perform a best effort to extract the correct duration and allows deserialization from any type. +/// For example, deserializing `DurationSeconds<f64, Flexible>` will discard any subsecond precision during deserialization from `f64` and will parse a `String` as an integer number. +/// +/// This type also supports [`chrono::Duration`] with the `chrono`-[feature flag]. +/// This type also supports [`time::Duration`][::time_0_3::Duration] with the `time_0_3`-[feature flag]. +/// +/// | Duration Type | Converter | Available `FORMAT`s | +/// | --------------------- | ------------------------- | ---------------------- | +/// | `std::time::Duration` | `DurationSeconds` | `u64`, `f64`, `String` | +/// | `std::time::Duration` | `DurationSecondsWithFrac` | `f64`, `String` | +/// | `chrono::Duration` | `DurationSeconds` | `i64`, `f64`, `String` | +/// | `chrono::Duration` | `DurationSecondsWithFrac` | `f64`, `String` | +/// | `time::Duration` | `DurationSeconds` | `i64`, `f64`, `String` | +/// | `time::Duration` | `DurationSecondsWithFrac` | `f64`, `String` | +/// +/// # Examples +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_json::json; +/// # use serde_with::{serde_as, DurationSeconds}; +/// use std::time::Duration; +/// +/// #[serde_as] +/// # #[derive(Debug, PartialEq)] +/// #[derive(Deserialize, Serialize)] +/// struct Durations { +/// #[serde_as(as = "DurationSeconds<u64>")] +/// d_u64: Duration, +/// #[serde_as(as = "DurationSeconds<f64>")] +/// d_f64: Duration, +/// #[serde_as(as = "DurationSeconds<String>")] +/// d_string: Duration, +/// }; +/// +/// // Serialization +/// // See how the values get rounded, since subsecond precision is not allowed. +/// +/// let d = Durations { +/// d_u64: Duration::new(12345, 0), // Create from seconds and nanoseconds +/// d_f64: Duration::new(12345, 500_000_000), +/// d_string: Duration::new(12345, 999_999_999), +/// }; +/// // Observe the different data types +/// let expected = json!({ +/// "d_u64": 12345, +/// "d_f64": 12346.0, +/// "d_string": "12346", +/// }); +/// assert_eq!(expected, serde_json::to_value(&d).unwrap()); +/// +/// // Deserialization works too +/// // Subsecond precision in numbers will be rounded away +/// +/// let json = json!({ +/// "d_u64": 12345, +/// "d_f64": 12345.5, +/// "d_string": "12346", +/// }); +/// let expected = Durations { +/// d_u64: Duration::new(12345, 0), // Create from seconds and nanoseconds +/// d_f64: Duration::new(12346, 0), +/// d_string: Duration::new(12346, 0), +/// }; +/// assert_eq!(expected, serde_json::from_value(json).unwrap()); +/// # } +/// ``` +/// +/// [`chrono::Duration`] is also supported when using the `chrono` feature. +/// It is a signed duration, thus can be de/serialized as an `i64` instead of a `u64`. +/// +/// ```rust +/// # #[cfg(all(feature = "macros", feature = "chrono"))] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_json::json; +/// # use serde_with::{serde_as, DurationSeconds}; +/// # use chrono_crate::Duration; +/// # /* Ugliness to make the docs look nicer since I want to hide the rename of the chrono crate +/// use chrono::Duration; +/// # */ +/// +/// #[serde_as] +/// # #[derive(Debug, PartialEq)] +/// #[derive(Deserialize, Serialize)] +/// struct Durations { +/// #[serde_as(as = "DurationSeconds<i64>")] +/// d_i64: Duration, +/// #[serde_as(as = "DurationSeconds<f64>")] +/// d_f64: Duration, +/// #[serde_as(as = "DurationSeconds<String>")] +/// d_string: Duration, +/// }; +/// +/// // Serialization +/// // See how the values get rounded, since subsecond precision is not allowed. +/// +/// let d = Durations { +/// d_i64: Duration::seconds(-12345), +/// d_f64: Duration::seconds(-12345) + Duration::milliseconds(500), +/// d_string: Duration::seconds(12345) + Duration::nanoseconds(999_999_999), +/// }; +/// // Observe the different data types +/// let expected = json!({ +/// "d_i64": -12345, +/// "d_f64": -12345.0, +/// "d_string": "12346", +/// }); +/// assert_eq!(expected, serde_json::to_value(&d).unwrap()); +/// +/// // Deserialization works too +/// // Subsecond precision in numbers will be rounded away +/// +/// let json = json!({ +/// "d_i64": -12345, +/// "d_f64": -12345.5, +/// "d_string": "12346", +/// }); +/// let expected = Durations { +/// d_i64: Duration::seconds(-12345), +/// d_f64: Duration::seconds(-12346), +/// d_string: Duration::seconds(12346), +/// }; +/// assert_eq!(expected, serde_json::from_value(json).unwrap()); +/// # } +/// ``` +/// +/// [`chrono::Duration`]: chrono_crate::Duration +/// [feature flag]: https://docs.rs/serde_with/1.14.0/serde_with/guide/feature_flags/index.html +#[derive(Copy, Clone, Debug, Default)] +pub struct DurationSeconds< + FORMAT: formats::Format = u64, + STRICTNESS: formats::Strictness = formats::Strict, +>(PhantomData<(FORMAT, STRICTNESS)>); + +/// De/Serialize Durations as number of seconds. +/// +/// De/serialize durations as number of seconds with subsecond precision. +/// Subsecond precision is *only* supported for [`DurationSecondsWithFrac`], but not for [`DurationSeconds`]. +/// You can configure the serialization format between integers, floats, and stringified numbers with the `FORMAT` specifier and configure the deserialization with the `STRICTNESS` specifier. +/// +/// The `STRICTNESS` specifier can either be [`formats::Strict`] or [`formats::Flexible`] and defaults to [`formats::Strict`]. +/// [`formats::Strict`] means that deserialization only supports the type given in `FORMAT`, e.g., if `FORMAT` is `u64` deserialization from a `f64` will error. +/// [`formats::Flexible`] means that deserialization will perform a best effort to extract the correct duration and allows deserialization from any type. +/// For example, deserializing `DurationSeconds<f64, Flexible>` will discard any subsecond precision during deserialization from `f64` and will parse a `String` as an integer number. +/// +/// This type also supports [`chrono::Duration`] with the `chrono`-[feature flag]. +/// This type also supports [`time::Duration`][::time_0_3::Duration] with the `time_0_3`-[feature flag]. +/// +/// | Duration Type | Converter | Available `FORMAT`s | +/// | --------------------- | ------------------------- | ---------------------- | +/// | `std::time::Duration` | `DurationSeconds` | `u64`, `f64`, `String` | +/// | `std::time::Duration` | `DurationSecondsWithFrac` | `f64`, `String` | +/// | `chrono::Duration` | `DurationSeconds` | `i64`, `f64`, `String` | +/// | `chrono::Duration` | `DurationSecondsWithFrac` | `f64`, `String` | +/// | `time::Duration` | `DurationSeconds` | `i64`, `f64`, `String` | +/// | `time::Duration` | `DurationSecondsWithFrac` | `f64`, `String` | +/// +/// # Examples +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_json::json; +/// # use serde_with::{serde_as, DurationSecondsWithFrac}; +/// use std::time::Duration; +/// +/// #[serde_as] +/// # #[derive(Debug, PartialEq)] +/// #[derive(Deserialize, Serialize)] +/// struct Durations { +/// #[serde_as(as = "DurationSecondsWithFrac<f64>")] +/// d_f64: Duration, +/// #[serde_as(as = "DurationSecondsWithFrac<String>")] +/// d_string: Duration, +/// }; +/// +/// // Serialization +/// // See how the values get rounded, since subsecond precision is not allowed. +/// +/// let d = Durations { +/// d_f64: Duration::new(12345, 500_000_000), // Create from seconds and nanoseconds +/// d_string: Duration::new(12345, 999_999_000), +/// }; +/// // Observe the different data types +/// let expected = json!({ +/// "d_f64": 12345.5, +/// "d_string": "12345.999999", +/// }); +/// assert_eq!(expected, serde_json::to_value(&d).unwrap()); +/// +/// // Deserialization works too +/// // Subsecond precision in numbers will be rounded away +/// +/// let json = json!({ +/// "d_f64": 12345.5, +/// "d_string": "12345.987654", +/// }); +/// let expected = Durations { +/// d_f64: Duration::new(12345, 500_000_000), // Create from seconds and nanoseconds +/// d_string: Duration::new(12345, 987_654_000), +/// }; +/// assert_eq!(expected, serde_json::from_value(json).unwrap()); +/// # } +/// ``` +/// +/// [`chrono::Duration`] is also supported when using the `chrono` feature. +/// It is a signed duration, thus can be de/serialized as an `i64` instead of a `u64`. +/// +/// ```rust +/// # #[cfg(all(feature = "macros", feature = "chrono"))] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_json::json; +/// # use serde_with::{serde_as, DurationSecondsWithFrac}; +/// # use chrono_crate::Duration; +/// # /* Ugliness to make the docs look nicer since I want to hide the rename of the chrono crate +/// use chrono::Duration; +/// # */ +/// +/// #[serde_as] +/// # #[derive(Debug, PartialEq)] +/// #[derive(Deserialize, Serialize)] +/// struct Durations { +/// #[serde_as(as = "DurationSecondsWithFrac<f64>")] +/// d_f64: Duration, +/// #[serde_as(as = "DurationSecondsWithFrac<String>")] +/// d_string: Duration, +/// }; +/// +/// // Serialization +/// +/// let d = Durations { +/// d_f64: Duration::seconds(-12345) + Duration::milliseconds(500), +/// d_string: Duration::seconds(12345) + Duration::nanoseconds(999_999_000), +/// }; +/// // Observe the different data types +/// let expected = json!({ +/// "d_f64": -12344.5, +/// "d_string": "12345.999999", +/// }); +/// assert_eq!(expected, serde_json::to_value(&d).unwrap()); +/// +/// // Deserialization works too +/// +/// let json = json!({ +/// "d_f64": -12344.5, +/// "d_string": "12345.987", +/// }); +/// let expected = Durations { +/// d_f64: Duration::seconds(-12345) + Duration::milliseconds(500), +/// d_string: Duration::seconds(12345) + Duration::milliseconds(987), +/// }; +/// assert_eq!(expected, serde_json::from_value(json).unwrap()); +/// # } +/// ``` +/// +/// [`chrono::Duration`]: chrono_crate::Duration +/// [feature flag]: https://docs.rs/serde_with/1.14.0/serde_with/guide/feature_flags/index.html +#[derive(Copy, Clone, Debug, Default)] +pub struct DurationSecondsWithFrac< + FORMAT: formats::Format = f64, + STRICTNESS: formats::Strictness = formats::Strict, +>(PhantomData<(FORMAT, STRICTNESS)>); + +/// Equivalent to [`DurationSeconds`] with milli-seconds as base unit. +/// +/// This type is equivalent to [`DurationSeconds`] except that each unit represents 1 milli-second instead of 1 second for [`DurationSeconds`]. +#[derive(Copy, Clone, Debug, Default)] +pub struct DurationMilliSeconds< + FORMAT: formats::Format = u64, + STRICTNESS: formats::Strictness = formats::Strict, +>(PhantomData<(FORMAT, STRICTNESS)>); + +/// Equivalent to [`DurationSecondsWithFrac`] with milli-seconds as base unit. +/// +/// This type is equivalent to [`DurationSecondsWithFrac`] except that each unit represents 1 milli-second instead of 1 second for [`DurationSecondsWithFrac`]. +#[derive(Copy, Clone, Debug, Default)] +pub struct DurationMilliSecondsWithFrac< + FORMAT: formats::Format = f64, + STRICTNESS: formats::Strictness = formats::Strict, +>(PhantomData<(FORMAT, STRICTNESS)>); + +/// Equivalent to [`DurationSeconds`] with micro-seconds as base unit. +/// +/// This type is equivalent to [`DurationSeconds`] except that each unit represents 1 micro-second instead of 1 second for [`DurationSeconds`]. +#[derive(Copy, Clone, Debug, Default)] +pub struct DurationMicroSeconds< + FORMAT: formats::Format = u64, + STRICTNESS: formats::Strictness = formats::Strict, +>(PhantomData<(FORMAT, STRICTNESS)>); + +/// Equivalent to [`DurationSecondsWithFrac`] with micro-seconds as base unit. +/// +/// This type is equivalent to [`DurationSecondsWithFrac`] except that each unit represents 1 micro-second instead of 1 second for [`DurationSecondsWithFrac`]. +#[derive(Copy, Clone, Debug, Default)] +pub struct DurationMicroSecondsWithFrac< + FORMAT: formats::Format = f64, + STRICTNESS: formats::Strictness = formats::Strict, +>(PhantomData<(FORMAT, STRICTNESS)>); + +/// Equivalent to [`DurationSeconds`] with nano-seconds as base unit. +/// +/// This type is equivalent to [`DurationSeconds`] except that each unit represents 1 nano-second instead of 1 second for [`DurationSeconds`]. +#[derive(Copy, Clone, Debug, Default)] +pub struct DurationNanoSeconds< + FORMAT: formats::Format = u64, + STRICTNESS: formats::Strictness = formats::Strict, +>(PhantomData<(FORMAT, STRICTNESS)>); + +/// Equivalent to [`DurationSecondsWithFrac`] with nano-seconds as base unit. +/// +/// This type is equivalent to [`DurationSecondsWithFrac`] except that each unit represents 1 nano-second instead of 1 second for [`DurationSecondsWithFrac`]. +#[derive(Copy, Clone, Debug, Default)] +pub struct DurationNanoSecondsWithFrac< + FORMAT: formats::Format = f64, + STRICTNESS: formats::Strictness = formats::Strict, +>(PhantomData<(FORMAT, STRICTNESS)>); + +/// De/Serialize timestamps as seconds since the UNIX epoch +/// +/// De/serialize timestamps as seconds since the UNIX epoch. +/// Subsecond precision is *only* supported for [`TimestampSecondsWithFrac`], but not for [`TimestampSeconds`]. +/// You can configure the serialization format between integers, floats, and stringified numbers with the `FORMAT` specifier and configure the deserialization with the `STRICTNESS` specifier. +/// +/// The `STRICTNESS` specifier can either be [`formats::Strict`] or [`formats::Flexible`] and defaults to [`formats::Strict`]. +/// [`formats::Strict`] means that deserialization only supports the type given in `FORMAT`, e.g., if `FORMAT` is `i64` deserialization from a `f64` will error. +/// [`formats::Flexible`] means that deserialization will perform a best effort to extract the correct timestamp and allows deserialization from any type. +/// For example, deserializing `TimestampSeconds<f64, Flexible>` will discard any subsecond precision during deserialization from `f64` and will parse a `String` as an integer number. +/// +/// This type also supports [`chrono::DateTime`][DateTime] with the `chrono`-[feature flag]. +/// This type also supports [`time::OffsetDateTime`][::time_0_3::OffsetDateTime] and [`time::PrimitiveDateTime`][::time_0_3::PrimitiveDateTime] with the `time_0_3`-[feature flag]. +/// +/// | Timestamp Type | Converter | Available `FORMAT`s | +/// | ------------------------- | -------------------------- | ---------------------- | +/// | `std::time::SystemTime` | `TimestampSeconds` | `i64`, `f64`, `String` | +/// | `std::time::SystemTime` | `TimestampSecondsWithFrac` | `f64`, `String` | +/// | `chrono::DateTime<Utc>` | `TimestampSeconds` | `i64`, `f64`, `String` | +/// | `chrono::DateTime<Utc>` | `TimestampSecondsWithFrac` | `f64`, `String` | +/// | `chrono::DateTime<Local>` | `TimestampSeconds` | `i64`, `f64`, `String` | +/// | `chrono::DateTime<Local>` | `TimestampSecondsWithFrac` | `f64`, `String` | +/// | `time::OffsetDateTime` | `TimestampSeconds` | `i64`, `f64`, `String` | +/// | `time::OffsetDateTime` | `TimestampSecondsWithFrac` | `f64`, `String` | +/// | `time::PrimitiveDateTime` | `TimestampSeconds` | `i64`, `f64`, `String` | +/// | `time::PrimitiveDateTime` | `TimestampSecondsWithFrac` | `f64`, `String` | +/// +/// # Examples +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_json::json; +/// # use serde_with::{serde_as, TimestampSeconds}; +/// use std::time::{Duration, SystemTime}; +/// +/// #[serde_as] +/// # #[derive(Debug, PartialEq)] +/// #[derive(Deserialize, Serialize)] +/// struct Timestamps { +/// #[serde_as(as = "TimestampSeconds<i64>")] +/// st_i64: SystemTime, +/// #[serde_as(as = "TimestampSeconds<f64>")] +/// st_f64: SystemTime, +/// #[serde_as(as = "TimestampSeconds<String>")] +/// st_string: SystemTime, +/// }; +/// +/// // Serialization +/// // See how the values get rounded, since subsecond precision is not allowed. +/// +/// let ts = Timestamps { +/// st_i64: SystemTime::UNIX_EPOCH.checked_add(Duration::new(12345, 0)).unwrap(), +/// st_f64: SystemTime::UNIX_EPOCH.checked_add(Duration::new(12345, 500_000_000)).unwrap(), +/// st_string: SystemTime::UNIX_EPOCH.checked_add(Duration::new(12345, 999_999_999)).unwrap(), +/// }; +/// // Observe the different data types +/// let expected = json!({ +/// "st_i64": 12345, +/// "st_f64": 12346.0, +/// "st_string": "12346", +/// }); +/// assert_eq!(expected, serde_json::to_value(&ts).unwrap()); +/// +/// // Deserialization works too +/// // Subsecond precision in numbers will be rounded away +/// +/// let json = json!({ +/// "st_i64": 12345, +/// "st_f64": 12345.5, +/// "st_string": "12346", +/// }); +/// let expected = Timestamps { +/// st_i64: SystemTime::UNIX_EPOCH.checked_add(Duration::new(12345, 0)).unwrap(), +/// st_f64: SystemTime::UNIX_EPOCH.checked_add(Duration::new(12346, 0)).unwrap(), +/// st_string: SystemTime::UNIX_EPOCH.checked_add(Duration::new(12346, 0)).unwrap(), +/// }; +/// assert_eq!(expected, serde_json::from_value(json).unwrap()); +/// # } +/// ``` +/// +/// [`chrono::DateTime<Utc>`][DateTime] and [`chrono::DateTime<Local>`][DateTime] are also supported when using the `chrono` feature. +/// Like [`SystemTime`], it is a signed timestamp, thus can be de/serialized as an `i64`. +/// +/// ```rust +/// # #[cfg(all(feature = "macros", feature = "chrono"))] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_json::json; +/// # use serde_with::{serde_as, TimestampSeconds}; +/// # use chrono_crate::{DateTime, Local, TimeZone, Utc}; +/// # /* Ugliness to make the docs look nicer since I want to hide the rename of the chrono crate +/// use chrono::{DateTime, Local, TimeZone, Utc}; +/// # */ +/// +/// #[serde_as] +/// # #[derive(Debug, PartialEq)] +/// #[derive(Deserialize, Serialize)] +/// struct Timestamps { +/// #[serde_as(as = "TimestampSeconds<i64>")] +/// dt_i64: DateTime<Utc>, +/// #[serde_as(as = "TimestampSeconds<f64>")] +/// dt_f64: DateTime<Local>, +/// #[serde_as(as = "TimestampSeconds<String>")] +/// dt_string: DateTime<Utc>, +/// }; +/// +/// // Serialization +/// // See how the values get rounded, since subsecond precision is not allowed. +/// +/// let ts = Timestamps { +/// dt_i64: Utc.timestamp(-12345, 0), +/// dt_f64: Local.timestamp(-12345, 500_000_000), +/// dt_string: Utc.timestamp(12345, 999_999_999), +/// }; +/// // Observe the different data types +/// let expected = json!({ +/// "dt_i64": -12345, +/// "dt_f64": -12345.0, +/// "dt_string": "12346", +/// }); +/// assert_eq!(expected, serde_json::to_value(&ts).unwrap()); +/// +/// // Deserialization works too +/// // Subsecond precision in numbers will be rounded away +/// +/// let json = json!({ +/// "dt_i64": -12345, +/// "dt_f64": -12345.5, +/// "dt_string": "12346", +/// }); +/// let expected = Timestamps { +/// dt_i64: Utc.timestamp(-12345, 0), +/// dt_f64: Local.timestamp(-12346, 0), +/// dt_string: Utc.timestamp(12346, 0), +/// }; +/// assert_eq!(expected, serde_json::from_value(json).unwrap()); +/// # } +/// ``` +/// +/// [`SystemTime`]: std::time::SystemTime +/// [DateTime]: chrono_crate::DateTime +/// [feature flag]: https://docs.rs/serde_with/1.14.0/serde_with/guide/feature_flags/index.html +#[derive(Copy, Clone, Debug, Default)] +pub struct TimestampSeconds< + FORMAT: formats::Format = i64, + STRICTNESS: formats::Strictness = formats::Strict, +>(PhantomData<(FORMAT, STRICTNESS)>); + +/// De/Serialize timestamps as seconds since the UNIX epoch +/// +/// De/serialize timestamps as seconds since the UNIX epoch. +/// Subsecond precision is *only* supported for [`TimestampSecondsWithFrac`], but not for [`TimestampSeconds`]. +/// You can configure the serialization format between integers, floats, and stringified numbers with the `FORMAT` specifier and configure the deserialization with the `STRICTNESS` specifier. +/// +/// The `STRICTNESS` specifier can either be [`formats::Strict`] or [`formats::Flexible`] and defaults to [`formats::Strict`]. +/// [`formats::Strict`] means that deserialization only supports the type given in `FORMAT`, e.g., if `FORMAT` is `i64` deserialization from a `f64` will error. +/// [`formats::Flexible`] means that deserialization will perform a best effort to extract the correct timestamp and allows deserialization from any type. +/// For example, deserializing `TimestampSeconds<f64, Flexible>` will discard any subsecond precision during deserialization from `f64` and will parse a `String` as an integer number. +/// +/// This type also supports [`chrono::DateTime`][DateTime] and [`chrono::NaiveDateTime`][NaiveDateTime] with the `chrono`-[feature flag]. +/// This type also supports [`time::OffsetDateTime`][::time_0_3::OffsetDateTime] and [`time::PrimitiveDateTime`][::time_0_3::PrimitiveDateTime] with the `time_0_3`-[feature flag]. +/// +/// | Timestamp Type | Converter | Available `FORMAT`s | +/// | ------------------------- | -------------------------- | ---------------------- | +/// | `std::time::SystemTime` | `TimestampSeconds` | `i64`, `f64`, `String` | +/// | `std::time::SystemTime` | `TimestampSecondsWithFrac` | `f64`, `String` | +/// | `chrono::DateTime<Utc>` | `TimestampSeconds` | `i64`, `f64`, `String` | +/// | `chrono::DateTime<Utc>` | `TimestampSecondsWithFrac` | `f64`, `String` | +/// | `chrono::DateTime<Local>` | `TimestampSeconds` | `i64`, `f64`, `String` | +/// | `chrono::DateTime<Local>` | `TimestampSecondsWithFrac` | `f64`, `String` | +/// | `chrono::NaiveDateTime` | `TimestampSeconds` | `i64`, `f64`, `String` | +/// | `chrono::NaiveDateTime` | `TimestampSecondsWithFrac` | `f64`, `String` | +/// | `time::OffsetDateTime` | `TimestampSeconds` | `i64`, `f64`, `String` | +/// | `time::OffsetDateTime` | `TimestampSecondsWithFrac` | `f64`, `String` | +/// | `time::PrimitiveDateTime` | `TimestampSeconds` | `i64`, `f64`, `String` | +/// | `time::PrimitiveDateTime` | `TimestampSecondsWithFrac` | `f64`, `String` | +/// +/// # Examples +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_json::json; +/// # use serde_with::{serde_as, TimestampSecondsWithFrac}; +/// use std::time::{Duration, SystemTime}; +/// +/// #[serde_as] +/// # #[derive(Debug, PartialEq)] +/// #[derive(Deserialize, Serialize)] +/// struct Timestamps { +/// #[serde_as(as = "TimestampSecondsWithFrac<f64>")] +/// st_f64: SystemTime, +/// #[serde_as(as = "TimestampSecondsWithFrac<String>")] +/// st_string: SystemTime, +/// }; +/// +/// // Serialization +/// // See how the values get rounded, since subsecond precision is not allowed. +/// +/// let ts = Timestamps { +/// st_f64: SystemTime::UNIX_EPOCH.checked_add(Duration::new(12345, 500_000_000)).unwrap(), +/// st_string: SystemTime::UNIX_EPOCH.checked_add(Duration::new(12345, 999_999_000)).unwrap(), +/// }; +/// // Observe the different data types +/// let expected = json!({ +/// "st_f64": 12345.5, +/// "st_string": "12345.999999", +/// }); +/// assert_eq!(expected, serde_json::to_value(&ts).unwrap()); +/// +/// // Deserialization works too +/// // Subsecond precision in numbers will be rounded away +/// +/// let json = json!({ +/// "st_f64": 12345.5, +/// "st_string": "12345.987654", +/// }); +/// let expected = Timestamps { +/// st_f64: SystemTime::UNIX_EPOCH.checked_add(Duration::new(12345, 500_000_000)).unwrap(), +/// st_string: SystemTime::UNIX_EPOCH.checked_add(Duration::new(12345, 987_654_000)).unwrap(), +/// }; +/// assert_eq!(expected, serde_json::from_value(json).unwrap()); +/// # } +/// ``` +/// +/// [`chrono::DateTime<Utc>`][DateTime] and [`chrono::DateTime<Local>`][DateTime] are also supported when using the `chrono` feature. +/// Like [`SystemTime`], it is a signed timestamp, thus can be de/serialized as an `i64`. +/// +/// ```rust +/// # #[cfg(all(feature = "macros", feature = "chrono"))] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_json::json; +/// # use serde_with::{serde_as, TimestampSecondsWithFrac}; +/// # use chrono_crate::{DateTime, Local, TimeZone, Utc}; +/// # /* Ugliness to make the docs look nicer since I want to hide the rename of the chrono crate +/// use chrono::{DateTime, Local, TimeZone, Utc}; +/// # */ +/// +/// #[serde_as] +/// # #[derive(Debug, PartialEq)] +/// #[derive(Deserialize, Serialize)] +/// struct Timestamps { +/// #[serde_as(as = "TimestampSecondsWithFrac<f64>")] +/// dt_f64: DateTime<Utc>, +/// #[serde_as(as = "TimestampSecondsWithFrac<String>")] +/// dt_string: DateTime<Local>, +/// }; +/// +/// // Serialization +/// +/// let ts = Timestamps { +/// dt_f64: Utc.timestamp(-12345, 500_000_000), +/// dt_string: Local.timestamp(12345, 999_999_000), +/// }; +/// // Observe the different data types +/// let expected = json!({ +/// "dt_f64": -12344.5, +/// "dt_string": "12345.999999", +/// }); +/// assert_eq!(expected, serde_json::to_value(&ts).unwrap()); +/// +/// // Deserialization works too +/// +/// let json = json!({ +/// "dt_f64": -12344.5, +/// "dt_string": "12345.987", +/// }); +/// let expected = Timestamps { +/// dt_f64: Utc.timestamp(-12345, 500_000_000), +/// dt_string: Local.timestamp(12345, 987_000_000), +/// }; +/// assert_eq!(expected, serde_json::from_value(json).unwrap()); +/// # } +/// ``` +/// +/// [`SystemTime`]: std::time::SystemTime +/// [DateTime]: chrono_crate::DateTime +/// [NaiveDateTime]: chrono_crate::NaiveDateTime +/// [feature flag]: https://docs.rs/serde_with/1.14.0/serde_with/guide/feature_flags/index.html +#[derive(Copy, Clone, Debug, Default)] +pub struct TimestampSecondsWithFrac< + FORMAT: formats::Format = f64, + STRICTNESS: formats::Strictness = formats::Strict, +>(PhantomData<(FORMAT, STRICTNESS)>); + +/// Equivalent to [`TimestampSeconds`] with milli-seconds as base unit. +/// +/// This type is equivalent to [`TimestampSeconds`] except that each unit represents 1 milli-second instead of 1 second for [`TimestampSeconds`]. +#[derive(Copy, Clone, Debug, Default)] +pub struct TimestampMilliSeconds< + FORMAT: formats::Format = i64, + STRICTNESS: formats::Strictness = formats::Strict, +>(PhantomData<(FORMAT, STRICTNESS)>); + +/// Equivalent to [`TimestampSecondsWithFrac`] with milli-seconds as base unit. +/// +/// This type is equivalent to [`TimestampSecondsWithFrac`] except that each unit represents 1 milli-second instead of 1 second for [`TimestampSecondsWithFrac`]. +#[derive(Copy, Clone, Debug, Default)] +pub struct TimestampMilliSecondsWithFrac< + FORMAT: formats::Format = f64, + STRICTNESS: formats::Strictness = formats::Strict, +>(PhantomData<(FORMAT, STRICTNESS)>); + +/// Equivalent to [`TimestampSeconds`] with micro-seconds as base unit. +/// +/// This type is equivalent to [`TimestampSeconds`] except that each unit represents 1 micro-second instead of 1 second for [`TimestampSeconds`]. +#[derive(Copy, Clone, Debug, Default)] +pub struct TimestampMicroSeconds< + FORMAT: formats::Format = i64, + STRICTNESS: formats::Strictness = formats::Strict, +>(PhantomData<(FORMAT, STRICTNESS)>); + +/// Equivalent to [`TimestampSecondsWithFrac`] with micro-seconds as base unit. +/// +/// This type is equivalent to [`TimestampSecondsWithFrac`] except that each unit represents 1 micro-second instead of 1 second for [`TimestampSecondsWithFrac`]. +#[derive(Copy, Clone, Debug, Default)] +pub struct TimestampMicroSecondsWithFrac< + FORMAT: formats::Format = f64, + STRICTNESS: formats::Strictness = formats::Strict, +>(PhantomData<(FORMAT, STRICTNESS)>); + +/// Equivalent to [`TimestampSeconds`] with nano-seconds as base unit. +/// +/// This type is equivalent to [`TimestampSeconds`] except that each unit represents 1 nano-second instead of 1 second for [`TimestampSeconds`]. +#[derive(Copy, Clone, Debug, Default)] +pub struct TimestampNanoSeconds< + FORMAT: formats::Format = i64, + STRICTNESS: formats::Strictness = formats::Strict, +>(PhantomData<(FORMAT, STRICTNESS)>); + +/// Equivalent to [`TimestampSecondsWithFrac`] with nano-seconds as base unit. +/// +/// This type is equivalent to [`TimestampSecondsWithFrac`] except that each unit represents 1 nano-second instead of 1 second for [`TimestampSecondsWithFrac`]. +#[derive(Copy, Clone, Debug, Default)] +pub struct TimestampNanoSecondsWithFrac< + FORMAT: formats::Format = f64, + STRICTNESS: formats::Strictness = formats::Strict, +>(PhantomData<(FORMAT, STRICTNESS)>); + +/// Optimized handling of owned and borrowed byte representations. +/// +/// Serialization of byte sequences like `&[u8]` or `Vec<u8>` is quite inefficient since each value will be serialized individually. +/// This converter type optimizes the serialization and deserialization. +/// +/// This is a port of the [`serde_bytes`] crate making it compatible with the `serde_as`-annotation, which allows it to be used in more cases than provided by [`serde_bytes`]. +/// +/// The type provides de/serialization for these types: +/// +/// * `[u8; N]`, not possible using `serde_bytes` +/// * `&[u8; N]`, not possible using `serde_bytes` +/// * `&[u8]` +/// * `Box<[u8; N]>`, not possible using `serde_bytes` +/// * `Box<[u8]>` +/// * `Vec<u8>` +/// * `Cow<'_, [u8]>` +/// * `Cow<'_, [u8; N]>`, not possible using `serde_bytes` +/// +/// [`serde_bytes`]: https://crates.io/crates/serde_bytes +/// +/// # Examples +/// +/// ``` +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_with::{serde_as, Bytes}; +/// # use std::borrow::Cow; +/// # +/// #[serde_as] +/// # #[derive(Debug, PartialEq)] +/// #[derive(Deserialize, Serialize)] +/// struct Test<'a> { +/// # #[cfg(FALSE)] +/// #[serde_as(as = "Bytes")] +/// array: [u8; 15], +/// #[serde_as(as = "Bytes")] +/// boxed: Box<[u8]>, +/// #[serde_as(as = "Bytes")] +/// #[serde(borrow)] +/// cow: Cow<'a, [u8]>, +/// # #[cfg(FALSE)] +/// #[serde_as(as = "Bytes")] +/// #[serde(borrow)] +/// cow_array: Cow<'a, [u8; 15]>, +/// #[serde_as(as = "Bytes")] +/// vec: Vec<u8>, +/// } +/// +/// let value = Test { +/// # #[cfg(FALSE)] +/// array: b"0123456789ABCDE".clone(), +/// boxed: b"...".to_vec().into_boxed_slice(), +/// cow: Cow::Borrowed(b"FooBar"), +/// # #[cfg(FALSE)] +/// cow_array: Cow::Borrowed(&[42u8; 15]), +/// vec: vec![0x41, 0x61, 0x21], +/// }; +/// let expected = r#"( +/// array: "MDEyMzQ1Njc4OUFCQ0RF", +/// boxed: "Li4u", +/// cow: "Rm9vQmFy", +/// cow_array: "KioqKioqKioqKioqKioq", +/// vec: "QWEh", +/// )"#; +/// # drop(expected); +/// # // Create a fake expected value that doesn't use const generics +/// # let expected = r#"( +/// # boxed: "Li4u", +/// # cow: "Rm9vQmFy", +/// # vec: "QWEh", +/// # )"#; +/// +/// # let pretty_config = ron::ser::PrettyConfig::new() +/// # .new_line("\n".into()); +/// assert_eq!(expected, ron::ser::to_string_pretty(&value, pretty_config).unwrap()); +/// assert_eq!(value, ron::from_str(&expected).unwrap()); +/// # } +/// ``` +/// +/// Fully borrowed types can also be used but you'll need a Deserializer that +/// supports Serde's 0-copy deserialization: +/// +/// ``` +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_with::{serde_as, Bytes}; +/// # use std::borrow::Cow; +/// # +/// #[serde_as] +/// # #[derive(Debug, PartialEq)] +/// #[derive(Deserialize, Serialize)] +/// struct TestBorrows<'a> { +/// # #[cfg(FALSE)] +/// #[serde_as(as = "Bytes")] +/// #[serde(borrow)] +/// array_buf: &'a [u8; 15], +/// #[serde_as(as = "Bytes")] +/// #[serde(borrow)] +/// buf: &'a [u8], +/// } +/// +/// let value = TestBorrows { +/// # #[cfg(FALSE)] +/// array_buf: &[10u8; 15], +/// buf: &[20u8, 21u8, 22u8], +/// }; +/// let expected = r#"( +/// array_buf: "CgoKCgoKCgoKCgoKCgoK", +/// buf: "FBUW", +/// )"#; +/// # drop(expected); +/// # // Create a fake expected value that doesn't use const generics +/// # let expected = r#"( +/// # buf: "FBUW", +/// # )"#; +/// +/// # let pretty_config = ron::ser::PrettyConfig::new() +/// # .new_line("\n".into()); +/// assert_eq!(expected, ron::ser::to_string_pretty(&value, pretty_config).unwrap()); +/// // RON doesn't support borrowed deserialization of byte arrays +/// # } +/// ``` +/// +/// ## Alternative to [`BytesOrString`] +/// +/// The [`Bytes`] can replace [`BytesOrString`]. +/// [`Bytes`] is implemented for more types, which makes it better. +/// The serialization behavior of [`Bytes`] differs from [`BytesOrString`], therefore only `deserialize_as` should be used. +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::Deserialize; +/// # use serde_json::json; +/// # use serde_with::{serde_as, Bytes}; +/// # +/// #[serde_as] +/// # #[derive(Debug, PartialEq)] +/// #[derive(Deserialize, serde::Serialize)] +/// struct Test { +/// #[serde_as(deserialize_as = "Bytes")] +/// from_bytes: Vec<u8>, +/// #[serde_as(deserialize_as = "Bytes")] +/// from_str: Vec<u8>, +/// } +/// +/// // Different serialized values ... +/// let j = json!({ +/// "from_bytes": [70,111,111,45,66,97,114], +/// "from_str": "Foo-Bar", +/// }); +/// +/// // can be deserialized ... +/// let test = Test { +/// from_bytes: b"Foo-Bar".to_vec(), +/// from_str: b"Foo-Bar".to_vec(), +/// }; +/// assert_eq!(test, serde_json::from_value(j).unwrap()); +/// +/// // and serialization will always be a byte sequence +/// # assert_eq!(json!( +/// { +/// "from_bytes": [70,111,111,45,66,97,114], +/// "from_str": [70,111,111,45,66,97,114], +/// } +/// # ), serde_json::to_value(&test).unwrap()); +/// # } +/// ``` +#[derive(Copy, Clone, Debug, Default)] +pub struct Bytes; + +/// Deserialize one or many elements +/// +/// Sometimes it is desirable to have a shortcut in writing 1-element lists in a config file. +/// Usually, this is done by either writing a list or the list element itself. +/// This distinction is not semantically important on the Rust side, thus both forms should deserialize into the same `Vec`. +/// +/// The `OneOrMany` adapter achieves exactly this use case. +/// The serialization behavior can be tweaked to either always serialize as a list using `PreferMany` or to serialize as the inner element if possible using `PreferOne`. +/// By default, `PreferOne` is assumed, which can also be omitted like `OneOrMany<_>`. +/// +/// # Examples +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::Deserialize; +/// # use serde_json::json; +/// # use serde_with::{serde_as, OneOrMany}; +/// # use serde_with::formats::{PreferOne, PreferMany}; +/// # +/// #[serde_as] +/// # #[derive(Debug, PartialEq)] +/// #[derive(Deserialize, serde::Serialize)] +/// struct Data { +/// #[serde_as(deserialize_as = "OneOrMany<_, PreferOne>")] +/// countries: Vec<String>, +/// #[serde_as(deserialize_as = "OneOrMany<_, PreferMany>")] +/// cities: Vec<String>, +/// } +/// +/// // The adapter allows deserializing a `Vec` from either +/// // a single element +/// let j = json!({ +/// "countries": "Spain", +/// "cities": "Berlin", +/// }); +/// assert!(serde_json::from_value::<Data>(j).is_ok()); +/// +/// // or from a list. +/// let j = json!({ +/// "countries": ["Germany", "France"], +/// "cities": ["Amsterdam"], +/// }); +/// assert!(serde_json::from_value::<Data>(j).is_ok()); +/// +/// // For serialization you can choose how a single element should be encoded. +/// // Either directly, with `PreferOne` (default), or as a list with `PreferMany`. +/// let data = Data { +/// countries: vec!["Spain".to_string()], +/// cities: vec!["Berlin".to_string()], +/// }; +/// let j = json!({ +/// "countries": "Spain", +/// "cities": ["Berlin"], +/// }); +/// assert_eq!(data, serde_json::from_value(j).unwrap()); +/// # } +/// ``` +#[derive(Copy, Clone, Debug, Default)] +pub struct OneOrMany<T, FORMAT: formats::Format = formats::PreferOne>(PhantomData<(T, FORMAT)>); + +/// Try multiple deserialization options until one succeeds. +/// +/// This adapter allows you to specify a list of deserialization options. +/// They are tried in order and the first one working is applied. +/// Serialization always picks the first option. +/// +/// `PickFirst` has one type parameter which must be instantiated with a tuple of two, three, or four elements. +/// For example, `PickFirst<(_, DisplayFromStr)>` on a field of type `u32` allows deserializing from a number or from a string via the `FromStr` trait. +/// The value will be serialized as a number, since that is what the first type `_` indicates. +/// +/// # Examples +/// +/// Deserialize a number from either a number or a string. +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_json::json; +/// # use serde_with::{serde_as, DisplayFromStr, PickFirst}; +/// # +/// #[serde_as] +/// # #[derive(Debug, PartialEq)] +/// #[derive(Deserialize, Serialize)] +/// struct Data { +/// #[serde_as(as = "PickFirst<(_, DisplayFromStr)>")] +/// as_number: u32, +/// #[serde_as(as = "PickFirst<(DisplayFromStr, _)>")] +/// as_string: u32, +/// } +/// let data = Data { +/// as_number: 123, +/// as_string: 456 +/// }; +/// +/// // Both fields can be deserialized from numbers: +/// let j = json!({ +/// "as_number": 123, +/// "as_string": 456, +/// }); +/// assert_eq!(data, serde_json::from_value(j).unwrap()); +/// +/// // or from a string: +/// let j = json!({ +/// "as_number": "123", +/// "as_string": "456", +/// }); +/// assert_eq!(data, serde_json::from_value(j).unwrap()); +/// +/// // For serialization the first type in the tuple determines the behavior. +/// // The `as_number` field will use the normal `Serialize` behavior and produce a number, +/// // while `as_string` used `Display` to produce a string. +/// let expected = json!({ +/// "as_number": 123, +/// "as_string": "456", +/// }); +/// assert_eq!(expected, serde_json::to_value(&data).unwrap()); +/// # } +/// ``` +#[derive(Copy, Clone, Debug, Default)] +pub struct PickFirst<T>(PhantomData<T>); + +/// Serialize value by converting to/from a proxy type with serde support. +/// +/// This adapter serializes a type `O` by converting it into a second type `T` and serializing `T`. +/// Deserializing works analogue, by deserializing a `T` and then converting into `O`. +/// +/// ```rust +/// # #[cfg(FALSE)] { +/// struct S { +/// #[serde_as(as = "FromInto<T>")] +/// value: O, +/// } +/// # } +/// ``` +/// +/// For serialization `O` needs to be `O: Into<T> + Clone`. +/// For deserialization the opposite `T: Into<O>` is required. +/// The `Clone` bound is required since `serialize` operates on a reference but `Into` implementations on references are uncommon. +/// +/// **Note**: [`TryFromInto`] is the more generalized version of this adapter which uses the [`TryInto`](std::convert::TryInto) trait instead. +/// +/// # Example +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_json::json; +/// # use serde_with::{serde_as, FromInto}; +/// # +/// #[derive(Clone, Debug, PartialEq)] +/// struct Rgb { +/// red: u8, +/// green: u8, +/// blue: u8, +/// } +/// +/// # /* +/// impl From<(u8, u8, u8)> for Rgb { ... } +/// impl From<Rgb> for (u8, u8, u8) { ... } +/// # */ +/// # +/// # impl From<(u8, u8, u8)> for Rgb { +/// # fn from(v: (u8, u8, u8)) -> Self { +/// # Rgb { +/// # red: v.0, +/// # green: v.1, +/// # blue: v.2, +/// # } +/// # } +/// # } +/// # +/// # impl From<Rgb> for (u8, u8, u8) { +/// # fn from(v: Rgb) -> Self { +/// # (v.red, v.green, v.blue) +/// # } +/// # } +/// +/// #[serde_as] +/// # #[derive(Debug, PartialEq)] +/// #[derive(Deserialize, Serialize)] +/// struct Color { +/// #[serde_as(as = "FromInto<(u8, u8, u8)>")] +/// rgb: Rgb, +/// } +/// let color = Color { +/// rgb: Rgb { +/// red: 128, +/// green: 64, +/// blue: 32, +/// }, +/// }; +/// +/// // Define our expected JSON form +/// let j = json!({ +/// "rgb": [128, 64, 32], +/// }); +/// // Ensure serialization and deserialization produce the expected results +/// assert_eq!(j, serde_json::to_value(&color).unwrap()); +/// assert_eq!(color, serde_json::from_value(j).unwrap()); +/// # } +/// ``` +#[derive(Copy, Clone, Debug, Default)] +pub struct FromInto<T>(PhantomData<T>); + +/// Serialize value by converting to/from a proxy type with serde support. +/// +/// This adapter serializes a type `O` by converting it into a second type `T` and serializing `T`. +/// Deserializing works analogue, by deserializing a `T` and then converting into `O`. +/// +/// ```rust +/// # #[cfg(FALSE)] { +/// struct S { +/// #[serde_as(as = "TryFromInto<T>")] +/// value: O, +/// } +/// # } +/// ``` +/// +/// For serialization `O` needs to be `O: TryInto<T> + Clone`. +/// For deserialization the opposite `T: TryInto<O>` is required. +/// The `Clone` bound is required since `serialize` operates on a reference but `TryInto` implementations on references are uncommon. +/// In both cases the `TryInto::Error` type must implement [`Display`](std::fmt::Display). +/// +/// **Note**: [`FromInto`] is the more specialized version of this adapter which uses the infallible [`Into`] trait instead. +/// [`TryFromInto`] is strictly more general and can also be used where [`FromInto`] is applicable. +/// The example shows a use case, when only the deserialization behavior is fallible, but not serializing. +/// +/// # Example +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_json::json; +/// # use serde_with::{serde_as, TryFromInto}; +/// # use std::convert::TryFrom; +/// # +/// #[derive(Clone, Debug, PartialEq)] +/// enum Boollike { +/// True, +/// False, +/// } +/// +/// # /* +/// impl From<Boollike> for u8 { ... } +/// # */ +/// # +/// impl TryFrom<u8> for Boollike { +/// type Error = String; +/// fn try_from(v: u8) -> Result<Self, Self::Error> { +/// match v { +/// 0 => Ok(Boollike::False), +/// 1 => Ok(Boollike::True), +/// _ => Err(format!("Boolikes can only be constructed from 0 or 1 but found {}", v)) +/// } +/// } +/// } +/// # +/// # impl From<Boollike> for u8 { +/// # fn from(v: Boollike) -> Self { +/// # match v { +/// # Boollike::True => 1, +/// # Boollike::False => 0, +/// # } +/// # } +/// # } +/// +/// #[serde_as] +/// # #[derive(Debug, PartialEq)] +/// #[derive(Deserialize, Serialize)] +/// struct Data { +/// #[serde_as(as = "TryFromInto<u8>")] +/// b: Boollike, +/// } +/// let data = Data { +/// b: Boollike::True, +/// }; +/// +/// // Define our expected JSON form +/// let j = json!({ +/// "b": 1, +/// }); +/// // Ensure serialization and deserialization produce the expected results +/// assert_eq!(j, serde_json::to_value(&data).unwrap()); +/// assert_eq!(data, serde_json::from_value(j).unwrap()); +/// +/// // Numbers besides 0 or 1 should be an error +/// let j = json!({ +/// "b": 2, +/// }); +/// assert_eq!("Boolikes can only be constructed from 0 or 1 but found 2", serde_json::from_value::<Data>(j).unwrap_err().to_string()); +/// # } +/// ``` +#[derive(Copy, Clone, Debug, Default)] +pub struct TryFromInto<T>(PhantomData<T>); + +/// Borrow `Cow` data during deserialization when possible. +/// +/// The types `Cow<'a, [u8]>`, `Cow<'a, [u8; N]>`, and `Cow<'a, str>` can borrow from the input data during deserialization. +/// serde supports this, by annotating the fields with `#[serde(borrow)]`. but does not support borrowing on nested types. +/// This gap is filled by this `BorrowCow` adapter. +/// +/// Using this adapter with `Cow<'a, [u8]>`/Cow<'a, [u8; N]>` will serialize the value as a sequence of `u8` values. +/// This *might* not allow to borrow the data during deserialization. +/// For a different format, which is also more efficient, use the [`Bytes`] adapter, which is also implemented for `Cow`. +/// +/// When combined with the [`serde_as`] attribute, the `#[serde(borrow)]` annotation will be added automatically. +/// If the annotation is wrong or too broad, for example because of multiple lifetime parameters, a manual annotation is required. +/// +/// # Examples +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_with::{serde_as, BorrowCow}; +/// # use std::borrow::Cow; +/// # +/// #[serde_as] +/// # #[derive(Debug, PartialEq)] +/// #[derive(Deserialize, Serialize)] +/// struct Data<'a, 'b, 'c> { +/// #[serde_as(as = "BorrowCow")] +/// str: Cow<'a, str>, +/// #[serde_as(as = "BorrowCow")] +/// slice: Cow<'b, [u8]>, +/// +/// #[serde_as(as = "Option<[BorrowCow; 1]>")] +/// nested: Option<[Cow<'c, str>; 1]>, +/// } +/// let data = Data { +/// str: "foobar".into(), +/// slice: b"foobar"[..].into(), +/// nested: Some(["HelloWorld".into()]), +/// }; +/// +/// // Define our expected JSON form +/// let j = r#"{ +/// "str": "foobar", +/// "slice": [ +/// 102, +/// 111, +/// 111, +/// 98, +/// 97, +/// 114 +/// ], +/// "nested": [ +/// "HelloWorld" +/// ] +/// }"#; +/// // Ensure serialization and deserialization produce the expected results +/// assert_eq!(j, serde_json::to_string_pretty(&data).unwrap()); +/// assert_eq!(data, serde_json::from_str(j).unwrap()); +/// +/// // Cow borrows from the input data +/// let deserialized: Data<'_, '_, '_> = serde_json::from_str(j).unwrap(); +/// assert!(matches!(deserialized.str, Cow::Borrowed(_))); +/// assert!(matches!(deserialized.nested, Some([Cow::Borrowed(_)]))); +/// // JSON does not allow borrowing bytes, so `slice` does not borrow +/// assert!(matches!(deserialized.slice, Cow::Owned(_))); +/// # } +/// ``` +#[derive(Copy, Clone, Debug, Default)] +pub struct BorrowCow; + +/// Deserialize a sequence into `Vec<T>`, skipping elements which fail to deserialize. +/// +/// The serialization behavior is identical to `Vec<T>`. This is an alternative to `Vec<T>` +/// which is resilient against unexpected data. +/// +/// # Examples +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_with::{serde_as, VecSkipError}; +/// # +/// # #[derive(Debug, PartialEq)] +/// #[derive(Deserialize, Serialize)] +/// # #[non_exhaustive] +/// enum Color { +/// Red, +/// Green, +/// Blue, +/// } +/// # use Color::*; +/// #[serde_as] +/// # #[derive(Debug, PartialEq)] +/// #[derive(Deserialize, Serialize)] +/// struct Palette(#[serde_as(as = "VecSkipError<_>")] Vec<Color>); +/// +/// let data = Palette(vec![Blue, Green,]); +/// let source_json = r#"["Blue", "Yellow", "Green"]"#; +/// let data_json = r#"["Blue","Green"]"#; +/// // Ensure serialization and deserialization produce the expected results +/// assert_eq!(data_json, serde_json::to_string(&data).unwrap()); +/// assert_eq!(data, serde_json::from_str(source_json).unwrap()); +/// # } +/// ``` + +#[derive(Copy, Clone, Debug, Default)] +pub struct VecSkipError<T>(PhantomData<T>); + +/// Deserialize a boolean from a number +/// +/// Deserialize a number (of `u8`) and turn it into a boolean. +/// The adapter supports a [`Strict`](crate::formats::Strict) and [`Flexible`](crate::formats::Flexible) format. +/// In `Strict` mode, the number must be `0` or `1`. +/// All other values produce an error. +/// In `Flexible` mode, the number any non-zero value is converted to `true`. +/// +/// During serialization only `0` or `1` are ever emitted. +/// +/// # Examples +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_json::json; +/// # use serde_with::{serde_as, BoolFromInt}; +/// # +/// #[serde_as] +/// # #[derive(Debug, PartialEq)] +/// #[derive(Deserialize, Serialize)] +/// struct Data(#[serde_as(as = "BoolFromInt")] bool); +/// +/// let data = Data(true); +/// let j = json!(1); +/// // Ensure serialization and deserialization produce the expected results +/// assert_eq!(j, serde_json::to_value(&data).unwrap()); +/// assert_eq!(data, serde_json::from_value(j).unwrap()); +/// +/// // false maps to 0 +/// let data = Data(false); +/// let j = json!(0); +/// assert_eq!(j, serde_json::to_value(&data).unwrap()); +/// assert_eq!(data, serde_json::from_value(j).unwrap()); +// +/// #[serde_as] +/// # #[derive(Debug, PartialEq)] +/// #[derive(Deserialize, Serialize)] +/// struct Flexible(#[serde_as(as = "BoolFromInt<serde_with::formats::Flexible>")] bool); +/// +/// // Flexible turns any non-zero number into true +/// let data = Flexible(true); +/// let j = json!(100); +/// assert_eq!(data, serde_json::from_value(j).unwrap()); +/// # } +/// ``` +#[derive(Copy, Clone, Debug, Default)] +pub struct BoolFromInt<S: formats::Strictness = formats::Strict>(PhantomData<S>); diff --git a/third_party/rust/serde_with/src/rust.rs b/third_party/rust/serde_with/src/rust.rs new file mode 100644 index 0000000000..cb64f0da9f --- /dev/null +++ b/third_party/rust/serde_with/src/rust.rs @@ -0,0 +1,1943 @@ +//! De/Serialization for Rust's builtin and std types + +use crate::{utils, Separator}; +#[cfg(doc)] +use alloc::collections::BTreeMap; +use alloc::{ + string::{String, ToString}, + vec::Vec, +}; +use core::{ + cmp::Eq, + fmt::{self, Display}, + hash::Hash, + iter::FromIterator, + marker::PhantomData, + str::FromStr, +}; +use serde::{ + de::{Deserialize, DeserializeOwned, Deserializer, Error, MapAccess, SeqAccess, Visitor}, + ser::{Serialize, Serializer}, +}; +#[cfg(doc)] +use std::collections::HashMap; + +/// De/Serialize using [`Display`] and [`FromStr`] implementation +/// +/// This allows deserializing a string as a number. +/// It can be very useful for serialization formats like JSON, which do not support integer +/// numbers and have to resort to strings to represent them. +/// +/// If you control the type you want to de/serialize, you can instead use the two derive macros, [`SerializeDisplay`] and [`DeserializeFromStr`]. +/// They properly implement the traits [`Serialize`] and [`Deserialize`] such that user of the type no longer have to use the with-attribute. +/// +/// ## Converting to `serde_as` +/// +/// The same functionality can be more clearly expressed via [`DisplayFromStr`] and using the [`serde_as`] macro. +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::Deserialize; +/// # use serde_with::{serde_as, DisplayFromStr}; +/// # +/// #[serde_as] +/// #[derive(Deserialize)] +/// struct A { +/// #[serde_as(as = "DisplayFromStr")] +/// value: mime::Mime, +/// } +/// # } +/// ``` +/// +/// # Examples +/// +/// ```rust +/// # use serde::{Deserialize, Serialize}; +/// # +/// #[derive(Deserialize, Serialize)] +/// struct A { +/// #[serde(with = "serde_with::rust::display_fromstr")] +/// mime: mime::Mime, +/// #[serde(with = "serde_with::rust::display_fromstr")] +/// number: u32, +/// } +/// +/// let v: A = serde_json::from_str(r#"{ +/// "mime": "text/plain", +/// "number": "159" +/// }"#).unwrap(); +/// assert_eq!(mime::TEXT_PLAIN, v.mime); +/// assert_eq!(159, v.number); +/// +/// let x = A { +/// mime: mime::STAR_STAR, +/// number: 777, +/// }; +/// assert_eq!( +/// r#"{"mime":"*/*","number":"777"}"#, +/// serde_json::to_string(&x).unwrap() +/// ); +/// ``` +/// +/// [`DeserializeFromStr`]: serde_with_macros::DeserializeFromStr +/// [`DisplayFromStr`]: crate::DisplayFromStr +/// [`serde_as`]: crate::guide::serde_as +/// [`SerializeDisplay`]: serde_with_macros::SerializeDisplay +pub mod display_fromstr { + use super::*; + + /// Deserialize T using [`FromStr`] + pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error> + where + D: Deserializer<'de>, + T: FromStr, + T::Err: Display, + { + struct Helper<S>(PhantomData<S>); + + impl<'de, S> Visitor<'de> for Helper<S> + where + S: FromStr, + <S as FromStr>::Err: Display, + { + type Value = S; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(formatter, "a string") + } + + fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> + where + E: Error, + { + value.parse::<Self::Value>().map_err(Error::custom) + } + } + + deserializer.deserialize_str(Helper(PhantomData)) + } + + /// Serialize T using [Display] + pub fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error> + where + T: Display, + S: Serializer, + { + serializer.collect_str(&value) + } +} + +/// De/Serialize sequences using [`FromIterator`] and [`IntoIterator`] implementation for it and [`Display`] and [`FromStr`] implementation for each element +/// +/// This allows to serialize and deserialize collections with elements which can be represented as strings. +/// +/// ## Converting to `serde_as` +/// +/// The same functionality can be more clearly expressed via [`DisplayFromStr`] and using the [`serde_as`] macro. +/// Instead of +/// +/// ```rust,ignore +/// #[serde(with = "serde_with::rust::seq_display_fromstr")] +/// addresses: BTreeSet<Ipv4Addr>, +/// ``` +/// you can write: +/// ```rust,ignore +/// #[serde_as(as = "BTreeSet<DisplayFromStr>")] +/// addresses: BTreeSet<Ipv4Addr>, +/// ``` +/// +/// This works for any container type, so also for `Vec`: +/// ```rust,ignore +/// #[serde_as(as = "Vec<DisplayFromStr>")] +/// bs: Vec<bool>, +/// ``` +/// +/// # Examples +/// +/// ``` +/// # use serde::{Deserialize, Serialize}; +/// # +/// use std::collections::BTreeSet; +/// use std::net::Ipv4Addr; +/// +/// #[derive(Deserialize, Serialize)] +/// struct A { +/// #[serde(with = "serde_with::rust::seq_display_fromstr")] +/// addresses: BTreeSet<Ipv4Addr>, +/// #[serde(with = "serde_with::rust::seq_display_fromstr")] +/// bs: Vec<bool>, +/// } +/// +/// let v: A = serde_json::from_str(r#"{ +/// "addresses": ["192.168.2.1", "192.168.2.2", "192.168.1.1", "192.168.2.2"], +/// "bs": ["true", "false"] +/// }"#).unwrap(); +/// assert_eq!(v.addresses.len(), 3); +/// assert!(v.addresses.contains(&Ipv4Addr::new(192, 168, 2, 1))); +/// assert!(v.addresses.contains(&Ipv4Addr::new(192, 168, 2, 2))); +/// assert!(!v.addresses.contains(&Ipv4Addr::new(192, 168, 1, 2))); +/// assert_eq!(v.bs.len(), 2); +/// assert!(v.bs[0]); +/// assert!(!v.bs[1]); +/// +/// let x = A { +/// addresses: vec![ +/// Ipv4Addr::new(127, 53, 0, 1), +/// Ipv4Addr::new(127, 53, 1, 1), +/// Ipv4Addr::new(127, 53, 0, 2) +/// ].into_iter().collect(), +/// bs: vec![false, true], +/// }; +/// assert_eq!( +/// r#"{"addresses":["127.53.0.1","127.53.0.2","127.53.1.1"],"bs":["false","true"]}"#, +/// serde_json::to_string(&x).unwrap() +/// ); +/// ``` +/// +/// [`DisplayFromStr`]: crate::DisplayFromStr +/// [`serde_as`]: crate::guide::serde_as +pub mod seq_display_fromstr { + use super::*; + + /// Deserialize collection T using [FromIterator] and [FromStr] for each element + pub fn deserialize<'de, D, T, I>(deserializer: D) -> Result<T, D::Error> + where + D: Deserializer<'de>, + T: FromIterator<I> + Sized, + I: FromStr, + I::Err: Display, + { + struct Helper<S>(PhantomData<S>); + + impl<'de, S> Visitor<'de> for Helper<S> + where + S: FromStr, + <S as FromStr>::Err: Display, + { + type Value = Vec<S>; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(formatter, "a sequence") + } + + fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error> + where + A: SeqAccess<'de>, + { + utils::SeqIter::new(seq) + .map(|res| { + res.and_then(|value: &str| value.parse::<S>().map_err(Error::custom)) + }) + .collect() + } + } + + deserializer + .deserialize_seq(Helper(PhantomData)) + .map(T::from_iter) + } + + /// Serialize collection T using [IntoIterator] and [Display] for each element + pub fn serialize<S, T, I>(value: &T, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + for<'a> &'a T: IntoIterator<Item = &'a I>, + I: Display, + { + struct SerializeString<'a, I>(&'a I); + + impl<'a, I> Serialize for SerializeString<'a, I> + where + I: Display, + { + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.collect_str(self.0) + } + } + + serializer.collect_seq(value.into_iter().map(SerializeString)) + } +} + +/// De/Serialize a delimited collection using [`Display`] and [`FromStr`] implementation +/// +/// You can define an arbitrary separator, by specifying a type which implements [`Separator`]. +/// Some common ones, like space and comma are already predefined and you can find them [here][Separator]. +/// +/// An empty string deserializes as an empty collection. +/// +/// ## Converting to `serde_as` +/// +/// The same functionality can also be expressed using the [`serde_as`] macro. +/// The usage is slightly different. +/// `StringWithSeparator` takes a second type, which needs to implement [`Display`]+[`FromStr`] and constitutes the inner type of the collection. +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::Deserialize; +/// # use serde_with::{serde_as, SpaceSeparator, StringWithSeparator}; +/// # +/// #[serde_as] +/// #[derive(Deserialize)] +/// struct A { +/// #[serde_as(as = "StringWithSeparator::<SpaceSeparator, String>")] +/// tags: Vec<String>, +/// } +/// # } +/// ``` +/// +/// # Examples +/// +/// ``` +/// # use serde::{Deserialize, Serialize}; +/// # +/// use serde_with::{CommaSeparator, SpaceSeparator}; +/// use std::collections::BTreeSet; +/// +/// #[derive(Deserialize, Serialize)] +/// struct A { +/// #[serde(with = "serde_with::rust::StringWithSeparator::<SpaceSeparator>")] +/// tags: Vec<String>, +/// #[serde(with = "serde_with::rust::StringWithSeparator::<CommaSeparator>")] +/// more_tags: BTreeSet<String>, +/// } +/// +/// let v: A = serde_json::from_str(r##"{ +/// "tags": "#hello #world", +/// "more_tags": "foo,bar,bar" +/// }"##).unwrap(); +/// assert_eq!(vec!["#hello", "#world"], v.tags); +/// assert_eq!(2, v.more_tags.len()); +/// +/// let x = A { +/// tags: vec!["1".to_string(), "2".to_string(), "3".to_string()], +/// more_tags: BTreeSet::new(), +/// }; +/// assert_eq!( +/// r#"{"tags":"1 2 3","more_tags":""}"#, +/// serde_json::to_string(&x).unwrap() +/// ); +/// ``` +/// +/// [`serde_as`]: crate::guide::serde_as +#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)] +pub struct StringWithSeparator<Sep, T = ()>(PhantomData<(Sep, T)>); + +impl<Sep> StringWithSeparator<Sep> +where + Sep: Separator, +{ + /// Serialize collection into a string with separator symbol + pub fn serialize<S, T, V>(values: T, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + T: IntoIterator<Item = V>, + V: Display, + { + let mut s = String::new(); + for v in values { + s.push_str(&*v.to_string()); + s.push_str(Sep::separator()); + } + serializer.serialize_str(if !s.is_empty() { + // remove trailing separator if present + &s[..s.len() - Sep::separator().len()] + } else { + &s[..] + }) + } + + /// Deserialize a collection from a string with separator symbol + pub fn deserialize<'de, D, T, V>(deserializer: D) -> Result<T, D::Error> + where + D: Deserializer<'de>, + T: FromIterator<V>, + V: FromStr, + V::Err: Display, + { + let s = String::deserialize(deserializer)?; + if s.is_empty() { + Ok(None.into_iter().collect()) + } else { + s.split(Sep::separator()) + .map(FromStr::from_str) + .collect::<Result<_, _>>() + .map_err(Error::custom) + } + } +} + +/// Makes a distinction between a missing, unset, or existing value +/// +/// Some serialization formats make a distinction between missing fields, fields with a `null` +/// value, and existing values. One such format is JSON. By default it is not easily possible to +/// differentiate between a missing value and a field which is `null`, as they deserialize to the +/// same value. This helper changes it, by using an `Option<Option<T>>` to deserialize into. +/// +/// * `None`: Represents a missing value. +/// * `Some(None)`: Represents a `null` value. +/// * `Some(Some(value))`: Represents an existing value. +/// +/// # Examples +/// +/// ```rust +/// # use serde::{Deserialize, Serialize}; +/// # +/// # #[derive(Debug, PartialEq, Eq)] +/// #[derive(Deserialize, Serialize)] +/// struct Doc { +/// #[serde( +/// default, // <- important for deserialization +/// skip_serializing_if = "Option::is_none", // <- important for serialization +/// with = "::serde_with::rust::double_option", +/// )] +/// a: Option<Option<u8>>, +/// } +/// // Missing Value +/// let s = r#"{}"#; +/// assert_eq!(Doc { a: None }, serde_json::from_str(s).unwrap()); +/// assert_eq!(s, serde_json::to_string(&Doc { a: None }).unwrap()); +/// +/// // Unset Value +/// let s = r#"{"a":null}"#; +/// assert_eq!(Doc { a: Some(None) }, serde_json::from_str(s).unwrap()); +/// assert_eq!(s, serde_json::to_string(&Doc { a: Some(None) }).unwrap()); +/// +/// // Existing Value +/// let s = r#"{"a":5}"#; +/// assert_eq!(Doc { a: Some(Some(5)) }, serde_json::from_str(s).unwrap()); +/// assert_eq!(s, serde_json::to_string(&Doc { a: Some(Some(5)) }).unwrap()); +/// ``` +#[allow(clippy::option_option)] +pub mod double_option { + use super::*; + + /// Deserialize potentially non-existing optional value + pub fn deserialize<'de, T, D>(deserializer: D) -> Result<Option<Option<T>>, D::Error> + where + T: Deserialize<'de>, + D: Deserializer<'de>, + { + Deserialize::deserialize(deserializer).map(Some) + } + + /// Serialize optional value + pub fn serialize<S, T>(values: &Option<Option<T>>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + T: Serialize, + { + match values { + None => serializer.serialize_unit(), + Some(None) => serializer.serialize_none(), + Some(Some(v)) => serializer.serialize_some(&v), + } + } +} + +/// Serialize inner value if [`Some`]`(T)`. If [`None`], serialize the unit struct `()`. +/// +/// When used in conjunction with `skip_serializing_if = "Option::is_none"` and +/// `default`, you can build an optional value by skipping if it is [`None`], or serializing its +/// inner value if [`Some`]`(T)`. +/// +/// Not all serialization formats easily support optional values. +/// While JSON uses the [`Option`] type to represent optional values and only serializes the inner +/// part of the [`Some`]`()`, other serialization formats, such as [RON][], choose to serialize the +/// [`Some`] around a value. +/// This helper helps building a truly optional value for such serializers. +/// +/// [RON]: https://github.com/ron-rs/ron +/// +/// # Example +/// +/// ```rust +/// # use serde::{Deserialize, Serialize}; +/// # +/// # #[derive(Debug, Eq, PartialEq)] +/// #[derive(Deserialize, Serialize)] +/// struct Doc { +/// mandatory: usize, +/// #[serde( +/// default, // <- important for deserialization +/// skip_serializing_if = "Option::is_none", // <- important for serialization +/// with = "::serde_with::rust::unwrap_or_skip", +/// )] +/// optional: Option<usize>, +/// } +/// +/// // Transparently add/remove Some() wrapper +/// # let pretty_config = ron::ser::PrettyConfig::new() +/// # .new_line("\n".into()); +/// let s = r#"( +/// mandatory: 1, +/// optional: 2, +/// )"#; +/// let v = Doc { +/// mandatory: 1, +/// optional: Some(2), +/// }; +/// assert_eq!(v, ron::de::from_str(s).unwrap()); +/// assert_eq!(s, ron::ser::to_string_pretty(&v, pretty_config).unwrap()); +/// +/// // Missing values are deserialized as `None` +/// // while `None` values are skipped during serialization. +/// # let pretty_config = ron::ser::PrettyConfig::new() +/// # .new_line("\n".into()); +/// let s = r#"( +/// mandatory: 1, +/// )"#; +/// let v = Doc { +/// mandatory: 1, +/// optional: None, +/// }; +/// assert_eq!(v, ron::de::from_str(s).unwrap()); +/// assert_eq!(s, ron::ser::to_string_pretty(&v, pretty_config).unwrap()); +/// ``` +pub mod unwrap_or_skip { + use super::*; + + /// Deserialize value wrapped in Some(T) + pub fn deserialize<'de, D, T>(deserializer: D) -> Result<Option<T>, D::Error> + where + D: Deserializer<'de>, + T: DeserializeOwned, + { + T::deserialize(deserializer).map(Some) + } + + /// Serialize value if Some(T), unit struct if None + pub fn serialize<T, S>(option: &Option<T>, serializer: S) -> Result<S::Ok, S::Error> + where + T: Serialize, + S: Serializer, + { + if let Some(value) = option { + value.serialize(serializer) + } else { + ().serialize(serializer) + } + } +} + +/// Ensure no duplicate values exist in a set. +/// +/// By default serde has a last-value-wins implementation, if duplicate values for a set exist. +/// Sometimes it is desirable to know when such an event happens, as the first value is overwritten +/// and it can indicate an error in the serialized data. +/// +/// This helper returns an error if two identical values exist in a set. +/// +/// The implementation supports both the [`HashSet`] and the [`BTreeSet`] from the standard library. +/// +/// [`HashSet`]: std::collections::HashSet +/// [`BTreeSet`]: std::collections::HashSet +/// +/// # Example +/// +/// ```rust +/// # use std::{collections::HashSet, iter::FromIterator}; +/// # use serde::Deserialize; +/// # +/// # #[derive(Debug, Eq, PartialEq)] +/// #[derive(Deserialize)] +/// struct Doc { +/// #[serde(with = "::serde_with::rust::sets_duplicate_value_is_error")] +/// set: HashSet<usize>, +/// } +/// +/// // Sets are serialized normally, +/// let s = r#"{"set": [1, 2, 3, 4]}"#; +/// let v = Doc { +/// set: HashSet::from_iter(vec![1, 2, 3, 4]), +/// }; +/// assert_eq!(v, serde_json::from_str(s).unwrap()); +/// +/// // but create an error if duplicate values, like the `1`, exist. +/// let s = r#"{"set": [1, 2, 3, 4, 1]}"#; +/// let res: Result<Doc, _> = serde_json::from_str(s); +/// assert!(res.is_err()); +/// ``` +pub mod sets_duplicate_value_is_error { + use super::*; + use crate::duplicate_key_impls::PreventDuplicateInsertsSet; + + /// Deserialize a set and return an error on duplicate values + pub fn deserialize<'de, D, T, V>(deserializer: D) -> Result<T, D::Error> + where + T: PreventDuplicateInsertsSet<V>, + V: Deserialize<'de>, + D: Deserializer<'de>, + { + struct SeqVisitor<T, V> { + marker: PhantomData<T>, + set_item_type: PhantomData<V>, + } + + impl<'de, T, V> Visitor<'de> for SeqVisitor<T, V> + where + T: PreventDuplicateInsertsSet<V>, + V: Deserialize<'de>, + { + type Value = T; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a sequence") + } + + #[inline] + fn visit_seq<A>(self, mut access: A) -> Result<Self::Value, A::Error> + where + A: SeqAccess<'de>, + { + let mut values = Self::Value::new(access.size_hint()); + + while let Some(value) = access.next_element()? { + if !values.insert(value) { + return Err(Error::custom("invalid entry: found duplicate value")); + }; + } + + Ok(values) + } + } + + let visitor = SeqVisitor { + marker: PhantomData, + set_item_type: PhantomData, + }; + deserializer.deserialize_seq(visitor) + } + + /// Serialize the set with the default serializer + pub fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error> + where + T: Serialize, + S: Serializer, + { + value.serialize(serializer) + } +} + +/// Ensure no duplicate keys exist in a map. +/// +/// By default serde has a last-value-wins implementation, if duplicate keys for a map exist. +/// Sometimes it is desirable to know when such an event happens, as the first value is overwritten +/// and it can indicate an error in the serialized data. +/// +/// This helper returns an error if two identical keys exist in a map. +/// +/// The implementation supports both the [`HashMap`] and the [`BTreeMap`] from the standard library. +/// +/// [`HashMap`]: std::collections::HashMap +/// [`BTreeMap`]: std::collections::HashMap +/// +/// # Example +/// +/// ```rust +/// # use serde::Deserialize; +/// # use std::collections::HashMap; +/// # +/// # #[derive(Debug, Eq, PartialEq)] +/// #[derive(Deserialize)] +/// struct Doc { +/// #[serde(with = "::serde_with::rust::maps_duplicate_key_is_error")] +/// map: HashMap<usize, usize>, +/// } +/// +/// // Maps are serialized normally, +/// let s = r#"{"map": {"1": 1, "2": 2, "3": 3}}"#; +/// let mut v = Doc { +/// map: HashMap::new(), +/// }; +/// v.map.insert(1, 1); +/// v.map.insert(2, 2); +/// v.map.insert(3, 3); +/// assert_eq!(v, serde_json::from_str(s).unwrap()); +/// +/// // but create an error if duplicate keys, like the `1`, exist. +/// let s = r#"{"map": {"1": 1, "2": 2, "1": 3}}"#; +/// let res: Result<Doc, _> = serde_json::from_str(s); +/// assert!(res.is_err()); +/// ``` +pub mod maps_duplicate_key_is_error { + use super::*; + use crate::duplicate_key_impls::PreventDuplicateInsertsMap; + + /// Deserialize a map and return an error on duplicate keys + pub fn deserialize<'de, D, T, K, V>(deserializer: D) -> Result<T, D::Error> + where + T: PreventDuplicateInsertsMap<K, V>, + K: Deserialize<'de>, + V: Deserialize<'de>, + D: Deserializer<'de>, + { + struct MapVisitor<T, K, V> { + marker: PhantomData<T>, + map_key_type: PhantomData<K>, + map_value_type: PhantomData<V>, + } + + impl<'de, T, K, V> Visitor<'de> for MapVisitor<T, K, V> + where + T: PreventDuplicateInsertsMap<K, V>, + K: Deserialize<'de>, + V: Deserialize<'de>, + { + type Value = T; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a map") + } + + #[inline] + fn visit_map<A>(self, mut access: A) -> Result<Self::Value, A::Error> + where + A: MapAccess<'de>, + { + let mut values = Self::Value::new(access.size_hint()); + + while let Some((key, value)) = access.next_entry()? { + if !values.insert(key, value) { + return Err(Error::custom("invalid entry: found duplicate key")); + }; + } + + Ok(values) + } + } + + let visitor = MapVisitor { + marker: PhantomData, + map_key_type: PhantomData, + map_value_type: PhantomData, + }; + deserializer.deserialize_map(visitor) + } + + /// Serialize the map with the default serializer + pub fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error> + where + T: Serialize, + S: Serializer, + { + value.serialize(serializer) + } +} + +/// *DEPRECATED* Ensure that the first value is taken, if duplicate values exist +/// +/// This module implements the default behavior in serde. +#[deprecated = "This module does nothing. Remove the attribute. Serde's default behavior is to use the first value when deserializing a set."] +#[allow(deprecated)] +pub mod sets_first_value_wins { + use super::*; + use crate::duplicate_key_impls::DuplicateInsertsFirstWinsSet; + + /// Deserialize a set and keep the first of equal values + #[deprecated = "This function does nothing. Remove the attribute. Serde's default behavior is to use the first value when deserializing a set."] + pub fn deserialize<'de, D, T, V>(deserializer: D) -> Result<T, D::Error> + where + T: DuplicateInsertsFirstWinsSet<V>, + V: Deserialize<'de>, + D: Deserializer<'de>, + { + struct SeqVisitor<T, V> { + marker: PhantomData<T>, + set_item_type: PhantomData<V>, + } + + impl<'de, T, V> Visitor<'de> for SeqVisitor<T, V> + where + T: DuplicateInsertsFirstWinsSet<V>, + V: Deserialize<'de>, + { + type Value = T; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a sequence") + } + + #[inline] + fn visit_seq<A>(self, mut access: A) -> Result<Self::Value, A::Error> + where + A: SeqAccess<'de>, + { + let mut values = Self::Value::new(access.size_hint()); + + while let Some(value) = access.next_element()? { + values.insert(value); + } + + Ok(values) + } + } + + let visitor = SeqVisitor { + marker: PhantomData, + set_item_type: PhantomData, + }; + deserializer.deserialize_seq(visitor) + } + + /// Serialize the set with the default serializer + #[deprecated = "This function does nothing. Remove the attribute. Serde's default behavior is to use the first value when deserializing a set."] + pub fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error> + where + T: Serialize, + S: Serializer, + { + value.serialize(serializer) + } +} + +/// Ensure that the last value is taken, if duplicate values exist +/// +/// By default serde has a first-value-wins implementation, if duplicate keys for a set exist. +/// Sometimes the opposite strategy is desired. This helper implements a first-value-wins strategy. +/// +/// The implementation supports both the [`HashSet`] and the [`BTreeSet`] from the standard library. +/// +/// [`HashSet`]: std::collections::HashSet +/// [`BTreeSet`]: std::collections::HashSet +pub mod sets_last_value_wins { + use super::*; + use crate::duplicate_key_impls::DuplicateInsertsLastWinsSet; + + /// Deserialize a set and keep the last of equal values + pub fn deserialize<'de, D, T, V>(deserializer: D) -> Result<T, D::Error> + where + T: DuplicateInsertsLastWinsSet<V>, + V: Deserialize<'de>, + D: Deserializer<'de>, + { + struct SeqVisitor<T, V> { + marker: PhantomData<T>, + set_item_type: PhantomData<V>, + } + + impl<'de, T, V> Visitor<'de> for SeqVisitor<T, V> + where + T: DuplicateInsertsLastWinsSet<V>, + V: Deserialize<'de>, + { + type Value = T; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a sequence") + } + + #[inline] + fn visit_seq<A>(self, mut access: A) -> Result<Self::Value, A::Error> + where + A: SeqAccess<'de>, + { + let mut values = Self::Value::new(access.size_hint()); + + while let Some(value) = access.next_element()? { + values.replace(value); + } + + Ok(values) + } + } + + let visitor = SeqVisitor { + marker: PhantomData, + set_item_type: PhantomData, + }; + deserializer.deserialize_seq(visitor) + } + + /// Serialize the set with the default serializer + pub fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error> + where + T: Serialize, + S: Serializer, + { + value.serialize(serializer) + } +} + +/// Ensure that the first key is taken, if duplicate keys exist +/// +/// By default serde has a last-key-wins implementation, if duplicate keys for a map exist. +/// Sometimes the opposite strategy is desired. This helper implements a first-key-wins strategy. +/// +/// The implementation supports both the [`HashMap`] and the [`BTreeMap`] from the standard library. +/// +/// [`HashMap`]: std::collections::HashMap +/// [`BTreeMap`]: std::collections::HashMap +/// +/// # Example +/// +/// ```rust +/// # use serde::Deserialize; +/// # use std::collections::HashMap; +/// # +/// # #[derive(Debug, Eq, PartialEq)] +/// #[derive(Deserialize)] +/// struct Doc { +/// #[serde(with = "::serde_with::rust::maps_first_key_wins")] +/// map: HashMap<usize, usize>, +/// } +/// +/// // Maps are serialized normally, +/// let s = r#"{"map": {"1": 1, "2": 2, "3": 3}}"#; +/// let mut v = Doc { +/// map: HashMap::new(), +/// }; +/// v.map.insert(1, 1); +/// v.map.insert(2, 2); +/// v.map.insert(3, 3); +/// assert_eq!(v, serde_json::from_str(s).unwrap()); +/// +/// // but create an error if duplicate keys, like the `1`, exist. +/// let s = r#"{"map": {"1": 1, "2": 2, "1": 3}}"#; +/// let mut v = Doc { +/// map: HashMap::new(), +/// }; +/// v.map.insert(1, 1); +/// v.map.insert(2, 2); +/// assert_eq!(v, serde_json::from_str(s).unwrap()); +/// ``` +pub mod maps_first_key_wins { + use super::*; + use crate::duplicate_key_impls::DuplicateInsertsFirstWinsMap; + + /// Deserialize a map and return an error on duplicate keys + pub fn deserialize<'de, D, T, K, V>(deserializer: D) -> Result<T, D::Error> + where + T: DuplicateInsertsFirstWinsMap<K, V>, + K: Deserialize<'de>, + V: Deserialize<'de>, + D: Deserializer<'de>, + { + struct MapVisitor<T, K, V> { + marker: PhantomData<T>, + map_key_type: PhantomData<K>, + map_value_type: PhantomData<V>, + } + + impl<'de, T, K, V> Visitor<'de> for MapVisitor<T, K, V> + where + T: DuplicateInsertsFirstWinsMap<K, V>, + K: Deserialize<'de>, + V: Deserialize<'de>, + { + type Value = T; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a map") + } + + #[inline] + fn visit_map<A>(self, mut access: A) -> Result<Self::Value, A::Error> + where + A: MapAccess<'de>, + { + let mut values = Self::Value::new(access.size_hint()); + + while let Some((key, value)) = access.next_entry()? { + values.insert(key, value); + } + + Ok(values) + } + } + + let visitor = MapVisitor { + marker: PhantomData, + map_key_type: PhantomData, + map_value_type: PhantomData, + }; + deserializer.deserialize_map(visitor) + } + + /// Serialize the map with the default serializer + pub fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error> + where + T: Serialize, + S: Serializer, + { + value.serialize(serializer) + } +} + +/// De/Serialize a [`Option<String>`] type while transforming the empty string to [`None`] +/// +/// Convert an [`Option<T>`] from/to string using [`FromStr`] and [`AsRef<str>`] implementations. +/// An empty string is deserialized as [`None`] and a [`None`] vice versa. +/// +/// ## Converting to `serde_as` +/// +/// The same functionality can be more clearly expressed via [`NoneAsEmptyString`] and using the [`serde_as`] macro. +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::Deserialize; +/// # use serde_with::{serde_as, NoneAsEmptyString}; +/// # +/// #[serde_as] +/// #[derive(Deserialize)] +/// struct A { +/// #[serde_as(as = "NoneAsEmptyString")] +/// value: Option<String>, +/// } +/// # } +/// ``` +/// +/// # Examples +/// +/// ``` +/// # use serde::{Deserialize, Serialize}; +/// # use serde_json::json; +/// # use serde_with::rust::string_empty_as_none; +/// # +/// #[derive(Deserialize, Serialize)] +/// struct A { +/// #[serde(with = "string_empty_as_none")] +/// tags: Option<String>, +/// } +/// +/// let v: A = serde_json::from_value(json!({ "tags": "" })).unwrap(); +/// assert_eq!(None, v.tags); +/// +/// let v: A = serde_json::from_value(json!({ "tags": "Hi" })).unwrap(); +/// assert_eq!(Some("Hi".to_string()), v.tags); +/// +/// let x = A { +/// tags: Some("This is text".to_string()), +/// }; +/// assert_eq!(json!({ "tags": "This is text" }), serde_json::to_value(&x).unwrap()); +/// +/// let x = A { +/// tags: None, +/// }; +/// assert_eq!(json!({ "tags": "" }), serde_json::to_value(&x).unwrap()); +/// ``` +/// +/// [`NoneAsEmptyString`]: crate::NoneAsEmptyString +/// [`serde_as`]: crate::guide::serde_as +pub mod string_empty_as_none { + use super::*; + + /// Deserialize an `Option<T>` from a string using `FromStr` + pub fn deserialize<'de, D, S>(deserializer: D) -> Result<Option<S>, D::Error> + where + D: Deserializer<'de>, + S: FromStr, + S::Err: Display, + { + struct OptionStringEmptyNone<S>(PhantomData<S>); + impl<'de, S> Visitor<'de> for OptionStringEmptyNone<S> + where + S: FromStr, + S::Err: Display, + { + type Value = Option<S>; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("any string") + } + + fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> + where + E: Error, + { + match value { + "" => Ok(None), + v => S::from_str(v).map(Some).map_err(Error::custom), + } + } + + fn visit_string<E>(self, value: String) -> Result<Self::Value, E> + where + E: Error, + { + match &*value { + "" => Ok(None), + v => S::from_str(v).map(Some).map_err(Error::custom), + } + } + + // handles the `null` case + fn visit_unit<E>(self) -> Result<Self::Value, E> + where + E: Error, + { + Ok(None) + } + } + + deserializer.deserialize_any(OptionStringEmptyNone(PhantomData)) + } + + /// Serialize a string from `Option<T>` using `AsRef<str>` or using the empty string if `None`. + pub fn serialize<T, S>(option: &Option<T>, serializer: S) -> Result<S::Ok, S::Error> + where + T: AsRef<str>, + S: Serializer, + { + if let Some(value) = option { + value.as_ref().serialize(serializer) + } else { + "".serialize(serializer) + } + } +} + +/// De/Serialize a Map into a list of tuples +/// +/// Some formats, like JSON, have limitations on the type of keys for maps. +/// In case of JSON, keys are restricted to strings. +/// Rust features more powerful keys, for example tuple, which can not be serialized to JSON. +/// +/// This helper serializes the Map into a list of tuples, which does not have the same type restrictions. +/// The module can be applied on any type implementing `IntoIterator<Item = (&'a K, &'a V)>` and `FromIterator<(K, V)>`, with `K` and `V` being the key and value types. +/// From the standard library both [`HashMap`] and [`BTreeMap`] fullfil the condition and can be used here. +/// +/// ## Converting to `serde_as` +/// +/// If the map is of type [`HashMap`] or [`BTreeMap`] the same functionality can be expressed more clearly using the [`serde_as`] macro. +/// The `_` is a placeholder which works for any type which implements [`Serialize`]/[`Deserialize`], such as the tuple and `u32` type. +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_with::serde_as; +/// # use std::collections::{BTreeMap, HashMap}; +/// # +/// #[serde_as] +/// #[derive(Deserialize, Serialize)] +/// struct A { +/// #[serde_as(as = "Vec<(_, _)>")] +/// hashmap: HashMap<(String, u32), u32>, +/// #[serde_as(as = "Vec<(_, _)>")] +/// btreemap: BTreeMap<(String, u32), u32>, +/// } +/// # } +/// ``` +/// +/// # Examples +/// +/// ``` +/// # use serde::{Deserialize, Serialize}; +/// # use serde_json::json; +/// # use std::collections::BTreeMap; +/// # +/// #[derive(Deserialize, Serialize)] +/// struct A { +/// #[serde(with = "serde_with::rust::map_as_tuple_list")] +/// s: BTreeMap<(String, u32), u32>, +/// } +/// +/// let v: A = serde_json::from_value(json!({ +/// "s": [ +/// [["Hello", 123], 0], +/// [["World", 456], 1] +/// ] +/// })).unwrap(); +/// +/// assert_eq!(2, v.s.len()); +/// assert_eq!(1, v.s[&("World".to_string(), 456)]); +/// ``` +/// +/// The helper is generic over the hasher type of the [`HashMap`] and works with different variants, such as `FnvHashMap`. +/// +/// ``` +/// # use serde::{Deserialize, Serialize}; +/// # use serde_json::json; +/// # +/// use fnv::FnvHashMap; +/// +/// #[derive(Deserialize, Serialize)] +/// struct A { +/// #[serde(with = "serde_with::rust::map_as_tuple_list")] +/// s: FnvHashMap<u32, bool>, +/// } +/// +/// let v: A = serde_json::from_value(json!({ +/// "s": [ +/// [0, false], +/// [1, true] +/// ] +/// })).unwrap(); +/// +/// assert_eq!(2, v.s.len()); +/// assert_eq!(true, v.s[&1]); +/// ``` +/// +/// [`serde_as`]: crate::guide::serde_as +pub mod map_as_tuple_list { + // Trait bounds based on this answer: https://stackoverflow.com/a/66600486/15470286 + use super::*; + + /// Serialize the map as a list of tuples + pub fn serialize<'a, T, K, V, S>(map: T, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + T: IntoIterator<Item = (&'a K, &'a V)>, + T::IntoIter: ExactSizeIterator, + K: Serialize + 'a, + V: Serialize + 'a, + { + serializer.collect_seq(map) + } + + /// Deserialize a map from a list of tuples + pub fn deserialize<'de, T, K, V, D>(deserializer: D) -> Result<T, D::Error> + where + D: Deserializer<'de>, + T: FromIterator<(K, V)>, + K: Deserialize<'de>, + V: Deserialize<'de>, + { + struct SeqVisitor<T, K, V>(PhantomData<(T, K, V)>); + + impl<'de, T, K, V> Visitor<'de> for SeqVisitor<T, K, V> + where + T: FromIterator<(K, V)>, + K: Deserialize<'de>, + V: Deserialize<'de>, + { + type Value = T; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a list of key-value pairs") + } + + fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error> + where + A: SeqAccess<'de>, + { + utils::SeqIter::new(seq).collect() + } + } + + deserializer.deserialize_seq(SeqVisitor(PhantomData)) + } +} + +/// DEPRECATED De/Serialize a [`HashMap`] into a list of tuples +/// +/// Use the [`map_as_tuple_list`] module which is more general than this. +/// It should work with everything convertible to and from an `Iterator` including [`BTreeMap`] and [`HashMap`]. +/// +/// --- +/// +/// Some formats, like JSON, have limitations on the type of keys for maps. +/// In case of JSON, keys are restricted to strings. +/// Rust features more powerful keys, for example tuple, which can not be serialized to JSON. +/// +/// This helper serializes the [`HashMap`] into a list of tuples, which does not have the same type restrictions. +/// +/// If you need to de/serialize a [`BTreeMap`] then use [`btreemap_as_tuple_list`]. +/// +/// ## Converting to `serde_as` +/// +/// The same functionality can be more clearly expressed using the [`serde_as`] macro. +/// The `_` is a placeholder which works for any type which implements [`Serialize`]/[`Deserialize`], such as the tuple and `u32` type. +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_with::serde_as; +/// # use std::collections::HashMap; +/// # +/// #[serde_as] +/// #[derive(Deserialize, Serialize)] +/// struct A { +/// #[serde_as(as = "Vec<(_, _)>")] +/// s: HashMap<(String, u32), u32>, +/// } +/// # } +/// ``` +/// +/// # Examples +/// +/// ``` +/// # use serde::{Deserialize, Serialize}; +/// # use serde_json::json; +/// # use std::collections::HashMap; +/// # +/// #[derive(Deserialize, Serialize)] +/// struct A { +/// #[serde(with = "serde_with::rust::hashmap_as_tuple_list")] +/// s: HashMap<(String, u32), u32>, +/// } +/// +/// let v: A = serde_json::from_value(json!({ +/// "s": [ +/// [["Hello", 123], 0], +/// [["World", 456], 1] +/// ] +/// })).unwrap(); +/// +/// assert_eq!(2, v.s.len()); +/// assert_eq!(1, v.s[&("World".to_string(), 456)]); +/// ``` +/// +/// The helper is generic over the hasher type of the [`HashMap`] and works with different variants, such as `FnvHashMap`. +/// +/// ``` +/// # use serde::{Deserialize, Serialize}; +/// # use serde_json::json; +/// # +/// use fnv::FnvHashMap; +/// +/// #[derive(Deserialize, Serialize)] +/// struct A { +/// #[serde(with = "serde_with::rust::hashmap_as_tuple_list")] +/// s: FnvHashMap<u32, bool>, +/// } +/// +/// let v: A = serde_json::from_value(json!({ +/// "s": [ +/// [0, false], +/// [1, true] +/// ] +/// })).unwrap(); +/// +/// assert_eq!(2, v.s.len()); +/// assert_eq!(true, v.s[&1]); +/// ``` +/// +/// [`serde_as`]: crate::guide::serde_as +#[deprecated( + since = "1.8.0", + note = "Use the more general map_as_tuple_list module." +)] +pub mod hashmap_as_tuple_list { + #[doc(inline)] + #[deprecated( + since = "1.8.0", + note = "Use the more general map_as_tuple_list::deserialize function." + )] + pub use super::map_as_tuple_list::deserialize; + #[doc(inline)] + #[deprecated( + since = "1.8.0", + note = "Use the more general map_as_tuple_list::serialize function." + )] + pub use super::map_as_tuple_list::serialize; +} + +/// DEPRECATED De/Serialize a [`BTreeMap`] into a list of tuples +/// +/// Use the [`map_as_tuple_list`] module which is more general than this. +/// It should work with everything convertible to and from an `Iterator` including [`BTreeMap`] and [`HashMap`]. +/// +/// --- +/// +/// Some formats, like JSON, have limitations on the type of keys for maps. +/// In case of JSON, keys are restricted to strings. +/// Rust features more powerful keys, for example tuple, which can not be serialized to JSON. +/// +/// This helper serializes the [`BTreeMap`] into a list of tuples, which does not have the same type restrictions. +/// +/// If you need to de/serialize a [`HashMap`] then use [`hashmap_as_tuple_list`]. +/// +/// ## Converting to `serde_as` +/// +/// The same functionality can be more clearly expressed using the [`serde_as`] macro. +/// The `_` is a placeholder which works for any type which implements [`Serialize`]/[`Deserialize`], such as the tuple and `u32` type. +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_with::serde_as; +/// # use std::collections::BTreeMap; +/// # +/// #[serde_as] +/// #[derive(Deserialize, Serialize)] +/// struct A { +/// #[serde_as(as = "Vec<(_, _)>")] +/// s: BTreeMap<(String, u32), u32>, +/// } +/// # } +/// ``` +/// +/// # Examples +/// +/// ``` +/// # use serde::{Deserialize, Serialize}; +/// # use serde_json::json; +/// # use std::collections::BTreeMap; +/// # +/// #[derive(Deserialize, Serialize)] +/// struct A { +/// #[serde(with = "serde_with::rust::btreemap_as_tuple_list")] +/// s: BTreeMap<(String, u32), u32>, +/// } +/// +/// let v: A = serde_json::from_value(json!({ +/// "s": [ +/// [["Hello", 123], 0], +/// [["World", 456], 1] +/// ] +/// })).unwrap(); +/// +/// assert_eq!(2, v.s.len()); +/// assert_eq!(1, v.s[&("World".to_string(), 456)]); +/// ``` +/// +/// [`serde_as`]: crate::guide::serde_as +#[deprecated( + since = "1.8.0", + note = "Use the more general map_as_tuple_list module." +)] +pub mod btreemap_as_tuple_list { + #[doc(inline)] + #[deprecated( + since = "1.8.0", + note = "Use the more general map_as_tuple_list::deserialize function." + )] + pub use super::map_as_tuple_list::deserialize; + #[doc(inline)] + #[deprecated( + since = "1.8.0", + note = "Use the more general map_as_tuple_list::serialize function." + )] + pub use super::map_as_tuple_list::serialize; +} + +/// This serializes a list of tuples into a map and back +/// +/// Normally, you want to use a [`HashMap`] or a [`BTreeMap`] when deserializing a map. +/// However, sometimes this is not possible due to type contains, e.g., if the type implements neither [`Hash`] nor [`Ord`]. +/// Another use case is deserializing a map with duplicate keys. +/// +/// The implementation is generic using the [`FromIterator`] and [`IntoIterator`] traits. +/// Therefore, all of [`Vec`], [`VecDeque`](std::collections::VecDeque), and [`LinkedList`](std::collections::LinkedList) and anything which implements those are supported. +/// +/// ## Converting to `serde_as` +/// +/// The same functionality can be more clearly expressed using the [`serde_as`] macro. +/// The `_` is a placeholder which works for any type which implements [`Serialize`]/[`Deserialize`], such as the tuple and `u32` type. +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::{Deserialize, Serialize}; +/// # use serde_with::serde_as; +/// # use std::collections::BTreeMap; +/// # +/// #[serde_as] +/// #[derive(Deserialize, Serialize)] +/// struct S { +/// #[serde_as(as = "BTreeMap<_, _>")] // HashMap will also work +/// s: Vec<(i32, String)>, +/// } +/// # } +/// ``` +/// +/// # Examples +/// +/// `Wrapper` does not implement [`Hash`] nor [`Ord`], thus prohibiting the use [`HashMap`] or [`BTreeMap`]. +/// +/// ``` +/// # use serde::{Deserialize, Serialize}; +/// # +/// #[derive(Debug, Deserialize, Serialize, Default)] +/// struct S { +/// #[serde(with = "serde_with::rust::tuple_list_as_map")] +/// s: Vec<(Wrapper<i32>, Wrapper<String>)>, +/// } +/// +/// #[derive(Clone, Debug, Serialize, Deserialize)] +/// #[serde(transparent)] +/// struct Wrapper<T>(T); +/// +/// let from = r#"{ +/// "s": { +/// "1": "Hi", +/// "2": "Cake", +/// "99": "Lie" +/// } +/// }"#; +/// let mut expected = S::default(); +/// expected.s.push((Wrapper(1), Wrapper("Hi".into()))); +/// expected.s.push((Wrapper(2), Wrapper("Cake".into()))); +/// expected.s.push((Wrapper(99), Wrapper("Lie".into()))); +/// +/// let res: S = serde_json::from_str(from).unwrap(); +/// for ((exp_k, exp_v), (res_k, res_v)) in expected.s.iter().zip(&res.s) { +/// assert_eq!(exp_k.0, res_k.0); +/// assert_eq!(exp_v.0, res_v.0); +/// } +/// assert_eq!(from, serde_json::to_string_pretty(&expected).unwrap()); +/// ``` +/// +/// In this example, the serialized format contains duplicate keys, which is not supported with [`HashMap`] or [`BTreeMap`]. +/// +/// ``` +/// # use serde::{Deserialize, Serialize}; +/// # +/// #[derive(Debug, Deserialize, Serialize, PartialEq, Default)] +/// struct S { +/// #[serde(with = "serde_with::rust::tuple_list_as_map")] +/// s: Vec<(i32, String)>, +/// } +/// +/// let from = r#"{ +/// "s": { +/// "1": "Hi", +/// "1": "Cake", +/// "1": "Lie" +/// } +/// }"#; +/// let mut expected = S::default(); +/// expected.s.push((1, "Hi".into())); +/// expected.s.push((1, "Cake".into())); +/// expected.s.push((1, "Lie".into())); +/// +/// let res: S = serde_json::from_str(from).unwrap(); +/// assert_eq!(3, res.s.len()); +/// assert_eq!(expected, res); +/// assert_eq!(from, serde_json::to_string_pretty(&expected).unwrap()); +/// ``` +/// +/// [`serde_as`]: crate::guide::serde_as +pub mod tuple_list_as_map { + use super::*; + + /// Serialize any iteration of tuples into a map. + pub fn serialize<'a, I, K, V, S>(iter: I, serializer: S) -> Result<S::Ok, S::Error> + where + I: IntoIterator<Item = &'a (K, V)>, + I::IntoIter: ExactSizeIterator, + K: Serialize + 'a, + V: Serialize + 'a, + S: Serializer, + { + // Convert &(K, V) to (&K, &V) for collect_map. + let iter = iter.into_iter().map(|(k, v)| (k, v)); + serializer.collect_map(iter) + } + + /// Deserialize a map into an iterator of tuples. + pub fn deserialize<'de, I, K, V, D>(deserializer: D) -> Result<I, D::Error> + where + I: FromIterator<(K, V)>, + K: Deserialize<'de>, + V: Deserialize<'de>, + D: Deserializer<'de>, + { + deserializer.deserialize_map(MapVisitor(PhantomData)) + } + + #[allow(clippy::type_complexity)] + struct MapVisitor<I, K, V>(PhantomData<fn() -> (I, K, V)>); + + impl<'de, I, K, V> Visitor<'de> for MapVisitor<I, K, V> + where + I: FromIterator<(K, V)>, + K: Deserialize<'de>, + V: Deserialize<'de>, + { + type Value = I; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a map") + } + + fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error> + where + A: MapAccess<'de>, + { + utils::MapIter::new(map).collect() + } + } +} + +/// Deserialize from bytes or string +/// +/// Any Rust [`String`] can be converted into bytes, i.e., `Vec<u8>`. +/// Accepting both as formats while deserializing can be helpful while interacting with language +/// which have a looser definition of string than Rust. +/// +/// ## Converting to `serde_as` +/// +/// The same functionality can be more clearly expressed via [`BytesOrString`] and using the [`serde_as`] macro. +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::Deserialize; +/// # use serde_with::{serde_as, BytesOrString}; +/// # +/// #[serde_as] +/// #[derive(Deserialize)] +/// struct A { +/// #[serde_as(as = "BytesOrString")] +/// bos: Vec<u8>, +/// } +/// # } +/// ``` +/// +/// # Example +/// ```rust +/// # use serde::{Deserialize, Serialize}; +/// # +/// #[derive(Debug, Deserialize, Serialize, PartialEq, Default)] +/// struct S { +/// #[serde(deserialize_with = "serde_with::rust::bytes_or_string::deserialize")] +/// bos: Vec<u8>, +/// } +/// +/// // Here we deserialize from a byte array ... +/// let from = r#"{ +/// "bos": [ +/// 0, +/// 1, +/// 2, +/// 3 +/// ] +/// }"#; +/// let expected = S { +/// bos: vec![0, 1, 2, 3], +/// }; +/// +/// let res: S = serde_json::from_str(from).unwrap(); +/// assert_eq!(expected, res); +/// +/// // and serialization works too. +/// assert_eq!(from, serde_json::to_string_pretty(&expected).unwrap()); +/// +/// // But we also support deserializing from a String +/// let from = r#"{ +/// "bos": "✨Works!" +/// }"#; +/// let expected = S { +/// bos: "✨Works!".as_bytes().to_vec(), +/// }; +/// +/// let res: S = serde_json::from_str(from).unwrap(); +/// assert_eq!(expected, res); +/// ``` +/// +/// [`BytesOrString`]: crate::BytesOrString +/// [`serde_as`]: crate::guide::serde_as +pub mod bytes_or_string { + use super::*; + + /// Deserialize a [`Vec<u8>`] from either bytes or string + pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error> + where + D: Deserializer<'de>, + { + deserializer.deserialize_any(BytesOrStringVisitor) + } + + struct BytesOrStringVisitor; + + impl<'de> Visitor<'de> for BytesOrStringVisitor { + type Value = Vec<u8>; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a list of bytes or a string") + } + + fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> { + Ok(v.to_vec()) + } + + fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E> { + Ok(v) + } + + fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> { + Ok(v.as_bytes().to_vec()) + } + + fn visit_string<E>(self, v: String) -> Result<Self::Value, E> { + Ok(v.into_bytes()) + } + + fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error> + where + A: SeqAccess<'de>, + { + utils::SeqIter::new(seq).collect() + } + } +} + +/// Deserialize value and return [`Default`] on error +/// +/// The main use case is ignoring error while deserializing. +/// Instead of erroring, it simply deserializes the [`Default`] variant of the type. +/// It is not possible to find the error location, i.e., which field had a deserialization error, with this method. +/// +/// ## Converting to `serde_as` +/// +/// The same functionality can be more clearly expressed via [`DefaultOnError`] and using the [`serde_as`] macro. +/// It can be combined with other converts as shown. +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::Deserialize; +/// # use serde_with::{serde_as, DefaultOnError, DisplayFromStr}; +/// # +/// #[serde_as] +/// #[derive(Deserialize)] +/// struct A { +/// #[serde_as(as = "DefaultOnError")] +/// value: u32, +/// #[serde_as(as = "DefaultOnError<DisplayFromStr>")] +/// value2: u32, +/// } +/// # } +/// ``` +/// +/// [`DefaultOnError`]: crate::DefaultOnError +/// [`serde_as`]: crate::guide::serde_as +/// +/// # Examples +/// +/// ``` +/// # use serde::Deserialize; +/// # +/// #[derive(Deserialize)] +/// struct A { +/// #[serde(deserialize_with = "serde_with::rust::default_on_error::deserialize")] +/// value: u32, +/// } +/// +/// let a: A = serde_json::from_str(r#"{"value": 123}"#).unwrap(); +/// assert_eq!(123, a.value); +/// +/// // null is of invalid type +/// let a: A = serde_json::from_str(r#"{"value": null}"#).unwrap(); +/// assert_eq!(0, a.value); +/// +/// // String is of invalid type +/// let a: A = serde_json::from_str(r#"{"value": "123"}"#).unwrap(); +/// assert_eq!(0, a.value); +/// +/// // Map is of invalid type +/// let a: A = serde_json::from_str(r#"{"value": {}}"#).unwrap(); +/// assert_eq!(0, a.value); +/// +/// // Missing entries still cause errors +/// assert!(serde_json::from_str::<A>(r#"{ }"#).is_err()); +/// ``` +/// +/// Deserializing missing values can be supported by adding the `default` field attribute: +/// +/// ``` +/// # use serde::Deserialize; +/// # +/// #[derive(Deserialize)] +/// struct B { +/// #[serde(default, deserialize_with = "serde_with::rust::default_on_error::deserialize")] +/// value: u32, +/// } +/// +/// let b: B = serde_json::from_str(r#"{ }"#).unwrap(); +/// assert_eq!(0, b.value); +/// ``` +pub mod default_on_error { + use super::*; + + /// Deserialize T and return the [`Default`] value on error + pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error> + where + D: Deserializer<'de>, + T: Deserialize<'de> + Default, + { + #[derive(Debug, serde::Deserialize)] + #[serde(untagged)] + enum GoodOrError<T> { + Good(T), + // This consumes one "item" when `T` errors while deserializing. + // This is necessary to make this work, when instead of having a direct value + // like integer or string, the deserializer sees a list or map. + Error(serde::de::IgnoredAny), + } + + Ok(match Deserialize::deserialize(deserializer) { + Ok(GoodOrError::Good(res)) => res, + _ => Default::default(), + }) + } + + /// Serialize value with the default serializer + pub fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error> + where + T: Serialize, + S: Serializer, + { + value.serialize(serializer) + } +} + +/// Deserialize default value if encountering `null`. +/// +/// One use case are JSON APIs in which the `null` value represents some default state. +/// This adapter allows to turn the `null` directly into the [`Default`] value of the type. +/// +/// ## Converting to `serde_as` +/// +/// The same functionality can be more clearly expressed via [`DefaultOnNull`] and using the [`serde_as`] macro. +/// It can be combined with other convertes as shown. +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::Deserialize; +/// # use serde_with::{serde_as, DefaultOnNull, DisplayFromStr}; +/// # +/// #[serde_as] +/// #[derive(Deserialize)] +/// struct A { +/// #[serde_as(as = "DefaultOnNull")] +/// value: u32, +/// #[serde_as(as = "DefaultOnNull<DisplayFromStr>")] +/// value2: u32, +/// } +/// # } +/// ``` +/// +/// [`DefaultOnNull`]: crate::DefaultOnNull +/// [`serde_as`]: crate::guide::serde_as +/// +/// # Examples +/// +/// ``` +/// # use serde::Deserialize; +/// # +/// #[derive(Deserialize)] +/// struct A { +/// #[serde(deserialize_with = "serde_with::rust::default_on_null::deserialize")] +/// value: u32, +/// } +/// +/// let a: A = serde_json::from_str(r#"{"value": 123}"#).unwrap(); +/// assert_eq!(123, a.value); +/// +/// let a: A = serde_json::from_str(r#"{"value": null}"#).unwrap(); +/// assert_eq!(0, a.value); +/// +/// // String is invalid type +/// assert!(serde_json::from_str::<A>(r#"{"value": "123"}"#).is_err()); +/// ``` +pub mod default_on_null { + use super::*; + + /// Deserialize T and return the [`Default`] value if original value is `null` + pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error> + where + D: Deserializer<'de>, + T: Deserialize<'de> + Default, + { + Ok(Option::deserialize(deserializer)?.unwrap_or_default()) + } + + /// Serialize value with the default serializer + pub fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error> + where + T: Serialize, + S: Serializer, + { + value.serialize(serializer) + } +} + +/// Deserialize any value, ignore it, and return the default value for the type being deserialized. +/// +/// This function can be used in two different ways: +/// +/// 1. It is useful for instance to create an enum with a catch-all variant that will accept any incoming data. +/// 2. [`untagged`] enum representations do not allow the `other` annotation as the fallback enum variant. +/// With this function you can emulate an `other` variant, which can deserialize any data carrying enum. +/// +/// **Note:** Using this function will prevent deserializing data-less enum variants. +/// If this is a problem depends on the data format. +/// For example, deserializing `"Bar"` as an enum in JSON would fail, since it carries no data. +/// +/// # Examples +/// +/// ## Deserializing a heterogeneous collection of XML nodes +/// +/// When [`serde-xml-rs`] deserializes an XML tag to an enum, it always maps the tag +/// name to the enum variant name, and the tag attributes and children to the enum contents. +/// This means that in order for an enum variant to accept any XML tag, it both has to use +/// `#[serde(other)]` to accept any tag name, and `#[serde(deserialize_with = "deserialize_ignore_any")]` +/// to accept any attributes and children. +/// +/// ```rust +/// # use serde::Deserialize; +/// use serde_with::rust::deserialize_ignore_any; +/// +/// # #[derive(Debug, PartialEq)] +/// #[derive(Deserialize)] +/// #[serde(rename_all = "lowercase")] +/// enum Item { +/// Foo(String), +/// Bar(String), +/// #[serde(other, deserialize_with = "deserialize_ignore_any")] +/// Other, +/// } +/// +/// // Deserialize this XML +/// # let items: Vec<Item> = serde_xml_rs::from_str( +/// r" +/// <foo>a</foo> +/// <bar>b</bar> +/// <foo>c</foo> +/// <unknown>d</unknown> +/// " +/// # ).unwrap(); +/// +/// // into these Items +/// # let expected = +/// vec![ +/// Item::Foo(String::from("a")), +/// Item::Bar(String::from("b")), +/// Item::Foo(String::from("c")), +/// Item::Other, +/// ] +/// # ; +/// # assert_eq!(expected, items); +/// ``` +/// +/// ## Simulating an `other` enum variant in an `untagged` enum +/// +/// ```rust +/// # use serde::Deserialize; +/// # use serde_json::json; +/// use serde_with::rust::deserialize_ignore_any; +/// +/// # #[derive(Debug, PartialEq)] +/// #[derive(Deserialize)] +/// #[serde(untagged)] +/// enum Item { +/// Foo{x: u8}, +/// #[serde(deserialize_with = "deserialize_ignore_any")] +/// Other, +/// } +/// +/// // Deserialize this JSON +/// # let items: Vec<Item> = serde_json::from_value( +/// json!([ +/// {"y": 1}, +/// {"x": 1}, +/// ]) +/// # ).unwrap(); +/// +/// // into these Items +/// # let expected = +/// vec![Item::Other, Item::Foo{x: 1}] +/// # ; +/// # assert_eq!(expected, items); +/// ``` +/// +/// [`serde-xml-rs`]: https://docs.rs/serde-xml-rs +/// [`untagged`]: https://serde.rs/enum-representations.html#untagged +pub fn deserialize_ignore_any<'de, D: Deserializer<'de>, T: Default>( + deserializer: D, +) -> Result<T, D::Error> { + serde::de::IgnoredAny::deserialize(deserializer).map(|_| T::default()) +} diff --git a/third_party/rust/serde_with/src/ser/const_arrays.rs b/third_party/rust/serde_with/src/ser/const_arrays.rs new file mode 100644 index 0000000000..b864f2b7e5 --- /dev/null +++ b/third_party/rust/serde_with/src/ser/const_arrays.rs @@ -0,0 +1,90 @@ +use super::*; +use alloc::{borrow::Cow, boxed::Box, collections::BTreeMap}; +use std::collections::HashMap; + +impl<T, As, const N: usize> SerializeAs<[T; N]> for [As; N] +where + As: SerializeAs<T>, +{ + fn serialize_as<S>(array: &[T; N], serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + use serde::ser::SerializeTuple; + let mut arr = serializer.serialize_tuple(N)?; + for elem in array { + arr.serialize_element(&SerializeAsWrap::<T, As>::new(elem))?; + } + arr.end() + } +} + +macro_rules! tuple_seq_as_map_impl_intern { + ($tyorig:ty, $ty:ident <K, V>) => { + #[allow(clippy::implicit_hasher)] + impl<K, KAs, V, VAs, const N: usize> SerializeAs<$tyorig> for $ty<KAs, VAs> + where + KAs: SerializeAs<K>, + VAs: SerializeAs<V>, + { + fn serialize_as<S>(source: &$tyorig, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.collect_map(source.iter().map(|(k, v)| { + ( + SerializeAsWrap::<K, KAs>::new(k), + SerializeAsWrap::<V, VAs>::new(v), + ) + })) + } + } + }; +} +tuple_seq_as_map_impl_intern!([(K, V); N], BTreeMap<K, V>); +tuple_seq_as_map_impl_intern!([(K, V); N], HashMap<K, V>); + +impl<const N: usize> SerializeAs<[u8; N]> for Bytes { + fn serialize_as<S>(bytes: &[u8; N], serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.serialize_bytes(bytes) + } +} + +impl<const N: usize> SerializeAs<&[u8; N]> for Bytes { + fn serialize_as<S>(bytes: &&[u8; N], serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.serialize_bytes(*bytes) + } +} + +impl<const N: usize> SerializeAs<Box<[u8; N]>> for Bytes { + fn serialize_as<S>(bytes: &Box<[u8; N]>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.serialize_bytes(&**bytes) + } +} + +impl<'a, const N: usize> SerializeAs<Cow<'a, [u8; N]>> for Bytes { + fn serialize_as<S>(bytes: &Cow<'a, [u8; N]>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.serialize_bytes(bytes.as_ref()) + } +} + +impl<'a, const N: usize> SerializeAs<Cow<'a, [u8; N]>> for BorrowCow { + fn serialize_as<S>(value: &Cow<'a, [u8; N]>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.collect_seq(value.iter()) + } +} diff --git a/third_party/rust/serde_with/src/ser/impls.rs b/third_party/rust/serde_with/src/ser/impls.rs new file mode 100644 index 0000000000..16a8fb913c --- /dev/null +++ b/third_party/rust/serde_with/src/ser/impls.rs @@ -0,0 +1,739 @@ +use super::*; +use crate::{ + formats::Strictness, rust::StringWithSeparator, utils::duration::DurationSigned, Separator, +}; +use alloc::{ + borrow::Cow, + boxed::Box, + collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque}, + rc::{Rc, Weak as RcWeak}, + string::{String, ToString}, + sync::{Arc, Weak as ArcWeak}, + vec::Vec, +}; +use core::{ + cell::{Cell, RefCell}, + convert::TryInto, + fmt::Display, + time::Duration, +}; +#[cfg(feature = "indexmap")] +use indexmap_crate::{IndexMap, IndexSet}; +use serde::ser::Error; +use std::{ + collections::{HashMap, HashSet}, + sync::{Mutex, RwLock}, + time::SystemTime, +}; + +/////////////////////////////////////////////////////////////////////////////// +// region: Simple Wrapper types (e.g., Box, Option) + +impl<'a, T, U> SerializeAs<&'a T> for &'a U +where + U: SerializeAs<T>, + T: ?Sized, + U: ?Sized, +{ + fn serialize_as<S>(source: &&'a T, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + SerializeAsWrap::<T, U>::new(source).serialize(serializer) + } +} + +impl<'a, T, U> SerializeAs<&'a mut T> for &'a mut U +where + U: SerializeAs<T>, + T: ?Sized, + U: ?Sized, +{ + fn serialize_as<S>(source: &&'a mut T, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + SerializeAsWrap::<T, U>::new(source).serialize(serializer) + } +} + +impl<T, U> SerializeAs<Box<T>> for Box<U> +where + U: SerializeAs<T>, +{ + fn serialize_as<S>(source: &Box<T>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + SerializeAsWrap::<T, U>::new(source).serialize(serializer) + } +} + +impl<T, U> SerializeAs<Option<T>> for Option<U> +where + U: SerializeAs<T>, +{ + fn serialize_as<S>(source: &Option<T>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + match *source { + Some(ref value) => serializer.serialize_some(&SerializeAsWrap::<T, U>::new(value)), + None => serializer.serialize_none(), + } + } +} + +impl<T, U> SerializeAs<Rc<T>> for Rc<U> +where + U: SerializeAs<T>, +{ + fn serialize_as<S>(source: &Rc<T>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + SerializeAsWrap::<T, U>::new(source).serialize(serializer) + } +} + +impl<T, U> SerializeAs<RcWeak<T>> for RcWeak<U> +where + U: SerializeAs<T>, +{ + fn serialize_as<S>(source: &RcWeak<T>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + SerializeAsWrap::<Option<Rc<T>>, Option<Rc<U>>>::new(&source.upgrade()) + .serialize(serializer) + } +} + +impl<T, U> SerializeAs<Arc<T>> for Arc<U> +where + U: SerializeAs<T>, +{ + fn serialize_as<S>(source: &Arc<T>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + SerializeAsWrap::<T, U>::new(source).serialize(serializer) + } +} + +impl<T, U> SerializeAs<ArcWeak<T>> for ArcWeak<U> +where + U: SerializeAs<T>, +{ + fn serialize_as<S>(source: &ArcWeak<T>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + SerializeAsWrap::<Option<Arc<T>>, Option<Arc<U>>>::new(&source.upgrade()) + .serialize(serializer) + } +} + +impl<T, U> SerializeAs<Cell<T>> for Cell<U> +where + U: SerializeAs<T>, + T: Copy, +{ + fn serialize_as<S>(source: &Cell<T>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + SerializeAsWrap::<T, U>::new(&source.get()).serialize(serializer) + } +} + +impl<T, U> SerializeAs<RefCell<T>> for RefCell<U> +where + U: SerializeAs<T>, +{ + fn serialize_as<S>(source: &RefCell<T>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + match source.try_borrow() { + Ok(source) => SerializeAsWrap::<T, U>::new(&*source).serialize(serializer), + Err(_) => Err(S::Error::custom("already mutably borrowed")), + } + } +} + +impl<T, U> SerializeAs<Mutex<T>> for Mutex<U> +where + U: SerializeAs<T>, +{ + fn serialize_as<S>(source: &Mutex<T>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + match source.lock() { + Ok(source) => SerializeAsWrap::<T, U>::new(&*source).serialize(serializer), + Err(_) => Err(S::Error::custom("lock poison error while serializing")), + } + } +} + +impl<T, U> SerializeAs<RwLock<T>> for RwLock<U> +where + U: SerializeAs<T>, +{ + fn serialize_as<S>(source: &RwLock<T>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + match source.read() { + Ok(source) => SerializeAsWrap::<T, U>::new(&*source).serialize(serializer), + Err(_) => Err(S::Error::custom("lock poison error while serializing")), + } + } +} + +impl<T, TAs, E, EAs> SerializeAs<Result<T, E>> for Result<TAs, EAs> +where + TAs: SerializeAs<T>, + EAs: SerializeAs<E>, +{ + fn serialize_as<S>(source: &Result<T, E>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + source + .as_ref() + .map(SerializeAsWrap::<T, TAs>::new) + .map_err(SerializeAsWrap::<E, EAs>::new) + .serialize(serializer) + } +} + +// endregion +/////////////////////////////////////////////////////////////////////////////// +// region: Collection Types (e.g., Maps, Sets, Vec) + +macro_rules! seq_impl { + ($ty:ident < T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound:ident )* >) => { + impl<T, U $(, $typaram)*> SerializeAs<$ty<T $(, $typaram)*>> for $ty<U $(, $typaram)*> + where + U: SerializeAs<T>, + $(T: ?Sized + $tbound1 $(+ $tbound2)*,)* + $($typaram: ?Sized + $bound,)* + { + fn serialize_as<S>(source: &$ty<T $(, $typaram)*>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.collect_seq(source.iter().map(|item| SerializeAsWrap::<T, U>::new(item))) + } + } + } +} + +type BoxedSlice<T> = Box<[T]>; +type Slice<T> = [T]; +seq_impl!(BinaryHeap<T>); +seq_impl!(BoxedSlice<T>); +seq_impl!(BTreeSet<T>); +seq_impl!(HashSet<T, H: Sized>); +seq_impl!(LinkedList<T>); +seq_impl!(Slice<T>); +seq_impl!(Vec<T>); +seq_impl!(VecDeque<T>); +#[cfg(feature = "indexmap")] +seq_impl!(IndexSet<T, H: Sized>); + +macro_rules! map_impl { + ($ty:ident < K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound:ident)* >) => { + impl<K, KU, V, VU $(, $typaram)*> SerializeAs<$ty<K, V $(, $typaram)*>> for $ty<KU, VU $(, $typaram)*> + where + KU: SerializeAs<K>, + VU: SerializeAs<V>, + $(K: ?Sized + $kbound1 $(+ $kbound2)*,)* + $($typaram: ?Sized + $bound,)* + { + fn serialize_as<S>(source: &$ty<K, V $(, $typaram)*>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.collect_map(source.iter().map(|(k, v)| (SerializeAsWrap::<K, KU>::new(k), SerializeAsWrap::<V, VU>::new(v)))) + } + } + } +} + +map_impl!(BTreeMap<K, V>); +map_impl!(HashMap<K, V, H: Sized>); +#[cfg(feature = "indexmap")] +map_impl!(IndexMap<K, V, H: Sized>); + +macro_rules! tuple_impl { + ($len:literal $($n:tt $t:ident $tas:ident)+) => { + impl<$($t, $tas,)+> SerializeAs<($($t,)+)> for ($($tas,)+) + where + $($tas: SerializeAs<$t>,)+ + { + fn serialize_as<S>(tuple: &($($t,)+), serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + use serde::ser::SerializeTuple; + let mut tup = serializer.serialize_tuple($len)?; + $( + tup.serialize_element(&SerializeAsWrap::<$t, $tas>::new(&tuple.$n))?; + )+ + tup.end() + } + } + }; +} + +tuple_impl!(1 0 T0 As0); +tuple_impl!(2 0 T0 As0 1 T1 As1); +tuple_impl!(3 0 T0 As0 1 T1 As1 2 T2 As2); +tuple_impl!(4 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3); +tuple_impl!(5 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4); +tuple_impl!(6 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5); +tuple_impl!(7 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6); +tuple_impl!(8 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7); +tuple_impl!(9 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7 8 T8 As8); +tuple_impl!(10 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7 8 T8 As8 9 T9 As9); +tuple_impl!(11 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7 8 T8 As8 9 T9 As9 10 T10 As10); +tuple_impl!(12 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7 8 T8 As8 9 T9 As9 10 T10 As10 11 T11 As11); +tuple_impl!(13 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7 8 T8 As8 9 T9 As9 10 T10 As10 11 T11 As11 12 T12 As12); +tuple_impl!(14 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7 8 T8 As8 9 T9 As9 10 T10 As10 11 T11 As11 12 T12 As12 13 T13 As13); +tuple_impl!(15 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7 8 T8 As8 9 T9 As9 10 T10 As10 11 T11 As11 12 T12 As12 13 T13 As13 14 T14 As14); +tuple_impl!(16 0 T0 As0 1 T1 As1 2 T2 As2 3 T3 As3 4 T4 As4 5 T5 As5 6 T6 As6 7 T7 As7 8 T8 As8 9 T9 As9 10 T10 As10 11 T11 As11 12 T12 As12 13 T13 As13 14 T14 As14 15 T15 As15); + +macro_rules! map_as_tuple_seq { + ($ty:ident < K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V >) => { + impl<K, KAs, V, VAs> SerializeAs<$ty<K, V>> for Vec<(KAs, VAs)> + where + KAs: SerializeAs<K>, + VAs: SerializeAs<V>, + { + fn serialize_as<S>(source: &$ty<K, V>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.collect_seq(source.iter().map(|(k, v)| { + ( + SerializeAsWrap::<K, KAs>::new(k), + SerializeAsWrap::<V, VAs>::new(v), + ) + })) + } + } + }; +} +map_as_tuple_seq!(BTreeMap<K, V>); +// TODO HashMap with a custom hasher support would be better, but results in "unconstrained type parameter" +map_as_tuple_seq!(HashMap<K, V>); +#[cfg(feature = "indexmap")] +map_as_tuple_seq!(IndexMap<K, V>); + +// endregion +/////////////////////////////////////////////////////////////////////////////// +// region: Conversion types which cause different serialization behavior + +impl<T> SerializeAs<T> for Same +where + T: Serialize + ?Sized, +{ + fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + source.serialize(serializer) + } +} + +impl<T> SerializeAs<T> for DisplayFromStr +where + T: Display, +{ + fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + crate::rust::display_fromstr::serialize(source, serializer) + } +} + +impl<T, U> SerializeAs<Vec<T>> for VecSkipError<U> +where + U: SerializeAs<T>, +{ + fn serialize_as<S>(source: &Vec<T>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + Vec::<U>::serialize_as(source, serializer) + } +} + +impl<AsRefStr> SerializeAs<Option<AsRefStr>> for NoneAsEmptyString +where + AsRefStr: AsRef<str>, +{ + fn serialize_as<S>(source: &Option<AsRefStr>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + crate::rust::string_empty_as_none::serialize(source, serializer) + } +} + +macro_rules! tuple_seq_as_map_impl_intern { + ($tyorig:ty, $ty:ident <K, V>) => { + #[allow(clippy::implicit_hasher)] + impl<K, KAs, V, VAs> SerializeAs<$tyorig> for $ty<KAs, VAs> + where + KAs: SerializeAs<K>, + VAs: SerializeAs<V>, + { + fn serialize_as<S>(source: &$tyorig, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.collect_map(source.iter().map(|(k, v)| { + ( + SerializeAsWrap::<K, KAs>::new(k), + SerializeAsWrap::<V, VAs>::new(v), + ) + })) + } + } + }; +} +macro_rules! tuple_seq_as_map_impl { + ($($ty:ty $(,)?)+) => {$( + tuple_seq_as_map_impl_intern!($ty, BTreeMap<K, V>); + tuple_seq_as_map_impl_intern!($ty, HashMap<K, V>); + )+} +} + +tuple_seq_as_map_impl! { + BinaryHeap<(K, V)>, + BTreeSet<(K, V)>, + LinkedList<(K, V)>, + Option<(K, V)>, + Vec<(K, V)>, + VecDeque<(K, V)>, +} +tuple_seq_as_map_impl!(HashSet<(K, V)>); +#[cfg(feature = "indexmap")] +tuple_seq_as_map_impl!(IndexSet<(K, V)>); + +impl<T, TAs> SerializeAs<T> for DefaultOnError<TAs> +where + TAs: SerializeAs<T>, +{ + fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + TAs::serialize_as(source, serializer) + } +} + +impl SerializeAs<Vec<u8>> for BytesOrString { + fn serialize_as<S>(source: &Vec<u8>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + source.serialize(serializer) + } +} + +impl<SEPARATOR, I, T> SerializeAs<I> for StringWithSeparator<SEPARATOR, T> +where + SEPARATOR: Separator, + for<'a> &'a I: IntoIterator<Item = &'a T>, + T: ToString, +{ + fn serialize_as<S>(source: &I, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + let mut s = String::new(); + for v in source { + s.push_str(&*v.to_string()); + s.push_str(SEPARATOR::separator()); + } + serializer.serialize_str(if !s.is_empty() { + // remove trailing separator if present + &s[..s.len() - SEPARATOR::separator().len()] + } else { + &s[..] + }) + } +} + +macro_rules! use_signed_duration { + ( + $main_trait:ident $internal_trait:ident => + { + $ty:ty => + $({ + $format:ty, $strictness:ty => + $($tbound:ident: $bound:ident $(,)?)* + })* + } + ) => { + $( + impl<$($tbound,)*> SerializeAs<$ty> for $main_trait<$format, $strictness> + where + $($tbound: $bound,)* + { + fn serialize_as<S>(source: &$ty, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + $internal_trait::<$format, $strictness>::serialize_as( + &DurationSigned::from(source), + serializer, + ) + } + } + )* + }; + ( + $( $main_trait:ident $internal_trait:ident, )+ => $rest:tt + ) => { + $( use_signed_duration!($main_trait $internal_trait => $rest); )+ + }; +} + +use_signed_duration!( + DurationSeconds DurationSeconds, + DurationMilliSeconds DurationMilliSeconds, + DurationMicroSeconds DurationMicroSeconds, + DurationNanoSeconds DurationNanoSeconds, + => { + Duration => + {u64, STRICTNESS => STRICTNESS: Strictness} + {f64, STRICTNESS => STRICTNESS: Strictness} + {String, STRICTNESS => STRICTNESS: Strictness} + } +); +use_signed_duration!( + DurationSecondsWithFrac DurationSecondsWithFrac, + DurationMilliSecondsWithFrac DurationMilliSecondsWithFrac, + DurationMicroSecondsWithFrac DurationMicroSecondsWithFrac, + DurationNanoSecondsWithFrac DurationNanoSecondsWithFrac, + => { + Duration => + {f64, STRICTNESS => STRICTNESS: Strictness} + {String, STRICTNESS => STRICTNESS: Strictness} + } +); + +use_signed_duration!( + TimestampSeconds DurationSeconds, + TimestampMilliSeconds DurationMilliSeconds, + TimestampMicroSeconds DurationMicroSeconds, + TimestampNanoSeconds DurationNanoSeconds, + => { + SystemTime => + {i64, STRICTNESS => STRICTNESS: Strictness} + {f64, STRICTNESS => STRICTNESS: Strictness} + {String, STRICTNESS => STRICTNESS: Strictness} + } +); +use_signed_duration!( + TimestampSecondsWithFrac DurationSecondsWithFrac, + TimestampMilliSecondsWithFrac DurationMilliSecondsWithFrac, + TimestampMicroSecondsWithFrac DurationMicroSecondsWithFrac, + TimestampNanoSecondsWithFrac DurationNanoSecondsWithFrac, + => { + SystemTime => + {f64, STRICTNESS => STRICTNESS: Strictness} + {String, STRICTNESS => STRICTNESS: Strictness} + } +); + +impl<T, U> SerializeAs<T> for DefaultOnNull<U> +where + U: SerializeAs<T>, +{ + fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.serialize_some(&SerializeAsWrap::<T, U>::new(source)) + } +} + +impl SerializeAs<&[u8]> for Bytes { + fn serialize_as<S>(bytes: &&[u8], serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.serialize_bytes(bytes) + } +} + +impl SerializeAs<Vec<u8>> for Bytes { + fn serialize_as<S>(bytes: &Vec<u8>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.serialize_bytes(bytes) + } +} + +impl SerializeAs<Box<[u8]>> for Bytes { + fn serialize_as<S>(bytes: &Box<[u8]>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.serialize_bytes(bytes) + } +} + +impl<'a> SerializeAs<Cow<'a, [u8]>> for Bytes { + fn serialize_as<S>(bytes: &Cow<'a, [u8]>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.serialize_bytes(bytes) + } +} + +impl<T, U> SerializeAs<Vec<T>> for OneOrMany<U, formats::PreferOne> +where + U: SerializeAs<T>, +{ + fn serialize_as<S>(source: &Vec<T>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + match source.len() { + 1 => SerializeAsWrap::<T, U>::new(source.iter().next().expect("Cannot be empty")) + .serialize(serializer), + _ => SerializeAsWrap::<Vec<T>, Vec<U>>::new(source).serialize(serializer), + } + } +} + +impl<T, U> SerializeAs<Vec<T>> for OneOrMany<U, formats::PreferMany> +where + U: SerializeAs<T>, +{ + fn serialize_as<S>(source: &Vec<T>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + SerializeAsWrap::<Vec<T>, Vec<U>>::new(source).serialize(serializer) + } +} + +impl<T, TAs1> SerializeAs<T> for PickFirst<(TAs1,)> +where + TAs1: SerializeAs<T>, +{ + fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + SerializeAsWrap::<T, TAs1>::new(source).serialize(serializer) + } +} + +impl<T, TAs1, TAs2> SerializeAs<T> for PickFirst<(TAs1, TAs2)> +where + TAs1: SerializeAs<T>, +{ + fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + SerializeAsWrap::<T, TAs1>::new(source).serialize(serializer) + } +} + +impl<T, TAs1, TAs2, TAs3> SerializeAs<T> for PickFirst<(TAs1, TAs2, TAs3)> +where + TAs1: SerializeAs<T>, +{ + fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + SerializeAsWrap::<T, TAs1>::new(source).serialize(serializer) + } +} + +impl<T, TAs1, TAs2, TAs3, TAs4> SerializeAs<T> for PickFirst<(TAs1, TAs2, TAs3, TAs4)> +where + TAs1: SerializeAs<T>, +{ + fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + SerializeAsWrap::<T, TAs1>::new(source).serialize(serializer) + } +} + +impl<T, U> SerializeAs<T> for FromInto<U> +where + T: Into<U> + Clone, + U: Serialize, +{ + fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + source.clone().into().serialize(serializer) + } +} + +impl<T, U> SerializeAs<T> for TryFromInto<U> +where + T: TryInto<U> + Clone, + <T as TryInto<U>>::Error: Display, + U: Serialize, +{ + fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + source + .clone() + .try_into() + .map_err(S::Error::custom)? + .serialize(serializer) + } +} + +impl<'a> SerializeAs<Cow<'a, str>> for BorrowCow { + fn serialize_as<S>(source: &Cow<'a, str>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.serialize_str(source) + } +} + +impl<'a> SerializeAs<Cow<'a, [u8]>> for BorrowCow { + fn serialize_as<S>(value: &Cow<'a, [u8]>, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.collect_seq(value.iter()) + } +} + +impl<STRICTNESS: Strictness> SerializeAs<bool> for BoolFromInt<STRICTNESS> { + fn serialize_as<S>(source: &bool, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.serialize_u8(*source as u8) + } +} + +// endregion diff --git a/third_party/rust/serde_with/src/ser/legacy_arrays.rs b/third_party/rust/serde_with/src/ser/legacy_arrays.rs new file mode 100644 index 0000000000..2c228ee640 --- /dev/null +++ b/third_party/rust/serde_with/src/ser/legacy_arrays.rs @@ -0,0 +1,34 @@ +use super::*; +use alloc::collections::BTreeMap; +use std::collections::HashMap; + +macro_rules! array_impl { + ($($len:literal)+) => {$( + impl<T, As> SerializeAs<[T; $len]> for [As; $len] + where + As: SerializeAs<T>, + { + fn serialize_as<S>(array: &[T; $len], serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + use serde::ser::SerializeTuple; + let mut arr = serializer.serialize_tuple($len)?; + for elem in array { + arr.serialize_element(&SerializeAsWrap::<T, As>::new(elem))?; + } + arr.end() + } + } + )+}; +} + +array_impl!(0 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); + +tuple_seq_as_map_impl! { + [(K, V); 0], [(K, V); 1], [(K, V); 2], [(K, V); 3], [(K, V); 4], [(K, V); 5], [(K, V); 6], + [(K, V); 7], [(K, V); 8], [(K, V); 9], [(K, V); 10], [(K, V); 11], [(K, V); 12], [(K, V); 13], + [(K, V); 14], [(K, V); 15], [(K, V); 16], [(K, V); 17], [(K, V); 18], [(K, V); 19], [(K, V); 20], + [(K, V); 21], [(K, V); 22], [(K, V); 23], [(K, V); 24], [(K, V); 25], [(K, V); 26], [(K, V); 27], + [(K, V); 28], [(K, V); 29], [(K, V); 30], [(K, V); 31], [(K, V); 32], +} diff --git a/third_party/rust/serde_with/src/ser/mod.rs b/third_party/rust/serde_with/src/ser/mod.rs new file mode 100644 index 0000000000..dda0891e89 --- /dev/null +++ b/third_party/rust/serde_with/src/ser/mod.rs @@ -0,0 +1,158 @@ +//! Module for [`SerializeAs`][] implementations +//! +//! The module contains the [`SerializeAs`][] trait and helper code. +//! Additionally, it contains implementations of [`SerializeAs`][] for types defined in the Rust Standard Library or this crate. +//! +//! You can find more details on how to implement this trait for your types in the documentation of the [`SerializeAs`][] trait and details about the usage in the [user guide][]. +//! +//! [user guide]: crate::guide + +mod const_arrays; +#[macro_use] +mod impls; + +use super::*; + +/// A **data structure** that can be serialized into any data format supported by Serde, analogue to [`Serialize`]. +/// +/// The trait is analogue to the [`serde::Serialize`][`Serialize`] trait, with the same meaning of input and output arguments. +/// It can and should the implemented using the same code structure as the [`Serialize`] trait. +/// As such, the same advice for [implementing `Serialize`][impl-serialize] applies here. +/// +/// # Differences to [`Serialize`] +/// +/// The trait is only required for container-like types or types implementing specific conversion functions. +/// Container-like types are [`Vec`], [`BTreeMap`], but also [`Option`] and [`Box`]. +/// Conversion types serialize into a different serde data type. +/// For example, [`DisplayFromStr`] uses the [`Display`] trait to serialize a String and [`DurationSeconds`] converts a [`Duration`] into either String or integer values. +/// +/// This code shows how to implement [`Serialize`] for [`Box`]: +/// +/// ```rust,ignore +/// impl<T> Serialize for Box<T> +/// where +/// T: Serialize, +/// { +/// #[inline] +/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> +/// where +/// S: Serializer, +/// { +/// (**self).serialize(serializer) +/// } +/// } +/// ``` +/// +/// and this code shows how to do the same using [`SerializeAs`][]: +/// +/// ```rust,ignore +/// impl<T, U> SerializeAs<Box<T>> for Box<U> +/// where +/// U: SerializeAs<T>, +/// { +/// fn serialize_as<S>(source: &Box<T>, serializer: S) -> Result<S::Ok, S::Error> +/// where +/// S: Serializer, +/// { +/// SerializeAsWrap::<T, U>::new(source).serialize(serializer) +/// } +/// } +/// ``` +/// +/// It uses two type parameters, `T` and `U` instead of only one and performs the serialization step using the `SerializeAsWrap` type. +/// The `T` type is the on the Rust side before serialization, whereas the `U` type determines how the value will be serialized. +/// These two changes are usually enough to make a container type implement [`SerializeAs`][]. +/// +/// [`SerializeAsWrap`] is a piece of glue code which turns [`SerializeAs`] into a serde compatible datatype, by converting all calls to `serialize` into `serialize_as`. +/// This allows us to implement [`SerializeAs`] such that it can be applied recursively throughout the whole data structure. +/// This is mostly important for container types, such as `Vec` or `BTreeMap`. +/// In a `BTreeMap` this allows us to specify two different serialization behaviors, one for key and one for value, using the [`SerializeAs`] trait. +/// +/// ## Implementing a converter Type +/// +/// This shows a simplified implementation for [`DisplayFromStr`]. +/// +/// ```rust +/// # #[cfg(all(feature = "macros"))] { +/// # use serde_with::SerializeAs; +/// # use std::fmt::Display; +/// struct DisplayFromStr; +/// +/// impl<T> SerializeAs<T> for DisplayFromStr +/// where +/// T: Display, +/// { +/// fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> +/// where +/// S: serde::Serializer, +/// { +/// serializer.collect_str(&source) +/// } +/// } +/// # +/// # #[serde_with::serde_as] +/// # #[derive(serde::Serialize)] +/// # struct S (#[serde_as(as = "DisplayFromStr")] bool); +/// # +/// # assert_eq!(r#""false""#, serde_json::to_string(&S(false)).unwrap()); +/// # } +/// ``` +/// +/// [`Box`]: std::boxed::Box +/// [`BTreeMap`]: std::collections::BTreeMap +/// [`Display`]: std::fmt::Display +/// [`Duration`]: std::time::Duration +/// [`Vec`]: std::vec::Vec +/// [impl-serialize]: https://serde.rs/impl-serialize.html +pub trait SerializeAs<T: ?Sized> { + /// Serialize this value into the given Serde serializer. + fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer; +} + +/// Helper type to implement [`SerializeAs`] for container-like types. +#[derive(Debug)] +pub struct SerializeAsWrap<'a, T: ?Sized, U: ?Sized> { + value: &'a T, + marker: PhantomData<U>, +} + +impl<'a, T, U> SerializeAsWrap<'a, T, U> +where + T: ?Sized, + U: ?Sized, +{ + /// Create new instance with provided value. + pub fn new(value: &'a T) -> Self { + Self { + value, + marker: PhantomData, + } + } +} + +impl<'a, T, U> Serialize for SerializeAsWrap<'a, T, U> +where + T: ?Sized, + U: ?Sized, + U: SerializeAs<T>, +{ + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + U::serialize_as(self.value, serializer) + } +} + +impl<'a, T, U> From<&'a T> for SerializeAsWrap<'a, T, U> +where + T: ?Sized, + U: ?Sized, + U: SerializeAs<T>, +{ + fn from(value: &'a T) -> Self { + Self::new(value) + } +} diff --git a/third_party/rust/serde_with/src/serde_conv.rs b/third_party/rust/serde_with/src/serde_conv.rs new file mode 100644 index 0000000000..c5659ebfda --- /dev/null +++ b/third_party/rust/serde_with/src/serde_conv.rs @@ -0,0 +1,147 @@ +/// Create new conversion adapters from functions +/// +/// The macro lets you create a new converter, which is usable for serde's with-attribute and `#[serde_as]`. +/// Its main use case is to write simple converters for types, which are not serializable. +/// Another use-case is to change the serialization behavior if the implemented `Serialize`/`Deserialize` trait is insufficient. +/// +/// The macro takes four arguments: +/// +/// 1. The name of the converter type. +/// The type can be prefixed with a visibility modifies like `pub` or `pub(crate)`. +/// By default, the type is not marked as public (`pub(self)`). +/// 2. The type `T` we want to extend with custom behavior. +/// 3. A function or macro taking a `&T` and returning a serializable type. +/// 4. A function or macro taking a deserializable type and returning a `Result<T, E>`. +/// The error type `E` must implement `Display`. +/// +/// # Example +/// +/// In this example, we write custom serialization behavior for a `Rgb` type. +/// We want to serialize it as a `[u8; 3]`. +/// +/// ```rust +/// # #[cfg(feature = "macros")] { +/// # use serde::{Serialize, Deserialize}; +/// +/// #[derive(Clone, Copy, Debug, PartialEq)] +/// struct Rgb { +/// red: u8, +/// green: u8, +/// blue: u8, +/// } +/// +/// serde_with::serde_conv!( +/// RgbAsArray, +/// Rgb, +/// |rgb: &Rgb| [rgb.red, rgb.green, rgb.blue], +/// |value: [u8; 3]| -> Result<_, std::convert::Infallible> { +/// Ok(Rgb { +/// red: value[0], +/// green: value[1], +/// blue: value[2], +/// }) +/// } +/// ); +/// +/// ////////////////////////////////////////////////// +/// +/// // We define some colors to be used later +/// +/// let green = Rgb {red: 0, green: 255, blue: 0}; +/// let orange = Rgb {red: 255, green: 128, blue: 0}; +/// let pink = Rgb {red: 255, green: 0, blue: 255}; +/// +/// ////////////////////////////////////////////////// +/// +/// // We can now use the `RgbAsArray` adapter with `serde_as`. +/// +/// #[serde_with::serde_as] +/// #[derive(Debug, PartialEq, Serialize, Deserialize)] +/// struct Colors { +/// #[serde_as(as = "RgbAsArray")] +/// one_rgb: Rgb, +/// #[serde_as(as = "Vec<RgbAsArray>")] +/// rgbs_in_vec: Vec<Rgb>, +/// } +/// +/// let data = Colors { +/// one_rgb: orange, +/// rgbs_in_vec: vec![green, pink], +/// }; +/// let json = serde_json::json!({ +/// "one_rgb": [255, 128, 0], +/// "rgbs_in_vec": [ +/// [0, 255, 0], +/// [255, 0, 255] +/// ] +/// }); +/// +/// assert_eq!(json, serde_json::to_value(&data).unwrap()); +/// assert_eq!(data, serde_json::from_value(json).unwrap()); +/// +/// ////////////////////////////////////////////////// +/// +/// // The types generated by `serde_conv` is also compatible with serde's with attribute +/// +/// #[derive(Debug, PartialEq, Serialize, Deserialize)] +/// struct ColorsWith { +/// #[serde(with = "RgbAsArray")] +/// rgb_with: Rgb, +/// } +/// +/// let data = ColorsWith { +/// rgb_with: pink, +/// }; +/// let json = serde_json::json!({ +/// "rgb_with": [255, 0, 255] +/// }); +/// +/// assert_eq!(json, serde_json::to_value(&data).unwrap()); +/// assert_eq!(data, serde_json::from_value(json).unwrap()); +/// # } +/// ``` +#[macro_export] +macro_rules! serde_conv { + ($m:ident, $t:ty, $ser:expr, $de:expr) => {$crate::serde_conv!(pub(self) $m, $t, $ser, $de);}; + ($vis:vis $m:ident, $t:ty, $ser:expr, $de:expr) => { + #[allow(non_camel_case_types)] + $vis struct $m; + + #[allow(clippy::ptr_arg)] + impl $m { + $vis fn serialize<S>(x: &$t, serializer: S) -> ::std::result::Result<S::Ok, S::Error> + where + S: $crate::serde::Serializer, + { + let y = $ser(x); + $crate::serde::Serialize::serialize(&y, serializer) + } + + $vis fn deserialize<'de, D>(deserializer: D) -> ::std::result::Result<$t, D::Error> + where + D: $crate::serde::Deserializer<'de>, + { + let y = $crate::serde::Deserialize::deserialize(deserializer)?; + $de(y).map_err($crate::serde::de::Error::custom) + } + } + + impl $crate::SerializeAs<$t> for $m { + fn serialize_as<S>(x: &$t, serializer: S) -> ::std::result::Result<S::Ok, S::Error> + where + S: $crate::serde::Serializer, + { + Self::serialize(x, serializer) + } + } + + impl<'de> $crate::DeserializeAs<'de, $t> for $m { + fn deserialize_as<D>(deserializer: D) -> ::std::result::Result<$t, D::Error> + where + D: $crate::serde::Deserializer<'de>, + { + Self::deserialize(deserializer) + } + } + }; +} diff --git a/third_party/rust/serde_with/src/time_0_3.rs b/third_party/rust/serde_with/src/time_0_3.rs new file mode 100644 index 0000000000..76dca006d2 --- /dev/null +++ b/third_party/rust/serde_with/src/time_0_3.rs @@ -0,0 +1,382 @@ +//! De/Serialization of [time v0.3][time] types +//! +//! This modules is only available if using the `time_0_3` feature of the crate. +//! +//! [time]: https://docs.rs/time/0.3/ + +use crate::{ + de::DeserializeAs, + formats::{Flexible, Format, Strict, Strictness}, + ser::SerializeAs, + utils::duration::{DurationSigned, Sign}, + DurationMicroSeconds, DurationMicroSecondsWithFrac, DurationMilliSeconds, + DurationMilliSecondsWithFrac, DurationNanoSeconds, DurationNanoSecondsWithFrac, + DurationSeconds, DurationSecondsWithFrac, TimestampMicroSeconds, TimestampMicroSecondsWithFrac, + TimestampMilliSeconds, TimestampMilliSecondsWithFrac, TimestampNanoSeconds, + TimestampNanoSecondsWithFrac, TimestampSeconds, TimestampSecondsWithFrac, +}; +use alloc::{format, string::String}; +use serde::{de, ser::Error as _, Deserializer, Serialize, Serializer}; +use std::{convert::TryInto, fmt, time::Duration as StdDuration}; +use time_0_3::{ + format_description::well_known::{Rfc2822, Rfc3339}, + Duration, OffsetDateTime, PrimitiveDateTime, +}; + +/// Create a [`PrimitiveDateTime`] for the Unix Epoch +fn unix_epoch_primitive() -> PrimitiveDateTime { + PrimitiveDateTime::new( + time_0_3::Date::from_ordinal_date(1970, 1).unwrap(), + time_0_3::Time::from_hms_nano(0, 0, 0, 0).unwrap(), + ) +} + +/// Convert a [`time::Duration`][time_0_3::Duration] into a [`DurationSigned`] +fn duration_into_duration_signed(dur: &Duration) -> DurationSigned { + let std_dur = StdDuration::new( + dur.whole_seconds().unsigned_abs(), + dur.subsec_nanoseconds().unsigned_abs(), + ); + + DurationSigned::with_duration( + // A duration of 0 is not positive, so check for negative value. + if dur.is_negative() { + Sign::Negative + } else { + Sign::Positive + }, + std_dur, + ) +} + +/// Convert a [`DurationSigned`] into a [`time_0_3::Duration`] +fn duration_from_duration_signed<'de, D>(sdur: DurationSigned) -> Result<Duration, D::Error> +where + D: Deserializer<'de>, +{ + let mut dur: Duration = match sdur.duration.try_into() { + Ok(dur) => dur, + Err(msg) => { + return Err(de::Error::custom(format!( + "Duration is outside of the representable range: {}", + msg + ))) + } + }; + if sdur.sign.is_negative() { + dur = -dur; + } + Ok(dur) +} + +macro_rules! use_duration_signed_ser { + ( + $main_trait:ident $internal_trait:ident => + { + $ty:ty; $converter:ident => + $({ + $format:ty, $strictness:ty => + $($tbound:ident: $bound:ident $(,)?)* + })* + } + ) => { + $( + impl<$($tbound ,)*> SerializeAs<$ty> for $main_trait<$format, $strictness> + where + $($tbound: $bound,)* + { + fn serialize_as<S>(source: &$ty, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + let dur: DurationSigned = $converter(source); + $internal_trait::<$format, $strictness>::serialize_as( + &dur, + serializer, + ) + } + } + )* + }; + ( + $( $main_trait:ident $internal_trait:ident, )+ => $rest:tt + ) => { + $( use_duration_signed_ser!($main_trait $internal_trait => $rest); )+ + }; +} + +fn offset_datetime_to_duration(source: &OffsetDateTime) -> DurationSigned { + duration_into_duration_signed(&(*source - OffsetDateTime::UNIX_EPOCH)) +} + +fn primitive_datetime_to_duration(source: &PrimitiveDateTime) -> DurationSigned { + duration_into_duration_signed(&(*source - unix_epoch_primitive())) +} + +use_duration_signed_ser!( + DurationSeconds DurationSeconds, + DurationMilliSeconds DurationMilliSeconds, + DurationMicroSeconds DurationMicroSeconds, + DurationNanoSeconds DurationNanoSeconds, + => { + Duration; duration_into_duration_signed => + {i64, STRICTNESS => STRICTNESS: Strictness} + {f64, STRICTNESS => STRICTNESS: Strictness} + {String, STRICTNESS => STRICTNESS: Strictness} + } +); +use_duration_signed_ser!( + TimestampSeconds DurationSeconds, + TimestampMilliSeconds DurationMilliSeconds, + TimestampMicroSeconds DurationMicroSeconds, + TimestampNanoSeconds DurationNanoSeconds, + => { + OffsetDateTime; offset_datetime_to_duration => + {i64, STRICTNESS => STRICTNESS: Strictness} + {f64, STRICTNESS => STRICTNESS: Strictness} + {String, STRICTNESS => STRICTNESS: Strictness} + } +); +use_duration_signed_ser!( + TimestampSeconds DurationSeconds, + TimestampMilliSeconds DurationMilliSeconds, + TimestampMicroSeconds DurationMicroSeconds, + TimestampNanoSeconds DurationNanoSeconds, + => { + PrimitiveDateTime; primitive_datetime_to_duration => + {i64, STRICTNESS => STRICTNESS: Strictness} + {f64, STRICTNESS => STRICTNESS: Strictness} + {String, STRICTNESS => STRICTNESS: Strictness} + } +); + +// Duration/Timestamp WITH FRACTIONS +use_duration_signed_ser!( + DurationSecondsWithFrac DurationSecondsWithFrac, + DurationMilliSecondsWithFrac DurationMilliSecondsWithFrac, + DurationMicroSecondsWithFrac DurationMicroSecondsWithFrac, + DurationNanoSecondsWithFrac DurationNanoSecondsWithFrac, + => { + Duration; duration_into_duration_signed => + {f64, STRICTNESS => STRICTNESS: Strictness} + {String, STRICTNESS => STRICTNESS: Strictness} + } +); +use_duration_signed_ser!( + TimestampSecondsWithFrac DurationSecondsWithFrac, + TimestampMilliSecondsWithFrac DurationMilliSecondsWithFrac, + TimestampMicroSecondsWithFrac DurationMicroSecondsWithFrac, + TimestampNanoSecondsWithFrac DurationNanoSecondsWithFrac, + => { + OffsetDateTime; offset_datetime_to_duration => + {f64, STRICTNESS => STRICTNESS: Strictness} + {String, STRICTNESS => STRICTNESS: Strictness} + } +); +use_duration_signed_ser!( + TimestampSecondsWithFrac DurationSecondsWithFrac, + TimestampMilliSecondsWithFrac DurationMilliSecondsWithFrac, + TimestampMicroSecondsWithFrac DurationMicroSecondsWithFrac, + TimestampNanoSecondsWithFrac DurationNanoSecondsWithFrac, + => { + PrimitiveDateTime; primitive_datetime_to_duration => + {f64, STRICTNESS => STRICTNESS: Strictness} + {String, STRICTNESS => STRICTNESS: Strictness} + } +); + +macro_rules! use_duration_signed_de { + ( + $main_trait:ident $internal_trait:ident => + { + $ty:ty; $converter:ident => + $({ + $format:ty, $strictness:ty => + $($tbound:ident: $bound:ident)* + })* + } + ) =>{ + $( + impl<'de, $($tbound,)*> DeserializeAs<'de, $ty> for $main_trait<$format, $strictness> + where + $($tbound: $bound,)* + { + fn deserialize_as<D>(deserializer: D) -> Result<$ty, D::Error> + where + D: Deserializer<'de>, + { + let dur: DurationSigned = $internal_trait::<$format, $strictness>::deserialize_as(deserializer)?; + $converter::<D>(dur) + } + } + )* + }; + ( + $( $main_trait:ident $internal_trait:ident, )+ => $rest:tt + ) => { + $( use_duration_signed_de!($main_trait $internal_trait => $rest); )+ + }; +} + +fn duration_to_offset_datetime<'de, D>(dur: DurationSigned) -> Result<OffsetDateTime, D::Error> +where + D: Deserializer<'de>, +{ + Ok(OffsetDateTime::UNIX_EPOCH + duration_from_duration_signed::<D>(dur)?) +} + +fn duration_to_primitive_datetime<'de, D>( + dur: DurationSigned, +) -> Result<PrimitiveDateTime, D::Error> +where + D: Deserializer<'de>, +{ + Ok(unix_epoch_primitive() + duration_from_duration_signed::<D>(dur)?) +} + +// No subsecond precision +use_duration_signed_de!( + DurationSeconds DurationSeconds, + DurationMilliSeconds DurationMilliSeconds, + DurationMicroSeconds DurationMicroSeconds, + DurationNanoSeconds DurationNanoSeconds, + => { + Duration; duration_from_duration_signed => + {i64, Strict =>} + {f64, Strict =>} + {String, Strict =>} + {FORMAT, Flexible => FORMAT: Format} + } +); +use_duration_signed_de!( + TimestampSeconds DurationSeconds, + TimestampMilliSeconds DurationMilliSeconds, + TimestampMicroSeconds DurationMicroSeconds, + TimestampNanoSeconds DurationNanoSeconds, + => { + OffsetDateTime; duration_to_offset_datetime => + {i64, Strict =>} + {f64, Strict =>} + {String, Strict =>} + {FORMAT, Flexible => FORMAT: Format} + } +); +use_duration_signed_de!( + TimestampSeconds DurationSeconds, + TimestampMilliSeconds DurationMilliSeconds, + TimestampMicroSeconds DurationMicroSeconds, + TimestampNanoSeconds DurationNanoSeconds, + => { + PrimitiveDateTime; duration_to_primitive_datetime => + {i64, Strict =>} + {f64, Strict =>} + {String, Strict =>} + {FORMAT, Flexible => FORMAT: Format} + } +); + +// Duration/Timestamp WITH FRACTIONS +use_duration_signed_de!( + DurationSecondsWithFrac DurationSecondsWithFrac, + DurationMilliSecondsWithFrac DurationMilliSecondsWithFrac, + DurationMicroSecondsWithFrac DurationMicroSecondsWithFrac, + DurationNanoSecondsWithFrac DurationNanoSecondsWithFrac, + => { + Duration; duration_from_duration_signed => + {f64, Strict =>} + {String, Strict =>} + {FORMAT, Flexible => FORMAT: Format} + } +); +use_duration_signed_de!( + TimestampSecondsWithFrac DurationSecondsWithFrac, + TimestampMilliSecondsWithFrac DurationMilliSecondsWithFrac, + TimestampMicroSecondsWithFrac DurationMicroSecondsWithFrac, + TimestampNanoSecondsWithFrac DurationNanoSecondsWithFrac, + => { + OffsetDateTime; duration_to_offset_datetime => + {f64, Strict =>} + {String, Strict =>} + {FORMAT, Flexible => FORMAT: Format} + } +); +use_duration_signed_de!( + TimestampSecondsWithFrac DurationSecondsWithFrac, + TimestampMilliSecondsWithFrac DurationMilliSecondsWithFrac, + TimestampMicroSecondsWithFrac DurationMicroSecondsWithFrac, + TimestampNanoSecondsWithFrac DurationNanoSecondsWithFrac, + => { + PrimitiveDateTime; duration_to_primitive_datetime => + {f64, Strict =>} + {String, Strict =>} + {FORMAT, Flexible => FORMAT: Format} + } +); + +impl SerializeAs<OffsetDateTime> for Rfc2822 { + fn serialize_as<S>(datetime: &OffsetDateTime, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + datetime + .format(&Rfc2822) + .map_err(S::Error::custom)? + .serialize(serializer) + } +} + +impl<'de> DeserializeAs<'de, OffsetDateTime> for Rfc2822 { + fn deserialize_as<D>(deserializer: D) -> Result<OffsetDateTime, D::Error> + where + D: Deserializer<'de>, + { + struct Visitor; + impl<'de> de::Visitor<'de> for Visitor { + type Value = OffsetDateTime; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a RFC2822-formatted `OffsetDateTime`") + } + + fn visit_str<E: de::Error>(self, value: &str) -> Result<Self::Value, E> { + Self::Value::parse(value, &Rfc2822).map_err(E::custom) + } + } + + deserializer.deserialize_str(Visitor) + } +} + +impl SerializeAs<OffsetDateTime> for Rfc3339 { + fn serialize_as<S>(datetime: &OffsetDateTime, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + datetime + .format(&Rfc3339) + .map_err(S::Error::custom)? + .serialize(serializer) + } +} + +impl<'de> DeserializeAs<'de, OffsetDateTime> for Rfc3339 { + fn deserialize_as<D>(deserializer: D) -> Result<OffsetDateTime, D::Error> + where + D: Deserializer<'de>, + { + struct Visitor; + impl<'de> de::Visitor<'de> for Visitor { + type Value = OffsetDateTime; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a RFC3339-formatted `OffsetDateTime`") + } + + fn visit_str<E: de::Error>(self, value: &str) -> Result<Self::Value, E> { + Self::Value::parse(value, &Rfc3339).map_err(E::custom) + } + } + + deserializer.deserialize_str(Visitor) + } +} diff --git a/third_party/rust/serde_with/src/utils.rs b/third_party/rust/serde_with/src/utils.rs new file mode 100644 index 0000000000..ccf99128e4 --- /dev/null +++ b/third_party/rust/serde_with/src/utils.rs @@ -0,0 +1,120 @@ +pub(crate) mod duration; + +use alloc::string::String; +use core::marker::PhantomData; +use serde::de::{Deserialize, MapAccess, SeqAccess}; + +/// Re-Implementation of `serde::private::de::size_hint::cautious` +#[inline] +pub(crate) fn size_hint_cautious(hint: Option<usize>) -> usize { + core::cmp::min(hint.unwrap_or(0), 4096) +} + +pub(crate) const NANOS_PER_SEC: u32 = 1_000_000_000; +// pub(crate) const NANOS_PER_MILLI: u32 = 1_000_000; +// pub(crate) const NANOS_PER_MICRO: u32 = 1_000; +// pub(crate) const MILLIS_PER_SEC: u64 = 1_000; +// pub(crate) const MICROS_PER_SEC: u64 = 1_000_000; + +pub(crate) struct MapIter<'de, A, K, V> { + pub(crate) access: A, + marker: PhantomData<(&'de (), K, V)>, +} + +impl<'de, A, K, V> MapIter<'de, A, K, V> { + pub(crate) fn new(access: A) -> Self + where + A: MapAccess<'de>, + { + Self { + access, + marker: PhantomData, + } + } +} + +impl<'de, A, K, V> Iterator for MapIter<'de, A, K, V> +where + A: MapAccess<'de>, + K: Deserialize<'de>, + V: Deserialize<'de>, +{ + type Item = Result<(K, V), A::Error>; + + fn next(&mut self) -> Option<Self::Item> { + self.access.next_entry().transpose() + } + + fn size_hint(&self) -> (usize, Option<usize>) { + match self.access.size_hint() { + Some(size) => (size, Some(size)), + None => (0, None), + } + } +} + +pub(crate) struct SeqIter<'de, A, T> { + access: A, + marker: PhantomData<(&'de (), T)>, +} + +impl<'de, A, T> SeqIter<'de, A, T> { + pub(crate) fn new(access: A) -> Self + where + A: SeqAccess<'de>, + { + Self { + access, + marker: PhantomData, + } + } +} + +impl<'de, A, T> Iterator for SeqIter<'de, A, T> +where + A: SeqAccess<'de>, + T: Deserialize<'de>, +{ + type Item = Result<T, A::Error>; + + fn next(&mut self) -> Option<Self::Item> { + self.access.next_element().transpose() + } + + fn size_hint(&self) -> (usize, Option<usize>) { + match self.access.size_hint() { + Some(size) => (size, Some(size)), + None => (0, None), + } + } +} + +pub(crate) fn duration_as_secs_f64(dur: &core::time::Duration) -> f64 { + (dur.as_secs() as f64) + (dur.subsec_nanos() as f64) / (NANOS_PER_SEC as f64) +} + +pub(crate) fn duration_signed_from_secs_f64( + secs: f64, +) -> Result<self::duration::DurationSigned, String> { + const MAX_NANOS_F64: f64 = ((u64::max_value() as u128 + 1) * (NANOS_PER_SEC as u128)) as f64; + // TODO why are the seconds converted to nanoseconds first? + // Does it make sense to just truncate the value? + let mut nanos = secs * (NANOS_PER_SEC as f64); + if !nanos.is_finite() { + return Err("got non-finite value when converting float to duration".into()); + } + if nanos >= MAX_NANOS_F64 { + return Err("overflow when converting float to duration".into()); + } + let mut sign = self::duration::Sign::Positive; + if nanos < 0.0 { + nanos = -nanos; + sign = self::duration::Sign::Negative; + } + let nanos = nanos as u128; + Ok(self::duration::DurationSigned::new( + sign, + (nanos / (NANOS_PER_SEC as u128)) as u64, + (nanos % (NANOS_PER_SEC as u128)) as u32, + )) +} diff --git a/third_party/rust/serde_with/src/utils/duration.rs b/third_party/rust/serde_with/src/utils/duration.rs new file mode 100644 index 0000000000..2d5a27e87d --- /dev/null +++ b/third_party/rust/serde_with/src/utils/duration.rs @@ -0,0 +1,559 @@ +//! Internal Helper types + +use crate::{ + formats::{Flexible, Format, Strict, Strictness}, + utils, DeserializeAs, DurationMicroSeconds, DurationMicroSecondsWithFrac, DurationMilliSeconds, + DurationMilliSecondsWithFrac, DurationNanoSeconds, DurationNanoSecondsWithFrac, + DurationSeconds, DurationSecondsWithFrac, SerializeAs, +}; +use alloc::{ + format, + string::{String, ToString}, + vec::Vec, +}; +use core::{fmt, ops::Neg, time::Duration}; +use serde::{ + de::{self, Unexpected, Visitor}, + ser, Deserialize, Deserializer, Serialize, Serializer, +}; +use std::time::SystemTime; + +#[derive(Copy, Clone, Debug, Eq, PartialEq)] +pub(crate) enum Sign { + Positive, + Negative, +} + +impl Sign { + #[allow(dead_code)] + pub(crate) fn is_positive(&self) -> bool { + *self == Sign::Positive + } + + #[allow(dead_code)] + pub(crate) fn is_negative(&self) -> bool { + *self == Sign::Negative + } + + pub(crate) fn apply<T>(&self, value: T) -> T + where + T: Neg<Output = T>, + { + match *self { + Sign::Positive => value, + Sign::Negative => value.neg(), + } + } +} + +#[derive(Copy, Clone, Debug)] +pub(crate) struct DurationSigned { + pub(crate) sign: Sign, + pub(crate) duration: Duration, +} + +impl DurationSigned { + pub(crate) fn new(sign: Sign, secs: u64, nanosecs: u32) -> Self { + Self { + sign, + duration: Duration::new(secs, nanosecs), + } + } + + #[cfg(any(feature = "chrono", feature = "time_0_3"))] + pub(crate) fn with_duration(sign: Sign, duration: Duration) -> Self { + Self { sign, duration } + } + + pub(crate) fn to_system_time<'de, D>(self) -> Result<SystemTime, D::Error> + where + D: Deserializer<'de>, + { + match self.sign { + Sign::Positive => SystemTime::UNIX_EPOCH.checked_add(self.duration), + Sign::Negative => SystemTime::UNIX_EPOCH.checked_sub(self.duration), + } + .ok_or_else(|| { + de::Error::custom("timestamp is outside the range for std::time::SystemTime") + }) + } + + pub(crate) fn to_std_duration<'de, D>(self) -> Result<Duration, D::Error> + where + D: Deserializer<'de>, + { + match self.sign { + Sign::Positive => Ok(self.duration), + Sign::Negative => Err(de::Error::custom("std::time::Duration cannot be negative")), + } + } +} + +impl From<&Duration> for DurationSigned { + fn from(&duration: &Duration) -> Self { + Self { + sign: Sign::Positive, + duration, + } + } +} + +impl From<&SystemTime> for DurationSigned { + fn from(time: &SystemTime) -> Self { + match time.duration_since(SystemTime::UNIX_EPOCH) { + Ok(dur) => DurationSigned { + sign: Sign::Positive, + duration: dur, + }, + Err(err) => DurationSigned { + sign: Sign::Negative, + duration: err.duration(), + }, + } + } +} + +impl core::ops::Mul<u32> for DurationSigned { + type Output = DurationSigned; + + fn mul(mut self, rhs: u32) -> Self::Output { + self.duration *= rhs; + self + } +} + +impl core::ops::Div<u32> for DurationSigned { + type Output = DurationSigned; + + fn div(mut self, rhs: u32) -> Self::Output { + self.duration /= rhs; + self + } +} + +impl<STRICTNESS> SerializeAs<DurationSigned> for DurationSeconds<u64, STRICTNESS> +where + STRICTNESS: Strictness, +{ + fn serialize_as<S>(source: &DurationSigned, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + if source.sign.is_negative() { + return Err(ser::Error::custom( + "cannot serialize a negative Duration as u64", + )); + } + + let mut secs = source.duration.as_secs(); + + // Properly round the value + if source.duration.subsec_millis() >= 500 { + if source.sign.is_positive() { + secs += 1; + } else { + secs -= 1; + } + } + secs.serialize(serializer) + } +} + +impl<STRICTNESS> SerializeAs<DurationSigned> for DurationSeconds<i64, STRICTNESS> +where + STRICTNESS: Strictness, +{ + fn serialize_as<S>(source: &DurationSigned, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + let mut secs = source.sign.apply(source.duration.as_secs() as i64); + + // Properly round the value + if source.duration.subsec_millis() >= 500 { + if source.sign.is_positive() { + secs += 1; + } else { + secs -= 1; + } + } + secs.serialize(serializer) + } +} + +impl<STRICTNESS> SerializeAs<DurationSigned> for DurationSeconds<f64, STRICTNESS> +where + STRICTNESS: Strictness, +{ + fn serialize_as<S>(source: &DurationSigned, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + let mut secs = source.sign.apply(source.duration.as_secs() as f64); + + // Properly round the value + if source.duration.subsec_millis() >= 500 { + if source.sign.is_positive() { + secs += 1.; + } else { + secs -= 1.; + } + } + secs.serialize(serializer) + } +} + +impl<STRICTNESS> SerializeAs<DurationSigned> for DurationSeconds<String, STRICTNESS> +where + STRICTNESS: Strictness, +{ + fn serialize_as<S>(source: &DurationSigned, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + let mut secs = source.sign.apply(source.duration.as_secs() as i64); + + // Properly round the value + if source.duration.subsec_millis() >= 500 { + if source.sign.is_positive() { + secs += 1; + } else { + secs -= 1; + } + } + secs.to_string().serialize(serializer) + } +} + +impl<STRICTNESS> SerializeAs<DurationSigned> for DurationSecondsWithFrac<f64, STRICTNESS> +where + STRICTNESS: Strictness, +{ + fn serialize_as<S>(source: &DurationSigned, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + source + .sign + .apply(utils::duration_as_secs_f64(&source.duration)) + .serialize(serializer) + } +} + +impl<STRICTNESS> SerializeAs<DurationSigned> for DurationSecondsWithFrac<String, STRICTNESS> +where + STRICTNESS: Strictness, +{ + fn serialize_as<S>(source: &DurationSigned, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + source + .sign + .apply(utils::duration_as_secs_f64(&source.duration)) + .to_string() + .serialize(serializer) + } +} + +macro_rules! duration_impls { + ($($inner:ident { $($factor:literal => $outer:ident,)+ })+) => { + $($( + + impl<FORMAT, STRICTNESS> SerializeAs<DurationSigned> for $outer<FORMAT, STRICTNESS> + where + FORMAT: Format, + STRICTNESS: Strictness, + $inner<FORMAT, STRICTNESS>: SerializeAs<DurationSigned> + { + fn serialize_as<S>(source: &DurationSigned, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + $inner::<FORMAT, STRICTNESS>::serialize_as(&(*source * $factor), serializer) + } + } + + impl<'de, FORMAT, STRICTNESS> DeserializeAs<'de, DurationSigned> for $outer<FORMAT, STRICTNESS> + where + FORMAT: Format, + STRICTNESS: Strictness, + $inner<FORMAT, STRICTNESS>: DeserializeAs<'de, DurationSigned>, + { + fn deserialize_as<D>(deserializer: D) -> Result<DurationSigned, D::Error> + where + D: Deserializer<'de>, + { + let dur = $inner::<FORMAT, STRICTNESS>::deserialize_as(deserializer)?; + Ok(dur / $factor) + } + } + + )+)+ }; +} +duration_impls!( + DurationSeconds { + 1000u32 => DurationMilliSeconds, + 1_000_000u32 => DurationMicroSeconds, + 1_000_000_000u32 => DurationNanoSeconds, + } + DurationSecondsWithFrac { + 1000u32 => DurationMilliSecondsWithFrac, + 1_000_000u32 => DurationMicroSecondsWithFrac, + 1_000_000_000u32 => DurationNanoSecondsWithFrac, + } +); + +struct DurationVisitorFlexible; +impl<'de> Visitor<'de> for DurationVisitorFlexible { + type Value = DurationSigned; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("an integer, a float, or a string containing a number") + } + + fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E> + where + E: de::Error, + { + if value >= 0 { + Ok(DurationSigned::new(Sign::Positive, value as u64, 0)) + } else { + Ok(DurationSigned::new(Sign::Negative, (-value) as u64, 0)) + } + } + + fn visit_u64<E>(self, secs: u64) -> Result<Self::Value, E> + where + E: de::Error, + { + Ok(DurationSigned::new(Sign::Positive, secs, 0)) + } + + fn visit_f64<E>(self, secs: f64) -> Result<Self::Value, E> + where + E: de::Error, + { + utils::duration_signed_from_secs_f64(secs).map_err(de::Error::custom) + } + + fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> + where + E: de::Error, + { + match parse_float_into_time_parts(value) { + Ok((sign, seconds, subseconds)) => Ok(DurationSigned::new(sign, seconds, subseconds)), + Err(ParseFloatError::InvalidValue) => { + Err(de::Error::invalid_value(Unexpected::Str(value), &self)) + } + Err(ParseFloatError::Custom(msg)) => Err(de::Error::custom(msg)), + } + } +} + +impl<'de> DeserializeAs<'de, DurationSigned> for DurationSeconds<u64, Strict> { + fn deserialize_as<D>(deserializer: D) -> Result<DurationSigned, D::Error> + where + D: Deserializer<'de>, + { + u64::deserialize(deserializer).map(|secs: u64| DurationSigned::new(Sign::Positive, secs, 0)) + } +} + +impl<'de> DeserializeAs<'de, DurationSigned> for DurationSeconds<i64, Strict> { + fn deserialize_as<D>(deserializer: D) -> Result<DurationSigned, D::Error> + where + D: Deserializer<'de>, + { + i64::deserialize(deserializer).map(|mut secs: i64| { + let mut sign = Sign::Positive; + if secs.is_negative() { + secs = -secs; + sign = Sign::Negative; + } + DurationSigned::new(sign, secs as u64, 0) + }) + } +} + +impl<'de> DeserializeAs<'de, DurationSigned> for DurationSeconds<f64, Strict> { + fn deserialize_as<D>(deserializer: D) -> Result<DurationSigned, D::Error> + where + D: Deserializer<'de>, + { + let val = f64::deserialize(deserializer)?.round(); + utils::duration_signed_from_secs_f64(val).map_err(de::Error::custom) + } +} + +impl<'de> DeserializeAs<'de, DurationSigned> for DurationSeconds<String, Strict> { + fn deserialize_as<D>(deserializer: D) -> Result<DurationSigned, D::Error> + where + D: Deserializer<'de>, + { + struct DurationDeserializationVisitor; + + impl<'de> Visitor<'de> for DurationDeserializationVisitor { + type Value = DurationSigned; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(formatter, "a string containing a number") + } + + fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> + where + E: de::Error, + { + let mut secs: i64 = value.parse().map_err(de::Error::custom)?; + let mut sign = Sign::Positive; + if secs.is_negative() { + secs = -secs; + sign = Sign::Negative; + } + Ok(DurationSigned::new(sign, secs as u64, 0)) + } + } + + deserializer.deserialize_str(DurationDeserializationVisitor) + } +} + +impl<'de, FORMAT> DeserializeAs<'de, DurationSigned> for DurationSeconds<FORMAT, Flexible> +where + FORMAT: Format, +{ + fn deserialize_as<D>(deserializer: D) -> Result<DurationSigned, D::Error> + where + D: Deserializer<'de>, + { + deserializer.deserialize_any(DurationVisitorFlexible) + } +} + +impl<'de> DeserializeAs<'de, DurationSigned> for DurationSecondsWithFrac<f64, Strict> { + fn deserialize_as<D>(deserializer: D) -> Result<DurationSigned, D::Error> + where + D: Deserializer<'de>, + { + let val = f64::deserialize(deserializer)?; + utils::duration_signed_from_secs_f64(val).map_err(de::Error::custom) + } +} + +impl<'de> DeserializeAs<'de, DurationSigned> for DurationSecondsWithFrac<String, Strict> { + fn deserialize_as<D>(deserializer: D) -> Result<DurationSigned, D::Error> + where + D: Deserializer<'de>, + { + let value = String::deserialize(deserializer)?; + match parse_float_into_time_parts(&value) { + Ok((sign, seconds, subseconds)) => Ok(DurationSigned { + sign, + duration: Duration::new(seconds, subseconds), + }), + Err(ParseFloatError::InvalidValue) => Err(de::Error::invalid_value( + Unexpected::Str(&value), + &"a string containing an integer or float", + )), + Err(ParseFloatError::Custom(msg)) => Err(de::Error::custom(msg)), + } + } +} + +impl<'de, FORMAT> DeserializeAs<'de, DurationSigned> for DurationSecondsWithFrac<FORMAT, Flexible> +where + FORMAT: Format, +{ + fn deserialize_as<D>(deserializer: D) -> Result<DurationSigned, D::Error> + where + D: Deserializer<'de>, + { + deserializer.deserialize_any(DurationVisitorFlexible) + } +} + +#[derive(Debug, Eq, PartialEq)] +pub(crate) enum ParseFloatError { + InvalidValue, + Custom(String), +} + +fn parse_float_into_time_parts(mut value: &str) -> Result<(Sign, u64, u32), ParseFloatError> { + let sign = match value.chars().next() { + // Advance by the size of the parsed char + Some('+') => { + value = &value[1..]; + Sign::Positive + } + Some('-') => { + value = &value[1..]; + Sign::Negative + } + _ => Sign::Positive, + }; + + let parts: Vec<_> = value.split('.').collect(); + match *parts.as_slice() { + [seconds] => { + if let Ok(seconds) = seconds.parse() { + Ok((sign, seconds, 0)) + } else { + Err(ParseFloatError::InvalidValue) + } + } + [seconds, subseconds] => { + if let Ok(seconds) = seconds.parse() { + let subseclen = subseconds.chars().count() as u32; + if subseclen > 9 { + return Err(ParseFloatError::Custom(format!( + "Duration and Timestamps with no more than 9 digits precision, but '{}' has more", + value + ))); + } + + if let Ok(mut subseconds) = subseconds.parse() { + // convert subseconds to nanoseconds (10^-9), require 9 places for nanoseconds + subseconds *= 10u32.pow(9 - subseclen); + Ok((sign, seconds, subseconds)) + } else { + Err(ParseFloatError::InvalidValue) + } + } else { + Err(ParseFloatError::InvalidValue) + } + } + + _ => Err(ParseFloatError::InvalidValue), + } +} + +#[test] +fn test_parse_float_into_time_parts() { + // Test normal behavior + assert_eq!( + Ok((Sign::Positive, 123, 456_000_000)), + parse_float_into_time_parts("+123.456") + ); + assert_eq!( + Ok((Sign::Negative, 123, 987_000)), + parse_float_into_time_parts("-123.000987") + ); + assert_eq!( + Ok((Sign::Positive, 18446744073709551615, 123_456_789)), + parse_float_into_time_parts("18446744073709551615.123456789") + ); + + // Test behavior around 0 + assert_eq!( + Ok((Sign::Positive, 0, 456_000_000)), + parse_float_into_time_parts("+0.456") + ); + assert_eq!( + Ok((Sign::Negative, 0, 987_000)), + parse_float_into_time_parts("-0.000987") + ); + assert_eq!( + Ok((Sign::Positive, 0, 123_456_789)), + parse_float_into_time_parts("0.123456789") + ); +} diff --git a/third_party/rust/serde_with/src/with_prefix.rs b/third_party/rust/serde_with/src/with_prefix.rs new file mode 100644 index 0000000000..b4b483c983 --- /dev/null +++ b/third_party/rust/serde_with/src/with_prefix.rs @@ -0,0 +1,612 @@ +use alloc::string::String; +use core::fmt; +use serde::{ + de::{self, DeserializeSeed, Deserializer, IgnoredAny, IntoDeserializer, MapAccess, Visitor}, + forward_to_deserialize_any, + ser::{self, Impossible, Serialize, SerializeMap, SerializeStruct, Serializer}, +}; + +/// Serialize with an added prefix on every field name and deserialize by +/// trimming away the prefix. +/// +/// You can set the visibility of the generated module by prefixing the module name with a module visibility. +/// `with_prefix!(pub(crate) prefix_foo "foo_");` creates a module with `pub(crate)` visibility. +/// The visibility is optional and by default `pub(self)`, i.e., private visibility is assumed. +/// +/// **Note:** Use of this macro is incompatible with applying the [`deny_unknown_fields`] attribute +/// on the container. +/// While deserializing, it will always warn about unknown fields, even though they are processed +/// by the `with_prefix` wrapper. +/// More details can be found in [this issue][issue-with_prefix-deny_unknown_fields]. +/// +/// # Example +/// +/// The [Challonge REST API] likes to use prefixes to group related fields. In +/// simplified form, their JSON may resemble the following: +/// +/// [Challonge REST API]: https://api.challonge.com/v1/documents/matches/show +/// +/// ```json +/// { +/// "player1_name": "name1", +/// "player1_votes": 1, +/// "player2_name": "name2", +/// "player2_votes": 2 +/// } +/// ``` +/// +/// In Rust, we would ideally like to model this data as a pair of `Player` +/// structs, rather than repeating the fields of `Player` for each prefix. +/// +/// ```rust +/// # #[allow(dead_code)] +/// struct Match { +/// player1: Player, +/// player2: Player, +/// } +/// +/// # #[allow(dead_code)] +/// struct Player { +/// name: String, +/// votes: u64, +/// } +/// ``` +/// +/// This `with_prefix!` macro produces an adapter that adds a prefix onto field +/// names during serialization and trims away the prefix during deserialization. +/// An implementation of the Challonge API would use `with_prefix!` like this: +/// +/// ```rust +/// use serde::{Deserialize, Serialize}; +/// use serde_with::with_prefix; +/// +/// #[derive(Serialize, Deserialize)] +/// struct Match { +/// #[serde(flatten, with = "prefix_player1")] +/// player1: Player, +/// #[serde(flatten, with = "prefix_player2")] +/// player2: Player, +/// } +/// +/// #[derive(Serialize, Deserialize)] +/// struct Player { +/// name: String, +/// votes: u64, +/// } +/// +/// with_prefix!(prefix_player1 "player1_"); +/// // You can also set the visibility of the generated prefix module, the default is private. +/// with_prefix!(pub prefix_player2 "player2_"); +/// # +/// # const EXPECTED: &str = r#"{ +/// # "player1_name": "name1", +/// # "player1_votes": 1, +/// # "player2_name": "name2", +/// # "player2_votes": 2 +/// # }"#; +/// +/// fn main() { +/// let m = Match { +/// player1: Player { +/// name: "name1".to_owned(), +/// votes: 1, +/// }, +/// player2: Player { +/// name: "name2".to_owned(), +/// votes: 2, +/// }, +/// }; +/// +/// let j = serde_json::to_string_pretty(&m).unwrap(); +/// println!("{}", j); +/// # +/// # assert_eq!(j, EXPECTED); +/// } +/// ``` +/// +/// [`deny_unknown_fields`]: https://serde.rs/container-attrs.html#deny_unknown_fields +/// [issue-with_prefix-deny_unknown_fields]: https://github.com/jonasbb/serde_with/issues/57 +#[macro_export] +macro_rules! with_prefix { + ($module:ident $prefix:expr) => {$crate::with_prefix!(pub(self) $module $prefix);}; + ($vis:vis $module:ident $prefix:expr) => { + $vis mod $module { + use $crate::serde::{Deserialize, Deserializer, Serialize, Serializer}; + use $crate::with_prefix::WithPrefix; + + #[allow(dead_code)] + pub fn serialize<T, S>(object: &T, serializer: S) -> ::std::result::Result<S::Ok, S::Error> + where + T: Serialize, + S: Serializer, + { + object.serialize(WithPrefix { + delegate: serializer, + prefix: $prefix, + }) + } + + #[allow(dead_code)] + pub fn deserialize<'de, T, D>(deserializer: D) -> ::std::result::Result<T, D::Error> + where + T: Deserialize<'de>, + D: Deserializer<'de>, + { + T::deserialize(WithPrefix { + delegate: deserializer, + prefix: $prefix, + }) + } + } + }; +} + +#[allow(missing_debug_implementations)] +pub struct WithPrefix<'a, T> { + pub delegate: T, + pub prefix: &'a str, +} + +impl<'a, T> Serialize for WithPrefix<'a, T> +where + T: Serialize, +{ + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + self.delegate.serialize(WithPrefix { + delegate: serializer, + prefix: self.prefix, + }) + } +} + +impl<'a, S> Serializer for WithPrefix<'a, S> +where + S: Serializer, +{ + type Ok = S::Ok; + type Error = S::Error; + type SerializeSeq = Impossible<Self::Ok, Self::Error>; + type SerializeTuple = Impossible<Self::Ok, Self::Error>; + type SerializeTupleStruct = Impossible<Self::Ok, Self::Error>; + type SerializeTupleVariant = Impossible<Self::Ok, Self::Error>; + type SerializeMap = WithPrefix<'a, S::SerializeMap>; + type SerializeStruct = WithPrefix<'a, S::SerializeMap>; + type SerializeStructVariant = Impossible<Self::Ok, Self::Error>; + + fn serialize_bool(self, _v: bool) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for with_prefix")) + } + + fn serialize_i8(self, _v: i8) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for with_prefix")) + } + + fn serialize_i16(self, _v: i16) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for with_prefix")) + } + + fn serialize_i32(self, _v: i32) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for with_prefix")) + } + + fn serialize_i64(self, _v: i64) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for with_prefix")) + } + + fn serialize_u8(self, _v: u8) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for with_prefix")) + } + + fn serialize_u16(self, _v: u16) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for with_prefix")) + } + + fn serialize_u32(self, _v: u32) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for with_prefix")) + } + + fn serialize_u64(self, _v: u64) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for with_prefix")) + } + + fn serialize_f32(self, _v: f32) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for with_prefix")) + } + + fn serialize_f64(self, _v: f64) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for with_prefix")) + } + + fn serialize_char(self, _v: char) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for with_prefix")) + } + + fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> { + self.delegate + .collect_str(&format_args!("{}{}", self.prefix, v)) + } + + fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for with_prefix")) + } + + fn serialize_none(self) -> Result<Self::Ok, Self::Error> { + self.delegate.serialize_none() + } + + fn serialize_some<T>(self, value: &T) -> Result<Self::Ok, Self::Error> + where + T: ?Sized + Serialize, + { + self.delegate.serialize_some(&WithPrefix { + delegate: value, + prefix: self.prefix, + }) + } + + fn serialize_unit(self) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for with_prefix")) + } + + fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> { + Err(ser::Error::custom("wrong type for with_prefix")) + } + + fn serialize_unit_variant( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + ) -> Result<Self::Ok, Self::Error> { + self.serialize_str(variant) + } + + fn serialize_newtype_struct<T>( + self, + _name: &'static str, + _value: &T, + ) -> Result<Self::Ok, Self::Error> + where + T: ?Sized + Serialize, + { + Err(ser::Error::custom("wrong type for with_prefix")) + } + + fn serialize_newtype_variant<T>( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + _value: &T, + ) -> Result<Self::Ok, Self::Error> + where + T: ?Sized + Serialize, + { + Err(ser::Error::custom("wrong type for with_prefix")) + } + + fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { + Err(ser::Error::custom("wrong type for with_prefix")) + } + + fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> { + Err(ser::Error::custom("wrong type for with_prefix")) + } + + fn serialize_tuple_struct( + self, + _name: &'static str, + _len: usize, + ) -> Result<Self::SerializeTupleStruct, Self::Error> { + Err(ser::Error::custom("wrong type for with_prefix")) + } + + fn serialize_tuple_variant( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + _len: usize, + ) -> Result<Self::SerializeTupleVariant, Self::Error> { + Err(ser::Error::custom("wrong type for with_prefix")) + } + + fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { + Ok(WithPrefix { + delegate: self.delegate.serialize_map(len)?, + prefix: self.prefix, + }) + } + + fn serialize_struct( + self, + _name: &'static str, + len: usize, + ) -> Result<Self::SerializeStruct, Self::Error> { + self.serialize_map(Some(len)) + } + + fn serialize_struct_variant( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + _len: usize, + ) -> Result<Self::SerializeStructVariant, Self::Error> { + Err(ser::Error::custom("wrong type for with_prefix")) + } +} + +impl<'a, S> SerializeMap for WithPrefix<'a, S> +where + S: SerializeMap, +{ + type Ok = S::Ok; + type Error = S::Error; + + fn serialize_key<T>(&mut self, key: &T) -> Result<(), Self::Error> + where + T: ?Sized + Serialize, + { + self.delegate.serialize_key(&WithPrefix { + delegate: key, + prefix: self.prefix, + }) + } + + fn serialize_value<T>(&mut self, value: &T) -> Result<(), Self::Error> + where + T: ?Sized + Serialize, + { + self.delegate.serialize_value(value) + } + + fn serialize_entry<K, V>(&mut self, key: &K, value: &V) -> Result<(), Self::Error> + where + K: ?Sized + Serialize, + V: ?Sized + Serialize, + { + self.delegate.serialize_entry( + &WithPrefix { + delegate: key, + prefix: self.prefix, + }, + value, + ) + } + + fn end(self) -> Result<Self::Ok, Self::Error> { + self.delegate.end() + } +} + +impl<'a, S> SerializeStruct for WithPrefix<'a, S> +where + S: SerializeMap, +{ + type Ok = S::Ok; + type Error = S::Error; + + fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> + where + T: ?Sized + Serialize, + { + let mut prefixed_key = String::with_capacity(self.prefix.len() + key.len()); + prefixed_key.push_str(self.prefix); + prefixed_key.push_str(key); + self.delegate.serialize_entry(&prefixed_key, value) + } + + fn end(self) -> Result<Self::Ok, Self::Error> { + self.delegate.end() + } +} + +impl<'de, 'a, T> DeserializeSeed<'de> for WithPrefix<'a, T> +where + T: DeserializeSeed<'de>, +{ + type Value = T::Value; + + fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> + where + D: Deserializer<'de>, + { + self.delegate.deserialize(WithPrefix { + delegate: deserializer, + prefix: self.prefix, + }) + } +} + +impl<'de, 'a, D> Deserializer<'de> for WithPrefix<'a, D> +where + D: Deserializer<'de>, +{ + type Error = D::Error; + + fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> + where + V: Visitor<'de>, + { + self.delegate.deserialize_map(WithPrefix { + delegate: visitor, + prefix: self.prefix, + }) + } + + fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error> + where + V: Visitor<'de>, + { + self.delegate.deserialize_any(WithPrefixOption { + first_key: None, + delegate: visitor, + prefix: self.prefix, + }) + } + + fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error> + where + V: Visitor<'de>, + { + self.delegate.deserialize_identifier(WithPrefix { + delegate: visitor, + prefix: self.prefix, + }) + } + + forward_to_deserialize_any! { + bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string + bytes byte_buf unit unit_struct newtype_struct seq tuple tuple_struct + map struct enum ignored_any + } +} + +impl<'de, 'a, V> Visitor<'de> for WithPrefix<'a, V> +where + V: Visitor<'de>, +{ + type Value = V::Value; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + self.delegate.expecting(formatter) + } + + fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error> + where + A: MapAccess<'de>, + { + self.delegate.visit_map(WithPrefix { + delegate: map, + prefix: self.prefix, + }) + } +} + +impl<'de, 'a, A> MapAccess<'de> for WithPrefix<'a, A> +where + A: MapAccess<'de>, +{ + type Error = A::Error; + + fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error> + where + K: DeserializeSeed<'de>, + { + while let Some(s) = self.delegate.next_key::<String>()? { + if let Some(without_prefix) = s.strip_prefix(self.prefix) { + return seed + .deserialize(without_prefix.into_deserializer()) + .map(Some); + } + self.delegate.next_value::<IgnoredAny>()?; + } + Ok(None) + } + + fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error> + where + V: DeserializeSeed<'de>, + { + self.delegate.next_value_seed(seed) + } +} + +#[allow(missing_debug_implementations)] +pub struct WithPrefixOption<'a, T> { + first_key: Option<String>, + delegate: T, + prefix: &'a str, +} + +impl<'de, 'a, V> Visitor<'de> for WithPrefixOption<'a, V> +where + V: Visitor<'de>, +{ + type Value = V::Value; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + self.delegate.expecting(formatter) + } + + fn visit_unit<E>(self) -> Result<Self::Value, E> + where + E: de::Error, + { + self.delegate.visit_none() + } + + fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> + where + A: MapAccess<'de>, + { + while let Some(s) = map.next_key::<String>()? { + if s.starts_with(self.prefix) { + return self.delegate.visit_some(WithPrefixOption { + first_key: Some(s), + delegate: map, + prefix: self.prefix, + }); + } + map.next_value::<IgnoredAny>()?; + } + self.delegate.visit_none() + } +} + +impl<'de, 'a, A> Deserializer<'de> for WithPrefixOption<'a, A> +where + A: MapAccess<'de>, +{ + type Error = A::Error; + + fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> + where + V: Visitor<'de>, + { + visitor.visit_map(self) + } + + forward_to_deserialize_any! { + bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string + bytes byte_buf option unit unit_struct newtype_struct seq tuple + tuple_struct map struct enum identifier ignored_any + } +} + +impl<'de, 'a, A> MapAccess<'de> for WithPrefixOption<'a, A> +where + A: MapAccess<'de>, +{ + type Error = A::Error; + + fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error> + where + K: DeserializeSeed<'de>, + { + if let Some(s) = self.first_key.take() { + let without_prefix = s[self.prefix.len()..].into_deserializer(); + return seed.deserialize(without_prefix).map(Some); + } + while let Some(s) = self.delegate.next_key::<String>()? { + if let Some(without_prefix) = s.strip_prefix(self.prefix) { + return seed + .deserialize(without_prefix.into_deserializer()) + .map(Some); + } + self.delegate.next_value::<IgnoredAny>()?; + } + Ok(None) + } + + fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error> + where + V: DeserializeSeed<'de>, + { + self.delegate.next_value_seed(seed) + } +} |