From: Ian Jackson Date: Wed, 22 Jul 2020 22:35:04 +0000 (+0100) Subject: move implementation X-Git-Tag: otter-0.2.0~1271 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=2ae7433d213590828fe7b4448e2e675d09310853;p=otter.git move implementation --- 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 { }, - } -}