Skip to content

Commit

Permalink
fix: Fixed Clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Le0X8 committed Aug 2, 2024
1 parent 8dbaec6 commit 57a701c
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 73 deletions.
76 changes: 37 additions & 39 deletions src/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ pub fn metadata<'a>(
let metadata = match format {
Formats::Zip => {
let metadata = formats::zip::parser::metadata(&mut file);
if check_integrity {
if !formats::zip::parser::check_integrity_all(
if check_integrity
&& !formats::zip::parser::check_integrity_all(
&mut file,
&metadata.files,
&buffer_size,
) {
return Err("Integrity check failed".to_string());
}
)
{
return Err("Integrity check failed".to_string());
}
OriginalArchiveMetadata::Zip(metadata)
}
Expand All @@ -61,14 +61,14 @@ pub fn extract(
let metadata: &dyn ArchiveMetadata = match format {
Formats::Zip => {
let metadata = formats::zip::parser::metadata(&mut file);
if check_integrity {
if !formats::zip::parser::check_integrity_all(
if check_integrity
&& !formats::zip::parser::check_integrity_all(
&mut file,
&metadata.files,
&buffer_size,
) {
return Err("Integrity check failed".to_string());
}
)
{
return Err("Integrity check failed".to_string());
}
&metadata.clone() as &dyn ArchiveMetadata
}
Expand All @@ -85,35 +85,33 @@ pub fn extract(
});
}
}
} else {
if index != None {
let index = index.unwrap();
if index >= files.len() as u32 {
return Err("Index out of range".to_string());
}
formats::zip::parser::extract(
&mut file,
&formats::zip::to_zip_entries(files),
&buffer_size,
&|path| format!("{}/{}", &output, &path),
);
} else {
let path = path.unwrap();
let files: Vec<ZipFileEntry> = metadata
.get_files()
.iter()
.filter_map(|file| {
if file.get_path().starts_with(&path) {
Some(formats::zip::to_zip_entry(*file))
} else {
None
}
})
.collect();
formats::zip::parser::extract(&mut file, &files, &buffer_size, &|path| {
format!("{}/{}", &output, &path)
});
} else if index.is_some() {
let index = index.unwrap();
if index >= files.len() as u32 {
return Err("Index out of range".to_string());
}
formats::zip::parser::extract(
&mut file,
&formats::zip::to_zip_entries(files),
&buffer_size,
&|path| format!("{}/{}", &output, &path),
);
} else {
let path = path.unwrap();
let files: Vec<ZipFileEntry> = metadata
.get_files()
.iter()
.filter_map(|file| {
if file.get_path().starts_with(&path) {
Some(formats::zip::to_zip_entry(*file))
} else {
None
}
})
.collect();
formats::zip::parser::extract(&mut file, &files, &buffer_size, &|path| {
format!("{}/{}", &output, &path)
});
};

Ok(())
Expand All @@ -127,7 +125,7 @@ pub struct EntrySource<'a> {
pub fn create(
format: Formats,
output: String,
input: &mut Vec<EntrySource>,
input: &mut [EntrySource],
buffer_size: u64,
) -> Result<(), String> {
let mut file = FileWriter::new(&output, &false);
Expand Down
6 changes: 3 additions & 3 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl<'a> FileReader {
let mut remaining = *len;

while remaining > 0 {
let to_read = min(*buffer_size as u64, remaining) as usize;
let to_read = min(*buffer_size, remaining) as usize;
let read = self.read(&mut buf[..to_read]);
target.write(read);
remaining -= to_read as u64;
Expand All @@ -122,7 +122,7 @@ impl<'a> FileReader {
Times {
created: metadata
.created()
.unwrap_or_else(|_| metadata.modified().unwrap().into())
.unwrap_or_else(|_| metadata.modified().unwrap())
.into(),
accessed: metadata.accessed().unwrap().into(),
modified: metadata.modified().unwrap().into(),
Expand Down Expand Up @@ -238,7 +238,6 @@ impl<'a> FileWriter {
pub fn new(path: &'a String, append: &bool) -> Self {
if *append {
let mut file = OpenOptions::new()
.write(true)
.create(true)
.append(true)
.open(path)
Expand All @@ -254,6 +253,7 @@ impl<'a> FileWriter {
let mut file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(path)
.unwrap();
file.rewind().unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src/formats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pub enum Formats {
Zip,
}

pub fn from_string(format: &String) -> Formats {
match format.as_str() {
pub fn from_string(format: &str) -> Formats {
match format {
"zip" => Formats::Zip,
_ => panic!("Unsupported format"),
}
Expand Down
2 changes: 1 addition & 1 deletion src/formats/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl<'a> FileEntry<'a> for ZipFileEntry<'a> {
}

fn get_original(&'a self) -> OriginalFileEntry<'a> {
OriginalFileEntry::Zip(&self)
OriginalFileEntry::Zip(self)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/formats/zip/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn extract(
&entry.offset,
&entry.size,
&mut target,
&entry.modified.into(),
&entry.modified,
buffer_size,
);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/formats/zip/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn write(target: &mut FileWriter, data: &mut ZipArchiveData, buffer_size: &u
&file.offset,
&file.size,
target,
&file.modified.into(),
&file.modified,
buffer_size,
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/hash/crc32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ pub fn hash(file: &mut FileReader, offset: &u64, size: &u64, buffer_size: &u64)

let mut remaining = *size;
while remaining > 0 {
let to_read = min(*buffer_size as u64, remaining) as usize;
let to_read = min(*buffer_size, remaining) as usize;
let read = file.read(&mut buf[..to_read]);
hasher.update(&read);
hasher.update(read);
remaining -= to_read as u64;
}

Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::too_many_arguments)]

pub mod archive;
pub mod file;
pub mod formats;
Expand Down
2 changes: 0 additions & 2 deletions tests/other.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use corelib;

#[test]
fn returns_real_version() {
assert_eq!(corelib::get_version(), env!("CARGO_PKG_VERSION"));
Expand Down
32 changes: 10 additions & 22 deletions tests/zip-external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ use corelib::{

#[test]
fn sample_000_metadata() {
let metadata = match *archive::metadata(
let OriginalArchiveMetadata::Zip(metadata) = *archive::metadata(
Formats::Zip,
"tests/samples/zip/000.zip".to_string(),
true,
1024,
)
.unwrap()
{
OriginalArchiveMetadata::Zip(metadata) => metadata,
};
.unwrap();
assert_eq!(metadata.files.len(), 1);
assert_eq!(metadata.files[0].path, "test.txt");
assert_eq!(metadata.files[0].size, 14);
Expand Down Expand Up @@ -49,16 +46,13 @@ fn sample_000_extract() {

#[test]
fn sample_001_metadata() {
let metadata = match *archive::metadata(
let OriginalArchiveMetadata::Zip(metadata) = *archive::metadata(
Formats::Zip,
"tests/samples/zip/001.zip".to_string(),
true,
1024,
)
.unwrap()
{
OriginalArchiveMetadata::Zip(metadata) => metadata,
};
.unwrap();
assert_eq!(metadata.files.len(), 2);
assert_eq!(metadata.files[0].path, "test.txt");
assert_eq!(metadata.files[0].size, 14);
Expand Down Expand Up @@ -103,16 +97,13 @@ fn sample_001_extract() {

#[test]
fn sample_002_metadata() {
let metadata = match *archive::metadata(
let OriginalArchiveMetadata::Zip(metadata) = *archive::metadata(
Formats::Zip,
"tests/samples/zip/002.zip".to_string(),
true,
1024,
)
.unwrap()
{
OriginalArchiveMetadata::Zip(metadata) => metadata,
};
.unwrap();
assert_eq!(metadata.files.len(), 3);
assert_eq!(metadata.files[0].path, "test/");
assert_eq!(metadata.files[0].size, 0);
Expand Down Expand Up @@ -176,7 +167,7 @@ fn create_000_metadata() {
archive::create(
Formats::Zip,
"tests/samples/zip/c000-external.zip".to_string(),
&mut vec![EntrySource {
&mut [EntrySource {
path: "test.txt".to_string(),
source: &mut FsFile::new(&"tests/samples/zip/c000-external/test.txt".to_string()),
}],
Expand All @@ -186,16 +177,13 @@ fn create_000_metadata() {

std::fs::remove_dir_all("tests/samples/zip/c000-external").unwrap();

let metadata = match *archive::metadata(
let OriginalArchiveMetadata::Zip(metadata) = *archive::metadata(
Formats::Zip,
"tests/samples/zip/c000-external.zip".to_string(),
true,
1024,
)
.unwrap()
{
OriginalArchiveMetadata::Zip(metadata) => metadata,
};
.unwrap();

assert_eq!(metadata.files.len(), 1);
assert_eq!(metadata.files[0].path, "test.txt");
Expand Down Expand Up @@ -227,7 +215,7 @@ fn create_000_extract() {
archive::create(
Formats::Zip,
"tests/samples/zip/c000-external2.zip".to_string(),
&mut vec![EntrySource {
&mut [EntrySource {
path: "test.txt".to_string(),
source: &mut FsFile::new(&"tests/samples/zip/c000-external2/test.txt".to_string()),
}],
Expand Down

0 comments on commit 57a701c

Please sign in to comment.