1 //! Our representation of all the supported compression formats.
3 use std::{ffi::OsStr, fmt, path::Path};
5 use self::CompressionFormat::*;
7 /// A wrapper around `CompressionFormat` that allows combinations like `tgz`
8 #[derive(Debug, Clone, Eq)]
10 pub struct Extension {
11 /// One extension like "tgz" can be made of multiple CompressionFormats ([Tar, Gz])
12 pub compression_formats: &'static [CompressionFormat],
13 /// The input text for this extension, like "tgz", "tar" or "xz"
14 pub display_text: String,
16 // The display_text should be ignored when comparing extensions
17 impl PartialEq for Extension {
18 fn eq(&self, other: &Self) -> bool {
19 self.compression_formats == other.compression_formats
25 /// Will panic if `formats` is empty
26 pub fn new(formats: &'static [CompressionFormat], text: impl ToString) -> Self {
27 assert!(!formats.is_empty());
29 compression_formats: formats,
30 display_text: text.to_string(),
34 /// Checks if the first format in `compression_formats` is an archive
35 pub fn is_archive(&self) -> bool {
36 // Safety: we check that `compression_formats` is not empty in `Self::new`
37 self.compression_formats[0].is_archive_format()
41 impl fmt::Display for Extension {
42 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43 self.display_text.fmt(f)
47 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
48 /// Accepted extensions for input and output
49 pub enum CompressionFormat {
60 /// tar, tgz, tbz, tbz2, txz, tlz4, tlzma, tsz, tzst
68 impl CompressionFormat {
69 /// Currently supported archive formats are .tar (and aliases to it) and .zip
70 pub fn is_archive_format(&self) -> bool {
71 // Keep this match like that without a wildcard `_` so we don't forget to update it
84 impl fmt::Display for CompressionFormat {
85 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
86 let text = match self {
101 // use crate::extension::CompressionFormat::*;
104 /// Extracts extensions from a path,
105 /// return both the remaining path and the list of extension objects
106 pub fn separate_known_extensions_from_name(mut path: &Path) -> (&Path, Vec<Extension>) {
107 // // TODO: check for file names with the name of an extension
108 // // TODO2: warn the user that currently .tar.gz is a .gz file named .tar
110 // let all = ["tar", "zip", "bz", "bz2", "gz", "xz", "lzma", "lz"];
111 // if path.file_name().is_some() && all.iter().any(|ext| path.file_name().unwrap() == *ext) {
112 // todo!("we found a extension in the path name instead, what to do with this???");
115 let mut extensions = vec![];
117 // While there is known extensions at the tail, grab them
118 while let Some(extension) = path.extension().and_then(OsStr::to_str) {
119 let formats: &[CompressionFormat] = match extension {
121 "tgz" => &[Tar, Gzip],
122 "tbz" | "tbz2" => &[Tar, Bzip],
123 "tlz4" => &[Tar, Lz4],
124 "txz" | "tlzma" => &[Tar, Lzma],
125 "tsz" => &[Tar, Snappy],
126 "tzst" => &[Tar, Zstd],
128 "bz" | "bz2" => &[Bzip],
131 "xz" | "lzma" => &[Lzma],
137 let extension = Extension::new(formats, extension);
138 extensions.push(extension);
140 // Update for the next iteration
141 path = if let Some(stem) = path.file_stem() {
147 // Put the extensions in the correct order: left to right
148 extensions.reverse();
153 /// Extracts extensions from a path, return only the list of extension objects
154 pub fn extensions_from_path(path: &Path) -> Vec<Extension> {
155 let (_, extensions) = separate_known_extensions_from_name(path);
164 fn test_extensions_from_path() {
165 use CompressionFormat::*;
166 let path = Path::new("bolovo.tar.gz");
168 let extensions: Vec<Extension> = extensions_from_path(path);
169 let formats: Vec<CompressionFormat> = flatten_compression_formats(&extensions);
171 assert_eq!(formats, vec![Tar, Gzip]);
175 // Panics if formats has an empty list of compression formats
176 pub fn split_first_compression_format(formats: &[Extension]) -> (CompressionFormat, Vec<CompressionFormat>) {
177 let mut extensions: Vec<CompressionFormat> = flatten_compression_formats(formats);
178 let first_extension = extensions.remove(0);
179 (first_extension, extensions)
182 pub fn flatten_compression_formats(extensions: &[Extension]) -> Vec<CompressionFormat> {
185 .flat_map(|extension| extension.compression_formats.iter())