diff --git a/gui/src/app/state/psbt.rs b/gui/src/app/state/psbt.rs index 417c1970..6f6bf976 100644 --- a/gui/src/app/state/psbt.rs +++ b/gui/src/app/state/psbt.rs @@ -52,6 +52,38 @@ pub trait Action { fn view<'a>(&'a self, content: Element<'a, view::Message>) -> Element<'a, view::Message>; } +pub enum PsbtAction { + Save(SaveAction), + Sign(SignAction), + Update(UpdateAction), + Broadcast(BroadcastAction), + Delete(DeleteAction), +} + +impl<'a> AsRef for PsbtAction { + fn as_ref(&self) -> &(dyn Action + 'a) { + match &self { + Self::Save(a) => a, + Self::Sign(a) => a, + Self::Update(a) => a, + Self::Broadcast(a) => a, + Self::Delete(a) => a, + } + } +} + +impl<'a> AsMut for PsbtAction { + fn as_mut(&mut self) -> &mut (dyn Action + 'a) { + match self { + Self::Save(a) => a, + Self::Sign(a) => a, + Self::Update(a) => a, + Self::Broadcast(a) => a, + Self::Delete(a) => a, + } + } +} + pub struct PsbtState { pub wallet: Arc, pub desc_policy: LianaPolicy, @@ -59,7 +91,7 @@ pub struct PsbtState { pub saved: bool, pub warning: Option, pub labels_edited: LabelsEdited, - pub action: Option>, + pub action: Option, } impl PsbtState { @@ -77,7 +109,7 @@ impl PsbtState { pub fn subscription(&self) -> Subscription { if let Some(action) = &self.action { - action.subscription() + action.as_ref().subscription() } else { Subscription::none() } @@ -85,7 +117,7 @@ impl PsbtState { pub fn load(&self, daemon: Arc) -> Command { if let Some(action) = &self.action { - action.load(daemon) + action.as_ref().load(daemon) } else { Command::none() } @@ -103,7 +135,7 @@ impl PsbtState { self.action = None; } view::SpendTxMessage::Delete => { - self.action = Some(Box::::default()); + self.action = Some(PsbtAction::Delete(DeleteAction::default())); } view::SpendTxMessage::Sign => { let action = SignAction::new( @@ -114,24 +146,26 @@ impl PsbtState { self.saved, ); let cmd = action.load(daemon); - self.action = Some(Box::new(action)); + self.action = Some(PsbtAction::Sign(action)); return cmd; } view::SpendTxMessage::EditPsbt => { let action = UpdateAction::new(self.wallet.clone(), self.tx.psbt.to_string()); let cmd = action.load(daemon); - self.action = Some(Box::new(action)); + self.action = Some(PsbtAction::Update(action)); return cmd; } view::SpendTxMessage::Broadcast => { - self.action = Some(Box::::default()); + self.action = Some(PsbtAction::Broadcast(BroadcastAction::default())); } view::SpendTxMessage::Save => { - self.action = Some(Box::::default()); + self.action = Some(PsbtAction::Save(SaveAction::default())); } _ => { if let Some(action) = self.action.as_mut() { - return action.update(daemon.clone(), message, &mut self.tx); + return action + .as_mut() + .update(daemon.clone(), message, &mut self.tx); } } }, @@ -152,12 +186,16 @@ impl PsbtState { Message::Updated(Ok(_)) => { self.saved = true; if let Some(action) = self.action.as_mut() { - return action.update(daemon.clone(), message, &mut self.tx); + return action + .as_mut() + .update(daemon.clone(), message, &mut self.tx); } } _ => { if let Some(action) = self.action.as_mut() { - return action.update(daemon.clone(), message, &mut self.tx); + return action + .as_mut() + .update(daemon.clone(), message, &mut self.tx); } } }; @@ -176,7 +214,7 @@ impl PsbtState { self.warning.as_ref(), ); if let Some(action) = &self.action { - action.view(content) + action.as_ref().view(content) } else { content } diff --git a/gui/src/app/state/spend/step.rs b/gui/src/app/state/spend/step.rs index d94b4888..9230d96a 100644 --- a/gui/src/app/state/spend/step.rs +++ b/gui/src/app/state/spend/step.rs @@ -649,7 +649,7 @@ impl Step for SaveSpend { spend.warning.as_ref(), ); if let Some(action) = &spend.action { - action.view(content) + action.as_ref().view(content) } else { content }