ig_client/utils/
display.rs

1#[macro_export]
2/// Implements the Display trait for a type by serializing it to JSON
3///
4/// This macro automatically implements the Display trait for one or more types
5/// by serializing them to JSON using serde_json. This is useful for debugging
6/// and logging purposes.
7///
8/// # Examples
9///
10/// ```
11/// use ig_client::impl_json_display;
12///
13/// #[derive(serde::Serialize)]
14/// struct MyStruct {
15///     field1: String,
16///     field2: i32,
17/// }
18///
19/// impl_json_display!(MyStruct);
20///
21/// let my_struct = MyStruct {
22///     field1: "value".to_string(),
23///     field2: 42,
24/// };
25///
26/// println!("{}", my_struct); // Outputs JSON representation
27/// ```
28macro_rules! impl_json_display {
29    ($($t:ty),+) => {
30        $(
31            impl std::fmt::Display for $t {
32                fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33                    match serde_json::to_string(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}