we_cdk/macros/
converts.rs

1/// Converts a string representation of a number to an equivalent integer number
2///
3/// # Usage
4/// ```
5/// use we_cdk::*;
6///
7/// #[action]
8/// fn _constructor() {
9///     let result: Integer = parse_int!("31337");
10/// }
11/// ```
12#[macro_export]
13macro_rules! parse_int {
14    ($value:expr) => {{
15        let (error, result) = wevm::v0::bindings::parse_int($value.as_ptr(), $value.len());
16        error!(error);
17        result
18    }};
19}
20
21/// Converts a string representation of a boolean to an equivalent logical value
22///
23/// # Usage
24/// ```
25/// use we_cdk::*;
26///
27/// #[action]
28/// fn _constructor() {
29///     let result: Boolean = parse_bool!("true");
30/// }
31/// ```
32#[macro_export]
33macro_rules! parse_bool {
34    ($value:expr) => {{
35        let (error, result) = wevm::v0::bindings::parse_bool($value.as_ptr(), $value.len());
36        error!(error);
37        result
38    }};
39}
40
41/// Converts an integer to a byte array
42///
43/// # Usage
44/// ```
45/// use we_cdk::*;
46///
47/// #[action]
48/// fn _constructor() {
49///     let result: Binary = to_bytes!(31337);
50/// }
51/// ```
52#[macro_export]
53macro_rules! to_bytes {
54    ($value:expr) => {{
55        let (error, ptr, len) = wevm::v0::bindings::to_bytes($value);
56        error!(error);
57        core::slice::from_raw_parts(ptr, len)
58    }};
59}
60
61/// Converts a byte array to an integer number
62///
63/// # Usage
64/// ```
65/// use we_cdk::*;
66///
67/// #[action]
68/// fn _constructor() {
69///     let binary: Binary = to_bytes!(31337);
70///     let result: Integer = to_int!(binary);
71/// }
72/// ```
73#[macro_export]
74macro_rules! to_int {
75    ($value:expr) => {{
76        let (error, result) = wevm::v0::bindings::to_int($value.as_ptr(), $value.len());
77        error!(error);
78        result
79    }};
80}
81
82/// Converts a logical value to a string
83///
84/// # Usage
85/// ```
86/// use we_cdk::*;
87///
88/// #[action]
89/// fn _constructor() {
90///     let result: String = to_string_bool!(true);
91/// }
92/// ```
93#[macro_export]
94macro_rules! to_string_bool {
95    ($value:expr) => {{
96        let (error, ptr, len) = wevm::v0::bindings::to_string_bool($value);
97        error!(error);
98        let bytes = core::slice::from_raw_parts(ptr, len);
99        core::str::from_utf8_unchecked(bytes)
100    }};
101}
102
103/// Converts an integer to a string
104///
105/// # Usage
106/// ```
107/// use we_cdk::*;
108///
109/// #[action]
110/// fn _constructor() {
111///     let result: String = to_string_int!(31337);
112/// }
113/// ```
114#[macro_export]
115macro_rules! to_string_int {
116    ($value:expr) => {{
117        let (error, ptr, len) = wevm::v0::bindings::to_string_int($value);
118        error!(error);
119        let bytes = core::slice::from_raw_parts(ptr, len);
120        core::str::from_utf8_unchecked(bytes)
121    }};
122}
123
124/// Converts an binary to a string
125///
126/// # Usage
127/// ```
128/// use we_cdk::*;
129///
130/// #[action]
131/// fn _constructor() {
132///     let result: String = to_string_binary!(&[0, 1]);
133/// }
134/// ```
135#[macro_export]
136macro_rules! to_string_binary {
137    ($value:expr) => {{
138        core::str::from_utf8_unchecked($value)
139    }};
140}