Closed
Description
I tried to write a macro which generates enum, FromStr, and ToStr:
macro_rules! some_macro(
($mod_name:ident, $enum_name:ident, $($a:ident ~ $b:expr),+) => (
mod $mod_name {
#[deriving(Eq)]
pub enum $enum_name {
$(
$a,
)+
}
impl ToStr for $enum_name {
fn to_str(&self) -> ~str {
match *self {
$(
$a => $b.to_owned(),
)+
_ => fail!("to_str failure: %?", self),
}
}
}
impl FromStr for $enum_name {
fn from_str(s: &str) -> Option<$enum_name> {
match s {
$(
$b => Some($a),
)+
_ => None,
}
}
}
}
)
)
some_macro!(TestMod, Test,
A ~ "a",
B ~ "b"
)
(mod $mod_name
is due to #4375.)
However this fails to build:
tmp.rs:68:29: 68:30 error: unexpected token: `"a"`
tmp.rs:68 $b => Some($a),
^
because $b
is expr
and not pat
. Changing $b:expr
to $b:pat
gives another error:
tmp.rs:57:35: 57:36 error: unexpected token: `an interpolated pattern`
tmp.rs:57 $a => $b.to_owned(),
^
since $b
is not expr
. As a workaround I had to use classical if-else block.
I'm not sure if this is intended design decision or just unexpected/unimplemented. I think it's good if I can specifiy $b
both expr
and pat
, or there is some other smart way to solve it.