Skip to content

Commit 6cc33d8

Browse files
committed
FourCC: Improve debug prints
1 parent 30f6dba commit 6cc33d8

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/format/fourcc.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{fmt, str};
22

3-
#[derive(Debug, Default, Copy, Clone, Eq)]
3+
#[derive(Default, Copy, Clone, Eq)]
44
/// Four character code representing a pixelformat
55
pub struct FourCC {
66
pub repr: [u8; 4],
@@ -38,6 +38,20 @@ impl FourCC {
3838
}
3939
}
4040

41+
impl fmt::Debug for FourCC {
42+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43+
let string = str::from_utf8(&self.repr);
44+
if let Ok(string) = string {
45+
write!(f, "FourCC(")?;
46+
string.fmt(f)?;
47+
write!(f, ")")?;
48+
} else {
49+
write!(f, "FourCC({:02x} {:02x} {:02x} {:02x})", self.repr[0], self.repr[1], self.repr[2], self.repr[3])?;
50+
}
51+
Ok(())
52+
}
53+
}
54+
4155
impl fmt::Display for FourCC {
4256
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4357
let string = str::from_utf8(&self.repr);
@@ -65,3 +79,19 @@ impl From<FourCC> for u32 {
6579
Self::from_le_bytes(fourcc.repr)
6680
}
6781
}
82+
83+
84+
#[cfg(test)]
85+
mod tests {
86+
use super::*;
87+
88+
#[test]
89+
fn debug_fourcc_string() {
90+
assert_eq!(format!("{:?}", FourCC::new(b"MJPG")), "FourCC(\"MJPG\")");
91+
}
92+
93+
#[test]
94+
fn debug_fourcc_nonascii() {
95+
assert_eq!(format!("{:?}", FourCC::new(&[0x01, 0xff, 0x20, 0xcd])), "FourCC(01 ff 20 cd)");
96+
}
97+
}

0 commit comments

Comments
 (0)