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 tracing::info;
12/// use ig_client::impl_json_display;
13///
14/// #[derive(serde::Serialize)]
15/// struct MyStruct {
16///     field1: String,
17///     field2: i32,
18/// }
19///
20/// impl_json_display!(MyStruct);
21///
22/// let my_struct = MyStruct {
23///     field1: "value".to_string(),
24///     field2: 42,
25/// };
26///
27/// info!("{}", my_struct); // Outputs JSON representation
28/// ```
29macro_rules! impl_json_display {
30    ($($t:ty),+) => {
31        $(
32            impl std::fmt::Display for $t {
33                fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34                    match serde_json::to_string(self) {
35                        Ok(pretty_json) => write!(f, "{}", pretty_json),
36                        Err(e) => write!(f, "Error serializing to JSON: {}", e),
37                    }
38                }
39            }
40        )+
41    }
42}