##// END OF EJS Templates
rhg: Replace subcommand boilerplate with a macro...
rhg: Replace subcommand boilerplate with a macro This removes some repetition, and will avoid additional repetition in the next commit. Differential Revision: https://phab.mercurial-scm.org/D9969

File last commit:

r47230:2e272cd7 default
r47230:2e272cd7 default
Show More
main.rs
94 lines | 2.6 KiB | application/rls-services+xml | RustLexer
Antoine Cezar
rhg: Add debug timing...
r46101 extern crate log;
Antoine Cezar
rhg: add a limited `rhg root` subcommand...
r45593 use clap::App;
use clap::AppSettings;
Antoine Cezar
rhg: add a limited `rhg debugdata` subcommand...
r46100 use clap::ArgMatches;
Simon Sapin
rhg: Simplify CommandError based on its use...
r47174 use format_bytes::format_bytes;
Antoine Cezar
rhg: add a limited `rhg root` subcommand...
r45593
Antoine Cezar
rhg: add Command trait for subcommands implemented by rhg...
r45515 mod error;
Antoine Cezar
rhg: add rhg crate...
r45503 mod exitcode;
Antoine Cezar
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`...
r45592 mod ui;
Antoine Cezar
rhg: add a limited `rhg debugdata` subcommand...
r46100 use error::CommandError;
Antoine Cezar
rhg: add rhg crate...
r45503
fn main() {
Antoine Cezar
rhg: Add debug timing...
r46101 env_logger::init();
Antoine Cezar
rhg: add a limited `rhg debugdata` subcommand...
r46100 let app = App::new("rhg")
Antoine Cezar
rhg: add a limited `rhg root` subcommand...
r45593 .setting(AppSettings::AllowInvalidUtf8)
.setting(AppSettings::SubcommandRequired)
.setting(AppSettings::VersionlessSubcommands)
Simon Sapin
rhg: Replace subcommand boilerplate with a macro...
r47230 .version("0.0.1");
let app = add_subcommand_args(app);
Antoine Cezar
rhg: add a limited `rhg root` subcommand...
r45593
Antoine Cezar
rhg: ask the error message from `CommandError`...
r45920 let ui = ui::Ui::new();
Simon Sapin
rhg: Replace subcommand boilerplate with a macro...
r47230 let matches = app.clone().get_matches_safe().unwrap_or_else(|err| {
let _ = ui.writeln_stderr_str(&err.message);
std::process::exit(exitcode::UNIMPLEMENTED)
});
let (subcommand_name, subcommand_matches) = matches.subcommand();
let run = subcommand_run_fn(subcommand_name)
.expect("unknown subcommand name from clap despite AppSettings::SubcommandRequired");
let args = subcommand_matches
.expect("no subcommand arguments from clap despite AppSettings::SubcommandRequired");
Antoine Cezar
rhg: add a limited `rhg root` subcommand...
r45593
Simon Sapin
rhg: Replace subcommand boilerplate with a macro...
r47230 let result = (|| -> Result<(), CommandError> {
let config = hg::config::Config::load()?;
run(&ui, &config, args)
})();
let exit_code = match result {
Simon Sapin
rhg: Simplify CommandError based on its use...
r47174 Ok(_) => exitcode::OK,
// Exit with a specific code and no error message to let a potential
// wrapper script fallback to Python-based Mercurial.
Err(CommandError::Unimplemented) => exitcode::UNIMPLEMENTED,
Err(CommandError::Abort { message }) => {
if !message.is_empty() {
// Ignore errors when writing to stderr, we’re already exiting
// with failure code so there’s not much more we can do.
let _ =
ui.write_stderr(&format_bytes!(b"abort: {}\n", message));
}
exitcode::ABORT
Antoine Cezar
rhg: ask the error message from `CommandError`...
r45920 }
Simon Sapin
rhg: Simplify CommandError based on its use...
r47174 };
std::process::exit(exit_code)
Antoine Cezar
rhg: add rhg crate...
r45503 }
Antoine Cezar
rhg: add a limited `rhg debugdata` subcommand...
r46100
Simon Sapin
rhg: Replace subcommand boilerplate with a macro...
r47230 macro_rules! subcommands {
($( $command: ident )+) => {
mod commands {
$(
pub mod $command;
)+
}
fn add_subcommand_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
app
$(
.subcommand(commands::$command::args())
)+
}
Simon Sapin
rhg: Parse system and user configuration at program start...
r47213
Simon Sapin
rhg: Replace subcommand boilerplate with a macro...
r47230 fn subcommand_run_fn(name: &str) -> Option<fn(
&ui::Ui,
&hg::config::Config,
&ArgMatches,
) -> Result<(), CommandError>> {
match name {
$(
stringify!($command) => Some(commands::$command::run),
)+
_ => None,
}
Antoine Cezar
rhg: add a limited `rhg cat -r` subcommand...
r46113 }
Simon Sapin
rhg: Replace subcommand boilerplate with a macro...
r47230 };
Antoine Cezar
rhg: add a limited `rhg debugdata` subcommand...
r46100 }
Simon Sapin
rhg: Replace subcommand boilerplate with a macro...
r47230
subcommands! {
cat
debugdata
debugrequirements
files
root
}