xo_api_client/
macros.rs

1#[macro_export]
2macro_rules! procedure_args {
3    ($($key:expr => $value:expr,)+) => (procedure_args!($($key => $value),+));
4
5    ($($key:expr => $value:expr),*) => {
6        {
7            #[allow(unused_mut)]
8            let mut map = ::std::collections::BTreeMap::new();
9            $(
10                let _ = map.insert($key, serde_json::Value::from($value));
11            )*
12            map
13        }
14    };
15}
16
17#[macro_export]
18macro_rules! procedure_object {
19    ($($key:expr => $value:expr,)+) => (procedure_args!($($key => $value),+));
20
21    ($($key:literal => $value:expr),*) => (procedure_object!($(String::from($key) => $value),+));
22
23    ($($key:expr => $value:expr),*) => {
24        {
25            #[allow(unused_mut)]
26            let mut map = ::serde_json::Map::<String, JsonValue>::new();
27            $(
28                let _ = map.insert($key, $value.into());
29            )*
30            map
31        }
32    };
33}
34
35#[macro_export]
36macro_rules! struct_to_map {
37    (let $var:ident = $s:expr) => {
38        // TODO: Find a better solution to this. Currently we go from
39        // struct -> JsonValue ->
40        // serde::Map -> BTreeMap<&str, JsonValue>
41        // which is less than optimal...
42        let __params = ::serde_json::to_value($s).unwrap();
43        let $var = match &__params {
44            ::serde_json::Value::Object(params) => params
45                .iter()
46                .map(|(k, v)| (k.as_str(), v.clone()))
47                .collect::<::std::collections::BTreeMap<&str, ::serde_json::Value>>(),
48            _ => unreachable!(),
49        };
50    };
51}
52
53#[macro_export]
54macro_rules! impl_to_json_value {
55    ($t:ty) => {
56        impl From<$t> for ::serde_json::Value {
57            fn from(val: $t) -> Self {
58                Self::String(val.0)
59            }
60        }
61    };
62}
63
64#[macro_export]
65macro_rules! impl_from_str {
66    ($t:path) => {
67        impl From<&str> for $t {
68            fn from(s: &str) -> Self {
69                $t(s.to_string())
70            }
71        }
72        impl From<String> for $t {
73            fn from(s: String) -> Self {
74                $t(s)
75            }
76        }
77        impl_to_json_value!($t);
78    };
79}
80
81#[macro_export]
82macro_rules! impl_xo_object {
83    ($t:ty => $object_type:expr, $id:ty) => {
84        impl crate::types::XoObject for $t {
85            const OBJECT_TYPE: &'static str = $object_type;
86            type IdType = $id;
87        }
88    };
89}
90
91#[macro_export]
92macro_rules! declare_id_type {
93    (
94        $(#[$meta:meta])*
95        $v:vis struct $t:ident;
96    ) => {
97        $(#[$meta])*
98        #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize, Hash)]
99        #[serde(transparent)]
100        $v struct $t(pub(crate) String);
101
102        impl crate::types::XoObjectId for $t {}
103
104        crate::impl_to_json_value!($t);
105    };
106}