diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2019-02-18 21:36:31 +0000 |
---|---|---|
committer | Robin Krahl <robin.krahl@ireas.org> | 2019-02-18 23:27:48 +0100 |
commit | 9df0f3bd565dfbf8c97d02969a17504c688bf381 (patch) | |
tree | da3a25d4900fc82a407ac729f5642f1e65cd0fbc /src/util.rs | |
parent | 49703cd770fd0522beae2dc53d1f882c9143c64e (diff) | |
download | ntw-9df0f3bd565dfbf8c97d02969a17504c688bf381.tar.gz ntw-9df0f3bd565dfbf8c97d02969a17504c688bf381.tar.bz2 |
Refactor command execution into commands module
This patch refactors the command execution. A command is represented by
a struct implementing the Command trait. The enum_cmd macro is used to
generate a mapping from the CommandId enum to a Command instance and to
execute the command. The request and response data is manually
converted from and to raw byte slices.
As we do not have a standard library, we cannot create a Box<Command>
from a CommandId. Instead, we directly delegate the execute method to
the corresponding Command.
Diffstat (limited to 'src/util.rs')
-rw-r--r-- | src/util.rs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/util.rs b/src/util.rs index 4fc4448..9325e9e 100644 --- a/src/util.rs +++ b/src/util.rs @@ -3,6 +3,35 @@ use core::marker::Sized; +macro_rules! enum_cmd { + ( + $(#[$outer:meta])* + pub enum $name:ident { + $($var:ident($cmd:ident) = $num:expr),+ + $(,)* + } + ) => { + enum_u8! { + $(#[$outer])* + pub enum $name { + $( + $var = $num, + )* + } + } + + impl $name { + pub fn execute(&self, data: &[u8], buf: &mut [u8]) -> CommandStatus { + match *self { + $( + $name::$var => $cmd::execute_raw(data, buf), + )* + } + } + } + }; +} + macro_rules! enum_u8 { ( $(#[$outer:meta])* |