Skip to content

Commit

Permalink
Add more NativeTokensBuilder helpers (#1353)
Browse files Browse the repository at this point in the history
* Add more NativeTokensBuilder helpers

* Add Debug derivation

* Add insert method
  • Loading branch information
thibault-martinez authored Apr 25, 2022
1 parent 787d72b commit e812e15
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion bee-message/src/output/native_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn verify_amount<const VERIFY: bool>(amount: &U256) -> Result<(), Error> {
}

/// A builder for [`NativeTokens`].
#[derive(Default)]
#[derive(Clone, Default, Debug)]
#[must_use]
pub struct NativeTokensBuilder(HashMap<TokenId, U256>);

Expand Down Expand Up @@ -85,6 +85,30 @@ impl NativeTokensBuilder {
Ok(())
}

/// Merges another [`NativeTokensBuilder`] into this one.
pub fn merge(&mut self, other: NativeTokensBuilder) -> Result<(), Error> {
for (token_id, amount) in other.0.into_iter() {
self.add_native_token(NativeToken::new(token_id, amount)?)?;
}

Ok(())
}

/// Returns whether the [`NativeTokensBuilder`] is empty or not.
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}

/// Gets an amount from the [`NativeTokensBuilder`].
pub fn get(&self, token_id: &TokenId) -> Option<&U256> {
self.0.get(token_id)
}

/// Inserts a new entry in the [`NativeTokensBuilder`].
pub fn insert(&mut self, token_id: TokenId, amount: U256) -> Option<U256> {
self.0.insert(token_id, amount)
}

/// Finishes the [`NativeTokensBuilder`] into [`NativeTokens`].
pub fn finish(self) -> Result<NativeTokens, Error> {
NativeTokens::try_from(
Expand Down

0 comments on commit e812e15

Please sign in to comment.