How to do a match statement with resource type?

82 Views Asked by At

I want to find out if a resource passed as a bucket to my method is a Fungible or non fungible resource (the action is different in each case)

I tried this:

let resource_man = bucket.resource_manager();
match resource_man.resource_type() {
  ResourceType::NonmFungible => {//do something here}
  ResourceType::Fungible => {//do another here}
}

But I get this error: error[E0533]: expected unit struct, unit variant or constant, found struct variant 'ResourceType::NonFungible'

How to work around this and getthe flow control I want?

1

There are 1 best solutions below

0
Yo. On

This should work:

let resource_man = bucket.resource_manager();
match resource_man.resource_type() {
   ResourceType::NonFungible { .. } => {//do something here},
   ResourceType::Fungible { .. } => {//do another here}
};

The types have their own fields which you have to match on: https://radixdlt.github.io/radixdlt-scrypto/scrypto/blueprints/resource/enum.ResourceType.html. The .. ignores these fields (I'm assuming you don't care about them).

Edit: to elaborate a bit more.. you could match on divisibility, for example:

let resource_man = bucket.resource_manager();
match resource_man.resource_type() {
   ResourceType::NonFungible { .. } => {//do something here},
   ResourceType::Fungible{ divisibility: 18 } => { // do something with divisibility 18 fungibles here},
   ResourceType::Fungible{ divisibility: 17 } => { // do something with divisibility 17 fungibles here},
   ResourceType::Fungible{ .. } => { // do something for the rest }
};