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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#[cfg(not(feature = "host-mock"))]
use super::externs::{_get_account_name, _get_account_name_length, _is_owner, _set_account_name};
#[cfg(not(feature = "host-mock"))]
use std::str;
#[cfg(feature = "host-mock")]
pub static mut NAME: Option<String> = None;
#[cfg(feature = "host-mock")]
pub static mut OWNER: bool = false;
#[cfg(not(feature = "host-mock"))]
pub fn get_name(key: Vec<u8>) -> String {
let len = unsafe { _get_account_name_length(key.as_ptr(), key.len()) };
let mut val = Vec::with_capacity(len as usize);
unsafe { val.set_len(len as usize) };
unsafe { _get_account_name(key.as_ptr(), key.len(), val.as_mut_ptr()) };
str::from_utf8(&val).unwrap().to_owned()
}
#[cfg(feature = "host-mock")]
pub fn get_name(_key: Vec<u8>) -> String {
unsafe {
match NAME.clone() {
Some(name) => name,
None => "".to_string(),
}
}
}
#[cfg(not(feature = "host-mock"))]
pub fn set_name(key: Vec<u8>, name: String) {
let val = name.into_bytes();
unsafe { _set_account_name(key.as_ptr(), key.len(), val.as_ptr(), val.len()) };
}
#[cfg(feature = "host-mock")]
pub fn set_name(_key: Vec<u8>, name: String) {
unsafe {
NAME = Some(name.clone());
}
}
#[cfg(not(feature = "host-mock"))]
pub fn is_owner(key: Vec<u8>) -> bool {
unsafe { _is_owner(key.as_ptr(), key.len()) }
}
#[cfg(feature = "host-mock")]
pub fn is_owner(_key: Vec<u8>) -> bool {
unsafe { OWNER }
}
#[cfg(test)]
#[cfg(feature = "host-mock")]
mod tests {
use super::*;
#[test]
fn test_is_owner_true() {
unsafe {
OWNER = true;
}
assert_eq!(is_owner(vec![]), true);
}
#[test]
fn test_get_name_set() {
set_name(vec![], "asdf".to_string());
assert_eq!(get_name(vec![]), "asdf".to_string());
}
}