deribit_base/utils/display.rs
1#[macro_export]
2/// Implements the `Display` trait for the specified types, formatting the output as pretty-printed JSON.
3///
4/// This macro generates an implementation of `std::fmt::Display` that serializes the object to a
5/// pretty-printed JSON string. If serialization fails, it displays an error message.
6///
7macro_rules! impl_json_display_pretty {
8 ($($t:ty),+) => {
9 $(
10 impl std::fmt::Display for $t {
11 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12 match serde_json::to_string_pretty(self) {
13 Ok(pretty_json) => write!(f, "{}", pretty_json),
14 Err(e) => write!(f, "Error serializing to JSON: {}", e),
15 }
16 }
17 }
18 )+
19 }
20}
21
22#[macro_export]
23/// Implements the `Debug` trait for the specified types, formatting the output as pretty-printed JSON.
24///
25/// This macro generates an implementation of `std::fmt::Debug` that serializes the object to a
26/// pretty-printed JSON string. If serialization fails, it displays an error message.
27///
28macro_rules! impl_json_debug_pretty {
29 ($($t:ty),+) => {
30 $(
31 impl std::fmt::Debug for $t {
32 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33 match serde_json::to_string_pretty(self) {
34 Ok(pretty_json) => write!(f, "{}", pretty_json),
35 Err(e) => write!(f, "Error serializing to JSON: {}", e),
36 }
37 }
38 }
39 )+
40 }
41}
42
43#[macro_export]
44/// Implements the `Debug` trait for the specified types, formatting the output as compact JSON.
45///
46/// This macro generates an implementation of `std::fmt::Debug` that serializes the object to a
47/// compact JSON string. If serialization fails, it displays an error message.
48///
49macro_rules! impl_json_debug {
50 ($($t:ty),+) => {
51 $(
52 impl std::fmt::Debug for $t {
53 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54 match serde_json::to_string(self) {
55 Ok(pretty_json) => write!(f, "{}", pretty_json),
56 Err(e) => write!(f, "Error serializing to JSON: {}", e),
57 }
58 }
59 }
60 )+
61 }
62}
63
64#[macro_export]
65/// Implements the `Display` trait for the specified types, formatting the output as compact JSON.
66///
67/// This macro generates an implementation of `std::fmt::Display` that serializes the object to a
68/// compact JSON string. If serialization fails, it displays an error message.
69///
70macro_rules! impl_json_display {
71 ($($t:ty),+) => {
72 $(
73 impl std::fmt::Display for $t {
74 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75 match serde_json::to_string(self) {
76 Ok(pretty_json) => write!(f, "{}", pretty_json),
77 Err(e) => write!(f, "Error serializing to JSON: {}", e),
78 }
79 }
80 }
81 )+
82 }
83}