Skip to content

Commit

Permalink
Refactor: replace contains_file_named with index_for_name
Browse files Browse the repository at this point in the history
  • Loading branch information
Pr0methean committed Apr 20, 2024
1 parent c22afbf commit 81e44d1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,5 +273,5 @@

### Added

- `contains_file_named`: check whether a file entry exists in a zip file, without initializing the
metadata or needing to mutably borrow the `ZipArchive`.
- `index_for_name`: get the index of a file given its name, without initializing metadata or needing to mutably borrow
the `ZipArchive`.
14 changes: 6 additions & 8 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,21 +651,19 @@ impl<R: Read + Seek> ZipArchive<R> {
self.by_name_with_optional_password(name, None)
}

/// Check for a file entry, but do not decrypt it or initialize metadata.
pub fn contains_file_named(&self, name: &str) -> bool {
self.shared.names_map.contains_key(name)
/// Get the index of a file entry by name, if it's present.
#[inline(always)]
pub fn index_for_name(&self, name: &str) -> Option<usize> {
self.shared.names_map.get(name).map(|index_ref| *index_ref)

Check failure on line 657 in src/read.rs

View workflow job for this annotation

GitHub Actions / clippy

you are using an explicit closure for copying elements
}

fn by_name_with_optional_password<'a>(
&'a mut self,
name: &str,
password: Option<&[u8]>,
) -> ZipResult<ZipFile<'a>> {
let index = match self.shared.names_map.get(name) {
Some(index) => *index,
None => {
return Err(ZipError::FileNotFound);
}
let Some(index) = self.index_for_name(name) else {
return Err(ZipError::FileNotFound);
};
self.by_index_with_optional_password(index, password)
}
Expand Down

0 comments on commit 81e44d1

Please sign in to comment.