1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#![cfg(feature = "std")]

use fuel_core_client::client::schema::resource::Resource as ClientResource;

use crate::{coin::Coin, message::Message};

#[derive(Debug)]
pub enum Resource {
    Coin(Coin),
    Message(Message),
}

impl TryFrom<ClientResource> for Resource {
    type Error = std::io::Error;

    fn try_from(client_resource: ClientResource) -> Result<Self, Self::Error> {
        match client_resource {
            ClientResource::Coin(coin) => Ok(Resource::Coin(coin.into())),
            ClientResource::Message(message) => Ok(Resource::Message(message.into())),
            ClientResource::Unknown => Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Got unknown `ClientResource`",
            )),
        }
    }
}

impl Resource {
    pub fn amount(&self) -> u64 {
        match self {
            Resource::Coin(coin) => coin.amount,
            Resource::Message(message) => message.amount,
        }
    }
}