multiversx_chain_vm/host/context/
tx_input_function.rs1use std::borrow::Cow;
2
3const TX_FUNC_NAME_UTF8_ERROR: &str = "error converting function name to utf-8";
4
5#[derive(Default, Clone, PartialEq, Eq, Debug)]
9pub struct TxFunctionName(Cow<'static, str>);
10
11impl From<String> for TxFunctionName {
12 fn from(value: String) -> Self {
13 TxFunctionName(value.into())
14 }
15}
16
17impl From<&str> for TxFunctionName {
18 fn from(value: &str) -> Self {
19 TxFunctionName(String::from(value).into())
20 }
21}
22
23impl From<Vec<u8>> for TxFunctionName {
24 fn from(value: Vec<u8>) -> Self {
25 TxFunctionName(
26 String::from_utf8(value)
27 .expect(TX_FUNC_NAME_UTF8_ERROR)
28 .into(),
29 )
30 }
31}
32
33impl From<&[u8]> for TxFunctionName {
34 fn from(value: &[u8]) -> Self {
35 value.to_vec().into()
36 }
37}
38
39impl From<&Vec<u8>> for TxFunctionName {
40 fn from(value: &Vec<u8>) -> Self {
41 value.clone().into()
42 }
43}
44
45impl TxFunctionName {
46 pub const fn from_static(name: &'static str) -> Self {
47 TxFunctionName(Cow::Borrowed(name))
48 }
49
50 pub const EMPTY: TxFunctionName = TxFunctionName::from_static("");
52
53 pub const INIT: TxFunctionName = TxFunctionName::from_static("init");
55
56 pub const UPGRADE: TxFunctionName = TxFunctionName::from_static("upgrade");
59
60 pub const CALLBACK: TxFunctionName = TxFunctionName::from_static("callBack");
62
63 pub const WHITEBOX_INIT: TxFunctionName = TxFunctionName::from_static("<whitebox-init>");
65
66 pub const WHITEBOX_CALL: TxFunctionName = TxFunctionName::from_static("<whitebox-call>");
68
69 pub const WHITEBOX_QUERY: TxFunctionName = TxFunctionName::from_static("<whitebox-query>");
71
72 pub const WHITEBOX_LEGACY: TxFunctionName = TxFunctionName::from_static("<whitebox-legacy>");
74
75 pub fn is_empty(&self) -> bool {
76 self.0.is_empty()
77 }
78
79 pub fn into_string(self) -> String {
80 self.0.into_owned()
81 }
82
83 pub fn into_bytes(self) -> Vec<u8> {
84 self.into_string().into_bytes()
85 }
86
87 pub fn to_bytes(&self) -> Vec<u8> {
88 self.0.as_bytes().to_vec()
89 }
90
91 pub fn as_str(&self) -> &str {
92 &self.0
93 }
94}
95
96impl core::fmt::Display for TxFunctionName {
97 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
98 self.0.fmt(f)
99 }
100}