fred/
macros.rs

1#![allow(unused_macros)]
2
3macro_rules! to(
4  ($val:ident) => {
5    crate::utils::try_into($val)
6  }
7);
8
9macro_rules! _trace(
10  ($inner:tt, $($arg:tt)*) => { {
11    if log::log_enabled!(log::Level::Trace) {
12      log::trace!("{}: {}", $inner.id, format!($($arg)*))
13    }
14   } }
15);
16
17macro_rules! _debug(
18  ($inner:tt, $($arg:tt)*) => { {
19    if log::log_enabled!(log::Level::Debug) {
20      log::debug!("{}: {}", $inner.id, format!($($arg)*))
21    }
22   } }
23);
24
25macro_rules! _error(
26  ($inner:tt, $($arg:tt)*) => { {
27    if log::log_enabled!(log::Level::Error) {
28      log::error!("{}: {}", $inner.id, format!($($arg)*))
29    }
30   } }
31);
32
33macro_rules! _warn(
34  ($inner:tt, $($arg:tt)*) => { {
35    if log::log_enabled!(log::Level::Warn) {
36      log::warn!("{}: {}", $inner.id, format!($($arg)*))
37    }
38   } }
39);
40
41macro_rules! _info(
42  ($inner:tt, $($arg:tt)*) => { {
43    if log::log_enabled!(log::Level::Info) {
44      log::info!("{}: {}", $inner.id, format!($($arg)*))
45    }
46   } }
47);
48
49/// Span used within the client that uses the command's span ID as the parent.
50#[cfg(any(feature = "full-tracing", feature = "partial-tracing"))]
51macro_rules! fspan (
52  ($cmd:ident, $lvl:expr, $($arg:tt)*) => { {
53    let _id = $cmd.traces.cmd.as_ref().and_then(|c| c.id());
54    span_lvl!($lvl, parent: _id, $($arg)*)
55  } }
56);
57
58macro_rules! span_lvl {
59    ($lvl:expr, $($args:tt)*) => {{
60        match $lvl {
61            tracing::Level::ERROR => tracing::error_span!($($args)*),
62            tracing::Level::WARN => tracing::warn_span!($($args)*),
63            tracing::Level::INFO => tracing::info_span!($($args)*),
64            tracing::Level::DEBUG => tracing::debug_span!($($args)*),
65            tracing::Level::TRACE => tracing::trace_span!($($args)*),
66        }
67    }};
68}
69
70/// Fake span used within the client that uses the command's span ID as the parent.
71#[cfg(not(any(feature = "full-tracing", feature = "partial-tracing")))]
72macro_rules! fspan (
73  ($cmd:ident, $($arg:tt)*) => {
74    crate::trace::Span {}
75  }
76);
77
78/// Similar to `try`/`?`, but `continue` instead of breaking out with an error.  
79macro_rules! try_or_continue (
80  ($expr:expr) => {
81    match $expr {
82      Ok(val) => val,
83      Err(_) => continue
84    }
85  }
86);
87
88/// A helper macro to wrap a string value in quotes via the [json](serde_json::json) macro.
89///
90/// See the [RedisJSON interface](crate::interfaces::RedisJsonInterface) for more information.
91#[cfg(feature = "i-redis-json")]
92#[cfg_attr(docsrs, doc(cfg(feature = "i-redis-json")))]
93#[macro_export]
94macro_rules! json_quote(
95  ($($json:tt)+) => {
96    serde_json::json!($($json)+).to_string()
97  }
98);
99
100/// Shorthand to create a [CustomCommand](crate::types::CustomCommand).
101///
102/// ```rust no_run
103/// # use fred::{cmd, types::{CustomCommand, ClusterHash}};
104/// let _cmd = cmd!("FOO.BAR");
105/// let _cmd = cmd!("FOO.BAR", blocking: true);
106/// let _cmd = cmd!("FOO.BAR", hash: ClusterHash::FirstKey);
107/// let _cmd = cmd!("FOO.BAR", hash: ClusterHash::FirstKey, blocking: true);
108/// // which is shorthand for
109/// let _cmd = CustomCommand::new("FOO.BAR", ClusterHash::FirstKey, true);
110/// ```
111#[macro_export]
112macro_rules! cmd(
113  ($name:expr) => {
114    fred::types::CustomCommand::new($name, fred::types::ClusterHash::FirstKey, false)
115  };
116  ($name:expr, blocking: $blk:expr) => {
117    fred::types::CustomCommand::new($name, fred::types::ClusterHash::FirstKey, $blk)
118  };
119  ($name:expr, hash: $hash:expr) => {
120    fred::types::CustomCommand::new($name, $hash, false)
121  };
122  ($name:expr, hash: $hash:expr, blocking: $blk:expr) => {
123    fred::types::CustomCommand::new($name, $hash, $blk)
124  };
125);
126
127macro_rules! static_val(
128  ($val:expr) => {
129    Value::from_static_str($val)
130  }
131);
132
133macro_rules! into (
134  ($val:ident) => (let $val = $val.into(););
135  ($v1:ident, $v2:ident) => (
136    let ($v1, $v2) = ($v1.into(), $v2.into());
137  );
138  ($v1:ident, $v2:ident, $v3:ident) => (
139    let ($v1, $v2, $v3) = ($v1.into(), $v2.into(), $v3.into());
140  );
141  ($v1:ident, $v2:ident, $v3:ident, $v4:ident) => (
142    let ($v1, $v2, $v3, $v4) = ($v1.into(), $v2.into(), $v3.into(), $v4.into());
143  );
144  ($v1:ident, $v2:ident, $v3:ident, $v4:ident, $v5:ident) => (
145    let ($v1, $v2, $v3, $v4, $v5) = ($v1.into(), $v2.into(), $v3.into(), $v4.into(), $v5.into());
146  );
147  // add to this as needed
148);
149
150macro_rules! try_into (
151  ($val:ident) => (let $val = to!($val)?;);
152  ($v1:ident, $v2:ident) => (
153    let ($v1, $v2) = (to!($v1)?, to!($v2)?);
154  );
155  ($v1:ident, $v2:ident, $v3:ident) => (
156    let ($v1, $v2, $v3) = (to!($v1)?, to!($v2)?, to!($v3)?);
157  );
158  ($v1:ident, $v2:ident, $v3:ident, $v4:ident) => (
159    let ($v1, $v2, $v3, $v4) = (to!($v1)?, to!($v2)?, to!($v3)?, to!($v4)?);
160  );
161  ($v1:ident, $v2:ident, $v3:ident, $v4:ident, $v5:ident) => (
162    let ($v1, $v2, $v3, $v4, $v5) = (to!($v1)?, to!($v2)?, to!($v3)?, to!($v4)?, to!($v5)?);
163  );
164  // add to this as needed
165);