1 //! Our representation of all the supported compression formats.
3 use std::{ffi::OsStr, fmt, path::Path};
6 use CompressionFormat::*;
8 use crate::{error::Error, utils::logger::warning};
10 pub const SUPPORTED_EXTENSIONS: &[&str] = &[
21 #[cfg(feature = "unrar")]
26 pub const SUPPORTED_ALIASES: &[&str] = &["tgz", "tbz", "tlz4", "txz", "tzlma", "tsz", "tzst"];
28 #[cfg(not(feature = "unrar"))]
29 pub const PRETTY_SUPPORTED_EXTENSIONS: &str = "tar, zip, bz, bz2, bz3, gz, lz4, xz, lzma, sz, zst, 7z";
30 #[cfg(feature = "unrar")]
31 pub const PRETTY_SUPPORTED_EXTENSIONS: &str = "tar, zip, bz, bz2, bz3, gz, lz4, xz, lzma, sz, zst, rar, 7z";
33 pub const PRETTY_SUPPORTED_ALIASES: &str = "tgz, tbz, tlz4, txz, tzlma, tsz, tzst";
35 /// A wrapper around `CompressionFormat` that allows combinations like `tgz`
36 #[derive(Debug, Clone)]
37 // Keep `PartialEq` only for testing because two formats are the same even if
38 // their `display_text` does not match (beware of aliases)
39 #[cfg_attr(test, derive(PartialEq))]
40 // Should only be built with constructors
42 pub struct Extension {
43 /// One extension like "tgz" can be made of multiple CompressionFormats ([Tar, Gz])
44 pub compression_formats: &'static [CompressionFormat],
45 /// The input text for this extension, like "tgz", "tar" or "xz"
51 /// Will panic if `formats` is empty
52 pub fn new(formats: &'static [CompressionFormat], text: impl ToString) -> Self {
53 assert!(!formats.is_empty());
55 compression_formats: formats,
56 display_text: text.to_string(),
60 /// Checks if the first format in `compression_formats` is an archive
61 pub fn is_archive(&self) -> bool {
62 // Safety: we check that `compression_formats` is not empty in `Self::new`
63 self.compression_formats[0].is_archive_format()
67 impl fmt::Display for Extension {
68 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
69 self.display_text.fmt(f)
73 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
74 /// Accepted extensions for input and output
75 pub enum CompressionFormat {
88 /// tar, tgz, tbz, tbz2, tbz3, txz, tlz4, tlzma, tsz, tzst
94 // even if built without RAR support, we still want to recognise the format
101 impl CompressionFormat {
102 /// Currently supported archive formats are .tar (and aliases to it) and .zip
103 fn is_archive_format(&self) -> bool {
104 // Keep this match like that without a wildcard `_` so we don't forget to update it
106 Tar | Zip | Rar | SevenZip => true,
118 fn to_extension(ext: &[u8]) -> Option<Extension> {
122 b"tgz" => &[Tar, Gzip],
123 b"tbz" | b"tbz2" => &[Tar, Bzip],
124 b"tbz3" => &[Tar, Bzip3],
125 b"tlz4" => &[Tar, Lz4],
126 b"txz" | b"tlzma" => &[Tar, Lzma],
127 b"tsz" => &[Tar, Snappy],
128 b"tzst" => &[Tar, Zstd],
130 b"bz" | b"bz2" => &[Bzip],
134 b"xz" | b"lzma" => &[Lzma],
138 b"7z" => &[SevenZip],
145 fn split_extension(name: &mut &[u8]) -> Option<Extension> {
146 let (new_name, ext) = name.rsplit_once_str(b".")?;
147 if matches!(new_name, b"" | b"." | b"..") {
150 let ext = to_extension(ext)?;
155 pub fn parse_format_flag(input: &OsStr) -> crate::Result<Vec<Extension>> {
156 let format = input.as_encoded_bytes();
158 let format = std::str::from_utf8(format).map_err(|_| Error::InvalidFormatFlag {
159 text: input.to_owned(),
160 reason: "Invalid UTF-8.".to_string(),
163 let extensions: Vec<Extension> = format
165 .filter(|extension| !extension.is_empty())
167 to_extension(extension.as_bytes()).ok_or_else(|| Error::InvalidFormatFlag {
168 text: input.to_owned(),
169 reason: format!("Unsupported extension '{}'", extension),
172 .collect::<crate::Result<_>>()?;
174 if extensions.is_empty() {
175 return Err(Error::InvalidFormatFlag {
176 text: input.to_owned(),
177 reason: "Parsing got an empty list of extensions.".to_string(),
184 /// Extracts extensions from a path.
186 /// Returns both the remaining path and the list of extension objects
187 pub fn separate_known_extensions_from_name(path: &Path) -> (&Path, Vec<Extension>) {
188 let mut extensions = vec![];
190 let Some(mut name) = path.file_name().and_then(<[u8] as ByteSlice>::from_os_str) else {
191 return (path, extensions);
194 // While there is known extensions at the tail, grab them
195 while let Some(extension) = split_extension(&mut name) {
196 extensions.insert(0, extension);
199 if let Ok(name) = name.to_str() {
200 let file_stem = name.trim_matches('.');
201 if SUPPORTED_EXTENSIONS.contains(&file_stem) || SUPPORTED_ALIASES.contains(&file_stem) {
203 "Received a file with name '{file_stem}', but {file_stem} was expected as the extension"
208 (name.to_path().unwrap(), extensions)
211 /// Extracts extensions from a path, return only the list of extension objects
212 pub fn extensions_from_path(path: &Path) -> Vec<Extension> {
213 let (_, extensions) = separate_known_extensions_from_name(path);
217 /// Panics if formats has an empty list of compression formats
218 pub fn split_first_compression_format(formats: &[Extension]) -> (CompressionFormat, Vec<CompressionFormat>) {
219 let mut extensions: Vec<CompressionFormat> = flatten_compression_formats(formats);
220 let first_extension = extensions.remove(0);
221 (first_extension, extensions)
224 pub fn flatten_compression_formats(extensions: &[Extension]) -> Vec<CompressionFormat> {
227 .flat_map(|extension| extension.compression_formats.iter())
232 /// Builds a suggested output file in scenarios where the user tried to compress
233 /// a folder into a non-archive compression format, for error message purposes
235 /// E.g.: `build_suggestion("file.bz.xz", ".tar")` results in `Some("file.tar.bz.xz")`
236 pub fn build_archive_file_suggestion(path: &Path, suggested_extension: &str) -> Option<String> {
237 let path = path.to_string_lossy();
238 let mut rest = &*path;
239 let mut position_to_insert = 0;
241 // Walk through the path to find the first supported compression extension
242 while let Some(pos) = rest.find('.') {
243 // Use just the text located after the dot we found
244 rest = &rest[pos + 1..];
245 position_to_insert += pos + 1;
247 // If the string contains more chained extensions, clip to the immediate one
248 let maybe_extension = {
249 let idx = rest.find('.').unwrap_or(rest.len());
253 // If the extension we got is a supported extension, generate the suggestion
254 // at the position we found
255 if SUPPORTED_EXTENSIONS.contains(&maybe_extension) || SUPPORTED_ALIASES.contains(&maybe_extension) {
256 let mut path = path.to_string();
257 path.insert_str(position_to_insert - 1, suggested_extension);
269 use crate::utils::logger::spawn_logger_thread;
272 fn test_extensions_from_path() {
273 let path = Path::new("bolovo.tar.gz");
275 let extensions: Vec<Extension> = extensions_from_path(path);
276 let formats: Vec<CompressionFormat> = flatten_compression_formats(&extensions);
278 assert_eq!(formats, vec![Tar, Gzip]);
282 /// Test extension parsing for input/output files
283 fn test_separate_known_extensions_from_name() {
284 let _handler = spawn_logger_thread();
286 separate_known_extensions_from_name("file".as_ref()),
287 ("file".as_ref(), vec![])
290 separate_known_extensions_from_name("tar".as_ref()),
291 ("tar".as_ref(), vec![])
294 separate_known_extensions_from_name(".tar".as_ref()),
295 (".tar".as_ref(), vec![])
298 separate_known_extensions_from_name("file.tar".as_ref()),
299 ("file".as_ref(), vec![Extension::new(&[Tar], "tar")])
302 separate_known_extensions_from_name("file.tar.gz".as_ref()),
305 vec![Extension::new(&[Tar], "tar"), Extension::new(&[Gzip], "gz")]
309 separate_known_extensions_from_name(".tar.gz".as_ref()),
310 (".tar".as_ref(), vec![Extension::new(&[Gzip], "gz")])
315 /// Test extension parsing of `--format FORMAT`
316 fn test_parse_of_format_flag() {
318 parse_format_flag(OsStr::new("tar")).unwrap(),
319 vec![Extension::new(&[Tar], "tar")]
322 parse_format_flag(OsStr::new(".tar")).unwrap(),
323 vec![Extension::new(&[Tar], "tar")]
326 parse_format_flag(OsStr::new("tar.gz")).unwrap(),
327 vec![Extension::new(&[Tar], "tar"), Extension::new(&[Gzip], "gz")]
330 parse_format_flag(OsStr::new(".tar.gz")).unwrap(),
331 vec![Extension::new(&[Tar], "tar"), Extension::new(&[Gzip], "gz")]
334 parse_format_flag(OsStr::new("..tar..gz.....")).unwrap(),
335 vec![Extension::new(&[Tar], "tar"), Extension::new(&[Gzip], "gz")]
338 assert!(parse_format_flag(OsStr::new("../tar.gz")).is_err());
339 assert!(parse_format_flag(OsStr::new("targz")).is_err());
340 assert!(parse_format_flag(OsStr::new("tar.gz.unknown")).is_err());
341 assert!(parse_format_flag(OsStr::new(".tar.gz.unknown")).is_err());
342 assert!(parse_format_flag(OsStr::new(".tar.!@#.gz")).is_err());
346 fn builds_suggestion_correctly() {
347 assert_eq!(build_archive_file_suggestion(Path::new("linux.png"), ".tar"), None);
349 build_archive_file_suggestion(Path::new("linux.xz.gz.zst"), ".tar").unwrap(),
350 "linux.tar.xz.gz.zst"
353 build_archive_file_suggestion(Path::new("linux.pkg.xz.gz.zst"), ".tar").unwrap(),
354 "linux.pkg.tar.xz.gz.zst"
357 build_archive_file_suggestion(Path::new("linux.pkg.zst"), ".tar").unwrap(),
361 build_archive_file_suggestion(Path::new("linux.pkg.info.zst"), ".tar").unwrap(),
362 "linux.pkg.info.tar.zst"