comparison rust/rhg/src/main.rs @ 46500:184e46550dc8

rhg: replace command structs with functions The `Command` trait was not used in any generic context, and the struct where nothing more than holders for values parsed from CLI arguments to be available to a `run` method. Differential Revision: https://phab.mercurial-scm.org/D9967
author Simon Sapin <simon.sapin@octobus.net>
date Mon, 08 Feb 2021 20:33:04 +0100
parents a6e4e4650bac
children 1ecaf09d9964
comparison
equal deleted inserted replaced
46499:eace48b4a786 46500:184e46550dc8
4 use clap::Arg; 4 use clap::Arg;
5 use clap::ArgGroup; 5 use clap::ArgGroup;
6 use clap::ArgMatches; 6 use clap::ArgMatches;
7 use clap::SubCommand; 7 use clap::SubCommand;
8 use format_bytes::format_bytes; 8 use format_bytes::format_bytes;
9 use hg::operations::DebugDataKind;
10 use std::convert::TryFrom;
11 9
12 mod commands; 10 mod commands;
13 mod error; 11 mod error;
14 mod exitcode; 12 mod exitcode;
15 mod ui; 13 mod ui;
16 use commands::Command;
17 use error::CommandError; 14 use error::CommandError;
18 15
19 fn main() { 16 fn main() {
20 env_logger::init(); 17 env_logger::init();
21 let app = App::new("rhg") 18 let app = App::new("rhg")
124 ui: &ui::Ui, 121 ui: &ui::Ui,
125 ) -> Result<(), CommandError> { 122 ) -> Result<(), CommandError> {
126 let config = hg::config::Config::load()?; 123 let config = hg::config::Config::load()?;
127 124
128 match matches.subcommand() { 125 match matches.subcommand() {
129 ("root", _) => commands::root::RootCommand::new().run(&ui, &config), 126 ("root", Some(matches)) => commands::root::run(ui, &config, matches),
130 ("files", Some(matches)) => { 127 ("files", Some(matches)) => commands::files::run(ui, &config, matches),
131 commands::files::FilesCommand::try_from(matches)?.run(&ui, &config) 128 ("cat", Some(matches)) => commands::cat::run(ui, &config, matches),
129 ("debugdata", Some(matches)) => {
130 commands::debugdata::run(ui, &config, matches)
132 } 131 }
133 ("cat", Some(matches)) => { 132 ("debugrequirements", Some(matches)) => {
134 commands::cat::CatCommand::try_from(matches)?.run(&ui, &config) 133 commands::debugrequirements::run(ui, &config, matches)
135 }
136 ("debugdata", Some(matches)) => {
137 commands::debugdata::DebugDataCommand::try_from(matches)?
138 .run(&ui, &config)
139 }
140 ("debugrequirements", _) => {
141 commands::debugrequirements::DebugRequirementsCommand::new()
142 .run(&ui, &config)
143 } 134 }
144 _ => unreachable!(), // Because of AppSettings::SubcommandRequired, 135 _ => unreachable!(), // Because of AppSettings::SubcommandRequired,
145 } 136 }
146 } 137 }
147
148 impl<'a> TryFrom<&'a ArgMatches<'_>> for commands::files::FilesCommand<'a> {
149 type Error = CommandError;
150
151 fn try_from(args: &'a ArgMatches) -> Result<Self, Self::Error> {
152 let rev = args.value_of("rev");
153 Ok(commands::files::FilesCommand::new(rev))
154 }
155 }
156
157 impl<'a> TryFrom<&'a ArgMatches<'_>> for commands::cat::CatCommand<'a> {
158 type Error = CommandError;
159
160 fn try_from(args: &'a ArgMatches) -> Result<Self, Self::Error> {
161 let rev = args.value_of("rev");
162 let files = match args.values_of("files") {
163 Some(files) => files.collect(),
164 None => vec![],
165 };
166 Ok(commands::cat::CatCommand::new(rev, files))
167 }
168 }
169
170 impl<'a> TryFrom<&'a ArgMatches<'_>>
171 for commands::debugdata::DebugDataCommand<'a>
172 {
173 type Error = CommandError;
174
175 fn try_from(args: &'a ArgMatches) -> Result<Self, Self::Error> {
176 let rev = args
177 .value_of("rev")
178 .expect("rev should be a required argument");
179 let kind = match (
180 args.is_present("changelog"),
181 args.is_present("manifest"),
182 ) {
183 (true, false) => DebugDataKind::Changelog,
184 (false, true) => DebugDataKind::Manifest,
185 (true, true) => {
186 unreachable!("Should not happen since options are exclusive")
187 }
188 (false, false) => {
189 unreachable!("Should not happen since options are required")
190 }
191 };
192 Ok(commands::debugdata::DebugDataCommand::new(rev, kind))
193 }
194 }