aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2019-02-19 11:28:28 +0000
committerRobin Krahl <robin.krahl@ireas.org>2019-02-19 12:35:33 +0100
commitdbcbc8472543d5c5b24bf6ce281637a9a8a70098 (patch)
tree2bef13d3d30ac4607ee4a11e791ea9c7c83cdd20 /src
parent3c4d23dd5f202eaba09b408d071e513f13df11cf (diff)
downloadntw-dbcbc8472543d5c5b24bf6ce281637a9a8a70098.tar.gz
ntw-dbcbc8472543d5c5b24bf6ce281637a9a8a70098.tar.bz2
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.
Diffstat (limited to 'src')
-rw-r--r--src/commands.rs30
-rw-r--r--src/util.rs2
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<Self::Response, CommandStatus>;
-
- fn execute_raw(data: &[u8], buf: &mut [u8]) -> CommandStatus {
- // TODO: better error if (de-)serialization fails
- if let Ok((request, _)) = ssmarshal::deserialize::<Self::Request>(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<C: Command>(data: &[u8], buf: &mut [u8]) -> CommandStatus {
+ // TODO: better error if (de-)serialization fails
+ if let Ok((request, _)) = ssmarshal::deserialize::<C::Request>(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),
)*
}
}