Skip to main content

spdlog_internal/pattern_parser/
mod.rs

1use std::borrow::Cow;
2
3pub mod error;
4mod helper;
5pub mod parse;
6mod registry;
7
8pub use error::{Error, Result};
9pub use registry::{check_custom_pattern_names, PatternRegistry};
10
11macro_rules! for_builtin_formatters {
12    ( ( $($macro_params:tt)* ) => { $($macro_impl:tt)* }; ) => {
13        mod __private {
14            macro_rules! __callback {
15                ( $($macro_params)* ) => { $($macro_impl)* };
16            }
17            pub(crate) use __callback;
18        }
19        __private::__callback! {
20            AbbrWeekdayName => "weekday_name",
21            WeekdayName => "weekday_name_full",
22            AbbrMonthName => "month_name",
23            MonthName => "month_name_full",
24            FullDateTime => "datetime",
25            ShortYear => "year_short",
26            Year => "year",
27            ShortDate => "date_short",
28            Date => "date",
29            Month => "month",
30            Day => "day",
31            Hour => "hour",
32            Hour12 => "hour_12",
33            Minute => "minute",
34            Second => "second",
35            Millisecond => "millisecond",
36            Microsecond => "microsecond",
37            Nanosecond => "nanosecond",
38            AmPm => "am_pm",
39            Time12 => "time_12",
40            ShortTime => "time_short",
41            Time => "time",
42            TzOffset => "tz_offset",
43            UnixTimestamp => "unix_timestamp",
44            Full => "full",
45            Level => "level",
46            ShortLevel => "level_short",
47            Source => "source",
48            SourceFilename => "file_name",
49            SourceFile => "file",
50            SourceLine => "line",
51            SourceColumn => "column",
52            SourceModulePath => "module_path",
53            LoggerName => "logger",
54            Payload => "payload",
55            KV => "kv",
56            ProcessId => "pid",
57            ThreadId => "tid",
58            Eol => "eol",
59        }
60    };
61}
62
63for_builtin_formatters! {
64    ( $( $variant:ident => $serialize:literal ),+ $(,)? ) => {
65        #[derive(Clone, Copy, Debug, Eq, PartialEq)]
66        pub enum BuiltInFormatter {
67            $( $variant ),+
68        }
69
70        impl BuiltInFormatter {
71            pub fn iter() -> impl Iterator<Item = Self> {
72                [ $( Self::$variant ),+ ].into_iter()
73            }
74
75            #[must_use]
76            pub fn struct_name(&self) -> &'static str {
77                match self {
78                    $( Self::$variant => stringify!($variant), )+
79                }
80            }
81
82            #[must_use]
83            pub fn placeholder(&self) -> &'static str {
84                match self {
85                    $( Self::$variant => $serialize, )+
86                }
87            }
88        }
89    };
90}
91
92#[derive(Clone, Debug, Eq, PartialEq)]
93pub enum PatternKind<F> {
94    BuiltIn(BuiltInFormatter),
95    Custom {
96        placeholder: Cow<'static, str>,
97        factory: F,
98    },
99}
100
101impl<F> PatternKind<F> {
102    pub(crate) fn placeholder(&self) -> &str {
103        match self {
104            PatternKind::BuiltIn(f) => f.placeholder(),
105            PatternKind::Custom { placeholder, .. } => placeholder,
106        }
107    }
108
109    pub(crate) fn to_factory_erased(&self) -> PatternKind<()> {
110        match self {
111            PatternKind::BuiltIn(b) => PatternKind::BuiltIn(*b),
112            PatternKind::Custom { placeholder, .. } => PatternKind::Custom {
113                placeholder: placeholder.clone(),
114                factory: (),
115            },
116        }
117    }
118}