diff --git a/src/format/fourcc.rs b/src/format/fourcc.rs index ce8c88a..a7f00e7 100644 --- a/src/format/fourcc.rs +++ b/src/format/fourcc.rs @@ -1,6 +1,6 @@ use std::{fmt, str}; -#[derive(Debug, Default, Copy, Clone, Eq)] +#[derive(Default, Copy, Clone, Eq)] /// Four character code representing a pixelformat pub struct FourCC { pub repr: [u8; 4], @@ -38,6 +38,20 @@ impl FourCC { } } +impl fmt::Debug for FourCC { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let string = str::from_utf8(&self.repr); + if let Ok(string) = string { + write!(f, "FourCC(")?; + string.fmt(f)?; + write!(f, ")")?; + } else { + write!(f, "FourCC({:02x} {:02x} {:02x} {:02x})", self.repr[0], self.repr[1], self.repr[2], self.repr[3])?; + } + Ok(()) + } +} + impl fmt::Display for FourCC { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let string = str::from_utf8(&self.repr); @@ -65,3 +79,19 @@ impl From for u32 { Self::from_le_bytes(fourcc.repr) } } + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn debug_fourcc_string() { + assert_eq!(format!("{:?}", FourCC::new(b"MJPG")), "FourCC(\"MJPG\")"); + } + + #[test] + fn debug_fourcc_nonascii() { + assert_eq!(format!("{:?}", FourCC::new(&[0x01, 0xff, 0x20, 0xcd])), "FourCC(01 ff 20 cd)"); + } +} \ No newline at end of file