From e6d471a45d1d8c50009d97a646b1747fc33182d7 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 3 May 2021 19:24:44 +0100 Subject: [PATCH] apitest: Reorganise did() in preparation for a lot of Explode Signed-off-by: Ian Jackson --- apitest/apitest.rs | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/apitest/apitest.rs b/apitest/apitest.rs index 68e1dbf9..ec3304e4 100644 --- a/apitest/apitest.rs +++ b/apitest/apitest.rs @@ -88,6 +88,7 @@ pub struct Instance(pub InstanceName); impl AsRef for Opts { fn as_ref(&self) -> &Opts { self } } +#[derive(Debug)] pub enum Explode { } impl From for Explode { fn from(e: E) -> Explode { panic!("exploding on error: {}", e) } @@ -95,6 +96,14 @@ impl From for Explode { impl From for anyhow::Error { fn from(e: Explode) -> AE { match e { } } } +#[ext(pub, name=ResultExplodeExt)] +impl Result { + fn y(self) -> T { match self { Ok(y) => y, Err(n) => match n { } } } + fn did(self, msg: &'static str) -> anyhow::Result { + ResultGenDidExt::<_,AE>::did(Ok(self.y()), msg) + } +} + /* impl From for E { fn from(e: Explode) -> E { match e { } } @@ -267,17 +276,26 @@ macro_rules! test { // -------------------- Extra anyhow result handling -------------------- -#[ext(pub)] -impl Result { +pub trait PropagateDid { + fn propagate_did(self, msg: &'static str) -> anyhow::Result; +} + +#[ext(pub, name=ResultGenDidExt)] +impl Result where Result: anyhow::Context { fn did(self, msg: &'static str) -> anyhow::Result - where Self: anyhow::Context { - let x = self.context(msg); - if x.is_ok() { info!("did {}.", msg) }; - x + match self { + Ok(y) => { info!("did {}.", msg); Ok(y) } + n@ Err(_) => n.context(msg), + } } +} - fn just_warn(self) -> Option where E: Display { +#[ext(pub)] +impl Result { + fn just_warn(self) -> Option + where E: Display + { match self { Ok(x) => Some(x), Err(e) => { -- 2.30.2