Skip to content

Commit

Permalink
Output builders replace methods don't fail anymore (#1597)
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez authored Feb 21, 2023
1 parent 7d43dd6 commit e8422a8
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 44 deletions.
18 changes: 9 additions & 9 deletions client/examples/output/recursive_alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ async fn main() -> Result<()> {
.clone()
.with_alias_id(alias_id_1)
// add a sender feature with the first alias
.replace_feature(Feature::Sender(SenderFeature::new(alias_0_address)))?
.replace_feature(Feature::Sender(SenderFeature::new(alias_0_address)))
.with_state_index(0)
.replace_unlock_condition(UnlockCondition::StateControllerAddress(
StateControllerAddressUnlockCondition::new(alias_0_address),
))?
))
.replace_unlock_condition(UnlockCondition::GovernorAddress(GovernorAddressUnlockCondition::new(
alias_0_address,
)))?
)))
.finish_output(token_supply)?,
// make third alias output be controlled by the second one (indirectly also by the first one)
alias_output_builder
Expand All @@ -116,10 +116,10 @@ async fn main() -> Result<()> {
.with_state_index(0)
.replace_unlock_condition(UnlockCondition::StateControllerAddress(
StateControllerAddressUnlockCondition::new(alias_1_address),
))?
))
.replace_unlock_condition(UnlockCondition::GovernorAddress(GovernorAddressUnlockCondition::new(
alias_1_address,
)))?
)))
.finish_output(token_supply)?,
];

Expand All @@ -146,10 +146,10 @@ async fn main() -> Result<()> {
.with_state_metadata(vec![3, 2, 1])
.replace_unlock_condition(UnlockCondition::StateControllerAddress(
StateControllerAddressUnlockCondition::new(alias_1_address),
))?
))
.replace_unlock_condition(UnlockCondition::GovernorAddress(GovernorAddressUnlockCondition::new(
alias_1_address,
)))?
)))
.finish_output(token_supply)?,
];

Expand All @@ -175,10 +175,10 @@ async fn main() -> Result<()> {
.with_state_metadata(vec![2, 1, 3])
.replace_unlock_condition(UnlockCondition::StateControllerAddress(
StateControllerAddressUnlockCondition::new(alias_1_address),
))?
))
.replace_unlock_condition(UnlockCondition::GovernorAddress(GovernorAddressUnlockCondition::new(
alias_1_address,
)))?
)))
.finish_output(token_supply)?,
];

Expand Down
11 changes: 11 additions & 0 deletions types/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Security -->

## 1.0.0-rc.7 - 2023-XX-XX

### Changed

- `{AliasOutputBuilder, BasicOutputBuilder, FoundryOutputBuilder, NftOutputBuilder}::{replace_unlock_condition, replace_feature, replace_immutable_feature}`
now inserts the given element if it wasn't already present instead of returning an error;

### Removed

- `Error::CannotReplaceMissingField`;

## 1.0.0-rc.6 - 2023-02-14

