From 9df0f3bd565dfbf8c97d02969a17504c688bf381 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Mon, 18 Feb 2019 21:36:31 +0000 Subject: 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 from a CommandId. Instead, we directly delegate the execute method to the corresponding Command. --- src/util.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/util.rs') 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])* -- cgit v1.2.1