Skip to content

Commit

Permalink
Remove ValueDeserialize macros
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Sep 22, 2024
1 parent 65b2b8c commit d0dc015
Show file tree
Hide file tree
Showing 7 changed files with 4 additions and 299 deletions.
289 changes: 0 additions & 289 deletions merde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,226 +219,6 @@ macro_rules! impl_deserialize {
($($tt:tt)*) => {};
}

#[doc(hidden)]
#[cfg(feature = "deserialize")]
#[macro_export]
macro_rules! impl_value_deserialize {
// owned tuple struct (transparent)
(struct $struct_name:ident transparent) => {
#[automatically_derived]
impl<'s> $crate::ValueDeserialize<'s> for $struct_name {
#[inline(always)]
fn from_value_ref<'val>(
value: Option<&'val $crate::Value<'s>>,
) -> Result<Self, $crate::MerdeError<'s>> {
Ok($struct_name($crate::ValueDeserialize::from_value_ref(value)?))
}

#[inline(always)]
fn from_value(
value: Option<$crate::Value<'s>>,
) -> Result<Self, $crate::MerdeError<'s>> {
Ok($struct_name($crate::ValueDeserialize::from_value(value)?))
}
}
};

// lifetimed tuple struct (transparent)
(struct $struct_name:ident <$lifetime:lifetime> transparent) => {
#[automatically_derived]
impl<$lifetime> $crate::ValueDeserialize<$lifetime> for $struct_name<$lifetime> {
#[inline(always)]
fn from_value_ref<'val>(
value: Option<&'val $crate::Value<$lifetime>>,
) -> Result<Self, $crate::MerdeError<$lifetime>> {
Ok($struct_name($crate::ValueDeserialize::from_value_ref(value)?))
}

#[inline(always)]
fn from_value(
value: Option<$crate::Value<$lifetime>>,
) -> Result<Self, $crate::MerdeError<$lifetime>> {
Ok($struct_name($crate::ValueDeserialize::from_value(value)?))
}
}
};

// owned struct
(struct $struct_name:ident { $($field:ident),* }) => {
#[automatically_derived]
impl<'s> $crate::ValueDeserialize<'s> for $struct_name
{
fn from_value_ref(
value: Option<&$crate::Value<'s>>,
) -> Result<Self, $crate::MerdeError<'s>> {
#[allow(unused_imports)]
use $crate::MerdeError;

let obj = value.ok_or(MerdeError::MissingValue)?.as_map()?;
Ok($struct_name {
$($field: obj.must_get(stringify!($field))?,)+
})
}

fn from_value(
value: Option<$crate::Value<'s>>,
) -> Result<Self, $crate::MerdeError> {
#[allow(unused_imports)]
use $crate::MerdeError;

let mut obj = value.ok_or(MerdeError::MissingValue)?.into_map()?;
Ok($struct_name {
$($field: obj.must_remove(stringify!($field))?,)+
})
}
}
};

// lifetimed struct
(struct $struct_name:ident <$lifetime:lifetime> { $($field:ident),* }) => {
#[automatically_derived]
impl<$lifetime> $crate::ValueDeserialize<$lifetime> for $struct_name<$lifetime>
{
fn from_value_ref<'val>(
value: Option<&'val $crate::Value<$lifetime>>,
) -> Result<Self, $crate::MerdeError<'s>> {
#[allow(unused_imports)]
use $crate::MerdeError;

let obj = value.ok_or(MerdeError::MissingValue)?.as_map()?;
Ok($struct_name {
$($field: obj.must_get(stringify!($field))?,)+
})
}

fn from_value(
value: Option<$crate::Value<$lifetime>>,
) -> Result<Self, $crate::MerdeError<'s>> {
#[allow(unused_imports)]
use $crate::MerdeError;

let mut obj = value.ok_or(MerdeError::MissingValue)?.into_map()?;
Ok($struct_name {
$($field: obj.must_remove(stringify!($field))?,)+
})
}
}
};

// owned enum (externally tagged)
(enum $enum_name:ident externally_tagged {
$($variant_str:literal => $variant:ident),* $(,)?
}) => {
#[automatically_derived]
impl<'s> $crate::ValueDeserialize<'s> for $enum_name {
fn from_value_ref(
value: Option<&$crate::Value<'s>>,
) -> Result<Self, $crate::MerdeError<'s>> {
#[allow(unused_imports)]
use $crate::MerdeError;

let map = value.ok_or(MerdeError::MissingValue)?.as_map()?;
let (key, val) = map.iter().next().ok_or(MerdeError::MissingValue)?;
match key.as_ref() {
$($variant_str => Ok($enum_name::$variant($crate::ValueDeserialize::from_value_ref(Some(val))?)),)*
_ => Err(MerdeError::UnknownProperty(key.clone())),
}
}

fn from_value(
value: Option<$crate::Value<'s>>,
) -> Result<Self, $crate::MerdeError<'s>> {
#[allow(unused_imports)]
use $crate::MerdeError;

let map = value.ok_or(MerdeError::MissingValue)?.into_map()?;
let (key, val) = map.into_iter().next().ok_or(MerdeError::MissingValue)?;
match key.as_ref() {
$($variant_str => Ok($enum_name::$variant($crate::ValueDeserialize::from_value(Some(val))?)),)*
_ => Err(MerdeError::UnknownProperty(key)),
}
}
}
};

