Skip to content

Commit 2a7dbaf

Browse files
committed
Add svec macro (builds Vec<String> from a list of &str)
1 parent 212a7d3 commit 2a7dbaf

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

odoo-api/src/macros.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,31 @@ macro_rules! jmap {
107107
};
108108
() => { compiler_error!("")};
109109
}
110+
111+
/// Helper macro to build a [`Vec<String>`]
112+
///
113+
/// Quite a few ORM methods take [`Vec<String>`] as an argument. Using the built-in
114+
/// `vec![]` macro requires that each element is converted into a `String`, which
115+
/// is very cumbersome.
116+
///
117+
/// Using this macro, we can write:
118+
/// ```
119+
/// # #[cfg(not(feature = "types-only"))]
120+
/// # fn test() {
121+
/// # use odoo_api::svec;
122+
/// let fields = svec!["string", "literals", "without", "to_string()"];
123+
/// # }
124+
/// ```
125+
#[macro_export]
126+
macro_rules! svec {
127+
[$($v:tt),*] => {
128+
{
129+
let mut vec = ::std::vec::Vec::<String>::new();
130+
$(
131+
vec.push($v.to_string());
132+
)*
133+
vec
134+
}
135+
};
136+
() => { compiler_error!("")};
137+
}

0 commit comments

Comments
 (0)