Skip to content

Commit

Permalink
Merge pull request #35 from AmineZouitine/fix_clippy
Browse files Browse the repository at this point in the history
fix clippy errors
  • Loading branch information
AmineZouitine authored Jan 2, 2023
2 parents 7061e8e + 44240db commit 637e298
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description = "remove files or directories and save them in trash."
documentation = "https://github.com/AmineZouitine/rmt.rs"
homepage = "https://github.com/AmineZouitine/rmt.rs"
repository = "https://github.com/AmineZouitine/rmt.rs"
version = "0.1.8"
version = "0.2.0"
edition = "2021"

[lib]
Expand Down
13 changes: 9 additions & 4 deletions src/display_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@ pub struct Filter {

impl Filter {
pub fn is_valid_item(&self, trash_item: &TrashItem) -> bool {
let filter_content = &self.content;
trash_item.name.contains(filter_content)
|| trash_item.date.contains(filter_content)
|| trash_item.path.contains(filter_content)
if !self.is_filter {
true
} else {
trash_item.name.contains(&self.content)
|| trash_item.date.contains(&self.content)
|| trash_item.path.contains(&self.content)
}
}
}

Expand All @@ -61,7 +64,9 @@ pub fn display_trash(
) -> i32 {
println!("Which elements do you want to restore ?\n\r");

// Getting all trash item from ddb
let mut trash_items = data_manager::find_all_trash_items(connection, is_test);
// filter item if the "filter mode" is activate
trash_items.retain(|item| display_infos.filter.is_valid_item(item));
display_infos.total_elements = trash_items.len();

Expand Down
1 change: 1 addition & 0 deletions src/trash_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct TrashItem {
}

impl TrashItem {
#[allow(clippy::too_many_arguments)]
pub fn new(
name: String,
hash: String,
Expand Down
51 changes: 20 additions & 31 deletions src/trash_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use rusqlite::Connection;
use sha256;
use std::process::Command;

use std::fs::{self, File};
use std::io::{Read, stdout, Write};
use std::path::Path;
use chacha20poly1305::{aead::stream, KeyInit, XChaCha20Poly1305};
use rand::{rngs::OsRng, RngCore};
use std::fs::{self, File};
use std::io::{stdout, Read, Write};
use std::path::Path;

pub fn add_element_to_trash(
connection: &Connection,
Expand Down Expand Up @@ -50,8 +50,7 @@ pub fn add_element_to_trash(
// TODO
} else if config.encryption && !element_is_directory {
let dist_path = format!("{}/{}", get_trash_directory_path(is_test), hash);
encrypt_element(element_path, &dist_path)
.expect(&format!("Error encrypting file"));
encrypt_element(element_path, &dist_path).expect("Error encrypting file");
fs::remove_file(element_path).unwrap();
is_encrypted = true;
} else {
Expand Down Expand Up @@ -116,8 +115,8 @@ fn encrypt_element(source_path: &str, dist_path: &str) -> Result<(), Box<dyn std
let mut dist_file = File::create(dist_path)?;

// store salt and nonce in the encrypted file to be used when decrypting
dist_file.write(&salt)?;
dist_file.write(&nonce)?;
dist_file.write_all(&salt)?;
dist_file.write_all(&nonce)?;
// encrypt data in small chunk of 500 bytes
const BUFFER_LEN: usize = 500;
let mut buffer = [0u8; BUFFER_LEN];
Expand All @@ -126,8 +125,7 @@ fn encrypt_element(source_path: &str, dist_path: &str) -> Result<(), Box<dyn std
let read_count = source_file.read(&mut buffer)?;

if read_count == BUFFER_LEN {
let ciphertext = stream_encryptor
.encrypt_next(buffer.as_slice());
let ciphertext = stream_encryptor.encrypt_next(buffer.as_slice());
match ciphertext {
Ok(ciphertext) => dist_file.write(&ciphertext)?,
Err(_) => {
Expand All @@ -136,8 +134,7 @@ fn encrypt_element(source_path: &str, dist_path: &str) -> Result<(), Box<dyn std
}
};
} else {
let ciphertext = stream_encryptor
.encrypt_last(&buffer[..read_count]);
let ciphertext = stream_encryptor.encrypt_last(&buffer[..read_count]);
match ciphertext {
Ok(ciphertext) => dist_file.write(&ciphertext)?,
Err(_) => {
Expand All @@ -151,7 +148,10 @@ fn encrypt_element(source_path: &str, dist_path: &str) -> Result<(), Box<dyn std
Ok(())
}

fn decrypt_element(encrypted_path: &str, dist_path: &str) -> Result<(), Box<dyn std::error::Error>> {
fn decrypt_element(
encrypted_path: &str,
dist_path: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let mut salt = [0u8; 32];
let mut nonce = [0u8; 19];

Expand Down Expand Up @@ -190,8 +190,7 @@ fn decrypt_element(encrypted_path: &str, dist_path: &str) -> Result<(), Box<dyn
let read_count = encrypted_file.read(&mut buffer)?;

if read_count == BUFFER_LEN {
let plaintext = stream_decryptor
.decrypt_next(buffer.as_slice());
let plaintext = stream_decryptor.decrypt_next(buffer.as_slice());
match plaintext {
Ok(plaintext) => dist_file.write(&plaintext)?,
Err(_) => {
Expand All @@ -200,8 +199,7 @@ fn decrypt_element(encrypted_path: &str, dist_path: &str) -> Result<(), Box<dyn
}
};
} else {
let plaintext = stream_decryptor
.decrypt_last(&buffer[..read_count]);
let plaintext = stream_decryptor.decrypt_last(&buffer[..read_count]);
match plaintext {
Ok(plaintext) => dist_file.write(&plaintext)?,
Err(_) => {
Expand Down Expand Up @@ -261,11 +259,7 @@ pub fn remove_all_elements(connection: &Connection, is_test: bool) {
}

fn remove_element(trash_item: &TrashItem, is_test: bool) {
let element_path = format!(
"{}/{}",
get_trash_directory_path(is_test),
trash_item.hash
);
let element_path = format!("{}/{}", get_trash_directory_path(is_test), trash_item.hash);
if Path::new(&element_path).is_dir() {
fs::remove_dir_all(&element_path).unwrap();
} else {
Expand All @@ -292,11 +286,7 @@ pub fn restore_all_elements_selected(
}

fn restore_element(trash_item: &TrashItem, is_test: bool) {
let path_in_trash = format!(
"{}/{}",
get_trash_directory_path(is_test),
trash_item.hash
);
let path_in_trash = format!("{}/{}", get_trash_directory_path(is_test), trash_item.hash);

let element_path_name = format!("{}/{}", &trash_item.path, &trash_item.name);

Expand All @@ -311,8 +301,7 @@ fn restore_element(trash_item: &TrashItem, is_test: bool) {
);
if trash_item.is_encrypted {
let dist_path = format!("{}/{}", &trash_item.path, trash_item.name);
decrypt_element(&path_in_trash, &dist_path)
.expect(&format!("Error decrypting file"));
decrypt_element(&path_in_trash, &dist_path).expect("Error decrypting file");
fs::remove_file(&path_in_trash).unwrap();
} else {
let element_path_renamed =
Expand All @@ -322,7 +311,8 @@ fn restore_element(trash_item: &TrashItem, is_test: bool) {
&[&element_path_renamed],
&trash_item.path,
&dir::CopyOptions::new(),
).unwrap();
)
.unwrap();
}
return;
}
Expand Down Expand Up @@ -368,8 +358,7 @@ fn restore_element(trash_item: &TrashItem, is_test: bool) {

if trash_item.is_encrypted {
let dist_path = format!("{}/{}", &new_path, trash_item.name);
decrypt_element(&path_in_trash, &dist_path)
.expect(&format!("Error decrypting file"));
decrypt_element(&path_in_trash, &dist_path).expect("Error decrypting file");
fs::remove_file(&path_in_trash).unwrap();
} else {
let new_name = format!("{}/{}", get_trash_directory_path(is_test), trash_item.name);
Expand Down

0 comments on commit 637e298

Please sign in to comment.