// lifetimed enum (externally tagged)
(enum $enum_name:ident <$lifetime:lifetime> externally_tagged {
$($variant_str:literal => $variant:ident),* $(,)?
}) => {
#[automatically_derived]
impl<$lifetime> $crate::ValueDeserialize<$lifetime> for $enum_name<$lifetime> {
fn from_value_ref<'val>(
value: Option<&'val $crate::Value<$lifetime>>,
) -> Result<Self, $crate::MerdeError<$lifetime>> {
#[allow(unused_imports)]
use $crate::MerdeError;

let map = value.ok_or(MerdeError::MissingValue)?.as_map()?;
let (key, val) = map.iter().next().ok_or(MerdeError::MissingValue)?;
match key.as_ref() {
$($variant_str => Ok($enum_name::$variant($crate::ValueDeserialize::from_value_ref(Some(val))?)),)*
_ => Err(MerdeError::UnknownProperty(key.clone())),
}
}

fn from_value(
value: Option<$crate::Value<$lifetime>>,
) -> Result<Self, $crate::MerdeError<$lifetime>> {
#[allow(unused_imports)]
use $crate::MerdeError;

let map = value.ok_or(MerdeError::MissingValue)?.into_map()?;
let (key, val) = map.into_iter().next().ok_or(MerdeError::MissingValue)?;
match key.as_ref() {
$($variant_str => Ok($enum_name::$variant($crate::ValueDeserialize::from_value(Some(val))?)),)*
_ => Err(MerdeError::UnknownProperty(key)),
}
}
}
};

// owned enum (externally tagged, string-like)
(enum $enum_name:ident string_like {
$($variant_str:literal => $variant:ident),* $(,)?
}) => {
#[automatically_derived]
impl<'s> $crate::ValueDeserialize<'s> for $enum_name {
fn from_value_ref(
value: Option<&$crate::Value<'s>>,
) -> Result<Self, $crate::MerdeError<'s>> {
#[allow(unused_imports)]
use $crate::MerdeError;

let s = value.ok_or(MerdeError::MissingValue)?.as_str()?;
match s.as_ref() {
$($variant_str => Ok($enum_name::$variant),)*
_ => Err(MerdeError::UnknownProperty(s.clone())),
}
}

fn from_value(
value: Option<$crate::Value<'s>>,
) -> Result<Self, $crate::MerdeError<'s>> {
#[allow(unused_imports)]
use $crate::MerdeError;

let s = value.ok_or(MerdeError::MissingValue)?.into_str()?;
match s.as_ref() {
$($variant_str => Ok($enum_name::$variant),)*
_ => Err(MerdeError::UnknownProperty(s)),
}
}
}
};
}

#[doc(hidden)]
#[cfg(not(feature = "deserialize"))]
#[macro_export]
macro_rules! impl_value_deserialize {
($($tt:tt)*) => {};
}

