Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code shared from the Rust Playground
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