1 //! Our representation of all the supported compression formats.
3 use std::{ffi::OsStr, fmt, path::Path};
5 use self::CompressionFormat::*;
8 /// A wrapper around `CompressionFormat` that allows combinations like `tgz`
9 #[derive(Debug, Clone, Eq)]
11 pub struct Extension {
12 /// One extension like "tgz" can be made of multiple CompressionFormats ([Tar, Gz])
13 pub compression_formats: &'static [CompressionFormat],
14 /// The input text for this extension, like "tgz", "tar" or "xz"
15 pub display_text: String,
17 // The display_text should be ignored when comparing extensions
18 impl PartialEq for Extension {
19 fn eq(&self, other: &Self) -> bool {
20 self.compression_formats == other.compression_formats
26 /// Will panic if `formats` is empty
27 pub fn new(formats: &'static [CompressionFormat], text: impl ToString) -> Self {
28 assert!(!formats.is_empty());
30 compression_formats: formats,
31 display_text: text.to_string(),
35 /// Checks if the first format in `compression_formats` is an archive
36 pub fn is_archive(&self) -> bool {
37 // Safety: we check that `compression_formats` is not empty in `Self::new`
38 self.compression_formats[0].is_archive_format()
42 impl fmt::Display for Extension {
43 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44 self.display_text.fmt(f)
48 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
49 /// Accepted extensions for input and output
50 pub enum CompressionFormat {
61 /// tar, tgz, tbz, tbz2, txz, tlz4, tlzma, tsz, tzst
69 impl CompressionFormat {
70 /// Currently supported archive formats are .tar (and aliases to it) and .zip
71 pub fn is_archive_format(&self) -> bool {
72 // Keep this match like that without a wildcard `_` so we don't forget to update it
85 impl fmt::Display for CompressionFormat {
86 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
87 let text = match self {
102 pub const SUPPORTED_EXTENSIONS: &[&str] = &[
103 "tar", "tgz", "tbz", "tlz4", "txz", "tzlma", "tsz", "tzst", "zip", "bz", "bz2", "gz", "lz4", "xz", "lzma", "sz",
107 /// Extracts extensions from a path.
109 /// Returns both the remaining path and the list of extension objects
110 pub fn separate_known_extensions_from_name(mut path: &Path) -> (&Path, Vec<Extension>) {
111 let mut extensions = vec![];
113 if let Some(file_stem) = path.file_stem().and_then(OsStr::to_str) {
114 let file_stem = file_stem.trim_matches('.');
116 if SUPPORTED_EXTENSIONS.contains(&file_stem) {
117 warning!("Received a file with name '{file_stem}', but {file_stem} was expected as the extension.");
121 // While there is known extensions at the tail, grab them
122 while let Some(extension) = path.extension().and_then(OsStr::to_str) {
123 let formats: &[CompressionFormat] = match extension {
125 "tgz" => &[Tar, Gzip],
126 "tbz" | "tbz2" => &[Tar, Bzip],
127 "tlz4" => &[Tar, Lz4],
128 "txz" | "tlzma" => &[Tar, Lzma],
129 "tsz" => &[Tar, Snappy],
130 "tzst" => &[Tar, Zstd],
132 "bz" | "bz2" => &[Bzip],
135 "xz" | "lzma" => &[Lzma],
141 let extension = Extension::new(formats, extension);
142 extensions.push(extension);
144 // Update for the next iteration
145 path = if let Some(stem) = path.file_stem() {
151 // Put the extensions in the correct order: left to right
152 extensions.reverse();
157 /// Extracts extensions from a path, return only the list of extension objects
158 pub fn extensions_from_path(path: &Path) -> Vec<Extension> {
159 let (_, extensions) = separate_known_extensions_from_name(path);
168 fn test_extensions_from_path() {
169 use CompressionFormat::*;
170 let path = Path::new("bolovo.tar.gz");
172 let extensions: Vec<Extension> = extensions_from_path(path);
173 let formats: Vec<CompressionFormat> = flatten_compression_formats(&extensions);
175 assert_eq!(formats, vec![Tar, Gzip]);
179 // Panics if formats has an empty list of compression formats
180 pub fn split_first_compression_format(formats: &[Extension]) -> (CompressionFormat, Vec<CompressionFormat>) {
181 let mut extensions: Vec<CompressionFormat> = flatten_compression_formats(formats);
182 let first_extension = extensions.remove(0);
183 (first_extension, extensions)
186 pub fn flatten_compression_formats(extensions: &[Extension]) -> Vec<CompressionFormat> {
189 .flat_map(|extension| extension.compression_formats.iter())