#[doc(hidden)]
#[macro_export]
#[cfg(feature = "core")]
Expand Down Expand Up @@ -811,75 +591,6 @@ macro_rules! impl_trait {

//------------------------------------------------------------------------------------

// owned struct
(@impl ValueDeserialize, struct $struct_name:ident { $($field:ident),* }) => {
$crate::impl_value_deserialize!(struct $struct_name { $($field),* });
$crate::impl_into_static!(struct $struct_name { $($field),* });
$crate::impl_with_lifetime!(struct $struct_name { $($field),* });
};
// lifetimed struct
(@impl ValueDeserialize, struct $struct_name:ident <$lifetime:lifetime> { $($field:ident),* }) => {
$crate::impl_value_deserialize!(struct $struct_name <$lifetime> { $($field),* });
$crate::impl_into_static!(struct $struct_name <$lifetime> { $($field),* });
$crate::impl_with_lifetime!(struct $struct_name <$lifetime> { $($field),* });
};
// owned enum (externally tagged)
(@impl ValueDeserialize, enum $enum_name:ident externally_tagged {
$($variant_str:literal => $variant:ident),* $(,)?
}) => {
$crate::impl_value_deserialize!(enum $enum_name externally_tagged {
$($variant_str => $variant),*
});
$crate::impl_into_static!(enum $enum_name externally_tagged {
$($variant_str => $variant),*
});
$crate::impl_with_lifetime!(enum $enum_name externally_tagged {
$($variant_str => $variant),*
});
};
// lifetimed enum (externally tagged)
(@impl ValueDeserialize, enum $enum_name:ident <$lifetime:lifetime> externally_tagged {
$($variant_str:literal => $variant:ident),* $(,)?
}) => {
$crate::impl_value_deserialize!(enum $enum_name <$lifetime> externally_tagged {
$($variant_str => $variant),*
});
$crate::impl_into_static!(enum $enum_name <$lifetime> externally_tagged {
$($variant_str => $variant),*
});
$crate::impl_with_lifetime!(enum $enum_name <$lifetime> externally_tagged {
$($variant_str => $variant),*
});
};
// owned enum (string-like)
(@impl ValueDeserialize, enum $enum_name:ident string_like {
$($variant_str:literal => $variant:ident),* $(,)?
}) => {
$crate::impl_value_deserialize!(enum $enum_name string_like {
$($variant_str => $variant),*
});
$crate::impl_into_static!(enum $enum_name string_like {
$($variant_str => $variant),*
});
$crate::impl_with_lifetime!(enum $enum_name string_like {
$($variant_str => $variant),*
});
};
// owned tuple struct (transparent)
(@impl ValueDeserialize, struct $struct_name:ident transparent) => {
$crate::impl_value_deserialize!(struct $struct_name transparent);
$crate::impl_into_static!(struct $struct_name transparent);
$crate::impl_with_lifetime!(struct $struct_name transparent);
};
// lifetimed tuple struct (transparent)
(@impl ValueDeserialize, struct $struct_name:ident <$lifetime:lifetime> transparent) => {
$crate::impl_value_deserialize!(struct $struct_name <$lifetime> transparent);
$crate::impl_into_static!(struct $struct_name <$lifetime> transparent);
$crate::impl_with_lifetime!(struct $struct_name <$lifetime> transparent);
};

//------------------------------------------------------------------------------------

// owned struct
(@impl Deserialize, struct $struct_name:ident { $($field:ident),* }) => {
$crate::impl_deserialize!(struct $struct_name { $($field),* });
Expand Down
2 changes: 1 addition & 1 deletion merde_core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ build-time-friendly alternative to `serde`.
This "core" crate provides core types like `Value`, `Array`, `Map`,
and `CowStr<'s>` (a copy-on-write string type that also leverages
[compact_str](https://crates.io/crates/compact_str)'s small string
optimization), and traits like `ValueDeserialize` and `IntoStatic`.
optimization), and traits like `Deserialize` and `IntoStatic`.

Crates that provide support for formats (like [merde_json](https://crates.io/crates/merde_json)),
and crates that provide wrappers around other crates' types, to allow serializing/deserializing
Expand Down
3 changes: 0 additions & 3 deletions merde_core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ pub enum ValueType {
#[non_exhaustive]
pub enum MerdeError<'s> {
/// We expected a certain type but got a different one.
///
/// Note that the default implementations of [crate::ValueDeserialize] have tolerances:
/// if we expect a `u32` but get a floating-point number, we'll round it.
MismatchedType {
/// The expected type.
expected: ValueType,
Expand Down
2 changes: 1 addition & 1 deletion merde_core/src/with_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
use crate::CowStr;

/// Allow instantiating a type with a lifetime parameter, which in
/// turn lets us require `ValueDeserialize<'s>` for `CowStr<'s>` for
/// turn lets us require `Deserialize<'s>` for `CowStr<'s>` for
/// example, even when `CowStr<'s>` is erased behind a `T`.
///
/// See <https://github.com/bearcove/merde/pull/60> for details
Expand Down
3 changes: 0 additions & 3 deletions merde_json/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ pub enum JsonFieldType {
#[non_exhaustive]
pub enum MerdeJsonError {
/// We expected a certain type but got a different one.
///
/// Note that the default implementations of [crate::ValueDeserialize] have tolerances:
/// if we expect a `u32` but get a floating-point number, we'll round it.
MismatchedType {
/// The expected type.
expected: JsonFieldType,
Expand Down
2 changes: 1 addition & 1 deletion merde_time/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ _Logo by [MisiasArt](https://misiasart.com)_

Provides an `Rfc3339` wrapper type that implements:

* `ValueDeserialize` from the [merde_core](https://crates.io/crates/merde_core) crate
* `Deserialize` from the [merde_core](https://crates.io/crates/merde_core) crate
* `JsonSerialize` from the [merde_json](https://crates.io/crates/merde_json) crate
2 changes: 1 addition & 1 deletion merde_time/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Provides [Rfc3339], a wrapper around [time::OffsetDateTime] that implements
//! [merde_json::JsonSerialize] and [merde_core::ValueDeserialize] when the right
//! [merde_json::JsonSerialize] and [merde_core::Deserialize] when the right
//! cargo features are enabled.
use std::{
Expand Down

0 comments on commit d0dc015

Please sign in to comment.