SHARE
TWEET

Untitled

a guest Oct 21st, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. macro_rules! commands {
  2.     (
  3.         ($cmd_enum:ident, $results_mod:ident, $ret_enum:ident),
  4.         $(
  5.             $cmd:ident -> $(
  6.                 $res:ident $(
  7.                     ($($inner:ty),+)
  8.                 )?
  9.             )|+
  10.         ),+
  11.         $(,)?
  12.     ) => {
  13.         commands!(@cmd_enum $cmd_enum {$($cmd),+});
  14.         pub mod $results_mod {
  15.             $(
  16.                 commands!(@res_enum $cmd $($res $(($($inner),+))?)|+);
  17.             )+
  18.         }
  19.         commands!(@ret_enum $ret_enum $results_mod $($cmd),+);
  20.     };
  21.    
  22.     (@cmd_enum $name:ident {$($cmd:ident),+}) => {
  23.         pub enum $name {
  24.             $($cmd),+
  25.         }
  26.     };
  27.    
  28.     (@res_enum $cmd:ident
  29.         $(
  30.             $res:ident $(
  31.                 ($($inner:ty),+)
  32.             )?
  33.         )|+
  34.     ) => {
  35.         pub enum $cmd {
  36.             $(
  37.                 $res $(
  38.                     ($($inner),+)
  39.                 )?
  40.             ),+
  41.         }
  42.     };
  43.    
  44.     (@ret_enum $ret_enum:ident $results_mod:ident
  45.         $($cmd:ident),+
  46.     ) => {
  47.         use $results_mod::*;
  48.         pub enum $ret_enum {
  49.             $(
  50.                $cmd($cmd)
  51.             ),+
  52.         }
  53.     }
  54. }
  55.  
  56. commands! {
  57.     (Command, results, CommandResult),
  58.     Quit -> Ok,
  59.     Init -> Ok | NeedCredentials | Fail(String),
  60.     Fetch -> Ok | Fail(i32, String),
  61. }
  62.  
  63. fn main() {
  64.     let _ = Command::Quit;
  65.     let _ = Command::Init;
  66.     let _ = Command::Fetch;
  67.    
  68.     let _ = results::Quit::Ok;
  69.     let _ = results::Init::Ok;
  70.     let _ = results::Init::NeedCredentials;
  71.     let _ = results::Init::Fail(String::from(""));
  72.     let _ = results::Fetch::Ok;
  73.     let _ = results::Fetch::Fail(1, String::from(""));
  74.    
  75.     let _ = CommandResult::Quit(results::Quit::Ok);
  76.     let _ = CommandResult::Init(results::Init::Ok);
  77.     let _ = CommandResult::Fetch(results::Fetch::Ok);
  78. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top