Created
October 3, 2019 07:53
-
-
Save rust-play/80d6860ee0a546ace67cbc63fcecd52f to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro_rules! commands { | |
( | |
($cmd_enum:ident, $results_mod:ident, $ret_enum:ident), | |
$( | |
$cmd:ident -> $( | |
$res:ident $( | |
($($inner:ty),+) | |
)? | |
)|+ | |
),+ | |
$(,)? | |
) => { | |
commands!(@cmd_enum $cmd_enum {$($cmd),+}); | |
pub mod $results_mod { | |
$( | |
commands!(@res_enum $cmd $($res $(($($inner),+))?)|+); | |
)+ | |
} | |
commands!(@ret_enum $ret_enum $results_mod $($cmd),+); | |
}; | |
(@cmd_enum $name:ident {$($cmd:ident),+}) => { | |
pub enum $name { | |
$($cmd),+ | |
} | |
}; | |
(@res_enum $cmd:ident | |
$( | |
$res:ident $( | |
($($inner:ty),+) | |
)? | |
)|+ | |
) => { | |
pub enum $cmd { | |
$( | |
$res $( | |
($($inner),+) | |
)? | |
),+ | |
} | |
}; | |
(@ret_enum $ret_enum:ident $results_mod:ident | |
$($cmd:ident),+ | |
) => { | |
use $results_mod::*; | |
pub enum $ret_enum { | |
$( | |
$cmd($cmd) | |
),+ | |
} | |
} | |
} | |
commands! { | |
(Command, results, CommandResult), | |
Quit -> Ok, | |
Init -> Ok | NeedCredentials | Fail(String), | |
Fetch -> Ok | Fail(i32, String), | |
} | |
fn main() { | |
let _ = Command::Quit; | |
let _ = Command::Init; | |
let _ = Command::Fetch; | |
let _ = results::Quit::Ok; | |
let _ = results::Init::Ok; | |
let _ = results::Init::NeedCredentials; | |
let _ = results::Init::Fail(String::from("")); | |
let _ = results::Fetch::Ok; | |
let _ = results::Fetch::Fail(1, String::from("")); | |
let _ = CommandResult::Quit(results::Quit::Ok); | |
let _ = CommandResult::Init(results::Init::Ok); | |
let _ = CommandResult::Fetch(results::Fetch::Ok); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment