falco_event/types/format/
option.rs

1use std::fmt::{Debug, Display, Formatter, LowerHex, Octal};
2
3/// A formatter for `Option<T>` types.
4///
5/// It formats `Some(value)` as the value itself, and `None` as "NULL".
6pub struct OptionFormatter<T>(pub Option<T>);
7
8impl<T: Display> Display for OptionFormatter<T> {
9    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
10        match &self.0 {
11            Some(val) => Display::fmt(val, f),
12            None => write!(f, "NULL"),
13        }
14    }
15}
16
17impl<T: Debug> Debug for OptionFormatter<T> {
18    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
19        match &self.0 {
20            Some(val) => Debug::fmt(val, f),
21            None => write!(f, "NULL"),
22        }
23    }
24}
25
26impl<T: LowerHex> LowerHex for OptionFormatter<T> {
27    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
28        match &self.0 {
29            Some(val) => LowerHex::fmt(val, f),
30            None => write!(f, "NULL"),
31        }
32    }
33}
34
35impl<T: Octal> Octal for OptionFormatter<T> {
36    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
37        match &self.0 {
38            Some(val) => Octal::fmt(val, f),
39            None => write!(f, "NULL"),
40        }
41    }
42}