logid_core/logging/
msg.rs

1#[derive(Debug, Clone)]
2pub struct LogMsg {
3    msg: String,
4
5    #[cfg(feature = "fmt")]
6    fmt: Option<FmtMsg>,
7}
8
9pub const NO_MSG: Option<LogMsg> = None;
10
11#[cfg(feature = "fmt")]
12impl LogMsg {
13    pub fn get_fmt_data(&self) -> Option<&crate::serde_json::Value> {
14        self.fmt.as_ref().map(|f| f.get_data())
15    }
16}
17
18impl evident::event::Msg for LogMsg {}
19
20impl std::fmt::Display for LogMsg {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        #[cfg(feature = "fmt")]
23        if let Some(fmt) = &self.fmt {
24            return write!(f, "{}", fmt);
25        }
26
27        write!(f, "{}", self.msg)
28    }
29}
30
31impl From<String> for LogMsg {
32    fn from(value: String) -> Self {
33        LogMsg {
34            msg: value,
35            #[cfg(feature = "fmt")]
36            fmt: None,
37        }
38    }
39}
40
41impl From<&str> for LogMsg {
42    fn from(value: &str) -> Self {
43        LogMsg {
44            msg: value.to_string(),
45            #[cfg(feature = "fmt")]
46            fmt: None,
47        }
48    }
49}
50
51#[cfg(feature = "fmt")]
52impl
53    From<(
54        for<'a> fn(&'a crate::serde_json::Value) -> String,
55        crate::serde_json::Value,
56    )> for LogMsg
57{
58    fn from(
59        value: (
60            for<'a> fn(&'a crate::serde_json::Value) -> String,
61            crate::serde_json::Value,
62        ),
63    ) -> Self {
64        LogMsg {
65            msg: String::default(),
66            #[cfg(feature = "fmt")]
67            fmt: Some(FmtMsg {
68                func: value.0,
69                data: value.1,
70            }),
71        }
72    }
73}
74
75#[cfg(feature = "fmt")]
76impl From<FmtMsg> for LogMsg {
77    fn from(value: FmtMsg) -> Self {
78        LogMsg {
79            msg: String::default(),
80            #[cfg(feature = "fmt")]
81            fmt: Some(value),
82        }
83    }
84}
85
86impl From<LogMsg> for String {
87    fn from(value: LogMsg) -> Self {
88        #[cfg(feature = "fmt")]
89        if let Some(fmt) = value.fmt {
90            return fmt.to_string();
91        }
92
93        value.msg
94    }
95}
96
97impl PartialEq<String> for LogMsg {
98    fn eq(&self, other: &String) -> bool {
99        #[cfg(feature = "fmt")]
100        if let Some(fmt) = &self.fmt {
101            return fmt.to_string().eq(other);
102        }
103
104        self.msg.eq(other)
105    }
106}
107
108impl PartialEq<str> for LogMsg {
109    fn eq(&self, other: &str) -> bool {
110        #[cfg(feature = "fmt")]
111        if let Some(fmt) = &self.fmt {
112            return fmt.to_string().eq(other);
113        }
114
115        self.msg.eq(other)
116    }
117}
118
119#[cfg(feature = "fmt")]
120#[derive(Clone)]
121pub struct FmtMsg {
122    func: for<'a> fn(&'a crate::serde_json::Value) -> String,
123    data: crate::serde_json::Value,
124}
125
126#[cfg(feature = "fmt")]
127impl FmtMsg {
128    pub fn new(
129        func: for<'a> fn(&'a crate::serde_json::Value) -> String,
130        data: crate::serde_json::Value,
131    ) -> Self {
132        FmtMsg { func, data }
133    }
134
135    pub fn get_data(&self) -> &crate::serde_json::Value {
136        &self.data
137    }
138}
139
140#[cfg(feature = "fmt")]
141impl std::fmt::Debug for FmtMsg {
142    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
143        write!(f, "{}", (self.func)(&self.data))
144    }
145}
146
147#[cfg(feature = "fmt")]
148impl std::fmt::Display for FmtMsg {
149    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
150        write!(f, "{}", (self.func)(&self.data))
151    }
152}
153
154#[cfg(feature = "fmt")]
155impl PartialEq<FmtMsg> for FmtMsg {
156    fn eq(&self, other: &FmtMsg) -> bool {
157        (self.func)(&self.data) == (other.func)(&other.data)
158    }
159}
160
161#[cfg(feature = "fmt")]
162impl Eq for FmtMsg {}