Skip to content

Commit

Permalink
Simplify some code
Browse files Browse the repository at this point in the history
  • Loading branch information
autarch committed May 27, 2024
1 parent 6973ba1 commit 41e5672
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,23 @@ impl Extension {

pub(crate) fn from_path<S: AsRef<str>>(path: S) -> Result<Option<Extension>> {
let path = path.as_ref();
let Some(ext_str) = Path::new(path).extension() else {
return Ok(None);
};

// We need to try the longest extension first so that ".tar.gz"
// matches before ".gz" and so on for other compression formats.
if let Some(ext) = Extension::iter()
match Extension::iter()
.sorted_by(|a, b| Ord::cmp(&a.extension().len(), &b.extension().len()))
.rev()
.find(|e| path.ends_with(e.extension()))
{
Ok(Some(ext))
} else {
if let Some(ext) = Path::new(path).extension() {
return Err(ExtensionError::UnknownExtension {
path: path.to_string(),
ext: ext.to_string_lossy().to_string(),
}
.into());
Some(ext) => Ok(Some(ext)),
None => Err(ExtensionError::UnknownExtension {
path: path.to_string(),
ext: ext_str.to_string_lossy().to_string(),
}
Ok(None)
.into()),
}
}
}
Expand Down

0 comments on commit 41e5672

Please sign in to comment.