use hiero_sdk_proto::services;
use crate::{
FromProtobuf,
ToProtobuf,
};
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
#[repr(C)]
pub enum TokenType {
FungibleCommon = 0,
NonFungibleUnique = 1,
}
impl FromProtobuf<services::TokenType> for TokenType {
fn from_protobuf(pb: services::TokenType) -> crate::Result<Self> {
Ok(match pb {
services::TokenType::FungibleCommon => Self::FungibleCommon,
services::TokenType::NonFungibleUnique => Self::NonFungibleUnique,
})
}
}
impl ToProtobuf for TokenType {
type Protobuf = services::TokenType;
fn to_protobuf(&self) -> Self::Protobuf {
match self {
Self::FungibleCommon => Self::Protobuf::FungibleCommon,
Self::NonFungibleUnique => Self::Protobuf::NonFungibleUnique,
}
}
}
#[cfg(test)]
mod tests {
use hiero_sdk_proto::services;
use crate::token::token_type::TokenType;
use crate::{
FromProtobuf,
ToProtobuf,
};
#[test]
fn it_can_convert_to_protobuf() -> anyhow::Result<()> {
let nft_token_type = TokenType::NonFungibleUnique;
let fungible_token_type = TokenType::FungibleCommon;
let nft_protobuf = nft_token_type.to_protobuf();
let fungible_protobuf = fungible_token_type.to_protobuf();
assert_eq!(nft_protobuf, services::TokenType::NonFungibleUnique);
assert_eq!(fungible_protobuf, services::TokenType::FungibleCommon);
Ok(())
}
#[test]
fn it_can_be_created_from_protobuf() -> anyhow::Result<()> {
let nft_protobuf = services::TokenType::NonFungibleUnique;
let fungible_protobuf = services::TokenType::FungibleCommon;
let nft_token_type = TokenType::from_protobuf(nft_protobuf)?;
let fungible_token_type = TokenType::from_protobuf(fungible_protobuf)?;
assert_eq!(nft_token_type, TokenType::NonFungibleUnique);
assert_eq!(fungible_token_type, TokenType::FungibleCommon);
Ok(())
}
}