Refactor: replace contains_file_named with index_for_name
This commit is contained in:
parent
c22afbf3c7
commit
81e44d1d41
2 changed files with 8 additions and 10 deletions
|
@ -273,5 +273,5 @@
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
- `contains_file_named`: check whether a file entry exists in a zip file, without initializing the
|
- `index_for_name`: get the index of a file given its name, without initializing metadata or needing to mutably borrow
|
||||||
metadata or needing to mutably borrow the `ZipArchive`.
|
the `ZipArchive`.
|
14
src/read.rs
14
src/read.rs
|
@ -651,9 +651,10 @@ impl<R: Read + Seek> ZipArchive<R> {
|
||||||
self.by_name_with_optional_password(name, None)
|
self.by_name_with_optional_password(name, None)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check for a file entry, but do not decrypt it or initialize metadata.
|
/// Get the index of a file entry by name, if it's present.
|
||||||
pub fn contains_file_named(&self, name: &str) -> bool {
|
#[inline(always)]
|
||||||
self.shared.names_map.contains_key(name)
|
pub fn index_for_name(&self, name: &str) -> Option<usize> {
|
||||||
|
self.shared.names_map.get(name).map(|index_ref| *index_ref)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn by_name_with_optional_password<'a>(
|
fn by_name_with_optional_password<'a>(
|
||||||
|
@ -661,11 +662,8 @@ impl<R: Read + Seek> ZipArchive<R> {
|
||||||
name: &str,
|
name: &str,
|
||||||
password: Option<&[u8]>,
|
password: Option<&[u8]>,
|
||||||
) -> ZipResult<ZipFile<'a>> {
|
) -> ZipResult<ZipFile<'a>> {
|
||||||
let index = match self.shared.names_map.get(name) {
|
let Some(index) = self.index_for_name(name) else {
|
||||||
Some(index) => *index,
|
return Err(ZipError::FileNotFound);
|
||||||
None => {
|
|
||||||
return Err(ZipError::FileNotFound);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
self.by_index_with_optional_password(index, password)
|
self.by_index_with_optional_password(index, password)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue