use core::fmt;
use alloc::string::String;
use alloc::vec::Vec;
use super::{Action, Bundle, Zip32Derivation};
impl Bundle {
pub fn update_with<F>(&mut self, f: F) -> Result<(), UpdaterError>
where
F: FnOnce(Updater<'_>) -> Result<(), UpdaterError>,
{
f(Updater(self))
}
}
#[derive(Debug)]
pub struct Updater<'a>(&'a mut Bundle);
impl Updater<'_> {
pub fn bundle(&self) -> &Bundle {
self.0
}
pub fn update_action_with<F>(&mut self, index: usize, f: F) -> Result<(), UpdaterError>
where
F: FnOnce(ActionUpdater<'_>) -> Result<(), UpdaterError>,
{
f(ActionUpdater(
self.0
.actions
.get_mut(index)
.ok_or(UpdaterError::InvalidIndex)?,
))
}
}
#[derive(Debug)]
pub struct ActionUpdater<'a>(&'a mut Action);
impl ActionUpdater<'_> {
pub fn set_spend_zip32_derivation(&mut self, derivation: Zip32Derivation) {
self.0.spend.zip32_derivation = Some(derivation);
}
pub fn set_spend_proprietary(&mut self, key: String, value: Vec<u8>) {
self.0.spend.proprietary.insert(key, value);
}
pub fn set_output_zip32_derivation(&mut self, derivation: Zip32Derivation) {
self.0.output.zip32_derivation = Some(derivation);
}
pub fn set_output_user_address(&mut self, user_address: String) {
self.0.output.user_address = Some(user_address);
}
pub fn set_output_proprietary(&mut self, key: String, value: Vec<u8>) {
self.0.output.proprietary.insert(key, value);
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum UpdaterError {
InvalidIndex,
}
impl fmt::Display for UpdaterError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
UpdaterError::InvalidIndex => write!(f, "Action index is out-of-bounds"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for UpdaterError {}