light_cli/
macros.rs

1
2/// This macro allows for an easy way to define key value commands while
3/// still allowing to define custom error handlers.
4/// 
5/// # Arguments 
6/// * `$cli`: The [`LightCliInput`] instance to parse data from.
7/// * `$cmd`: The identifier to use to access the current command.
8/// * `$key`: The identifier to use to access the curernt key.
9/// * `$val`: The identifier to use to access the curernt value.
10/// * `$cmdv`: The name of the command.
11/// * `$keyv`: The key for command `$cmdv`.
12/// * `$action`: What to do with the value `$val` for the given command and key.
13/// * `$done`: What to do when the command is complete.
14/// * `$nomatch1`: What to do when the command value is not found 
15///             while trying to find a key action.
16/// * `$nomatch2`: What to do when the key value is not found
17///             while trying to find a key action.
18/// * `$nomatch3`: What to do when the command value is not found
19///             while trying to execute a command.
20/// 
21/// [`LightCliInput`]: struct.LightCliInput.html
22/// 
23/// # Remarks
24/// For a simpler way to write a command see the macro [`lightcli!`].
25/// This macro makes use of the underlying function [`parse_data`].
26/// 
27/// [`lightcli_adv!`]: macro.lightcli_adv.html
28/// [`parse_data`]: struct.LightCliInput.html#method.parse_data
29#[macro_export]
30macro_rules! lightcli_adv {
31    ($cli:expr, $cmd:ident, $key:ident, $val:ident, [ 
32        $(
33            $cmdv:pat => [
34                $( $keyv:pat => $action:expr ),*
35            ] => $done:expr
36        );*
37    ], $nomatch1:expr, $nomatch2:expr, $nomatch3:expr) => {
38        let _ = $cli.parse_data(|cbcmd| {
39            match cbcmd {
40                $crate::CallbackCommand::Attribute($cmd, $key, $val) => {
41                    match $cmd {
42                        $(
43                        $cmdv => {
44                            match $key {
45                                $(
46                                    $keyv => { $action },
47                                )*
48                                _ => $nomatch2,
49                            }
50                        }
51                        )*
52                        _ => $nomatch1,
53                    }
54                },
55                $crate::CallbackCommand::Command($cmd) => {
56                    match $cmd {
57                        $(
58                            $cmdv => $done,
59                        )*
60                        _ => $nomatch3,
61                    }
62                }
63            }
64        });
65    };
66}
67
68
69/// This macro allows for an easy way to define key value commands.
70/// 
71/// # Arguments 
72/// * `$cli`: The [`LightCliInput`] instance to parse data from.
73/// * `$cl_out`: The [`LightCliOutput`] instance to write errors to.
74/// * `$cmd`: The identifier to use to access the current command.
75/// * `$key`: The identifier to use to access the curernt key.
76/// * `$val`: The identifier to use to access the curernt value.
77/// * `$cmdv`: The name of the command.
78/// * `$keyv`: The key for command `$cmdv`.
79/// * `$action`: What to do with the value `$val` for the given command and key.
80/// * `$done`: What to do when the command is complete.
81/// 
82/// [`LightCliInput`]: struct.LightCliInput.html
83/// [`LightCliOutput`]: struct.LightCliOutput.html
84/// 
85/// # Remarks
86/// For a command that doesn't use the output and allows for custom 
87/// error handling see the macro [`lightcli_adv!`]. This macro makes use
88/// of the underlying function [`parse_data`].
89/// 
90/// [`lightcli_adv!`]: macro.lightcli_adv.html
91/// [`parse_data`]: struct.LightCliInput.html#method.parse_data
92#[macro_export]
93macro_rules! lightcli {
94    ($cli:expr, $cl_out:expr, $cmd:ident, $key:ident, $val:ident, [ 
95        $(
96            $cmdv:pat => [
97                $( $keyv:pat => $action:expr ),*
98            ] => $done:expr
99        );*
100    ]) => {
101        lightcli_adv!($cli, $cmd, $key, $val, [
102                $(
103                    $cmdv => [
104                        $( $keyv => $action ),*
105                    ] => $done
106                );*
107            ], 
108            {}, 
109            {writeln!($cl_out, "Unknown key for command {}: {}", $cmd, $key).unwrap()}, 
110            {writeln!($cl_out, "Unknown command: {}", $cmd).unwrap()}
111        );
112    };
113}