use-zstd 0.1.0

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

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

/// Common zstd file extension.
pub const ZSTD_EXTENSION: &str = "zst";
/// Common zstd-compressed tar extension.
pub const TAR_ZSTD_EXTENSION: &str = "tar.zst";
/// Common Zstandard label.
pub const ZSTD_LABEL: &str = "zstd";
/// Common zstd-related extensions.
pub const ZSTD_EXTENSIONS: &[&str] = &["zst", "zstd", "tzst", "tar.zst", "tar.zstd"];

/// Zstd frame shape labels.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum ZstdFrameKind {
    /// Standard zstd frame.
    #[default]
    Standard,
    /// Single segment zstd frame.
    SingleSegment,
    /// Skippable zstd frame.
    Skippable,
    /// Unknown or intentionally unspecified frame kind.
    Unknown,
}

impl ZstdFrameKind {
    /// Returns a stable lowercase label.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Standard => "standard",
            Self::SingleSegment => "single-segment",
            Self::Skippable => "skippable",
            Self::Unknown => "unknown",
        }
    }
}

/// Zstd option metadata.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ZstdOptions {
    /// Numeric zstd compression level, when specified by the caller.
    pub level: Option<i32>,
    /// Whether a checksum should be described as present.
    pub checksum: bool,
    /// Optional dictionary identifier.
    pub dictionary_id: Option<u32>,
    /// Frame shape label.
    pub frame_kind: ZstdFrameKind,
}

impl ZstdOptions {
    /// Creates default zstd option metadata.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            level: None,
            checksum: false,
            dictionary_id: None,
            frame_kind: ZstdFrameKind::Standard,
        }
    }

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

    /// Sets whether checksum metadata is present.
    #[must_use]
    pub const fn with_checksum(mut self, checksum: bool) -> Self {
        self.checksum = checksum;
        self
    }

    /// Adds a dictionary identifier.
    #[must_use]
    pub const fn with_dictionary_id(mut self, dictionary_id: u32) -> Self {
        self.dictionary_id = Some(dictionary_id);
        self
    }

    /// Sets the frame shape label.
    #[must_use]
    pub const fn with_frame_kind(mut self, frame_kind: ZstdFrameKind) -> Self {
        self.frame_kind = frame_kind;
        self
    }
}

#[cfg(test)]
mod tests {
    use super::{TAR_ZSTD_EXTENSION, ZSTD_EXTENSIONS, ZstdFrameKind, ZstdOptions};

    #[test]
    fn exposes_zstd_labels() {
        assert_eq!(TAR_ZSTD_EXTENSION, "tar.zst");
        assert!(ZSTD_EXTENSIONS.contains(&"tar.zstd"));
    }

    #[test]
    fn stores_option_metadata() {
        let options = ZstdOptions::new()
            .with_level(3)
            .with_checksum(true)
            .with_dictionary_id(42)
            .with_frame_kind(ZstdFrameKind::SingleSegment);

        assert_eq!(options.level, Some(3));
        assert!(options.checksum);
        assert_eq!(options.dictionary_id, Some(42));
        assert_eq!(options.frame_kind.as_str(), "single-segment");
    }
}