This repository was archived by the owner on Aug 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathmacros.rs
67 lines (62 loc) · 1.91 KB
/
macros.rs
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
macro_rules! concat_parts {
($parts_head:expr) => {
// `stringify!` will convert the expression *as it is* into a string.
format!(
"{}{}",
$crate::models::service::transactions::ID_SEPARATOR,
$parts_head
);
};
($parts_head:expr, $($parts_tail:expr),+) => {
// `stringify!` will convert the expression *as it is* into a string.
format!(
"{}{}{}",
$crate::models::service::transactions::ID_SEPARATOR,
$parts_head,
concat_parts!($($parts_tail),+)
);
};
}
macro_rules! create_id {
($tx_type:expr, $($parts:expr),+) => {
// `stringify!` will convert the expression *as it is* into a string.
format!("{}{}", $tx_type, concat_parts!($($parts),+));
};
}
macro_rules! bail {
($msg:literal $(,)?) => {
return Err($crate::api_error!($msg))
};
($fmt:expr, $($arg:tt)*) => {
return Err($crate::api_error!($fmt, $($arg)*))
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! api_error {
($msg:literal $(,)?) => {
// Handle $:literal as a special case to make cargo-expanded code more
// concise in the common case.
$crate::utils::errors::ApiError::new_from_message($msg)
};
($fmt:expr, $($arg:tt)*) => {
$crate::utils::errors::ApiError::new_from_message(format!($fmt, $($arg)*))
$crate::private::new_adhoc()
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! client_error {
($status_code:expr, $message:expr) => {
$crate::utils::errors::ApiError::new_from_message_with_code($status_code, format!($message))
};
}
macro_rules! to_hex_string {
($input:expr) => {{
let mut output = String::new();
for byte in $input.iter() {
output.push_str(&format!("{:02x?}", byte)) // uppercase x is for uppercase hex char.
}
format!("0x{}", output)
}};
}