Skip to content

Commit 1d47c38

Browse files
develatterestk
authored andcommitted
fix(pattern/parser): allow '_' in name identifiers
Add support for the '_' character in the parser's "name" production so identifiers like "thread_id" parse correctly. This updates the pattern parser to treat underscore as a valid name character. Changes: - Modified src/encode/pattern/parser.rs to accept '_' in names. - Updated/added tests related to thread_id and name parsing. No breaking API changes. Related to #431.
1 parent ee27af5 commit 1d47c38

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/encode/pattern/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,24 @@ mod tests {
871871
.unwrap();
872872
}
873873

874+
#[test]
875+
#[cfg(feature = "simple_writer")]
876+
fn thread_and_thread_id() {
877+
thread::Builder::new()
878+
.name("foobar".to_string())
879+
.spawn(|| {
880+
let pw = PatternEncoder::new("{thread}:{thread_id}");
881+
let mut buf = vec![];
882+
pw.encode(&mut SimpleWriter(&mut buf), &Record::builder().build())
883+
.unwrap();
884+
let expected = format!("{}:{}", thread::current().name().unwrap_or("unnamed"), thread_id::get());
885+
assert_eq!(buf, expected.as_bytes());
886+
})
887+
.unwrap()
888+
.join()
889+
.unwrap();
890+
}
891+
874892
#[test]
875893
#[cfg(feature = "simple_writer")]
876894
fn process_id() {

src/encode/pattern/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl<'a> Parser<'a> {
8686

8787
loop {
8888
match self.it.peek() {
89-
Some(&(_, ch)) if ch.is_alphanumeric() => {
89+
Some(&(_, ch)) if ch.is_alphanumeric() || ch == '_' => {
9090
self.it.next();
9191
}
9292
Some(&(end, _)) => return &self.pattern[start..end],

0 commit comments

Comments
 (0)