siera_cloudagent_python/macros.rs
1#[macro_export]
2/// Macro to fill a vector<(str,str)> with a structure where fields can be optional
3///
4/// ```ignore
5/// struct Foo<'a> {
6/// a: Option<'a str>
7/// b: Option<'a str>
8/// }
9///
10/// let foo = Foo {a: "baz", b: "bar"}
11///
12/// ```
13///
14/// will convert to
15///
16/// ```ignore
17/// let query = fill_query!(foo, a, b);
18/// println!("{:?}", query); // [("a", "baz"), ("b", "bar")]
19/// ```
20macro_rules! fill_query {
21 ($options:expr, $($field:ident),*) => {
22 {
23 let mut query = Vec::new();
24 $(
25 $options.$field.as_ref().map(|c| query.push((stringify!($field), c.to_owned())));
26 )*
27 query
28 }
29 };
30}