xmtp_common/
macros.rs

1/// Turn the `Result<T, E>` into an `Option<T>`, logging the error with `tracing::error` and
2/// returning `None` if the value matches on Result::Err().
3/// Optionally pass a message as the second argument.
4#[macro_export]
5macro_rules! optify {
6    ( $e: expr ) => {
7        match $e {
8            Ok(v) => Some(v),
9            Err(e) => {
10                tracing::error!("{}", e);
11                None
12            }
13        }
14    };
15    ( $e: expr, $msg: tt ) => {
16        match $e {
17            Ok(v) => Some(v),
18            Err(e) => {
19                tracing::error!("{}: {:?}", $msg, e);
20                None
21            }
22        }
23    };
24}
25
26#[macro_export]
27macro_rules! wasm_or_native {
28    (wasm => $wasm_block:block $(,)? native => $native_block:block $(,)?) => {
29        #[cfg(all(target_family = "wasm", target_os = "unknown"))]
30        $wasm_block
31        #[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
32        $native_block
33    };
34    (native => $native_block:block $(,)? wasm => $wasm_block:block $(,)?) => {
35        $crate::wasm_or_native!(wasm => $wasm_block, native => $native_block)
36    };
37}
38
39/// Convenience macro to easily evaluate an expression for wasm or native
40/// # Example
41/// ```ignore
42/// let path = wasm_or_native_expr! {
43///     wasm => "wasm".to_string(),
44///     native => "native".into(),
45/// };
46/// ```
47#[macro_export]
48macro_rules! wasm_or_native_expr {
49    (wasm => $wasm_expr:expr, native => $native_expr:expr $(,)?) => {
50        if cfg!(all(target_family = "wasm", target_os = "unknown")) {
51            $wasm_expr
52        } else {
53            $native_expr
54        }
55    };
56    (native => $native_expr:expr, wasm => $wasm_expr:expr $(,)?) => {
57        $crate::wasm_or_native_expr!(wasm => $wasm_expr, native => $native_expr)
58    };
59}
60
61/// Convenience macro to easily export items for wasm
62#[macro_export]
63macro_rules! if_wasm {
64    // Starting with @ is a hack to allow for the macro to be used with token types which match pretty much anything
65    (@ $($tt:tt)*) => {
66        #[cfg(all(target_family = "wasm", target_os = "unknown"))] {
67            $($tt)*
68        }
69    };
70    ($($item:item)*) => {$(
71        #[cfg(all(target_family = "wasm", target_os = "unknown"))]
72        $item
73    )*};
74}
75
76/// Convenience macro to easily export items for native
77#[macro_export]
78macro_rules! if_native {
79    // Starting with @ is a hack to allow for the macro to be used with token types which match pretty much anything
80    (@ $($tt:tt)*) => {
81        #[cfg(not(all(target_family = "wasm", target_os = "unknown")))] {
82            $($tt)*
83        }
84    };
85    ($($item:item)*) => {$(
86        #[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
87        $item
88    )*};
89}
90
91/// Convenience macro to easily export items for d14n
92#[macro_export]
93macro_rules! if_d14n {
94    ($($item:item)*) => {$(
95        #[cfg(feature = "d14n")]
96        $item
97    )*}
98}
99
100/// Convenience macro to easily export items for d14n
101#[macro_export]
102macro_rules! if_v3 {
103    ($($item:item)*) => {$(
104        #[cfg(not(feature = "d14n"))]
105        $item
106    )*}
107}
108
109/// Feature flag for dev network
110#[macro_export]
111macro_rules! if_dev {
112    ($($item:item)*) => {$(
113        #[cfg(feature = "dev")]
114        $item
115    )*}
116}
117
118#[macro_export]
119macro_rules! if_local {
120    ($($item:item)*) => {$(
121        #[cfg(not(feature = "dev"))]
122            $item
123    )*}
124}
125
126#[macro_export]
127macro_rules! if_test {
128    ($($item:item)*) => {$(
129        #[cfg(any(test, feature = "test-utils"))]
130        $item
131    )*}
132}
133
134// cfg only test but not any extra test-utils features
135#[macro_export]
136macro_rules! if_only_test {
137    ($($item:item)*) => {$(
138        #[cfg(test)]
139        $item
140    )*}
141}
142
143#[macro_export]
144macro_rules! if_not_test {
145    ($($item:item)*) => {$(
146        #[cfg(not(any(test, feature = "test-utils")))]
147        $item
148    )*}
149}