Skip to content

Commit

Permalink
FourCC: Improve debug prints
Browse files Browse the repository at this point in the history
  • Loading branch information
dcz-self committed Oct 6, 2024
1 parent 30f6dba commit 6cc33d8
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/format/fourcc.rs
Original file line number Diff line number Diff line change
@@ -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],
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -65,3 +79,19 @@ impl From<FourCC> 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)");
}
}

0 comments on commit 6cc33d8

Please sign in to comment.