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
use core::{
convert::TryFrom,
fmt::{self, Display, Formatter},
};
use crate::ApiError;
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum SystemContractType {
Mint,
ProofOfStake,
}
impl From<SystemContractType> for u32 {
fn from(system_contract_type: SystemContractType) -> u32 {
match system_contract_type {
SystemContractType::Mint => 0,
SystemContractType::ProofOfStake => 1,
}
}
}
#[doc(hidden)]
impl TryFrom<u32> for SystemContractType {
type Error = ApiError;
fn try_from(value: u32) -> Result<SystemContractType, Self::Error> {
match value {
0 => Ok(SystemContractType::Mint),
1 => Ok(SystemContractType::ProofOfStake),
_ => Err(ApiError::InvalidSystemContract),
}
}
}
impl Display for SystemContractType {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match *self {
SystemContractType::Mint => write!(f, "mint"),
SystemContractType::ProofOfStake => write!(f, "pos"),
}
}
}
#[cfg(test)]
mod tests {
use std::string::ToString;
use super::*;
#[test]
fn get_index_of_mint_contract() {
let index: u32 = SystemContractType::Mint.into();
assert_eq!(index, 0u32);
assert_eq!(SystemContractType::Mint.to_string(), "mint");
}
#[test]
fn get_index_of_pos_contract() {
let index: u32 = SystemContractType::ProofOfStake.into();
assert_eq!(index, 1u32);
assert_eq!(SystemContractType::ProofOfStake.to_string(), "pos");
}
#[test]
fn create_mint_variant_from_int() {
let mint = SystemContractType::try_from(0).ok().unwrap();
assert_eq!(mint, SystemContractType::Mint);
}
#[test]
fn create_pos_variant_from_int() {
let pos = SystemContractType::try_from(0).ok().unwrap();
assert_eq!(pos, SystemContractType::Mint);
}
#[test]
fn create_unknown_system_contract_variant() {
assert!(SystemContractType::try_from(2).is_err());
assert!(SystemContractType::try_from(3).is_err());
assert!(SystemContractType::try_from(10).is_err());
assert!(SystemContractType::try_from(u32::max_value()).is_err());
}
}