### Changed
Expand Down
2 changes: 0 additions & 2 deletions types/src/block/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use crate::block::{
#[derive(Debug, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum Error {
CannotReplaceMissingField,
ConsumedAmountOverflow,
ConsumedNativeTokensAmountOverflow,
CreatedAmountOverflow,
Expand Down Expand Up @@ -131,7 +130,6 @@ impl std::error::Error for Error {}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::CannotReplaceMissingField => write!(f, "cannot replace missing field"),
Self::ConsumedAmountOverflow => write!(f, "consumed amount overflow"),
Self::ConsumedNativeTokensAmountOverflow => write!(f, "consumed native tokens amount overflow"),
Self::CreatedAmountOverflow => write!(f, "created amount overflow"),
Expand Down
18 changes: 9 additions & 9 deletions types/src/block/output/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,16 @@ impl AliasOutputBuilder {
}

///
pub fn replace_unlock_condition(mut self, unlock_condition: UnlockCondition) -> Result<Self, Error> {
pub fn replace_unlock_condition(mut self, unlock_condition: UnlockCondition) -> Self {
match self
.unlock_conditions
.iter_mut()
.find(|u| u.kind() == unlock_condition.kind())
{
Some(u) => *u = unlock_condition,
None => return Err(Error::CannotReplaceMissingField),
None => self.unlock_conditions.push(unlock_condition),
}
Ok(self)
self
}

///
Expand All @@ -196,12 +196,12 @@ impl AliasOutputBuilder {
}

///
pub fn replace_feature(mut self, feature: Feature) -> Result<Self, Error> {
pub fn replace_feature(mut self, feature: Feature) -> Self {
match self.features.iter_mut().find(|f| f.kind() == feature.kind()) {
Some(f) => *f = feature,
None => return Err(Error::CannotReplaceMissingField),
None => self.features.push(feature),
}
Ok(self)
self
}

///
Expand All @@ -219,16 +219,16 @@ impl AliasOutputBuilder {
}

///
pub fn replace_immutable_feature(mut self, immutable_feature: Feature) -> Result<Self, Error> {
pub fn replace_immutable_feature(mut self, immutable_feature: Feature) -> Self {
match self
.immutable_features
.iter_mut()
.find(|f| f.kind() == immutable_feature.kind())
{
Some(f) => *f = immutable_feature,
None => return Err(Error::CannotReplaceMissingField),
None => self.immutable_features.push(immutable_feature),
}
Ok(self)
self
}

///
Expand Down
12 changes: 6 additions & 6 deletions types/src/block/output/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,16 @@ impl BasicOutputBuilder {
}

///
pub fn replace_unlock_condition(mut self, unlock_condition: UnlockCondition) -> Result<Self, Error> {
pub fn replace_unlock_condition(mut self, unlock_condition: UnlockCondition) -> Self {
match self
.unlock_conditions
.iter_mut()
.find(|u| u.kind() == unlock_condition.kind())
{
Some(u) => *u = unlock_condition,
None => return Err(Error::CannotReplaceMissingField),
None => self.unlock_conditions.push(unlock_condition),
}
Ok(self)
self
}

///
Expand All @@ -122,12 +122,12 @@ impl BasicOutputBuilder {
}

///
pub fn replace_feature(mut self, feature: Feature) -> Result<Self, Error> {
pub fn replace_feature(mut self, feature: Feature) -> Self {
match self.features.iter_mut().find(|f| f.kind() == feature.kind()) {
Some(f) => *f = feature,
None => return Err(Error::CannotReplaceMissingField),
None => self.features.push(feature),
}
Ok(self)
self
}

///
Expand Down
18 changes: 9 additions & 9 deletions types/src/block/output/foundry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,16 @@ impl FoundryOutputBuilder {
}

///
pub fn replace_unlock_condition(mut self, unlock_condition: UnlockCondition) -> Result<Self, Error> {
pub fn replace_unlock_condition(mut self, unlock_condition: UnlockCondition) -> Self {
match self
.unlock_conditions
.iter_mut()
.find(|u| u.kind() == unlock_condition.kind())
{
Some(u) => *u = unlock_condition,
None => return Err(Error::CannotReplaceMissingField),
None => self.unlock_conditions.push(unlock_condition),
}
Ok(self)
self
}

///
Expand All @@ -154,12 +154,12 @@ impl FoundryOutputBuilder {
}

///
pub fn replace_feature(mut self, feature: Feature) -> Result<Self, Error> {
pub fn replace_feature(mut self, feature: Feature) -> Self {
match self.features.iter_mut().find(|f| f.kind() == feature.kind()) {
Some(f) => *f = feature,
None => return Err(Error::CannotReplaceMissingField),
None => self.features.push(feature),
}
Ok(self)
self
}

///
Expand All @@ -177,16 +177,16 @@ impl FoundryOutputBuilder {
}

///
pub fn replace_immutable_feature(mut self, immutable_feature: Feature) -> Result<Self, Error> {
pub fn replace_immutable_feature(mut self, immutable_feature: Feature) -> Self {
match self
.immutable_features
.iter_mut()
.find(|f| f.kind() == immutable_feature.kind())
{
Some(f) => *f = immutable_feature,
None => return Err(Error::CannotReplaceMissingField),
None => self.immutable_features.push(immutable_feature),
}
Ok(self)
self
}

///
Expand Down
18 changes: 9 additions & 9 deletions types/src/block/output/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,16 @@ impl NftOutputBuilder {
}

///
pub fn replace_unlock_condition(mut self, unlock_condition: UnlockCondition) -> Result<Self, Error> {
pub fn replace_unlock_condition(mut self, unlock_condition: UnlockCondition) -> Self {
match self
.unlock_conditions
.iter_mut()
.find(|u| u.kind() == unlock_condition.kind())
{
Some(u) => *u = unlock_condition,
None => return Err(Error::CannotReplaceMissingField),
None => self.unlock_conditions.push(unlock_condition),
}
Ok(self)
self
}

///
Expand All @@ -136,12 +136,12 @@ impl NftOutputBuilder {
}

///
pub fn replace_feature(mut self, feature: Feature) -> Result<Self, Error> {
pub fn replace_feature(mut self, feature: Feature) -> Self {
match self.features.iter_mut().find(|f| f.kind() == feature.kind()) {
Some(f) => *f = feature,
None => return Err(Error::CannotReplaceMissingField),
None => self.features.push(feature),
}
Ok(self)
self
}

///
Expand All @@ -159,16 +159,16 @@ impl NftOutputBuilder {
}

///
pub fn replace_immutable_feature(mut self, immutable_feature: Feature) -> Result<Self, Error> {
pub fn replace_immutable_feature(mut self, immutable_feature: Feature) -> Self {
match self
.immutable_features
.iter_mut()
.find(|f| f.kind() == immutable_feature.kind())
{
Some(f) => *f = immutable_feature,
None => return Err(Error::CannotReplaceMissingField),
None => self.immutable_features.push(immutable_feature),
}
Ok(self)
self
}

///
Expand Down

0 comments on commit e8422a8

Please sign in to comment.