Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subscriber: impl fmt::Display for filter::Targets #2343

Merged
merged 2 commits into from
Oct 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions tracing-subscriber/src/filter/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::{
#[cfg(not(feature = "std"))]
use alloc::string::String;
use core::{
fmt,
iter::{Extend, FilterMap, FromIterator},
slice,
str::FromStr,
Expand Down Expand Up @@ -488,6 +489,20 @@ impl<'a> IntoIterator for &'a Targets {
}
}

impl fmt::Display for Targets {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut directives = self.0.directives();
if let Some(directive) = directives.next() {
write!(f, "{}", directive)?;
for directive in directives {
write!(f, ",{}", directive)?;
}
}

Ok(())
}
}

/// An owning iterator over the [target]-[level] pairs of a `Targets` filter.
///
/// This struct is created by the `IntoIterator` trait implementation of [`Targets`].
Expand Down Expand Up @@ -778,4 +793,42 @@ mod tests {
crate2=debug,crate3=trace,crate3::mod2::mod1=off",
);
}

/// Test that the `fmt::Display` implementation for `Targets` emits a string
/// that can itself be parsed as a `Targets`, and that the parsed `Targets`
/// is equivalent to the original one.
#[test]
fn display_roundtrips() {
fn test_roundtrip(s: &str) {
let filter = expect_parse(s);
// we don't assert that the display output is equivalent to the
// original parsed filter string, because the `Display` impl always
// uses lowercase level names and doesn't use the
// target-without-level shorthand syntax. while they may not be
// textually equivalent, though, they should still *parse* to the
// same filter.
let formatted = filter.to_string();
let filter2 = match dbg!(&formatted).parse::<Targets>() {
Ok(filter) => filter,
Err(e) => panic!(
"failed to parse formatted filter string {:?}: {}",
formatted, e
),
};
assert_eq!(filter, filter2);
}

test_roundtrip("crate1::mod1=error,crate1::mod2,crate2=debug,crate3=off");
test_roundtrip(
"crate1::mod1=ERROR,crate1::mod2=WARN,crate1::mod2::mod3=INFO,\
crate2=DEBUG,crate3=TRACE,crate3::mod2::mod1=OFF",
);
test_roundtrip(
"crate1::mod1=error,crate1::mod2=warn,crate1::mod2::mod3=info,\
crate2=debug,crate3=trace,crate3::mod2::mod1=off",
);
test_roundtrip("crate1::mod1,crate1::mod2,info");
test_roundtrip("crate1");
test_roundtrip("info");
}
}