use-xz 0.1.0

XZ labels and option metadata for RustUse
Documentation
#![forbid(unsafe_code)]
#![doc = include_str!("../README.md")]

//! XZ labels and option metadata for `RustUse`.

/// Common xz file extension.
pub const XZ_EXTENSION: &str = "xz";
/// Common xz-compressed tar extension.
pub const TAR_XZ_EXTENSION: &str = "tar.xz";
/// Common LZMA label.
pub const LZMA_LABEL: &str = "lzma";
/// Common xz-related extensions.
pub const XZ_EXTENSIONS: &[&str] = &["xz", "lzma", "txz", "tar.xz"];

/// XZ integrity check labels.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum XzCheckType {
    /// No integrity check.
    None,
    /// CRC32 integrity check.
    Crc32,
    /// CRC64 integrity check.
    #[default]
    Crc64,
    /// SHA-256 integrity check.
    Sha256,
    /// Unknown or intentionally unspecified check type.
    Unknown,
}

impl XzCheckType {
    /// Returns a stable lowercase label.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::None => "none",
            Self::Crc32 => "crc32",
            Self::Crc64 => "crc64",
            Self::Sha256 => "sha256",
            Self::Unknown => "unknown",
        }
    }
}

/// XZ option metadata.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct XzOptions {
    /// Numeric xz preset level, when specified by the caller.
    pub level: Option<u32>,
    /// Integrity check type label.
    pub check_type: XzCheckType,
}

impl XzOptions {
    /// Creates default xz option metadata.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            level: None,
            check_type: XzCheckType::Crc64,
        }
    }

    /// Adds a numeric preset level label.
    #[must_use]
    pub const fn with_level(mut self, level: u32) -> Self {
        self.level = Some(level);
        self
    }

    /// Sets the integrity check type label.
    #[must_use]
    pub const fn with_check_type(mut self, check_type: XzCheckType) -> Self {
        self.check_type = check_type;
        self
    }
}

#[cfg(test)]
mod tests {
    use super::{TAR_XZ_EXTENSION, XZ_EXTENSIONS, XzCheckType, XzOptions};

    #[test]
    fn exposes_xz_labels() {
        assert_eq!(TAR_XZ_EXTENSION, "tar.xz");
        assert!(XZ_EXTENSIONS.contains(&"txz"));
    }

    #[test]
    fn stores_option_metadata() {
        let options = XzOptions::new()
            .with_level(6)
            .with_check_type(XzCheckType::Sha256);

        assert_eq!(options.level, Some(6));
        assert_eq!(options.check_type.as_str(), "sha256");
    }
}