title: How to generate complex enum variants with a macro in rust
body: <p>I'm writing a little library for parsing OVPN config files. OVPN config files have this format</p>
  
  <pre><code>command arg1 arg2
  othercommand arg1 arg2
  </code></pre>
  
  <p>There's a fixed set of commands, some of them have optional arguments. I want to represent the parsed commands as an enum. So the above might end up being represented like this:</p>
  
  <pre><code>enum ConfigDirective{
      Command{arg1: String},
      OtherCommand{arg1: String, optinal_arg1: Option&lt;String&gt;},
  }
  
  fn parse_line(command: String, args: Vec&lt;String&gt;) -&gt; ConfigDirective {
      match command {
          "command" =&gt; ConfigDirective::Command{arg1: args[0]},
          "other_command" =&gt; ConfigDirective:OtherCommand{arg1: args[0], optional_arg1: args.get(1),
      }
  }
  </code></pre>
  
  <p>I like this structure but there are a lot of possible commands (somewhere in the region of 280). So I want to write a macro to generate most of the boilerplate. Ideally I would write something like the following:</p>
  
  <pre><code>define_config_directive!{
      {command =&gt; "command1", rust_name =&gt; CommandOne, args =&gt; [arg1], optional_args =&gt; []},    
      {command =&gt; "other_command", rust_name =&gt; OtherCommand, args =&gt; [arg1], optional_args =&gt; [optional_arg1]},
  }
  </code></pre>
  
  <p>The closest I've been able to get so far is this:</p>
  
  <pre><code>macro_rules! define_config_directives {
      ($({
          rust_name =&gt; $rust_name:ident,
          required =&gt; [$($required:ident),*],
          optional =&gt; [$($optional:ident),*]
      }),*) =&gt; {
          #[derive(PartialEq, Eq, Debug)]
          pub enum ConfigDirective {
              $($rust_name{
                  $($required: String),*,
                  $($optional: Option&lt;String&gt;),*,
              }),*
          }
      };
  }
  </code></pre>
  
  <p>So I have a few problems: </p>
  
  <ol>
  <li>I don't know how to implement the <code>parse_line</code> function in this macro, I need to iterate over each required argument in order writing some code to pull the corresponding argument out of the line and the same for optional arguments</li>
  <li>I don't know how to handle situations where there are no arguments at all, ideally that would be a simple enum variant without fields.</li>
  </ol>
  
  <p>Does anyone know if there's a way to solve this on stable rust? Or should I just generate the code using a python script?</p>