[Solved] Defining a symbol with macro_rules!

#1

I am trying to define a stringy_enum! macro that will save me some typing when converting to and from strings:

However, when I call stringy_enum!(FooBar => [Foo, Bar, Baz]), the compiler complains that Foo, Bar and Baz are undefined in this scope. I thought that the hole point of this exercise was to write a macro that defines those symbols, but apparently I am missing something.

My question is two fold:

  1. How to define a symbol (in this case an enum variant) using macro_rules!
  2. Is there a more idiomatic way to define an enum that can parse and display itself, e.g. "Foo" -> Foobar::Foo and Foobar::Foo -> "Foo"?

edit: fixed playground link

#2

I think you linked the wrong playpen.

1 Like
#3

It’s nothing to do with the macro system: you didn’t qualify the variants on line 14. Ok($t::$v).

1 Like
#4

Thanks to @DanielKeep’s comment I got to the correct implementation