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, PartialEq, 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,
19 /// Will panic if `formats` is empty
20 pub fn new(formats: &'static [CompressionFormat], text: impl Into<String>) -> Self {
21 assert!(!formats.is_empty());
22 Self { compression_formats: formats, display_text: text.into() }
25 /// Checks if the first format in `compression_formats` is an archive
26 pub fn is_archive(&self) -> bool {
27 // Safety: we check that `compression_formats` is not empty in `Self::new`
28 self.compression_formats[0].is_archive_format()
31 /// Iteration to inner compression formats, useful for flat_mapping
32 pub fn iter(&self) -> impl Iterator<Item = &CompressionFormat> {
33 self.compression_formats.iter()
37 impl fmt::Display for Extension {
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 self.display_text.fmt(f)
43 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
44 /// Accepted extensions for input and output
45 pub enum CompressionFormat {
54 /// tar, tgz, tbz, tbz2, txz, tlz, tlz4, tlzma, tzst
62 impl CompressionFormat {
63 /// Currently supported archive formats are .tar (and aliases to it) and .zip
64 pub fn is_archive_format(&self) -> bool {
65 // Keep this match like that without a wildcard `_` so we don't forget to update it
77 impl fmt::Display for CompressionFormat {
78 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
95 // use crate::extension::CompressionFormat::*;
98 /// Extracts extensions from a path,
99 /// return both the remaining path and the list of extension objects
100 pub fn separate_known_extensions_from_name(mut path: &Path) -> (&Path, Vec<Extension>) {
101 // // TODO: check for file names with the name of an extension
102 // // TODO2: warn the user that currently .tar.gz is a .gz file named .tar
104 // let all = ["tar", "zip", "bz", "bz2", "gz", "xz", "lzma", "lz"];
105 // if path.file_name().is_some() && all.iter().any(|ext| path.file_name().unwrap() == *ext) {
106 // todo!("we found a extension in the path name instead, what to do with this???");
109 let mut extensions = vec![];
111 // While there is known extensions at the tail, grab them
112 while let Some(extension) = path.extension().and_then(OsStr::to_str) {
113 let formats: &[CompressionFormat] = match extension {
115 "tgz" => &[Tar, Gzip],
116 "tbz" | "tbz2" => &[Tar, Bzip],
117 "tlz4" => &[Tar, Lz4],
118 "txz" | "tlz" | "tlzma" => &[Tar, Lzma],
119 "tzst" => &[Tar, Zstd],
121 "bz" | "bz2" => &[Bzip],
124 "xz" | "lzma" | "lz" => &[Lzma],
129 let extension = Extension::new(formats, extension);
130 extensions.push(extension);
132 // Update for the next iteration
133 path = if let Some(stem) = path.file_stem() { Path::new(stem) } else { Path::new("") };
135 // Put the extensions in the correct order: left to right
136 extensions.reverse();
141 /// Extracts extensions from a path, return only the list of extension objects
142 pub fn extensions_from_path(path: &Path) -> Vec<Extension> {
143 let (_, extensions) = separate_known_extensions_from_name(path);
152 fn test_extensions_from_path() {
153 use CompressionFormat::*;
154 let path = Path::new("bolovo.tar.gz");
156 let extensions: Vec<Extension> = extensions_from_path(path);
157 let formats: Vec<&CompressionFormat> = extensions.iter().flat_map(Extension::iter).collect::<Vec<_>>();
159 assert_eq!(formats, vec![&Tar, &Gzip]);