Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)");
}
}