Skip to content

Instantly share code, notes, and snippets.

@Cerber-Ursi
Forked from rust-play/playground.rs
Created October 3, 2019 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cerber-Ursi/85878e5b635fadb2958e9d3ed487a67b to your computer and use it in GitHub Desktop.
Save Cerber-Ursi/85878e5b635fadb2958e9d3ed487a67b to your computer and use it in GitHub Desktop.
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