drm-fourcc 1.0.1

Provides an enum with every valid Direct Rendering Manager (DRM) format fourcc
//! [`DrmFormat`] is an enum representing every pixel format supported by DRM
//! (as of kernel version 5.8.0).
//!
//! A [fourcc][fourcc_wiki] is four bytes of ascii representing some data format. This enum contains
//! every fourcc representing a pixel format supported by [DRM][drm_wiki], the Linux Direct
//! Rendering Manager.
//!
//! To get the bytes of the fourcc representing the format, cast to `u32`.
//!
//! ```
//! # use drm_fourcc::DrmFormat;
//! assert_eq!(DrmFormat::Xrgb8888 as u32, 875713112);
//! ```
//!
//! To get the string form of the fourcc, use [`DrmFormat::string_form`].
//!
//! ```
//! # use drm_fourcc::DrmFormat;
//! assert_eq!(DrmFormat::Xrgb8888.string_form(), "XR24");
//! ```
//!
//! The enum is autogenerated from the [canonical list][canonical] in the Linux source code.
//!
//! [fourcc_wiki]: https://en.wikipedia.org/wiki/FourCC
//! [drm_wiki]: https://en.wikipedia.org/wiki/Direct_Rendering_Managerz
//! [canonical]: https://github.com/torvalds/linux/blame/master/include/uapi/drm/drm_fourcc.h

use std::fmt;
use std::fmt::{Debug, Display, Formatter};

pub use as_enum::DrmFormat;

mod as_enum;
mod consts;

impl DrmFormat {
    /// Get the string representation of the format's fourcc.
    pub fn string_form(&self) -> String {
        String::from_utf8((*self as u32).to_le_bytes().to_vec()).expect("Fourcc must be ascii")
    }
}

impl Debug for DrmFormat {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "DrmFormat({})", self.string_form())
    }
}

impl Display for DrmFormat {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "DrmFormat({})", self.string_form())
    }
}

#[cfg(test)]
pub mod tests {
    use super::*;

    #[test]
    fn a_specific_var_has_correct_value() {
        assert_eq!(consts::DRM_FOURCC_AYUV, 1448433985);
    }

    #[test]
    fn enum_member_casts_to_const() {
        assert_eq!(
            DrmFormat::Xrgb8888 as u32,
            consts::DRM_FOURCC_XRGB8888 as u32
        );
    }

    #[test]
    fn enum_member_has_correct_string_format() {
        assert_eq!(DrmFormat::Xrgb8888.string_form(), "XR24");
    }
}