From 7e8a9e789f65a225dcf29659b86f8f4cb3378f0d Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 28 Dec 2020 17:58:08 +0000 Subject: [PATCH] move cmd into mgmtchannel Signed-off-by: Ian Jackson --- src/bin/otter.rs | 31 ++++++++----------------------- src/mgmtchannel.rs | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/bin/otter.rs b/src/bin/otter.rs index 3a5f1c76..c0faf783 100644 --- a/src/bin/otter.rs +++ b/src/bin/otter.rs @@ -440,30 +440,15 @@ struct Conn { chan: MgmtChannel, } -impl Conn { - #[throws(AE)] - fn cmd(&mut self, cmd: &MgmtCommand) -> MgmtResponse { - use MgmtResponse::*; - self.chan.write(&cmd).context("send command")?; - let resp = self.chan.read().context("read response")?; - match &resp { - Fine | GamesList{..} | LibraryItems(_) => { }, - AlterGame { error: None, .. } => { }, - Error { error } => { - Err(error.clone()).context( - format!("got error response to: {:?}",&cmd) - )?; - }, - AlterGame { error: Some(error), .. } => { - Err(error.clone()).context(format!( - "game alterations failed (maybe partially); response to: {:?}", - &cmd - ))?; - } - }; - resp - } +impl Deref for Conn { + type Target = MgmtChannel; + fn deref(&self) -> &MgmtChannel { &self.chan } +} +impl DerefMut for Conn { + fn deref_mut(&mut self) -> &mut MgmtChannel { &mut self.chan } +} +impl Conn { #[throws(AE)] fn prep_access_account(&mut self, ma: &MainOpts) { #[derive(Debug)] diff --git a/src/mgmtchannel.rs b/src/mgmtchannel.rs index 245510fe..03869729 100644 --- a/src/mgmtchannel.rs +++ b/src/mgmtchannel.rs @@ -53,6 +53,29 @@ impl MgmtChannel { write!(self.write, "\n")?; self.write.flush()?; } + + #[throws(AE)] + pub fn cmd(&mut self, cmd: &MgmtCommand) -> MgmtResponse { + use MgmtResponse::*; + self.write(&cmd).context("send command")?; + let resp = self.read().context("read response")?; + match &resp { + Fine | GamesList{..} | LibraryItems(_) => { }, + AlterGame { error: None, .. } => { }, + Error { error } => { + Err(error.clone()).context( + format!("got error response to: {:?}",&cmd) + )?; + }, + AlterGame { error: Some(error), .. } => { + Err(error.clone()).context(format!( + "game alterations failed (maybe partially); response to: {:?}", + &cmd + ))?; + } + }; + resp + } } pub trait IoTryClone: Sized { -- 2.30.2