From 27fef21be0a817d3c3c3b1aa7ee97fbdb0ad3154 Mon Sep 17 00:00:00 2001 From: Valentin Bersier Date: Mon, 9 Jan 2023 16:17:50 +0100 Subject: [PATCH 1/6] fix: no unwrap in params.rs --- src/params.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/params.rs b/src/params.rs index 0148b9b..5531c80 100644 --- a/src/params.rs +++ b/src/params.rs @@ -1,6 +1,6 @@ use std::path::PathBuf; use clap::Parser; -use anyhow::Result; +use anyhow::{ anyhow, Result }; use std::fs; #[derive(Parser, Debug)] @@ -19,20 +19,16 @@ pub struct Params { impl Params { pub fn get_directory(&self) -> Result { - let dir_string: String = self + let dir_pathbuf: PathBuf = self .dir .clone() .unwrap_or(std::env::current_dir()?) - .as_os_str() - .to_str() - .unwrap() - .to_string(); + .as_os_str().into(); - let dir_pathbuf = PathBuf::from(&dir_string); - let dir = fs::canonicalize(&dir_pathbuf)? + let dir = fs::canonicalize(dir_pathbuf)? .as_os_str() .to_str() - .unwrap() + .ok_or_else(|| anyhow!("Invalid directory"))? .to_string(); Ok(dir) From a76163f24faf98937c3ac220adbedb5d50806808 Mon Sep 17 00:00:00 2001 From: Valentin Bersier Date: Mon, 9 Jan 2023 16:21:13 +0100 Subject: [PATCH 2/6] style: format --- src/params.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/params.rs b/src/params.rs index 5531c80..d2e84ed 100644 --- a/src/params.rs +++ b/src/params.rs @@ -1,7 +1,7 @@ -use std::path::PathBuf; +use std::{fs, path::PathBuf}; + +use anyhow::{anyhow, Result}; use clap::Parser; -use anyhow::{ anyhow, Result }; -use std::fs; #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] @@ -23,7 +23,8 @@ impl Params { .dir .clone() .unwrap_or(std::env::current_dir()?) - .as_os_str().into(); + .as_os_str() + .into(); let dir = fs::canonicalize(dir_pathbuf)? .as_os_str() From 254e61cabea2605c92ddd38cc4bba2dc9d9ed61d Mon Sep 17 00:00:00 2001 From: Valentin Bersier Date: Mon, 9 Jan 2023 16:27:52 +0100 Subject: [PATCH 3/6] fix: clippy warnings --- src/database.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/database.rs b/src/database.rs index 4713aa7..ebf3ac4 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,4 +1,5 @@ use anyhow::Result; + use crate::params::Params; #[derive(Debug, Clone)] @@ -10,12 +11,12 @@ pub struct File { pub fn get_connection(args: &Params) -> Result { let connection_url = match args.nocache { false => "/tmp/deduplicator.db", - true => ":memory:" + true => ":memory:", }; - sqlite::open(connection_url).and_then(|conn| { + sqlite::open(connection_url).map(|conn| { setup(&conn).ok(); - Ok(conn) + conn }) } @@ -30,15 +31,12 @@ pub fn put(file: &File, connection: &sqlite::Connection) -> Result<()> { "INSERT INTO files (file_identifier, hash) VALUES (\"{}\", \"{}\")", file.path, file.hash ); - let result = connection.execute(query)?; - - Ok(result) + connection.execute(query)?; + Ok(()) } pub fn indexed_paths(connection: &sqlite::Connection) -> Result> { - let query = format!( - "SELECT * FROM files" - ); + let query = "SELECT * FROM files"; let result: Vec = connection .prepare(query)? @@ -49,7 +47,7 @@ pub fn indexed_paths(connection: &sqlite::Connection) -> Result> { let hash = row.read::("hash").to_string(); File { path, hash } }) - .collect(); + .collect(); Ok(result) } @@ -65,7 +63,8 @@ pub fn duplicate_hashes(connection: &sqlite::Connection, path: &String) -> Resul ON a.hash = b.hash WHERE a.file_identifier LIKE \"{}%\" ORDER BY a.file_identifier - ", path + ", + path ); let result: Vec = connection From c76ad81a55c7145a2f64ccda7da65036ed51309a Mon Sep 17 00:00:00 2001 From: Valentin Bersier Date: Mon, 9 Jan 2023 16:41:55 +0100 Subject: [PATCH 4/6] refactor: do no unwrap in database.rs --- src/database.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/database.rs b/src/database.rs index ebf3ac4..2e06e7c 100644 --- a/src/database.rs +++ b/src/database.rs @@ -41,7 +41,7 @@ pub fn indexed_paths(connection: &sqlite::Connection) -> Result> { let result: Vec = connection .prepare(query)? .into_iter() - .map(|row_result| row_result.unwrap()) + .filter_map(|row_result| row_result.ok()) .map(|row| { let path = row.read::<&str, _>("file_identifier").to_string(); let hash = row.read::("hash").to_string(); @@ -52,7 +52,7 @@ pub fn indexed_paths(connection: &sqlite::Connection) -> Result> { Ok(result) } -pub fn duplicate_hashes(connection: &sqlite::Connection, path: &String) -> Result> { +pub fn duplicate_hashes(connection: &sqlite::Connection, path: &str) -> Result> { let query = format!( " SELECT a.* FROM files a @@ -70,7 +70,7 @@ pub fn duplicate_hashes(connection: &sqlite::Connection, path: &String) -> Resul let result: Vec = connection .prepare(query)? .into_iter() - .map(|row_result| row_result.unwrap()) + .filter_map(|row_result| row_result.ok()) .map(|row| { let path = row.read::<&str, _>("file_identifier").to_string(); let hash = row.read::("hash").to_string(); From bc170f139e5d22796274cc9f35328d2c928caeae Mon Sep 17 00:00:00 2001 From: Valentin Bersier Date: Mon, 9 Jan 2023 16:46:12 +0100 Subject: [PATCH 5/6] style: remove trailing spaces --- src/database.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/database.rs b/src/database.rs index 2e06e7c..857e9a5 100644 --- a/src/database.rs +++ b/src/database.rs @@ -54,10 +54,10 @@ pub fn indexed_paths(connection: &sqlite::Connection) -> Result> { pub fn duplicate_hashes(connection: &sqlite::Connection, path: &str) -> Result> { let query = format!( - " + " SELECT a.* FROM files a JOIN (SELECT file_identifier, hash, COUNT(*) - FROM files + FROM files GROUP BY hash HAVING count(*) > 1 ) b ON a.hash = b.hash From 138b66038fefe14c1da37a06b86a6a3a5a1020df Mon Sep 17 00:00:00 2001 From: Valentin Bersier Date: Mon, 9 Jan 2023 16:57:43 +0100 Subject: [PATCH 6/6] refactor: various clippy fixes --- src/app/event_handler.rs | 6 +++--- src/app/events.rs | 2 +- src/app/mod.rs | 24 +++++++++++++----------- src/app/ui.rs | 3 ++- src/main.rs | 7 ++++--- 5 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/app/event_handler.rs b/src/app/event_handler.rs index 69260ae..da7129b 100644 --- a/src/app/event_handler.rs +++ b/src/app/event_handler.rs @@ -1,7 +1,8 @@ use std::time::Duration; -use crossterm::event::{self, KeyCode, KeyEvent}; use anyhow::Result; +use crossterm::event::{self, KeyCode, KeyEvent}; + use super::events; pub struct EventHandler; @@ -21,8 +22,7 @@ impl EventHandler { fn handle_keypress(keyevent: KeyEvent) -> Result { match keyevent.code { KeyCode::Char('q') => Ok(events::Event::Exit), - _ => Ok(events::Event::Noop) + _ => Ok(events::Event::Noop), } } } - diff --git a/src/app/events.rs b/src/app/events.rs index a889cc3..c7167d0 100644 --- a/src/app/events.rs +++ b/src/app/events.rs @@ -1,4 +1,4 @@ pub enum Event { Exit, - Noop + Noop, } diff --git a/src/app/mod.rs b/src/app/mod.rs index 92f6736..3a8b102 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -1,18 +1,13 @@ mod event_handler; mod events; -mod ui; mod formatter; +mod ui; + +use std::{io, thread, time::Duration}; -use crate::database; -use crate::output; -use crate::params::Params; -use crate::scanner; use anyhow::{anyhow, Result}; use crossterm::{event, execute, terminal}; use event_handler::EventHandler; -use std::io; -use std::thread; -use std::time::Duration; use tui::{ backend::CrosstermBackend, widgets::{Block, Borders, Widget}, @@ -20,19 +15,24 @@ use tui::{ }; use ui::Ui; +use crate::database; +use crate::output; +use crate::params::Params; +use crate::scanner; + pub struct App; impl App { pub fn init(app_args: &Params) -> Result<()> { // let mut term = Self::init_terminal()?; - let connection = database::get_connection(&app_args)?; - let duplicates = scanner::duplicates(&app_args, &connection)?; + let connection = database::get_connection(app_args)?; + let duplicates = scanner::duplicates(app_args, &connection)?; // Self::init_render_loop(&mut term)?; // Self::cleanup(&mut term)?; - output::print(duplicates, &app_args); /* TODO: APP TUI INIT FUNCTION */ + output::print(duplicates, app_args); /* TODO: APP TUI INIT FUNCTION */ Ok(()) } @@ -56,6 +56,8 @@ impl App { } fn init_render_loop(term: &mut Terminal>) -> Result<()> { + // this could be simplified with a `while Self::render_cycle(term).is_ok() {}` in the current state, but maybe + // it's good to keep it to handle errors in the future loop { match Self::render_cycle(term) { Ok(_) => continue, diff --git a/src/app/ui.rs b/src/app/ui.rs index 19413a4..9b8c63b 100644 --- a/src/app/ui.rs +++ b/src/app/ui.rs @@ -1,5 +1,6 @@ -use anyhow::Result; use std::io; + +use anyhow::Result; use tui::{ backend::{Backend, CrosstermBackend}, layout::{Constraint, Direction, Layout, Rect}, diff --git a/src/main.rs b/src/main.rs index 7cb8c91..21ac960 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,13 @@ -mod params; +#![allow(unused)] // TODO: remove this once TUI is implemented +mod app; mod database; mod output; +mod params; mod scanner; -mod app; use anyhow::Result; -use clap::Parser; use app::App; +use clap::Parser; #[tokio::main] async fn main() -> Result<()> {