async_zip/spec/
compression.rs

1// Copyright (c) 2021 Harry [Majored] [hello@majored.pw]
2// MIT License (https://github.com/Majored/rs-async-zip/blob/main/LICENSE)
3
4use crate::error::{Result, ZipError};
5
6/// A compression method supported by this crate.
7#[non_exhaustive]
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum Compression {
10    Stored,
11    #[cfg(feature = "deflate")]
12    Deflate,
13    #[cfg(feature = "deflate64")]
14    Deflate64,
15    #[cfg(feature = "bzip2")]
16    Bz,
17    #[cfg(feature = "lzma")]
18    Lzma,
19    #[cfg(feature = "zstd")]
20    Zstd,
21    #[cfg(feature = "xz")]
22    Xz,
23}
24
25impl TryFrom<u16> for Compression {
26    type Error = ZipError;
27
28    // Convert a u16 stored with little endianness into a supported compression method.
29    // https://github.com/Majored/rs-async-zip/blob/main/SPECIFICATION.md#445
30    fn try_from(value: u16) -> Result<Self> {
31        match value {
32            0 => Ok(Compression::Stored),
33            #[cfg(feature = "deflate")]
34            8 => Ok(Compression::Deflate),
35            #[cfg(feature = "deflate64")]
36            9 => Ok(Compression::Deflate64),
37            #[cfg(feature = "bzip2")]
38            12 => Ok(Compression::Bz),
39            #[cfg(feature = "lzma")]
40            14 => Ok(Compression::Lzma),
41            #[cfg(feature = "zstd")]
42            93 => Ok(Compression::Zstd),
43            #[cfg(feature = "xz")]
44            95 => Ok(Compression::Xz),
45            _ => Err(ZipError::CompressionNotSupported(value)),
46        }
47    }
48}
49
50impl From<&Compression> for u16 {
51    // Convert a supported compression method into its relevant u16 stored with little endianness.
52    // https://github.com/Majored/rs-async-zip/blob/main/SPECIFICATION.md#445
53    fn from(compression: &Compression) -> u16 {
54        match compression {
55            Compression::Stored => 0,
56            #[cfg(feature = "deflate")]
57            Compression::Deflate => 8,
58            #[cfg(feature = "deflate64")]
59            Compression::Deflate64 => 9,
60            #[cfg(feature = "bzip2")]
61            Compression::Bz => 12,
62            #[cfg(feature = "lzma")]
63            Compression::Lzma => 14,
64            #[cfg(feature = "zstd")]
65            Compression::Zstd => 93,
66            #[cfg(feature = "xz")]
67            Compression::Xz => 95,
68        }
69    }
70}
71
72impl From<Compression> for u16 {
73    fn from(compression: Compression) -> u16 {
74        (&compression).into()
75    }
76}
77
78/// Level of compression data should be compressed with for deflate.
79#[derive(Debug, Clone, Copy)]
80pub enum DeflateOption {
81    // Normal (-en) compression option was used.
82    Normal,
83
84    // Maximum (-exx/-ex) compression option was used.
85    Maximum,
86
87    // Fast (-ef) compression option was used.
88    Fast,
89
90    // Super Fast (-es) compression option was used.
91    Super,
92
93    /// Other implementation defined level.
94    Other(i32),
95}