Merge pull request #13 from beeb/panics

Fix some more unhandled errors
This commit is contained in:
Sreedev Kodichath
2023-01-09 11:03:12 -05:00
committed by GitHub
7 changed files with 43 additions and 41 deletions

View File

@@ -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<events::Event> {
match keyevent.code {
KeyCode::Char('q') => Ok(events::Event::Exit),
_ => Ok(events::Event::Noop)
_ => Ok(events::Event::Noop),
}
}
}

View File

@@ -1,4 +1,4 @@
pub enum Event {
Exit,
Noop
Noop,
}

View File

@@ -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<CrosstermBackend<io::Stdout>>) -> 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,

View File

@@ -1,5 +1,6 @@
use anyhow::Result;
use std::io;
use anyhow::Result;
use tui::{
backend::{Backend, CrosstermBackend},
layout::{Constraint, Direction, Layout, Rect},

View File

@@ -1,7 +1,9 @@
use crate::params::Params;
use anyhow::Result;
use std::env::temp_dir;
use anyhow::Result;
use crate::params::Params;
#[derive(Debug, Clone)]
pub struct File {
pub path: String,
@@ -21,7 +23,7 @@ fn db_connection_url(args: &Params) -> String {
pub fn get_connection(args: &Params) -> Result<sqlite::Connection, sqlite::Error> {
sqlite::open(db_connection_url(args)).and_then(|conn| {
setup(&conn).ok();
Ok(conn)
conn
})
}
@@ -36,18 +38,17 @@ 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<Vec<File>> {
let query = format!("SELECT * FROM files");
let query = "SELECT * FROM files";
let result: Vec<File> = 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::<i64, _>("hash").to_string();
@@ -58,12 +59,12 @@ pub fn indexed_paths(connection: &sqlite::Connection) -> Result<Vec<File>> {
Ok(result)
}
pub fn duplicate_hashes(connection: &sqlite::Connection, path: &String) -> Result<Vec<File>> {
pub fn duplicate_hashes(connection: &sqlite::Connection, path: &str) -> Result<Vec<File>> {
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
@@ -76,7 +77,7 @@ pub fn duplicate_hashes(connection: &sqlite::Connection, path: &String) -> Resul
let result: Vec<File> = 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::<i64, _>("hash").to_string();

View File

@@ -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<()> {

View File

@@ -1,7 +1,7 @@
use std::path::PathBuf;
use std::{fs, path::PathBuf};
use anyhow::{anyhow, Result};
use clap::Parser;
use anyhow::Result;
use std::fs;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
@@ -19,20 +19,17 @@ pub struct Params {
impl Params {
pub fn get_directory(&self) -> Result<String> {
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();
.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)