fix: fix Clippy warnings
- fix a bunch of Clippy warnings - fix some usages of assert! (change to assert_ne) Tested: - Local unit-tests run
This commit is contained in:
parent
82cb917883
commit
061cdf149f
11 changed files with 43 additions and 44 deletions
|
@ -59,5 +59,6 @@ fn real_main() -> i32 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
|
0
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,5 +27,5 @@ fn real_main() -> i32 {
|
||||||
file.read_to_string(&mut contents).unwrap();
|
file.read_to_string(&mut contents).unwrap();
|
||||||
println!("{}", contents);
|
println!("{}", contents);
|
||||||
|
|
||||||
return 0;
|
0
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,5 +49,6 @@ fn real_main() -> i32 {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
|
0
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,5 +30,6 @@ fn real_main() -> i32 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
|
0
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,7 +54,7 @@ fn real_main() -> i32 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn zip_dir<T>(
|
fn zip_dir<T>(
|
||||||
|
|
|
@ -18,7 +18,7 @@ fn real_main() -> i32 {
|
||||||
Err(e) => println!("Error: {:?}", e),
|
Err(e) => println!("Error: {:?}", e),
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn doit(filename: &str) -> zip::result::ZipResult<()> {
|
fn doit(filename: &str) -> zip::result::ZipResult<()> {
|
||||||
|
|
25
src/read.rs
25
src/read.rs
|
@ -189,11 +189,11 @@ fn make_crypto_reader<'a>(
|
||||||
Ok(Ok(reader))
|
Ok(Ok(reader))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_reader<'a>(
|
fn make_reader(
|
||||||
compression_method: CompressionMethod,
|
compression_method: CompressionMethod,
|
||||||
crc32: u32,
|
crc32: u32,
|
||||||
reader: CryptoReader<'a>,
|
reader: CryptoReader,
|
||||||
) -> ZipFileReader<'a> {
|
) -> ZipFileReader {
|
||||||
match compression_method {
|
match compression_method {
|
||||||
CompressionMethod::Stored => ZipFileReader::Stored(Crc32Reader::new(reader, crc32)),
|
CompressionMethod::Stored => ZipFileReader::Stored(Crc32Reader::new(reader, crc32)),
|
||||||
#[cfg(any(
|
#[cfg(any(
|
||||||
|
@ -303,7 +303,7 @@ impl<R: Read + io::Seek> ZipArchive<R> {
|
||||||
let directory_start = footer
|
let directory_start = footer
|
||||||
.central_directory_offset
|
.central_directory_offset
|
||||||
.checked_add(archive_offset)
|
.checked_add(archive_offset)
|
||||||
.ok_or_else(|| {
|
.ok_or({
|
||||||
ZipError::InvalidArchive("Invalid central directory size or offset")
|
ZipError::InvalidArchive("Invalid central directory size or offset")
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
@ -332,7 +332,7 @@ impl<R: Read + io::Seek> ZipArchive<R> {
|
||||||
let mut files = Vec::new();
|
let mut files = Vec::new();
|
||||||
let mut names_map = HashMap::new();
|
let mut names_map = HashMap::new();
|
||||||
|
|
||||||
if let Err(_) = reader.seek(io::SeekFrom::Start(directory_start)) {
|
if reader.seek(io::SeekFrom::Start(directory_start)).is_err() {
|
||||||
return Err(ZipError::InvalidArchive(
|
return Err(ZipError::InvalidArchive(
|
||||||
"Could not seek to start of central directory",
|
"Could not seek to start of central directory",
|
||||||
));
|
));
|
||||||
|
@ -457,14 +457,14 @@ impl<R: Read + io::Seek> ZipArchive<R> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a contained file by index
|
/// Get a contained file by index
|
||||||
pub fn by_index<'a>(&'a mut self, file_number: usize) -> ZipResult<ZipFile<'a>> {
|
pub fn by_index(&mut self, file_number: usize) -> ZipResult<ZipFile> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.by_index_with_optional_password(file_number, None)?
|
.by_index_with_optional_password(file_number, None)?
|
||||||
.unwrap())
|
.unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a contained file by index without decompressing it
|
/// Get a contained file by index without decompressing it
|
||||||
pub fn by_index_raw<'a>(&'a mut self, file_number: usize) -> ZipResult<ZipFile<'a>> {
|
pub fn by_index_raw(&mut self, file_number: usize) -> ZipResult<ZipFile> {
|
||||||
let reader = &mut self.reader;
|
let reader = &mut self.reader;
|
||||||
self.files
|
self.files
|
||||||
.get_mut(file_number)
|
.get_mut(file_number)
|
||||||
|
@ -1020,7 +1020,7 @@ mod test {
|
||||||
let mut v = Vec::new();
|
let mut v = Vec::new();
|
||||||
v.extend_from_slice(include_bytes!("../tests/data/zip64_demo.zip"));
|
v.extend_from_slice(include_bytes!("../tests/data/zip64_demo.zip"));
|
||||||
let reader = ZipArchive::new(io::Cursor::new(v)).unwrap();
|
let reader = ZipArchive::new(io::Cursor::new(v)).unwrap();
|
||||||
assert!(reader.len() == 1);
|
assert_eq!(reader.len(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -1031,7 +1031,7 @@ mod test {
|
||||||
let mut v = Vec::new();
|
let mut v = Vec::new();
|
||||||
v.extend_from_slice(include_bytes!("../tests/data/mimetype.zip"));
|
v.extend_from_slice(include_bytes!("../tests/data/mimetype.zip"));
|
||||||
let mut reader = ZipArchive::new(io::Cursor::new(v)).unwrap();
|
let mut reader = ZipArchive::new(io::Cursor::new(v)).unwrap();
|
||||||
assert!(reader.comment() == b"");
|
assert_eq!(reader.comment(), b"");
|
||||||
assert_eq!(reader.by_index(0).unwrap().central_header_start(), 77);
|
assert_eq!(reader.by_index(0).unwrap().central_header_start(), 77);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1044,9 +1044,8 @@ mod test {
|
||||||
v.extend_from_slice(include_bytes!("../tests/data/mimetype.zip"));
|
v.extend_from_slice(include_bytes!("../tests/data/mimetype.zip"));
|
||||||
let mut reader = io::Cursor::new(v);
|
let mut reader = io::Cursor::new(v);
|
||||||
loop {
|
loop {
|
||||||
match read_zipfile_from_stream(&mut reader).unwrap() {
|
if read_zipfile_from_stream(&mut reader).unwrap().is_none() {
|
||||||
None => break,
|
break;
|
||||||
_ => (),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1089,7 +1088,7 @@ mod test {
|
||||||
|
|
||||||
assert_eq!(buf1, buf2);
|
assert_eq!(buf1, buf2);
|
||||||
assert_eq!(buf3, buf4);
|
assert_eq!(buf3, buf4);
|
||||||
assert!(buf1 != buf3);
|
assert_ne!(buf1, buf3);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
15
src/types.rs
15
src/types.rs
|
@ -65,7 +65,7 @@ impl DateTime {
|
||||||
let seconds = (timepart & 0b0000000000011111) << 1;
|
let seconds = (timepart & 0b0000000000011111) << 1;
|
||||||
let minutes = (timepart & 0b0000011111100000) >> 5;
|
let minutes = (timepart & 0b0000011111100000) >> 5;
|
||||||
let hours = (timepart & 0b1111100000000000) >> 11;
|
let hours = (timepart & 0b1111100000000000) >> 11;
|
||||||
let days = (datepart & 0b0000000000011111) >> 0;
|
let days = datepart & 0b0000000000011111;
|
||||||
let months = (datepart & 0b0000000111100000) >> 5;
|
let months = (datepart & 0b0000000111100000) >> 5;
|
||||||
let years = (datepart & 0b1111111000000000) >> 9;
|
let years = (datepart & 0b1111111000000000) >> 9;
|
||||||
|
|
||||||
|
@ -256,10 +256,7 @@ impl ZipFileData {
|
||||||
|
|
||||||
::std::path::Path::new(&filename)
|
::std::path::Path::new(&filename)
|
||||||
.components()
|
.components()
|
||||||
.filter(|component| match *component {
|
.filter(|component| matches!(*component, ::std::path::Component::Normal(..)))
|
||||||
::std::path::Component::Normal(..) => true,
|
|
||||||
_ => false,
|
|
||||||
})
|
|
||||||
.fold(::std::path::PathBuf::new(), |mut path, ref cur| {
|
.fold(::std::path::PathBuf::new(), |mut path, ref cur| {
|
||||||
path.push(cur.as_os_str());
|
path.push(cur.as_os_str());
|
||||||
path
|
path
|
||||||
|
@ -329,15 +326,15 @@ mod test {
|
||||||
use super::DateTime;
|
use super::DateTime;
|
||||||
let dt = DateTime::default();
|
let dt = DateTime::default();
|
||||||
assert_eq!(dt.timepart(), 0);
|
assert_eq!(dt.timepart(), 0);
|
||||||
assert_eq!(dt.datepart(), 0b0000000_0001_00001);
|
assert_eq!(dt.datepart(), 0b0000_0000_0010_0001);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn datetime_max() {
|
fn datetime_max() {
|
||||||
use super::DateTime;
|
use super::DateTime;
|
||||||
let dt = DateTime::from_date_and_time(2107, 12, 31, 23, 59, 60).unwrap();
|
let dt = DateTime::from_date_and_time(2107, 12, 31, 23, 59, 60).unwrap();
|
||||||
assert_eq!(dt.timepart(), 0b10111_111011_11110);
|
assert_eq!(dt.timepart(), 0b1011_1111_0111_1110);
|
||||||
assert_eq!(dt.datepart(), 0b1111111_1100_11111);
|
assert_eq!(dt.datepart(), 0b1111_1111_1001_1111);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -394,7 +391,7 @@ mod test {
|
||||||
|
|
||||||
#[cfg(feature = "time")]
|
#[cfg(feature = "time")]
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
format!("{}", dt.to_time().unwrap().format(&Rfc3339).unwrap()),
|
dt.to_time().unwrap().format(&Rfc3339).unwrap(),
|
||||||
"2018-11-17T10:38:30Z"
|
"2018-11-17T10:38:30Z"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
18
src/write.rs
18
src/write.rs
|
@ -237,7 +237,10 @@ impl<A: Read + Write + io::Seek> ZipWriter<A> {
|
||||||
let (archive_offset, directory_start, number_of_files) =
|
let (archive_offset, directory_start, number_of_files) =
|
||||||
ZipArchive::get_directory_counts(&mut readwriter, &footer, cde_start_pos)?;
|
ZipArchive::get_directory_counts(&mut readwriter, &footer, cde_start_pos)?;
|
||||||
|
|
||||||
if let Err(_) = readwriter.seek(io::SeekFrom::Start(directory_start)) {
|
if readwriter
|
||||||
|
.seek(io::SeekFrom::Start(directory_start))
|
||||||
|
.is_err()
|
||||||
|
{
|
||||||
return Err(ZipError::InvalidArchive(
|
return Err(ZipError::InvalidArchive(
|
||||||
"Could not seek to start of central directory",
|
"Could not seek to start of central directory",
|
||||||
));
|
));
|
||||||
|
@ -307,7 +310,7 @@ impl<W: Write + io::Seek> ZipWriter<W> {
|
||||||
{
|
{
|
||||||
self.finish_file()?;
|
self.finish_file()?;
|
||||||
|
|
||||||
let raw_values = raw_values.unwrap_or_else(|| ZipRawValues {
|
let raw_values = raw_values.unwrap_or(ZipRawValues {
|
||||||
crc32: 0,
|
crc32: 0,
|
||||||
compressed_size: 0,
|
compressed_size: 0,
|
||||||
uncompressed_size: 0,
|
uncompressed_size: 0,
|
||||||
|
@ -548,7 +551,7 @@ impl<W: Write + io::Seek> ZipWriter<W> {
|
||||||
}
|
}
|
||||||
let file = self.files.last_mut().unwrap();
|
let file = self.files.last_mut().unwrap();
|
||||||
|
|
||||||
validate_extra_data(&file)?;
|
validate_extra_data(file)?;
|
||||||
|
|
||||||
if !self.writing_to_central_extra_field_only {
|
if !self.writing_to_central_extra_field_only {
|
||||||
let writer = self.inner.get_plain();
|
let writer = self.inner.get_plain();
|
||||||
|
@ -858,10 +861,7 @@ impl<W: Write + io::Seek> GenericZipWriter<W> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_closed(&self) -> bool {
|
fn is_closed(&self) -> bool {
|
||||||
match *self {
|
matches!(*self, GenericZipWriter::Closed)
|
||||||
GenericZipWriter::Closed => true,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_plain(&mut self) -> &mut W {
|
fn get_plain(&mut self) -> &mut W {
|
||||||
|
@ -935,7 +935,7 @@ fn write_local_file_header<T: Write>(writer: &mut T, file: &ZipFileData) -> ZipR
|
||||||
writer.write_all(file.file_name.as_bytes())?;
|
writer.write_all(file.file_name.as_bytes())?;
|
||||||
// zip64 extra field
|
// zip64 extra field
|
||||||
if file.large_file {
|
if file.large_file {
|
||||||
write_local_zip64_extra_field(writer, &file)?;
|
write_local_zip64_extra_field(writer, file)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -1053,7 +1053,7 @@ fn validate_extra_data(file: &ZipFileData) -> ZipResult<()> {
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
while data.len() > 0 {
|
while !data.is_empty() {
|
||||||
let left = data.len();
|
let left = data.len();
|
||||||
if left < 4 {
|
if left < 4 {
|
||||||
return Err(ZipError::Io(io::Error::new(
|
return Err(ZipError::Io(io::Error::new(
|
||||||
|
|
|
@ -47,7 +47,7 @@ impl ZipCryptoKeys {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn crc32(crc: Wrapping<u32>, input: u8) -> Wrapping<u32> {
|
fn crc32(crc: Wrapping<u32>, input: u8) -> Wrapping<u32> {
|
||||||
return (crc >> 8) ^ Wrapping(CRCTABLE[((crc & Wrapping(0xff)).0 as u8 ^ input) as usize]);
|
(crc >> 8) ^ Wrapping(CRCTABLE[((crc & Wrapping(0xff)).0 as u8 ^ input) as usize])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,7 +71,7 @@ impl<R: std::io::Read> ZipCryptoReader<R> {
|
||||||
/// password byte sequence that is unrepresentable in UTF-8.
|
/// password byte sequence that is unrepresentable in UTF-8.
|
||||||
pub fn new(file: R, password: &[u8]) -> ZipCryptoReader<R> {
|
pub fn new(file: R, password: &[u8]) -> ZipCryptoReader<R> {
|
||||||
let mut result = ZipCryptoReader {
|
let mut result = ZipCryptoReader {
|
||||||
file: file,
|
file,
|
||||||
keys: ZipCryptoKeys::new(),
|
keys: ZipCryptoKeys::new(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -129,11 +129,11 @@ pub struct ZipCryptoReaderValid<R> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R: std::io::Read> std::io::Read for ZipCryptoReaderValid<R> {
|
impl<R: std::io::Read> std::io::Read for ZipCryptoReaderValid<R> {
|
||||||
fn read(&mut self, mut buf: &mut [u8]) -> std::io::Result<usize> {
|
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||||
// Note: There might be potential for optimization. Inspiration can be found at:
|
// Note: There might be potential for optimization. Inspiration can be found at:
|
||||||
// https://github.com/kornelski/7z/blob/master/CPP/7zip/Crypto/ZipCrypto.cpp
|
// https://github.com/kornelski/7z/blob/master/CPP/7zip/Crypto/ZipCrypto.cpp
|
||||||
|
|
||||||
let result = self.reader.file.read(&mut buf);
|
let result = self.reader.file.read(buf);
|
||||||
for byte in buf.iter_mut() {
|
for byte in buf.iter_mut() {
|
||||||
*byte = self.reader.keys.decrypt_byte(*byte);
|
*byte = self.reader.keys.decrypt_byte(*byte);
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,7 +100,7 @@ fn read_zip<R: Read + Seek>(zip_file: R) -> zip::result::ZipResult<zip::ZipArchi
|
||||||
"test_with_extra_data/🐢.txt",
|
"test_with_extra_data/🐢.txt",
|
||||||
ENTRY_NAME,
|
ENTRY_NAME,
|
||||||
];
|
];
|
||||||
let expected_file_names = HashSet::from_iter(expected_file_names.iter().map(|&v| v));
|
let expected_file_names = HashSet::from_iter(expected_file_names.iter().copied());
|
||||||
let file_names = archive.file_names().collect::<HashSet<_>>();
|
let file_names = archive.file_names().collect::<HashSet<_>>();
|
||||||
assert_eq!(file_names, expected_file_names);
|
assert_eq!(file_names, expected_file_names);
|
||||||
|
|
||||||
|
@ -134,7 +134,7 @@ fn check_zip_contents(zip_file: &mut Cursor<Vec<u8>>, name: &str) {
|
||||||
|
|
||||||
fn check_zip_file_contents<R: Read + Seek>(archive: &mut zip::ZipArchive<R>, name: &str) {
|
fn check_zip_file_contents<R: Read + Seek>(archive: &mut zip::ZipArchive<R>, name: &str) {
|
||||||
let file_contents: String = read_zip_file(archive, name).unwrap();
|
let file_contents: String = read_zip_file(archive, name).unwrap();
|
||||||
assert!(file_contents.as_bytes() == LOREM_IPSUM);
|
assert_eq!(file_contents.as_bytes(), LOREM_IPSUM);
|
||||||
}
|
}
|
||||||
|
|
||||||
const LOREM_IPSUM : &'static [u8] = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In tellus elit, tristique vitae mattis egestas, ultricies vitae risus. Quisque sit amet quam ut urna aliquet
|
const LOREM_IPSUM : &'static [u8] = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In tellus elit, tristique vitae mattis egestas, ultricies vitae risus. Quisque sit amet quam ut urna aliquet
|
||||||
|
@ -144,7 +144,7 @@ vitae tristique consectetur, neque lectus pulvinar dui, sed feugiat purus diam i
|
||||||
inceptos himenaeos. Maecenas feugiat velit in ex ultrices scelerisque id id neque.
|
inceptos himenaeos. Maecenas feugiat velit in ex ultrices scelerisque id id neque.
|
||||||
";
|
";
|
||||||
|
|
||||||
const EXTRA_DATA: &'static [u8] = b"Extra Data";
|
const EXTRA_DATA: &[u8] = b"Extra Data";
|
||||||
|
|
||||||
const ENTRY_NAME: &str = "test/lorem_ipsum.txt";
|
const ENTRY_NAME: &str = "test/lorem_ipsum.txt";
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue