Skip to main content

use_xz/
lib.rs

1#![forbid(unsafe_code)]
2#![doc = include_str!("../README.md")]
3
4//! XZ labels and option metadata for `RustUse`.
5
6/// Common xz file extension.
7pub const XZ_EXTENSION: &str = "xz";
8/// Common xz-compressed tar extension.
9pub const TAR_XZ_EXTENSION: &str = "tar.xz";
10/// Common LZMA label.
11pub const LZMA_LABEL: &str = "lzma";
12/// Common xz-related extensions.
13pub const XZ_EXTENSIONS: &[&str] = &["xz", "lzma", "txz", "tar.xz"];
14
15/// XZ integrity check labels.
16#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
17pub enum XzCheckType {
18    /// No integrity check.
19    None,
20    /// CRC32 integrity check.
21    Crc32,
22    /// CRC64 integrity check.
23    #[default]
24    Crc64,
25    /// SHA-256 integrity check.
26    Sha256,
27    /// Unknown or intentionally unspecified check type.
28    Unknown,
29}
30
31impl XzCheckType {
32    /// Returns a stable lowercase label.
33    #[must_use]
34    pub const fn as_str(self) -> &'static str {
35        match self {
36            Self::None => "none",
37            Self::Crc32 => "crc32",
38            Self::Crc64 => "crc64",
39            Self::Sha256 => "sha256",
40            Self::Unknown => "unknown",
41        }
42    }
43}
44
45/// XZ option metadata.
46#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
47pub struct XzOptions {
48    /// Numeric xz preset level, when specified by the caller.
49    pub level: Option<u32>,
50    /// Integrity check type label.
51    pub check_type: XzCheckType,
52}
53
54impl XzOptions {
55    /// Creates default xz option metadata.
56    #[must_use]
57    pub const fn new() -> Self {
58        Self {
59            level: None,
60            check_type: XzCheckType::Crc64,
61        }
62    }
63
64    /// Adds a numeric preset level label.
65    #[must_use]
66    pub const fn with_level(mut self, level: u32) -> Self {
67        self.level = Some(level);
68        self
69    }
70
71    /// Sets the integrity check type label.
72    #[must_use]
73    pub const fn with_check_type(mut self, check_type: XzCheckType) -> Self {
74        self.check_type = check_type;
75        self
76    }
77}
78
79#[cfg(test)]
80mod tests {
81    use super::{TAR_XZ_EXTENSION, XZ_EXTENSIONS, XzCheckType, XzOptions};
82
83    #[test]
84    fn exposes_xz_labels() {
85        assert_eq!(TAR_XZ_EXTENSION, "tar.xz");
86        assert!(XZ_EXTENSIONS.contains(&"txz"));
87    }
88
89    #[test]
90    fn stores_option_metadata() {
91        let options = XzOptions::new()
92            .with_level(6)
93            .with_check_type(XzCheckType::Sha256);
94
95        assert_eq!(options.level, Some(6));
96        assert_eq!(options.check_type.as_str(), "sha256");
97    }
98}