Commit 10d1cef849

10d1cef849f32a96e66891672d6e48e26477ef15

parent: 3a35bebb68

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2020-10-18 11:38 UTC

commands: Refactor the error handling for commands.

Layout: unified · split

src/commands/devices.rs +4 −16
@@ -10,6 +10,7 @@ use weechat::{
1010 Args, Weechat,
1111};
1212
13use super::parse_and_run;
1314use crate::Servers;
1415
1516pub struct DevicesCommand {
@@ -120,21 +121,8 @@ impl CommandCallback for DevicesCommand {
120121 .setting(ArgParseSettings::SubcommandRequiredElseHelp)
121122 .subcommands(Self::subcommands());
122123
123 let matches = match argparse.get_matches_from_safe(arguments) {
124 Ok(m) => m,
125 Err(e) => {
126 Weechat::print(
127 &Weechat::execute_modifier(
128 "color_decode_ansi",
129 "1",
130 &e.to_string(),
131 )
132 .unwrap(),
133 );
134 return;
135 }
136 };
137
138 Self::run(buffer, &self.servers, &matches)
124 parse_and_run(argparse, arguments, |matches| {
125 Self::run(buffer, &self.servers, &matches)
126 });
139127 }
140128}
src/commands/keys.rs +4 −16
@@ -11,6 +11,7 @@ use weechat::{
1111 Args, Weechat,
1212};
1313
14use super::parse_and_run;
1415use crate::{MatrixServer, Servers};
1516
1617pub struct KeysCommand {
@@ -121,21 +122,8 @@ impl CommandCallback for KeysCommand {
121122 .setting(ArgParseSettings::SubcommandRequiredElseHelp)
122123 .subcommands(Self::subcommands());
123124
124 let matches = match argparse.get_matches_from_safe(arguments) {
125 Ok(m) => m,
126 Err(e) => {
127 Weechat::print(
128 &Weechat::execute_modifier(
129 "color_decode_ansi",
130 "1",
131 &e.to_string(),
132 )
133 .unwrap(),
134 );
135 return;
136 }
137 };
138
139 Self::run(buffer, &self.servers, &matches)
125 parse_and_run(argparse, arguments, |matches| {
126 Self::run(buffer, &self.servers, matches)
127 });
140128 }
141129}
src/commands/matrix.rs +17 −27
@@ -10,6 +10,7 @@ use weechat::{
1010 Args, Weechat,
1111};
1212
13use super::parse_and_run;
1314use crate::{
1415 commands::{DevicesCommand, KeysCommand},
1516 config::ConfigHandle,
@@ -216,6 +217,21 @@ Use /matrix [command] help to find out more.\n",
216217 self.server_not_found(server_name)
217218 }
218219 }
220
221 fn run(&self, buffer: &Buffer, args: &ArgMatches) {
222 match args.subcommand() {
223 ("connect", Some(subargs)) => self.connect_command(subargs),
224 ("disconnect", Some(subargs)) => self.disconnect_command(subargs),
225 ("server", Some(subargs)) => self.server_command(subargs),
226 ("devices", Some(subargs)) => {
227 DevicesCommand::run(buffer, &self.servers, subargs)
228 }
229 ("keys", Some(subargs)) => {
230 KeysCommand::run(buffer, &self.servers, subargs)
231 }
232 _ => unreachable!(),
233 }
234 }
219235}
220236
221237impl CommandCallback for MatrixCommand {
@@ -296,32 +312,6 @@ impl CommandCallback for MatrixCommand {
296312 ),
297313 );
298314
299 let matches = match argparse.get_matches_from_safe(arguments) {
300 Ok(m) => m,
301 Err(e) => {
302 Weechat::print(
303 &Weechat::execute_modifier(
304 "color_decode_ansi",
305 "1",
306 &e.to_string(),
307 )
308 .unwrap(),
309 );
310 return;
311 }
312 };
313
314 match matches.subcommand() {
315 ("connect", Some(subargs)) => self.connect_command(subargs),
316 ("disconnect", Some(subargs)) => self.disconnect_command(subargs),
317 ("server", Some(subargs)) => self.server_command(subargs),
318 ("devices", Some(subargs)) => {
319 DevicesCommand::run(buffer, &self.servers, subargs)
320 }
321 ("keys", Some(subargs)) => {
322 KeysCommand::run(buffer, &self.servers, subargs)
323 }
324 _ => unreachable!(),
325 }
315 parse_and_run(argparse, arguments, |args| self.run(buffer, args));
326316 }
327317}
src/commands/mod.rs +22 −1
@@ -1,5 +1,7 @@
1use clap::{App, ArgMatches};
2use weechat::{hooks::Command, Args, Weechat};
3
14use crate::{config::ConfigHandle, Servers};
2use weechat::hooks::Command;
35
46mod devices;
57mod keys;
@@ -27,3 +29,22 @@ impl Commands {
2729 })
2830 }
2931}
32
33fn parse_and_run(
34 parser: App,
35 arguments: Args,
36 command: impl FnOnce(&ArgMatches),
37) {
38 match parser.get_matches_from_safe(arguments) {
39 Ok(m) => command(&m),
40 Err(e) => {
41 let error = Weechat::execute_modifier(
42 "color_decode_ansi",
43 "1",
44 &e.to_string(),
45 )
46 .expect("Can't color decode ansi string");
47 Weechat::print(&error);
48 }
49 }
50}