1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

/// This macro allows for an easy way to define key value commands while
/// still allowing to define custom error handlers.
/// 
/// # Arguments 
/// * `$cli`: The [`LightCliInput`] instance to parse data from.
/// * `$cmd`: The identifier to use to access the current command.
/// * `$key`: The identifier to use to access the curernt key.
/// * `$val`: The identifier to use to access the curernt value.
/// * `$cmdv`: The name of the command.
/// * `$keyv`: The key for command `$cmdv`.
/// * `$action`: What to do with the value `$val` for the given command and key.
/// * `$done`: What to do when the command is complete.
/// * `$nomatch1`: What to do when the command value is not found 
///             while trying to find a key action.
/// * `$nomatch2`: What to do when the key value is not found
///             while trying to find a key action.
/// * `$nomatch3`: What to do when the command value is not found
///             while trying to execute a command.
/// 
/// [`LightCliInput`]: struct.LightCliInput.html
/// 
/// # Remarks
/// For a simpler way to write a command see the macro [`lightcli!`].
/// This macro makes use of the underlying function [`parse_data`].
/// 
/// [`lightcli_adv!`]: macro.lightcli_adv.html
/// [`parse_data`]: struct.LightCliInput.html#method.parse_data
#[macro_export]
macro_rules! lightcli_adv {
    ($cli:expr, $cmd:ident, $key:ident, $val:ident, [ 
        $(
            $cmdv:pat => [
                $( $keyv:pat => $action:expr ),*
            ] => $done:expr
        );*
    ], $nomatch1:expr, $nomatch2:expr, $nomatch3:expr) => {
        let _ = $cli.parse_data(|cbcmd| {
            match cbcmd {
                $crate::CallbackCommand::Attribute($cmd, $key, $val) => {
                    match $cmd {
                        $(
                        $cmdv => {
                            match $key {
                                $(
                                    $keyv => { $action },
                                )*
                                _ => $nomatch2,
                            }
                        }
                        )*
                        _ => $nomatch1,
                    }
                },
                $crate::CallbackCommand::Command($cmd) => {
                    match $cmd {
                        $(
                            $cmdv => $done,
                        )*
                        _ => $nomatch3,
                    }
                }
            }
        });
    };
}


/// This macro allows for an easy way to define key value commands.
/// 
/// # Arguments 
/// * `$cli`: The [`LightCliInput`] instance to parse data from.
/// * `$cl_out`: The [`LightCliOutput`] instance to write errors to.
/// * `$cmd`: The identifier to use to access the current command.
/// * `$key`: The identifier to use to access the curernt key.
/// * `$val`: The identifier to use to access the curernt value.
/// * `$cmdv`: The name of the command.
/// * `$keyv`: The key for command `$cmdv`.
/// * `$action`: What to do with the value `$val` for the given command and key.
/// * `$done`: What to do when the command is complete.
/// 
/// [`LightCliInput`]: struct.LightCliInput.html
/// [`LightCliOutput`]: struct.LightCliOutput.html
/// 
/// # Remarks
/// For a command that doesn't use the output and allows for custom 
/// error handling see the macro [`lightcli_adv!`]. This macro makes use
/// of the underlying function [`parse_data`].
/// 
/// [`lightcli_adv!`]: macro.lightcli_adv.html
/// [`parse_data`]: struct.LightCliInput.html#method.parse_data
#[macro_export]
macro_rules! lightcli {
    ($cli:expr, $cl_out:expr, $cmd:ident, $key:ident, $val:ident, [ 
        $(
            $cmdv:pat => [
                $( $keyv:pat => $action:expr ),*
            ] => $done:expr
        );*
    ]) => {
        lightcli_adv!($cli, $cmd, $key, $val, [
                $(
                    $cmdv => [
                        $( $keyv => $action ),*
                    ] => $done
                );*
            ], 
            {}, 
            {writeln!($cl_out, "Unknown key for command {}: {}", $cmd, $key).unwrap()}, 
            {writeln!($cl_out, "Unknown command: {}", $cmd).unwrap()}
        );
    };
}