From 2ae7433d213590828fe7b4448e2e675d09310853 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Wed, 22 Jul 2020 23:35:04 +0100 Subject: [PATCH] move implementation --- src/cmdlistener.rs | 30 ++++++++++++++++++++++++++++++ src/commands.rs | 30 +----------------------------- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/cmdlistener.rs b/src/cmdlistener.rs index ef047d92..e2c64ba8 100644 --- a/src/cmdlistener.rs +++ b/src/cmdlistener.rs @@ -40,6 +40,36 @@ impl CommandStream { } } +impl From for MgmtError { + fn from(je: serde_json::Error) -> ME { + ParseFailed(format!("{}", &je)) + } +} + +use MgmtCommand::*; +use MgmtResponse::*; +use MgmtError::*; + +type ME = MgmtError; + +pub fn decode_and_process(s: &str) -> MgmtResponse { + self::decode_process_inner(s) + .unwrap_or_else(|e| MgmtResponse::Error(format!("{}", e))) +} + +#[throws(ME)] +fn decode_process_inner(s: &str)-> MgmtResponse { + let cmd : MgmtCommand = serde_json::from_str(s)?; + execute(cmd)? +} + +#[throws(ME)] +fn execute(cmd: MgmtCommand) -> MgmtResponse { + match cmd { + Noop { } => Fine { }, + } +} + impl CommandListener { #[throws(StartupError)] pub fn new() -> Self { diff --git a/src/commands.rs b/src/commands.rs index 38e9d1ee..a5ba28d4 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -13,36 +13,8 @@ pub enum MgmtResponse { } #[derive(Debug,Error)] -enum MgmtError { +pub enum MgmtError { ParseFailed(String), } display_as_debug!{MgmtError} -type ME = MgmtError; - use MgmtCommand::*; - use MgmtResponse::*; - use MgmtError::*; - -impl From for MgmtError { - fn from(je: serde_json::Error) -> ME { - ParseFailed(format!("{}", &je)) - } -} - -pub fn decode_and_process(s: &str) -> MgmtResponse { - self::decode_process_inner(s) - .unwrap_or_else(|e| MgmtResponse::Error(format!("{}", e))) -} - -#[throws(ME)] -fn decode_process_inner(s: &str)-> MgmtResponse { - let cmd : MgmtCommand = serde_json::from_str(s)?; - execute(cmd)? -} - -#[throws(ME)] -fn execute(cmd: MgmtCommand) -> MgmtResponse { - match cmd { - Noop { } => Fine { }, - } -} -- 2.30.2