From dbcbc8472543d5c5b24bf6ce281637a9a8a70098 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Tue, 19 Feb 2019 11:28:28 +0000 Subject: Refactor Command::execute_raw into execute The execute_raw method does not have to be part of the Command trait. In order to simplify the Command trait and to make future changes easier, we remove the execute_raw method and replace it with the execute function. --- src/commands.rs | 30 +++++++++++++++--------------- src/util.rs | 2 +- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index e8e332b..3a7d16a 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -23,21 +23,6 @@ trait Command { type Response: Serialize; fn execute(data: Self::Request) -> Result; - - fn execute_raw(data: &[u8], buf: &mut [u8]) -> CommandStatus { - // TODO: better error if (de-)serialization fails - if let Ok((request, _)) = ssmarshal::deserialize::(data) { - match Self::execute(request) { - Ok(response) => match ssmarshal::serialize(buf, &response) { - Ok(_) => CommandStatus::Ok, - Err(_) => CommandStatus::NotSupported, - }, - Err(status) => status, - } - } else { - CommandStatus::NotSupported - } - } } #[derive(Debug, Default, Serialize)] @@ -95,3 +80,18 @@ impl Command for ReadSlotNameCommand { } } } + +fn execute(data: &[u8], buf: &mut [u8]) -> CommandStatus { + // TODO: better error if (de-)serialization fails + if let Ok((request, _)) = ssmarshal::deserialize::(data) { + match C::execute(request) { + Ok(response) => match ssmarshal::serialize(buf, &response) { + Ok(_) => CommandStatus::Ok, + Err(_) => CommandStatus::NotSupported, + }, + Err(status) => status, + } + } else { + CommandStatus::NotSupported + } +} diff --git a/src/util.rs b/src/util.rs index 9325e9e..c83d3f3 100644 --- a/src/util.rs +++ b/src/util.rs @@ -24,7 +24,7 @@ macro_rules! enum_cmd { pub fn execute(&self, data: &[u8], buf: &mut [u8]) -> CommandStatus { match *self { $( - $name::$var => $cmd::execute_raw(data, buf), + $name::$var => crate::commands::execute::<$cmd>(data, buf), )* } } -- cgit v1.2.1