diff --git a/src/mappers.rs b/src/mappers.rs index 0008d0b..cc6fbf6 100644 --- a/src/mappers.rs +++ b/src/mappers.rs @@ -284,10 +284,7 @@ where } /// lowercase the input and pass it to the next mapper. -pub fn lowercase(inner: M) -> Lowercase -where - M: Mapper<[u8]>, -{ +pub fn lowercase(inner: M) -> Lowercase { Lowercase(inner) } /// The `Lowercase` mapper returned by [lowercase()](fn.lowercase.html) @@ -328,6 +325,31 @@ impl fmt::Debug for MapFn { } } +/// inspect the input and pass it to the next mapper. +/// +/// This logs the value as it passes it to the next mapper unchanged. Can be +/// useful when troubleshooting why a matcher may not be working as intended. +pub fn inspect(inner: M) -> Inspect { + Inspect(inner) +} +/// The `Inspect` mapper returned by [inspect()](fn.inspect.html) +#[derive(Debug)] +pub struct Inspect(M); +impl Mapper for Inspect +where + IN: fmt::Debug + ?Sized, + M: Mapper, + M::Out: fmt::Debug, +{ + type Out = M::Out; + + fn map(&mut self, input: &IN) -> M::Out { + let output = self.0.map(input); + log::debug!("{:?}.map({:?}) == {:?}", self.0, input, output); + output + } +} + #[cfg(test)] mod tests { use super::*; @@ -445,4 +467,12 @@ mod tests { assert_eq!(true, c.map(&0)); assert_eq!(false, c.map(&11)); } + + #[test] + fn test_inspect() { + let _ = pretty_env_logger::try_init(); + let mut c = inspect(lowercase(matches("^foobar$"))); + assert_eq!(true, c.map("Foobar")); + assert_eq!(false, c.map("Foobar1")); + } }