-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move format enum into its own module
- Loading branch information
1 parent
435fb9b
commit 347b55c
Showing
2 changed files
with
136 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
use std::{fmt, str::FromStr}; | ||
|
||
use super::util::split_filename_and_extensions; | ||
|
||
/** | ||
An artifact format supported by Rokit. | ||
*/ | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
pub enum ArtifactFormat { | ||
Zip, | ||
Tar, | ||
TarGz, | ||
} | ||
|
||
impl ArtifactFormat { | ||
#[must_use] | ||
pub fn as_str(&self) -> &'static str { | ||
match self { | ||
Self::Zip => "zip", | ||
Self::Tar => "tar", | ||
Self::TarGz => "tar.gz", | ||
} | ||
} | ||
|
||
#[must_use] | ||
pub fn from_extensions<'a>(extensions: impl AsRef<[&'a str]>) -> Option<Self> { | ||
match extensions.as_ref() { | ||
[.., ext] if ext.eq_ignore_ascii_case("zip") => Some(Self::Zip), | ||
[.., ext] if ext.eq_ignore_ascii_case("tar") => Some(Self::Tar), | ||
[.., ext] if ext.eq_ignore_ascii_case("tgz") => Some(Self::TarGz), | ||
[.., ext1, ext2] | ||
if ext1.eq_ignore_ascii_case("tar") && ext2.eq_ignore_ascii_case("gz") => | ||
{ | ||
Some(Self::TarGz) | ||
} | ||
_ => None, | ||
} | ||
} | ||
|
||
#[must_use] | ||
pub fn from_path_or_url(path_or_url: impl AsRef<str>) -> Option<Self> { | ||
let path_or_url = path_or_url.as_ref(); | ||
let (_, extensions) = split_filename_and_extensions(path_or_url); | ||
Self::from_extensions(extensions) | ||
} | ||
} | ||
|
||
impl FromStr for ArtifactFormat { | ||
type Err = String; | ||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
let l = s.trim().to_lowercase(); | ||
match l.as_str() { | ||
"zip" => Ok(Self::Zip), | ||
"tar" => Ok(Self::Tar), | ||
"tar.gz" | "tgz" => Ok(Self::TarGz), | ||
_ => Err(format!("unknown artifact format '{l}'")), | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Display for ArtifactFormat { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
self.as_str().fmt(f) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
fn format_from_str(s: &str) -> Option<ArtifactFormat> { | ||
let (_, extensions) = split_filename_and_extensions(s); | ||
ArtifactFormat::from_extensions(extensions) | ||
} | ||
|
||
#[test] | ||
fn format_from_extensions_valid() { | ||
assert_eq!(format_from_str("file.zip"), Some(ArtifactFormat::Zip)); | ||
assert_eq!(format_from_str("file.tar"), Some(ArtifactFormat::Tar)); | ||
assert_eq!(format_from_str("file.tar.gz"), Some(ArtifactFormat::TarGz)); | ||
assert_eq!( | ||
format_from_str("file.with.many.extensions.tar.gz.zip"), | ||
Some(ArtifactFormat::Zip) | ||
); | ||
assert_eq!( | ||
format_from_str("file.with.many.extensions.zip.gz.tar"), | ||
Some(ArtifactFormat::Tar) | ||
); | ||
assert_eq!( | ||
format_from_str("file.with.many.extensions.tar.gz"), | ||
Some(ArtifactFormat::TarGz) | ||
); | ||
} | ||
|
||
#[test] | ||
fn format_from_extensions_invalid() { | ||
assert_eq!(format_from_str("file-name"), None); | ||
assert_eq!(format_from_str("some/file.exe"), None); | ||
assert_eq!(format_from_str("really.long.file.name"), None); | ||
} | ||
|
||
#[test] | ||
fn format_from_real_tools() { | ||
assert_eq!( | ||
format_from_str("wally-v0.3.2-linux.zip"), | ||
Some(ArtifactFormat::Zip) | ||
); | ||
assert_eq!( | ||
format_from_str("lune-0.8.6-macos-aarch64.zip"), | ||
Some(ArtifactFormat::Zip) | ||
); | ||
assert_eq!( | ||
format_from_str("just-1.31.0-aarch64-apple-darwin.tar.gz"), | ||
Some(ArtifactFormat::TarGz) | ||
); | ||
assert_eq!( | ||
format_from_str("sentry-cli-linux-i686-2.32.1.tgz"), | ||
Some(ArtifactFormat::TarGz) | ||
); | ||
} | ||
|
||
#[test] | ||
fn format_case_sensitivity() { | ||
assert_eq!(format_from_str("file.ZIP"), Some(ArtifactFormat::Zip)); | ||
assert_eq!(format_from_str("file.zip"), Some(ArtifactFormat::Zip)); | ||
assert_eq!(format_from_str("file.Zip"), Some(ArtifactFormat::Zip)); | ||
assert_eq!(format_from_str("file.tar"), Some(ArtifactFormat::Tar)); | ||
assert_eq!(format_from_str("file.TAR"), Some(ArtifactFormat::Tar)); | ||
assert_eq!(format_from_str("file.Tar"), Some(ArtifactFormat::Tar)); | ||
assert_eq!(format_from_str("file.tar.gz"), Some(ArtifactFormat::TarGz)); | ||
assert_eq!(format_from_str("file.TAR.GZ"), Some(ArtifactFormat::TarGz)); | ||
assert_eq!(format_from_str("file.Tar.Gz"), Some(ArtifactFormat::TarGz)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters