use std::fmt;
#[derive(Clone, Copy, Debug)]
pub enum Format {
#[cfg(feature = "tar")]
Tar,
#[cfg(all(
feature = "tar",
any(feature = "bzip2", feature = "bzip2-rs")
))]
TarBzip2,
#[cfg(all(feature = "tar", feature = "flate2"))]
TarGzip,
#[cfg(all(feature = "tar", feature = "lz4"))]
TarLz4,
#[cfg(all(feature = "tar", feature = "xz2"))]
TarXz,
#[cfg(all(feature = "tar", feature = "zstd"))]
TarZstd,
}
impl Format {
#[must_use]
pub const fn name(&self) -> &'static str {
match self {
#[cfg(feature = "tar")]
Self::Tar => "Tar",
#[cfg(all(
feature = "tar",
any(feature = "bzip2", feature = "bzip2-rs")
))]
Self::TarBzip2 => "TarBzip2",
#[cfg(all(feature = "tar", feature = "flate2"))]
Self::TarGzip => "TarGz",
#[cfg(all(feature = "tar", feature = "lz4"))]
Self::TarLz4 => "TarLz4",
#[cfg(all(feature = "tar", feature = "xz2"))]
Self::TarXz => "TarXz",
#[cfg(all(feature = "tar", feature = "zstd"))]
Self::TarZstd => "TarZstd",
}
}
#[must_use]
pub const fn description(&self) -> &'static str {
match self {
#[cfg(feature = "tar")]
Self::Tar => "tarball",
#[cfg(all(
feature = "tar",
any(feature = "bzip2", feature = "bzip2-rs")
))]
Self::TarBzip2 => "bzip2-compressed tarball",
#[cfg(all(feature = "tar", feature = "flate2"))]
Self::TarGzip => "gzip-compressed tarball",
#[cfg(all(feature = "tar", feature = "lz4"))]
Self::TarLz4 => "lz4-compressed tarball",
#[cfg(all(feature = "tar", feature = "xz2"))]
Self::TarXz => "xz-compressed tarball",
#[cfg(all(feature = "tar", feature = "zstd"))]
Self::TarZstd => "zstd-compressed tarball",
}
}
#[must_use]
pub fn file_endings(&self) -> Vec<&'static str> {
match self {
#[cfg(feature = "tar")]
Self::Tar => vec!["*.tar"],
#[cfg(all(
feature = "tar",
any(feature = "bzip2", feature = "bzip2-rs")
))]
Self::TarBzip2 => {
vec!["*.tar.bz2", "*.tbz", "*.tbz2"]
}
#[cfg(all(feature = "tar", feature = "flate2"))]
Self::TarGzip => vec!["*.tar.gz", "*.tgz"],
#[cfg(all(feature = "tar", feature = "lz4"))]
Self::TarLz4 => vec!["*.tar.lz4"],
#[cfg(all(feature = "tar", feature = "xz2"))]
Self::TarXz => vec!["*.tar.xz", "*.txz"],
#[cfg(all(feature = "tar", feature = "zstd"))]
Self::TarZstd => vec!["*.tar.zst"],
}
}
#[must_use]
pub const fn all_names<'a>() -> &'a [Self] {
&[
#[cfg(feature = "tar")]
Self::Tar,
#[cfg(all(
feature = "tar",
any(feature = "bzip2", feature = "bzip2-rs")
))]
Self::TarBzip2,
#[cfg(all(feature = "tar", feature = "flate2"))]
Self::TarGzip,
#[cfg(all(feature = "tar", feature = "lz4"))]
Self::TarLz4,
#[cfg(all(feature = "tar", feature = "xz2"))]
Self::TarXz,
#[cfg(all(feature = "tar", feature = "zstd"))]
Self::TarZstd,
]
}
#[must_use]
pub fn all_file_endings() -> Vec<String> {
vec![
#[cfg(feature = "tar")]
Self::Tar.describe(),
#[cfg(all(
feature = "tar",
any(feature = "bzip2", feature = "bzip2-rs")
))]
Self::TarBzip2.describe(),
#[cfg(all(feature = "tar", feature = "flate2"))]
Self::TarGzip.describe(),
#[cfg(all(feature = "tar", feature = "lz4"))]
Self::TarLz4.describe(),
#[cfg(all(feature = "tar", feature = "xz2"))]
Self::TarXz.describe(),
#[cfg(all(feature = "tar", feature = "zstd"))]
Self::TarZstd.describe(),
]
}
fn describe(self) -> String {
format!(
"{} {} {:?}",
self.name(),
self.description(),
self.file_endings()
)
}
}
impl fmt::Display for Format {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.description())
}
}