Address clippy issues

This commit is contained in:
Ryan Levick 2020-06-15 11:11:17 +02:00
parent ebb07348ee
commit 31b4b5c842
6 changed files with 89 additions and 87 deletions

View file

@ -29,7 +29,7 @@ impl FromCp437 for Vec<u8> {
if self.iter().all(|c| *c < 0x80) {
String::from_utf8(self).unwrap()
} else {
self.into_iter().map(|c| to_char(c)).collect()
self.into_iter().map(to_char).collect()
}
}
}

View file

@ -16,7 +16,7 @@ impl<R> Crc32Reader<R> {
/// Get a new Crc32Reader which check the inner reader against checksum.
pub fn new(inner: R, checksum: u32) -> Crc32Reader<R> {
Crc32Reader {
inner: inner,
inner,
hasher: Hasher::new(),
check: checksum,
}

View file

@ -152,7 +152,7 @@ impl<R: Read + io::Seek> ZipArchive<R> {
let directory_start = footer.central_directory_offset as u64 + archive_offset;
let number_of_files = footer.number_of_files_on_this_disk as usize;
return Ok((archive_offset, directory_start, number_of_files));
Ok((archive_offset, directory_start, number_of_files))
}
Some(locator64) => {
// If we got here, this is indeed a ZIP64 file.
@ -225,9 +225,9 @@ impl<R: Read + io::Seek> ZipArchive<R> {
}
Ok(ZipArchive {
reader: reader,
files: files,
names_map: names_map,
reader,
files,
names_map,
offset: archive_offset,
comment: footer.zip_file_comment,
})
@ -249,6 +249,11 @@ impl<R: Read + io::Seek> ZipArchive<R> {
self.files.len()
}
/// Whether this zip archive contains no files
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Get the offset from the beginning of the underlying reader that this zip begins at, in bytes.
///
/// Normally this value is zero, but if the zip has arbitrary data prepended to it, then this value will be the size
@ -283,7 +288,7 @@ impl<R: Read + io::Seek> ZipArchive<R> {
if file_number >= self.files.len() {
return Err(ZipError::FileNotFound);
}
let ref mut data = self.files[file_number];
let data = &mut self.files[file_number];
if data.encrypted {
return unsupported_zip_error("Encrypted files are not supported");
@ -365,15 +370,15 @@ fn central_header_to_zip_file<R: Read + io::Seek>(
let mut result = ZipFileData {
system: System::from_u8((version_made_by >> 8) as u8),
version_made_by: version_made_by as u8,
encrypted: encrypted,
encrypted,
compression_method: CompressionMethod::from_u16(compression_method),
last_modified_time: DateTime::from_msdos(last_mod_date, last_mod_time),
crc32: crc32,
crc32,
compressed_size: compressed_size as u64,
uncompressed_size: uncompressed_size as u64,
file_name: file_name,
file_name_raw: file_name_raw,
file_comment: file_comment,
file_name,
file_name_raw,
file_comment,
header_start: offset,
data_start: 0,
external_attributes: external_file_attributes,
@ -381,7 +386,7 @@ fn central_header_to_zip_file<R: Read + io::Seek>(
match parse_extra_field(&mut result, &*extra_field) {
Ok(..) | Err(ZipError::Io(..)) => {}
Err(e) => Err(e)?,
Err(e) => return Err(e),
}
// Account for shifted zip offsets.
@ -397,25 +402,22 @@ fn parse_extra_field(file: &mut ZipFileData, data: &[u8]) -> ZipResult<()> {
let kind = reader.read_u16::<LittleEndian>()?;
let len = reader.read_u16::<LittleEndian>()?;
let mut len_left = len as i64;
match kind {
// Zip64 extended information extra field
0x0001 => {
if file.uncompressed_size == 0xFFFFFFFF {
file.uncompressed_size = reader.read_u64::<LittleEndian>()?;
len_left -= 8;
}
if file.compressed_size == 0xFFFFFFFF {
file.compressed_size = reader.read_u64::<LittleEndian>()?;
len_left -= 8;
}
if file.header_start == 0xFFFFFFFF {
file.header_start = reader.read_u64::<LittleEndian>()?;
len_left -= 8;
}
// Unparsed fields:
// u32: disk start number
// Zip64 extended information extra field
if kind == 0x0001 {
if file.uncompressed_size == 0xFFFFFFFF {
file.uncompressed_size = reader.read_u64::<LittleEndian>()?;
len_left -= 8;
}
_ => {}
if file.compressed_size == 0xFFFFFFFF {
file.compressed_size = reader.read_u64::<LittleEndian>()?;
len_left -= 8;
}
if file.header_start == 0xFFFFFFFF {
file.header_start = reader.read_u64::<LittleEndian>()?;
len_left -= 8;
}
// Unparsed fields:
// u32: disk start number
}
// We could also check for < 0 to check for errors
@ -619,14 +621,14 @@ pub fn read_zipfile_from_stream<'a, R: io::Read>(
let mut result = ZipFileData {
system: System::from_u8((version_made_by >> 8) as u8),
version_made_by: version_made_by as u8,
encrypted: encrypted,
compression_method: compression_method,
encrypted,
compression_method,
last_modified_time: DateTime::from_msdos(last_mod_date, last_mod_time),
crc32: crc32,
crc32,
compressed_size: compressed_size as u64,
uncompressed_size: uncompressed_size as u64,
file_name: file_name,
file_name_raw: file_name_raw,
file_name,
file_name_raw,
file_comment: String::new(), // file comment is only available in the central directory
// header_start and data start are not available, but also don't matter, since seeking is
// not available.
@ -640,7 +642,7 @@ pub fn read_zipfile_from_stream<'a, R: io::Read>(
match parse_extra_field(&mut result, &extra_field) {
Ok(..) | Err(ZipError::Io(..)) => {}
Err(e) => Err(e)?,
Err(e) => return Err(e),
}
if encrypted {

View file

@ -35,13 +35,13 @@ impl CentralDirectoryEnd {
let zip_file_comment = ReadPodExt::read_exact(reader, zip_file_comment_length)?;
Ok(CentralDirectoryEnd {
disk_number: disk_number,
disk_with_central_directory: disk_with_central_directory,
number_of_files_on_this_disk: number_of_files_on_this_disk,
number_of_files: number_of_files,
central_directory_size: central_directory_size,
central_directory_offset: central_directory_offset,
zip_file_comment: zip_file_comment,
disk_number,
disk_with_central_directory,
number_of_files_on_this_disk,
number_of_files,
central_directory_size,
central_directory_offset,
zip_file_comment,
})
}
@ -52,9 +52,7 @@ impl CentralDirectoryEnd {
const BYTES_BETWEEN_MAGIC_AND_COMMENT_SIZE: u64 = HEADER_SIZE - 6;
let file_length = reader.seek(io::SeekFrom::End(0))?;
let search_upper_bound = file_length
.checked_sub(HEADER_SIZE + ::std::u16::MAX as u64)
.unwrap_or(0);
let search_upper_bound = file_length.saturating_sub(HEADER_SIZE + ::std::u16::MAX as u64);
if file_length < HEADER_SIZE {
return Err(ZipError::InvalidArchive("Invalid zip header"));
@ -116,9 +114,9 @@ impl Zip64CentralDirectoryEndLocator {
let number_of_disks = reader.read_u32::<LittleEndian>()?;
Ok(Zip64CentralDirectoryEndLocator {
disk_with_central_directory: disk_with_central_directory,
end_of_central_directory_offset: end_of_central_directory_offset,
number_of_disks: number_of_disks,
disk_with_central_directory,
end_of_central_directory_offset,
number_of_disks,
})
}
}
@ -163,14 +161,14 @@ impl Zip64CentralDirectoryEnd {
return Ok((
Zip64CentralDirectoryEnd {
version_made_by: version_made_by,
version_needed_to_extract: version_needed_to_extract,
disk_number: disk_number,
disk_with_central_directory: disk_with_central_directory,
number_of_files_on_this_disk: number_of_files_on_this_disk,
number_of_files: number_of_files,
central_directory_size: central_directory_size,
central_directory_offset: central_directory_offset,
version_made_by,
version_needed_to_extract,
disk_number,
disk_with_central_directory,
number_of_files_on_this_disk,
number_of_files,
central_directory_size,
central_directory_offset,
},
archive_offset,
));

View file

@ -1,12 +1,11 @@
//! Types that specify what is contained in a ZIP.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum System {
Dos = 0,
Unix = 3,
Unknown,
#[doc(hidden)]
__Nonexhaustive,
}
impl System {
@ -100,12 +99,12 @@ impl DateTime {
&& second <= 60
{
Ok(DateTime {
year: year,
month: month,
day: day,
hour: hour,
minute: minute,
second: second,
year,
month,
day,
hour,
minute,
second,
})
} else {
Err(())
@ -250,7 +249,7 @@ impl ZipFileData {
let separator = ::std::path::MAIN_SEPARATOR;
let opposite_separator = match separator {
'/' => '\\',
'\\' | _ => '/',
_ => '/',
};
let filename =
no_null_filename.replace(&opposite_separator.to_string(), &separator.to_string());

View file

@ -82,7 +82,7 @@ impl FileOptions {
#[cfg(not(feature = "deflate"))]
compression_method: CompressionMethod::Stored,
#[cfg(feature = "time")]
last_modified_time: DateTime::from_time(time::now()).unwrap_or(DateTime::default()),
last_modified_time: DateTime::from_time(time::now()).unwrap_or_default(),
#[cfg(not(feature = "time"))]
last_modified_time: DateTime::default(),
permissions: None,
@ -211,10 +211,10 @@ impl<W: Write + io::Seek> ZipWriter<W> {
crc32: 0,
compressed_size: 0,
uncompressed_size: 0,
file_name: file_name,
file_name_raw: file_name_raw,
file_name,
file_name_raw,
file_comment: String::new(),
header_start: header_start,
header_start,
data_start: 0,
external_attributes: permissions << 16,
};
@ -316,7 +316,7 @@ impl<W: Write + io::Seek> ZipWriter<W> {
path: &std::path::Path,
options: FileOptions,
) -> ZipResult<()> {
self.add_directory(path_to_string(path.into()), options)
self.add_directory(path_to_string(path), options)
}
/// Finish the last file and write all other zip-structures
@ -372,10 +372,13 @@ impl<W: Write + io::Seek> GenericZipWriter<W> {
fn switch_to(&mut self, compression: CompressionMethod) -> ZipResult<()> {
match self.current_compression() {
Some(method) if method == compression => return Ok(()),
None => Err(io::Error::new(
io::ErrorKind::BrokenPipe,
"ZipWriter was already closed",
))?,
None => {
return Err(io::Error::new(
io::ErrorKind::BrokenPipe,
"ZipWriter was already closed",
)
.into())
}
_ => {}
}
@ -385,10 +388,13 @@ impl<W: Write + io::Seek> GenericZipWriter<W> {
GenericZipWriter::Deflater(w) => w.finish()?,
#[cfg(feature = "bzip2")]
GenericZipWriter::Bzip2(w) => w.finish()?,
GenericZipWriter::Closed => Err(io::Error::new(
io::ErrorKind::BrokenPipe,
"ZipWriter was already closed",
))?,
GenericZipWriter::Closed => {
return Err(io::Error::new(
io::ErrorKind::BrokenPipe,
"ZipWriter was already closed",
)
.into())
}
};
*self = match compression {
@ -562,14 +568,11 @@ fn build_extra_field(_file: &ZipFileData) -> ZipResult<Vec<u8>> {
fn path_to_string(path: &std::path::Path) -> String {
let mut path_str = String::new();
for component in path.components() {
match component {
std::path::Component::Normal(os_str) => {
if path_str.len() != 0 {
path_str.push('/');
}
path_str.push_str(&*os_str.to_string_lossy());
if let std::path::Component::Normal(os_str) = component {
if !path_str.is_empty() {
path_str.push('/');
}
_ => (),
path_str.push_str(&*os_str.to_string_lossy());
}
}
path_str