#![forbid(unsafe_code)]
#![doc = include_str!("../README.md")]
use core::fmt;
pub const ZIP_EXTENSION: &str = "zip";
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum ZipCompressionMethod {
#[default]
Stored,
Deflated,
Bzip2,
Zstd,
Lzma,
Unknown(u16),
}
impl ZipCompressionMethod {
#[must_use]
pub const fn code(self) -> u16 {
match self {
Self::Stored => 0,
Self::Deflated => 8,
Self::Bzip2 => 12,
Self::Lzma => 14,
Self::Zstd => 93,
Self::Unknown(code) => code,
}
}
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Stored => "stored",
Self::Deflated => "deflated",
Self::Bzip2 => "bzip2",
Self::Zstd => "zstd",
Self::Lzma => "lzma",
Self::Unknown(_) => "unknown",
}
}
#[must_use]
pub const fn from_code(code: u16) -> Self {
match code {
0 => Self::Stored,
8 => Self::Deflated,
12 => Self::Bzip2,
14 => Self::Lzma,
93 => Self::Zstd,
other => Self::Unknown(other),
}
}
}
impl fmt::Display for ZipCompressionMethod {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
#[cfg(test)]
mod tests {
use super::{ZIP_EXTENSION, ZipCompressionMethod};
#[test]
fn maps_zip_method_codes() {
assert_eq!(
ZipCompressionMethod::from_code(0),
ZipCompressionMethod::Stored
);
assert_eq!(
ZipCompressionMethod::from_code(8),
ZipCompressionMethod::Deflated
);
assert_eq!(
ZipCompressionMethod::from_code(93),
ZipCompressionMethod::Zstd
);
assert_eq!(
ZipCompressionMethod::from_code(65000),
ZipCompressionMethod::Unknown(65000)
);
}
#[test]
fn exposes_zip_extension_label() {
assert_eq!(ZIP_EXTENSION, "zip");
}
}