Skip to main content

use_gzip/
lib.rs

1#![forbid(unsafe_code)]
2#![doc = include_str!("../README.md")]
3
4//! Gzip labels and option metadata for `RustUse`.
5
6/// Common gzip file extension.
7pub const GZIP_EXTENSION: &str = "gz";
8/// Common gzip-compressed tar extension.
9pub const TAR_GZIP_EXTENSION: &str = "tar.gz";
10/// Common HTTP content-encoding label for gzip.
11pub const GZIP_ENCODING_LABEL: &str = "gzip";
12/// Common gzip media type.
13pub const GZIP_MEDIA_TYPE: &str = "application/gzip";
14/// Common gzip-related extensions.
15pub const GZIP_EXTENSIONS: &[&str] = &["gz", "gzip", "tgz", "tar.gz"];
16
17/// Metadata strategy for gzip headers.
18#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
19pub enum GzipHeaderMode {
20    /// Minimal metadata.
21    #[default]
22    Minimal,
23    /// Include file metadata labels when available.
24    FileMetadata,
25    /// Unknown or intentionally unspecified header behavior.
26    Unknown,
27}
28
29impl GzipHeaderMode {
30    /// Returns a stable lowercase label.
31    #[must_use]
32    pub const fn as_str(self) -> &'static str {
33        match self {
34            Self::Minimal => "minimal",
35            Self::FileMetadata => "file-metadata",
36            Self::Unknown => "unknown",
37        }
38    }
39}
40
41/// Gzip option metadata.
42#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
43pub struct GzipOptions {
44    /// Numeric gzip level, when specified by the caller.
45    pub level: Option<u32>,
46    /// Gzip header metadata strategy.
47    pub header_mode: GzipHeaderMode,
48}
49
50impl GzipOptions {
51    /// Creates default gzip option metadata.
52    #[must_use]
53    pub const fn new() -> Self {
54        Self {
55            level: None,
56            header_mode: GzipHeaderMode::Minimal,
57        }
58    }
59
60    /// Adds a numeric compression level label.
61    #[must_use]
62    pub const fn with_level(mut self, level: u32) -> Self {
63        self.level = Some(level);
64        self
65    }
66
67    /// Sets the gzip header metadata strategy.
68    #[must_use]
69    pub const fn with_header_mode(mut self, header_mode: GzipHeaderMode) -> Self {
70        self.header_mode = header_mode;
71        self
72    }
73}
74
75#[cfg(test)]
76mod tests {
77    use super::{GZIP_ENCODING_LABEL, GZIP_EXTENSIONS, GzipHeaderMode, GzipOptions};
78
79    #[test]
80    fn exposes_gzip_labels() {
81        assert_eq!(GZIP_ENCODING_LABEL, "gzip");
82        assert!(GZIP_EXTENSIONS.contains(&"tar.gz"));
83    }
84
85    #[test]
86    fn stores_option_metadata() {
87        let options = GzipOptions::new()
88            .with_level(6)
89            .with_header_mode(GzipHeaderMode::FileMetadata);
90
91        assert_eq!(options.level, Some(6));
92        assert_eq!(options.header_mode.as_str(), "file-metadata");
93    }
94}