Skip to content

Commit b77ccef

Browse files
authored
Merge pull request #409 from Thomasdezeeuw/log-as_str
Add Level::as_str
2 parents 469a441 + 186efed commit b77ccef

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

src/lib.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ impl FromStr for Level {
479479

480480
impl fmt::Display for Level {
481481
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
482-
fmt.pad(LOG_LEVEL_NAMES[*self as usize])
482+
fmt.pad(self.as_str())
483483
}
484484
}
485485

@@ -506,6 +506,13 @@ impl Level {
506506
pub fn to_level_filter(&self) -> LevelFilter {
507507
LevelFilter::from_usize(*self as usize).unwrap()
508508
}
509+
510+
/// Returns the string representation of the `Level`.
511+
///
512+
/// This returns the same string as the `fmt::Display` implementation.
513+
pub fn as_str(&self) -> &'static str {
514+
LOG_LEVEL_NAMES[*self as usize]
515+
}
509516
}
510517

511518
/// An enum representing the available verbosity level filters of the logger.
@@ -632,7 +639,7 @@ impl FromStr for LevelFilter {
632639

633640
impl fmt::Display for LevelFilter {
634641
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
635-
fmt.pad(LOG_LEVEL_NAMES[*self as usize])
642+
fmt.pad(self.as_str())
636643
}
637644
}
638645

@@ -661,6 +668,13 @@ impl LevelFilter {
661668
pub fn to_level(&self) -> Option<Level> {
662669
Level::from_usize(*self as usize)
663670
}
671+
672+
/// Returns the string representation of the `LevelFilter`.
673+
///
674+
/// This returns the same string as the `fmt::Display` implementation.
675+
pub fn as_str(&self) -> &'static str {
676+
LOG_LEVEL_NAMES[*self as usize]
677+
}
664678
}
665679

666680
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
@@ -1496,6 +1510,20 @@ mod tests {
14961510
}
14971511
}
14981512

1513+
#[test]
1514+
fn test_level_as_str() {
1515+
let tests = &[
1516+
(Level::Error, "ERROR"),
1517+
(Level::Warn, "WARN"),
1518+
(Level::Info, "INFO"),
1519+
(Level::Debug, "DEBUG"),
1520+
(Level::Trace, "TRACE"),
1521+
];
1522+
for (input, expected) in tests {
1523+
assert_eq!(*expected, input.as_str());
1524+
}
1525+
}
1526+
14991527
#[test]
15001528
fn test_level_show() {
15011529
assert_eq!("INFO", Level::Info.to_string());
@@ -1535,6 +1563,21 @@ mod tests {
15351563
assert_eq!(LevelFilter::Trace, Level::Trace.to_level_filter());
15361564
}
15371565

1566+
#[test]
1567+
fn test_level_filter_as_str() {
1568+
let tests = &[
1569+
(LevelFilter::Off, "OFF"),
1570+
(LevelFilter::Error, "ERROR"),
1571+
(LevelFilter::Warn, "WARN"),
1572+
(LevelFilter::Info, "INFO"),
1573+
(LevelFilter::Debug, "DEBUG"),
1574+
(LevelFilter::Trace, "TRACE"),
1575+
];
1576+
for (input, expected) in tests {
1577+
assert_eq!(*expected, input.as_str());
1578+
}
1579+
}
1580+
15381581
#[test]
15391582
#[cfg(feature = "std")]
15401583
fn test_error_trait() {

0 commit comments

Comments
 (0)