src/commands/mod.rs

4fddc50d6d9f3d82220a4e3264d792d54fc940cf
weechat-matrix-rs/src/commands/mod.rs history · blame · raw

71 lines · 1709 bytes

 1use clap::{App, ArgMatches};
 2use verification::VerificationCommand;
 3use weechat::{
 4    hooks::{Command, CommandRun},
 5    Args, Weechat,
 6};
 7
 8use crate::{config::ConfigHandle, Servers};
 9
10mod buffer_clear;
11mod devices;
12mod keys;
13mod matrix;
14mod me;
15mod media;
16mod page_up;
17mod verification;
18
19use buffer_clear::BufferClearCommand;
20use devices::DevicesCommand;
21use keys::KeysCommand;
22use matrix::MatrixCommand;
23use me::MeCommand;
24use media::MediaCommand;
25use page_up::PageUpCommand;
26
27pub struct Commands {
28    _matrix: Command,
29    _keys: Command,
30    _devices: Command,
31    _page_up: CommandRun,
32    _verification: Command,
33    _buffer_clear: CommandRun,
34    _me: CommandRun,
35}
36
37impl Commands {
38    pub fn hook_all(
39        servers: &Servers,
40        config: &ConfigHandle,
41    ) -> Result<Commands, ()> {
42        Ok(Commands {
43            _matrix: MatrixCommand::create(servers, config)?,
44            _devices: DevicesCommand::create(servers)?,
45            _keys: KeysCommand::create(servers)?,
46            _page_up: PageUpCommand::create(servers)?,
47            _verification: VerificationCommand::create(servers)?,
48            _buffer_clear: BufferClearCommand::create(servers)?,
49            _me: MeCommand::create(servers)?,
50        })
51    }
52}
53
54fn parse_and_run(
55    parser: App,
56    arguments: Args,
57    command: impl FnOnce(&ArgMatches),
58) {
59    match parser.get_matches_from_safe(arguments) {
60        Ok(m) => command(&m),
61        Err(e) => {
62            let error = Weechat::execute_modifier(
63                "color_decode_ansi",
64                "1",
65                &e.to_string(),
66            )
67            .expect("Can't color decode ansi string");
68            Weechat::print(&error);
69        }
70    }
71}