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 Into<String>) -> Self {
27 assert!(!formats.is_empty());
28 Self { compression_formats: formats, display_text: text.into() }
31 /// Checks if the first format in `compression_formats` is an archive
32 pub fn is_archive(&self) -> bool {
33 // Safety: we check that `compression_formats` is not empty in `Self::new`
34 self.compression_formats[0].is_archive_format()
37 /// Iteration to inner compression formats, useful for flat_mapping
38 pub fn iter(&self) -> impl Iterator<Item = &CompressionFormat> {
39 self.compression_formats.iter()
43 impl fmt::Display for Extension {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45 self.display_text.fmt(f)
49 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
50 /// Accepted extensions for input and output
51 pub enum CompressionFormat {
60 /// tar, tgz, tbz, tbz2, txz, tlz, tlz4, tlzma, 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
83 impl fmt::Display for CompressionFormat {
84 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
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" | "tlz" | "tlzma" => &[Tar, Lzma],
125 "tzst" => &[Tar, Zstd],
127 "bz" | "bz2" => &[Bzip],
130 "xz" | "lzma" => &[Lzma],
135 let extension = Extension::new(formats, extension);
136 extensions.push(extension);
138 // Update for the next iteration
139 path = if let Some(stem) = path.file_stem() { Path::new(stem) } else { Path::new("") };
141 // Put the extensions in the correct order: left to right
142 extensions.reverse();
147 /// Extracts extensions from a path, return only the list of extension objects
148 pub fn extensions_from_path(path: &Path) -> Vec<Extension> {
149 let (_, extensions) = separate_known_extensions_from_name(path);
158 fn test_extensions_from_path() {
159 use CompressionFormat::*;
160 let path = Path::new("bolovo.tar.gz");
162 let extensions: Vec<Extension> = extensions_from_path(path);
163 let formats: Vec<&CompressionFormat> = extensions.iter().flat_map(Extension::iter).collect::<Vec<_>>();
165 assert_eq!(formats, vec![&Tar, &Gzip]);