smart_config/de/
macros.rs

1#[doc(hidden)]
2#[macro_export]
3macro_rules! _basic_types {
4    (@expand bool $($tail:tt)+) => {
5        $crate::metadata::BasicTypes::BOOL.or($crate::_basic_types!($($tail)+))
6    };
7    (@expand int $($tail:tt)+) => {
8        $crate::metadata::BasicTypes::INTEGER.or($crate::_basic_types!(@expand $($tail)+))
9    };
10    (@expand float $($tail:tt)+) => {
11        $crate::metadata::BasicTypes::FLOAT.or($crate::_basic_types!(@expand $($tail)+))
12    };
13    (@expand str $($tail:tt)+) => {
14        $crate::metadata::BasicTypes::STRING.or($crate::_basic_types!(@expand $($tail)+))
15    };
16    (@expand array $($tail:tt)+) => {
17        $crate::metadata::BasicTypes::ARRAY.or($crate::_basic_types!(@expand $($tail)+))
18    };
19    (@expand object $($tail:tt)+) => {
20        $crate::metadata::BasicTypes::OBJECT.or($crate::_basic_types!(@expand $($tail)+))
21    };
22
23    (@expand bool) => {
24        $crate::metadata::BasicTypes::BOOL
25    };
26    (@expand int) => {
27        $crate::metadata::BasicTypes::INTEGER
28    };
29    (@expand float) => {
30        $crate::metadata::BasicTypes::FLOAT
31    };
32    (@expand str) => {
33        $crate::metadata::BasicTypes::STRING
34    };
35    (@expand array) => {
36        $crate::metadata::BasicTypes::ARRAY
37    };
38    (@expand object) => {
39        $crate::metadata::BasicTypes::OBJECT
40    };
41
42    ($($expecting:tt)+) => {
43        $crate::metadata::BasicTypes::raw($crate::_basic_types!(@expand $($expecting)+))
44    };
45}
46
47/// Constructor of [`Serde`](struct@crate::de::Serde) types / instances.
48///
49/// The macro accepts a comma-separated list of expected basic types from the following set: `bool`, `int`,
50/// `float`, `str`, `array`, `object`. As a shortcut, `Serde![*]` signals to accept any input.
51///
52/// # Examples
53///
54/// ```
55/// # use serde::{Deserialize, Deserializer, Serialize, Serializer};
56/// # use smart_config::{de::Serde, DescribeConfig, DeserializeConfig};
57/// #[derive(Debug)]
58/// struct ComplexType {
59///     // ...
60/// }
61///
62/// impl Serialize for ComplexType {
63///     // Complex serialization logic...
64/// # fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
65/// #     unreachable!()
66/// # }
67/// }
68///
69/// impl<'de> Deserialize<'de> for ComplexType {
70///     // Complex deserialization logic...
71/// # fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
72/// #     unreachable!()
73/// # }
74/// }
75///
76/// #[derive(DescribeConfig, DeserializeConfig)]
77/// struct TestConfig {
78///     /// Will try to deserialize any integer, string or object delegating
79///     /// to the `Deserialize` impl. Will error on other inputs (e.g., arrays).
80///     #[config(with = Serde![int, str, object])]
81///     complex_param: ComplexType,
82///     #[config(with = Serde![*])]
83///     anything: serde_json::Value,
84/// }
85/// ```
86#[macro_export]
87#[allow(non_snake_case)]
88macro_rules! Serde {
89    (*) => {
90        $crate::de::Serde::<{ $crate::metadata::BasicTypes::ANY.raw() }>
91    };
92    ($($expecting:tt),+ $(,)?) => {
93        $crate::de::Serde::<{ $crate::_basic_types!($($expecting)+) }>
94    };
95}
96
97pub use Serde;