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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
*
* Hedera Rust SDK
*
* Copyright (C) 2022 - 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
use hedera_proto::services;
use crate::{
FromProtobuf,
ToProtobuf,
};
/// Possible token types.
///
/// Apart from fungible and non-fungible, tokens can have either a common or
/// unique representation.
///
/// Only `FungibleCommon` and `NonFungibleUnique` are supported right now. More
/// may be added in the future.
///
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
#[repr(C)]
pub enum TokenType {
/// Interchangeable value with one another, where any quantity of them has the same value as
/// another equal quantity if they are in the same class. Share a single set of properties, not
/// distinct from one another. Simply represented as a balance or quantity to a given Hedera
/// account.
FungibleCommon = 0,
/// Unique, not interchangeable with other tokens of the same type as they typically have
/// different values. Individually traced and can carry unique properties (e.g. serial number).
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 hedera_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(())
}
}