archive_rs/
support.rs

1//! Which formats and file endings this library supports, depends on cargo
2//! features.
3
4use std::fmt;
5
6/// Supported formats.
7#[derive(Clone, Copy, Debug)]
8pub enum Format {
9    #[cfg(feature = "tar")]
10    /// tarball
11    Tar,
12
13    #[cfg(all(
14        feature = "tar",
15        any(feature = "bzip2", feature = "bzip2-rs")
16    ))]
17    /// bzip2-compressed tarball
18    TarBzip2,
19
20    #[cfg(all(feature = "tar", feature = "flate2"))]
21    /// gzip-compressed tarball
22    TarGzip,
23
24    #[cfg(all(feature = "tar", feature = "lz4"))]
25    /// lz4-compressed tarball
26    TarLz4,
27
28    #[cfg(all(feature = "tar", feature = "xz2"))]
29    /// xz-compressed tarball
30    TarXz,
31
32    #[cfg(all(feature = "tar", feature = "zstd"))]
33    /// zstd-compressed tarball
34    TarZstd,
35}
36
37impl Format {
38    /// Returns the format name.
39    #[must_use]
40    pub const fn name(&self) -> &'static str {
41        match self {
42            #[cfg(feature = "tar")]
43            Self::Tar => "Tar",
44
45            #[cfg(all(
46                feature = "tar",
47                any(feature = "bzip2", feature = "bzip2-rs")
48            ))]
49            Self::TarBzip2 => "TarBzip2",
50
51            #[cfg(all(feature = "tar", feature = "flate2"))]
52            Self::TarGzip => "TarGz",
53
54            #[cfg(all(feature = "tar", feature = "lz4"))]
55            Self::TarLz4 => "TarLz4",
56
57            #[cfg(all(feature = "tar", feature = "xz2"))]
58            Self::TarXz => "TarXz",
59
60            #[cfg(all(feature = "tar", feature = "zstd"))]
61            Self::TarZstd => "TarZstd",
62        }
63    }
64
65    /// Returns the format description, like *gzip-compressed tarball*.
66    #[must_use]
67    pub const fn description(&self) -> &'static str {
68        match self {
69            #[cfg(feature = "tar")]
70            Self::Tar => "tarball",
71
72            #[cfg(all(
73                feature = "tar",
74                any(feature = "bzip2", feature = "bzip2-rs")
75            ))]
76            Self::TarBzip2 => "bzip2-compressed tarball",
77
78            #[cfg(all(feature = "tar", feature = "flate2"))]
79            Self::TarGzip => "gzip-compressed tarball",
80
81            #[cfg(all(feature = "tar", feature = "lz4"))]
82            Self::TarLz4 => "lz4-compressed tarball",
83
84            #[cfg(all(feature = "tar", feature = "xz2"))]
85            Self::TarXz => "xz-compressed tarball",
86
87            #[cfg(all(feature = "tar", feature = "zstd"))]
88            Self::TarZstd => "zstd-compressed tarball",
89        }
90    }
91
92    /// Returns the supported file endings for format auto-detection.
93    #[must_use]
94    pub fn file_endings(&self) -> Vec<&'static str> {
95        match self {
96            #[cfg(feature = "tar")]
97            Self::Tar => vec!["*.tar"],
98
99            #[cfg(all(
100                feature = "tar",
101                any(feature = "bzip2", feature = "bzip2-rs")
102            ))]
103            Self::TarBzip2 => {
104                vec!["*.tar.bz2", "*.tbz", "*.tbz2"]
105            }
106
107            #[cfg(all(feature = "tar", feature = "flate2"))]
108            Self::TarGzip => vec!["*.tar.gz", "*.tgz"],
109
110            #[cfg(all(feature = "tar", feature = "lz4"))]
111            Self::TarLz4 => vec!["*.tar.lz4"],
112
113            #[cfg(all(feature = "tar", feature = "xz2"))]
114            Self::TarXz => vec!["*.tar.xz", "*.txz"],
115
116            #[cfg(all(feature = "tar", feature = "zstd"))]
117            Self::TarZstd => vec!["*.tar.zst"],
118        }
119    }
120
121    /// Returns all supported format names.
122    #[must_use]
123    pub const fn all_names<'a>() -> &'a [Self] {
124        &[
125            #[cfg(feature = "tar")]
126            Self::Tar,
127            #[cfg(all(
128                feature = "tar",
129                any(feature = "bzip2", feature = "bzip2-rs")
130            ))]
131            Self::TarBzip2,
132            #[cfg(all(feature = "tar", feature = "flate2"))]
133            Self::TarGzip,
134            #[cfg(all(feature = "tar", feature = "lz4"))]
135            Self::TarLz4,
136            #[cfg(all(feature = "tar", feature = "xz2"))]
137            Self::TarXz,
138            #[cfg(all(feature = "tar", feature = "zstd"))]
139            Self::TarZstd,
140        ]
141    }
142
143    /// Returns all pre-formatted formats in the form of `name description
144    /// [endings..]`.
145    #[must_use]
146    pub fn all_file_endings() -> Vec<String> {
147        vec![
148            #[cfg(feature = "tar")]
149            Self::Tar.describe(),
150            #[cfg(all(
151                feature = "tar",
152                any(feature = "bzip2", feature = "bzip2-rs")
153            ))]
154            Self::TarBzip2.describe(),
155            #[cfg(all(feature = "tar", feature = "flate2"))]
156            Self::TarGzip.describe(),
157            #[cfg(all(feature = "tar", feature = "lz4"))]
158            Self::TarLz4.describe(),
159            #[cfg(all(feature = "tar", feature = "xz2"))]
160            Self::TarXz.describe(),
161            #[cfg(all(feature = "tar", feature = "zstd"))]
162            Self::TarZstd.describe(),
163        ]
164    }
165
166    fn describe(self) -> String {
167        format!(
168            "{} {} {:?}",
169            self.name(),
170            self.description(),
171            self.file_endings()
172        )
173    }
174}
175
176impl fmt::Display for Format {
177    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
178        write!(f, "{}", self.description())
179    }
180}