hiero_sdk/token/
token_type.rs1use hiero_sdk_proto::services;
4
5use crate::{
6 FromProtobuf,
7 ToProtobuf,
8};
9
10#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
19#[repr(C)]
20pub enum TokenType {
21 FungibleCommon = 0,
26
27 NonFungibleUnique = 1,
30}
31
32impl FromProtobuf<services::TokenType> for TokenType {
33 fn from_protobuf(pb: services::TokenType) -> crate::Result<Self> {
34 Ok(match pb {
35 services::TokenType::FungibleCommon => Self::FungibleCommon,
36 services::TokenType::NonFungibleUnique => Self::NonFungibleUnique,
37 })
38 }
39}
40
41impl ToProtobuf for TokenType {
42 type Protobuf = services::TokenType;
43
44 fn to_protobuf(&self) -> Self::Protobuf {
45 match self {
46 Self::FungibleCommon => Self::Protobuf::FungibleCommon,
47 Self::NonFungibleUnique => Self::Protobuf::NonFungibleUnique,
48 }
49 }
50}
51
52#[cfg(test)]
53mod tests {
54 use hiero_sdk_proto::services;
55
56 use crate::token::token_type::TokenType;
57 use crate::{
58 FromProtobuf,
59 ToProtobuf,
60 };
61
62 #[test]
63 fn it_can_convert_to_protobuf() -> anyhow::Result<()> {
64 let nft_token_type = TokenType::NonFungibleUnique;
65 let fungible_token_type = TokenType::FungibleCommon;
66
67 let nft_protobuf = nft_token_type.to_protobuf();
68 let fungible_protobuf = fungible_token_type.to_protobuf();
69
70 assert_eq!(nft_protobuf, services::TokenType::NonFungibleUnique);
71 assert_eq!(fungible_protobuf, services::TokenType::FungibleCommon);
72
73 Ok(())
74 }
75
76 #[test]
77 fn it_can_be_created_from_protobuf() -> anyhow::Result<()> {
78 let nft_protobuf = services::TokenType::NonFungibleUnique;
79 let fungible_protobuf = services::TokenType::FungibleCommon;
80
81 let nft_token_type = TokenType::from_protobuf(nft_protobuf)?;
82 let fungible_token_type = TokenType::from_protobuf(fungible_protobuf)?;
83
84 assert_eq!(nft_token_type, TokenType::NonFungibleUnique);
85 assert_eq!(fungible_token_type, TokenType::FungibleCommon);
86
87 Ok(())
88 }
89}