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
use std::borrow::Cow;
const TX_FUNC_NAME_UTF8_ERROR: &str = "error converting function name to utf-8";
#[derive(Default, Clone, PartialEq, Eq, Debug)]
pub struct TxFunctionName(Cow<'static, str>);
impl From<String> for TxFunctionName {
fn from(value: String) -> Self {
TxFunctionName(value.into())
}
}
impl From<&str> for TxFunctionName {
fn from(value: &str) -> Self {
TxFunctionName(String::from(value).into())
}
}
impl From<Vec<u8>> for TxFunctionName {
fn from(value: Vec<u8>) -> Self {
TxFunctionName(
String::from_utf8(value)
.expect(TX_FUNC_NAME_UTF8_ERROR)
.into(),
)
}
}
impl From<&[u8]> for TxFunctionName {
fn from(value: &[u8]) -> Self {
value.to_vec().into()
}
}
impl From<&Vec<u8>> for TxFunctionName {
fn from(value: &Vec<u8>) -> Self {
value.clone().into()
}
}
impl TxFunctionName {
pub const fn from_static(name: &'static str) -> Self {
TxFunctionName(Cow::Borrowed(name))
}
pub const EMPTY: TxFunctionName = TxFunctionName::from_static("");
pub const INIT: TxFunctionName = TxFunctionName::from_static("init");
pub const CALLBACK: TxFunctionName = TxFunctionName::from_static("callBack");
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn into_string(self) -> String {
self.0.into_owned()
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl core::fmt::Display for TxFunctionName {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.0.fmt(f)
}
}