Is there a way to make these two macros the same macro?
macro_rules! bg_color_test {
($($color:path, $flag:literal),*$(,)?) => {
let mut options = Options::default();
options.text = String::from("my text");
$(
options.background = $color;
assert_eq!(
parse_args(vec![
"path/to/bin".to_string(),
"my text".to_string(),
"-b".to_string(),
$flag.to_string()
]),
options
);
)*
}
}
// used like below:
bg_color_test!(
Colors::Red,"red",
Colors::Green,"green",
Colors::Blue,"blue",
);
And this one:
macro_rules! color_test {
($($color:expr, $flag:literal),*$(,)?) => {
let mut options = Options::default();
options.text = String::from("my text");
$(
options.colors = $color;
assert_eq!(
parse_args(vec![
"path/to/bin".to_string(),
"my text".to_string(),
"-c".to_string(),
$flag.to_string()
]),
options
);
)*
}
}
// used like below:
color_test!(
vec![Colors::Red],"red",
vec![Colors::Green],"green",
vec![Colors::Blue],"blue",
);
I’m struggling specifically with this bit:
options.background = $color;
vs options.colors = $color;
… no idea how I can build that into the macro…
Within the macro I tried below to make the key dynamic:
options.$kind = $color; // syntax error
options[$kind] = $color; // syntax error
options.$($kind)* = $color; // empty expression error
The way I use the macro is like this:
bg_color_test!(
"background", Colors::Red,"red",
"background", Colors::Green,"green",
"background", Colors::Blue,"blue",
);
Which will give me:
error: no rules expected the token `"background"`
--> tests/parse_args_test.rs:491:3
|
464 | macro_rules! bg_color_test {
| ------------------------ when calling this macro
...
491 | "background", BgColors::Red, "red",
| ^^^^^^^^^^^^ no rules expected this token in macro call
error: could not compile `lib` due to previous error