Skip to main content

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/// Feature flag for dev network
92#[macro_export]
93macro_rules! if_dev {
94    ($($item:item)*) => {$(
95        #[cfg(feature = "dev")]
96        $item
97    )*}
98}
99
100#[macro_export]
101macro_rules! if_local {
102    ($($item:item)*) => {$(
103        #[cfg(not(feature = "dev"))]
104            $item
105    )*}
106}
107
108#[macro_export]
109macro_rules! if_test {
110    ($($item:item)*) => {$(
111        #[cfg(any(test, feature = "test-utils"))]
112        $item
113    )*}
114}
115
116// cfg only test but not any extra test-utils features
117#[macro_export]
118macro_rules! if_only_test {
119    ($($item:item)*) => {$(
120        #[cfg(test)]
121        $item
122    )*}
123}
124
125#[macro_export]
126macro_rules! if_not_test {
127    ($($item:item)*) => {$(
128        #[cfg(not(any(test, feature = "test-utils")))]
129        $item
130    )*}
131}