Skip to content

Commit bd772aa

Browse files
committed
Split pattern template parser from spdlog-macros to spdlog-internal
1 parent a655dfd commit bd772aa

File tree

14 files changed

+949
-778
lines changed

14 files changed

+949
-778
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
resolver = "2"
33
members = [
44
"spdlog",
5+
"spdlog-internal",
56
"spdlog-macros",
67
]

spdlog-internal/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "spdlog-internal"
3+
version = "0.1.0"
4+
edition = "2021"
5+
rust-version = "1.56"
6+
7+
[dependencies]
8+
nom = "7.1.3"
9+
strum = { version = "0.24.1", features = ["derive"] }
10+
strum_macros = "0.24.3"
11+
thiserror = "1.0.40"

spdlog-internal/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
pub mod pattern_parser;
2+
3+
#[macro_export]
4+
macro_rules! impossible {
5+
( $dbg_lit:literal, $($fmt_arg:expr),* ) => {
6+
panic!(
7+
"this should not happen, please open an issue on 'spdlog-rs' Bug Tracker\n\nsource: {}\ndebug:{}",
8+
format!("{}:{}", file!(), line!()),
9+
format!($dbg_lit, $($fmt_arg),*),
10+
)
11+
};
12+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
use std::fmt::{self, Display};
2+
3+
use nom::error::Error as NomError;
4+
use thiserror::Error;
5+
6+
use super::PatternKind;
7+
use crate::impossible;
8+
9+
#[derive(Error, Debug)]
10+
pub enum Error {
11+
ConflictName {
12+
existing: PatternKind<()>,
13+
incoming: PatternKind<()>,
14+
},
15+
Template(TemplateError),
16+
Parse(NomError<String>),
17+
}
18+
19+
impl Display for Error {
20+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21+
match self {
22+
Error::ConflictName { existing, incoming } => match (existing, incoming) {
23+
(PatternKind::BuiltIn(_), PatternKind::Custom { .. }) => {
24+
write!(
25+
f,
26+
"'{}' is already a built-in pattern, please try another name",
27+
existing.placeholder()
28+
)
29+
}
30+
(PatternKind::Custom { .. }, PatternKind::Custom { .. }) => {
31+
write!(
32+
f,
33+
"the constructor of custom pattern '{}' is specified more than once",
34+
existing.placeholder()
35+
)
36+
}
37+
(_, PatternKind::BuiltIn { .. }) => {
38+
impossible!("{}", self)
39+
}
40+
},
41+
Error::Template(err) => {
42+
write!(f, "template ill-format: {}", err)
43+
}
44+
Error::Parse(err) => {
45+
write!(f, "failed to parse template string: {}", err)
46+
}
47+
}
48+
}
49+
}
50+
51+
#[derive(Error, Debug)]
52+
pub enum TemplateError {
53+
WrongPatternKindReference {
54+
is_builtin_as_custom: bool,
55+
placeholder: String,
56+
},
57+
UnknownPatternReference {
58+
is_custom: bool,
59+
placeholder: String,
60+
},
61+
MultipleStyleRange,
62+
}
63+
64+
impl Display for TemplateError {
65+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66+
match self {
67+
TemplateError::WrongPatternKindReference {
68+
is_builtin_as_custom,
69+
placeholder,
70+
} => {
71+
if *is_builtin_as_custom {
72+
write!(
73+
f,
74+
"'{}' is a built-in pattern, it cannot be used as a custom pattern. try to replace it with `{{{}}}`",
75+
placeholder, placeholder
76+
)
77+
} else {
78+
write!(
79+
f,
80+
"'{}' is a custom pattern, it cannot be used as a built-in pattern. try to replace it with `{{${}}}`",
81+
placeholder, placeholder
82+
)
83+
}
84+
}
85+
TemplateError::UnknownPatternReference {
86+
is_custom,
87+
placeholder,
88+
} => {
89+
if *is_custom {
90+
write!(
91+
f,
92+
"the constructor of custom pattern '{}' is not specified",
93+
placeholder
94+
)
95+
} else {
96+
write!(f, "no built-in pattern named '{}'", placeholder)
97+
}
98+
}
99+
TemplateError::MultipleStyleRange => {
100+
write!(f, "multiple style ranges are not currently supported")
101+
}
102+
}
103+
}
104+
}
105+
106+
pub type Result<T> = std::result::Result<T, Error>;
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
use std::borrow::Cow;
2+
3+
use strum::IntoEnumIterator;
4+
use strum_macros::{EnumDiscriminants, EnumIter, EnumString, IntoStaticStr};
5+
6+
pub mod error;
7+
mod helper;
8+
pub mod parse;
9+
mod registry;
10+
11+
pub use error::{Error, Result};
12+
pub use registry::PatternRegistry;
13+
14+
#[derive(
15+
Clone, Copy, Debug, Eq, PartialEq, IntoStaticStr, EnumDiscriminants, EnumIter, EnumString,
16+
)]
17+
#[strum_discriminants(derive(IntoStaticStr))]
18+
pub enum BuiltInFormatterInner {
19+
#[strum(serialize = "weekday_name")]
20+
AbbrWeekdayName,
21+
#[strum(serialize = "weekday_name_full")]
22+
WeekdayName,
23+
#[strum(serialize = "month_name")]
24+
AbbrMonthName,
25+
#[strum(serialize = "month_name_full")]
26+
MonthName,
27+
#[strum(serialize = "datetime")]
28+
FullDateTime,
29+
#[strum(serialize = "year_short")]
30+
ShortYear,
31+
#[strum(serialize = "year")]
32+
Year,
33+
#[strum(serialize = "date_short")]
34+
ShortDate,
35+
#[strum(serialize = "date")]
36+
Date,
37+
#[strum(serialize = "month")]
38+
Month,
39+
#[strum(serialize = "day")]
40+
Day,
41+
#[strum(serialize = "hour")]
42+
Hour,
43+
#[strum(serialize = "hour_12")]
44+
Hour12,
45+
#[strum(serialize = "minute")]
46+
Minute,
47+
#[strum(serialize = "second")]
48+
Second,
49+
#[strum(serialize = "millisecond")]
50+
Millisecond,
51+
#[strum(serialize = "microsecond")]
52+
Microsecond,
53+
#[strum(serialize = "nanosecond")]
54+
Nanosecond,
55+
#[strum(serialize = "am_pm")]
56+
AmPm,
57+
#[strum(serialize = "time_12")]
58+
Time12,
59+
#[strum(serialize = "time_short")]
60+
ShortTime,
61+
#[strum(serialize = "time")]
62+
Time,
63+
#[strum(serialize = "tz_offset")]
64+
TzOffset,
65+
#[strum(serialize = "unix_timestamp")]
66+
UnixTimestamp,
67+
#[strum(serialize = "full")]
68+
Full,
69+
#[strum(serialize = "level")]
70+
Level,
71+
#[strum(serialize = "level_short")]
72+
ShortLevel,
73+
#[strum(serialize = "source")]
74+
Source,
75+
#[strum(serialize = "file_name")]
76+
SourceFilename,
77+
#[strum(serialize = "file")]
78+
SourceFile,
79+
#[strum(serialize = "line")]
80+
SourceLine,
81+
#[strum(serialize = "column")]
82+
SourceColumn,
83+
#[strum(serialize = "module_path")]
84+
SourceModulePath,
85+
#[strum(serialize = "logger")]
86+
LoggerName,
87+
#[strum(serialize = "payload")]
88+
Payload,
89+
#[strum(serialize = "pid")]
90+
ProcessId,
91+
#[strum(serialize = "tid")]
92+
ThreadId,
93+
#[strum(serialize = "eol")]
94+
Eol,
95+
}
96+
97+
#[derive(Clone, Debug, Eq, PartialEq)]
98+
pub struct BuiltInFormatter(BuiltInFormatterInner);
99+
100+
impl BuiltInFormatter {
101+
pub fn iter() -> impl Iterator<Item = BuiltInFormatter> {
102+
BuiltInFormatterInner::iter().map(BuiltInFormatter)
103+
}
104+
105+
pub fn struct_name(&self) -> &'static str {
106+
BuiltInFormatterInnerDiscriminants::from(self.0).into()
107+
}
108+
109+
pub fn placeholder(&self) -> &'static str {
110+
self.0.into()
111+
}
112+
113+
pub fn inner(&self) -> BuiltInFormatterInner {
114+
self.0
115+
}
116+
}
117+
118+
#[derive(Clone, Debug, Eq, PartialEq)]
119+
pub enum PatternKind<F> {
120+
BuiltIn(BuiltInFormatter),
121+
Custom {
122+
placeholder: Cow<'static, str>,
123+
factory: F,
124+
},
125+
}
126+
127+
impl<F> PatternKind<F> {
128+
pub(crate) fn placeholder(&self) -> &str {
129+
match self {
130+
PatternKind::BuiltIn(f) => f.placeholder(),
131+
PatternKind::Custom { placeholder, .. } => placeholder,
132+
}
133+
}
134+
135+
pub(crate) fn to_factory_erased(&self) -> PatternKind<()> {
136+
match self {
137+
PatternKind::BuiltIn(b) => PatternKind::BuiltIn(b.clone()),
138+
PatternKind::Custom { placeholder, .. } => PatternKind::Custom {
139+
placeholder: placeholder.clone(),
140+
factory: (),
141+
},
142+
}
143+
}
144+
}

0 commit comments

Comments
 (0)