From 952e83453a89f842158f3446862aa1327f4e2a85 Mon Sep 17 00:00:00 2001 From: elipeter Date: Mon, 23 Jun 2025 17:49:15 +0200 Subject: [PATCH] Refactor and simplify code in `index.rs`, `scan.rs`, and `database.rs` - Removed unnecessary references and improved clarity in `Indexer` method calls. - Reorganized conditionals and eliminated redundant variable assignments in `scan.rs`. - Simplified database initialization handling by removing unused commented-out code and improving formatting in `database.rs`. --- src/commands/index.rs | 4 ++-- src/commands/scan.rs | 11 +++++------ src/database.rs | 12 ++---------- 3 files changed, 9 insertions(+), 18 deletions(-) diff --git a/src/commands/index.rs b/src/commands/index.rs index 3411789d..bc9142f3 100644 --- a/src/commands/index.rs +++ b/src/commands/index.rs @@ -53,7 +53,7 @@ pub fn build_index( let pool = Indexer::init(db_path)?; { - let idx = Indexer::from_pool(&project_name, &pool).unwrap(); + let idx = Indexer::from_pool(project_name, &pool).unwrap(); idx.clear()?; } @@ -83,7 +83,7 @@ pub fn build_index( }).unwrap(); { - let idx = Indexer::from_pool(&project_name, &pool)?; + let idx = Indexer::from_pool(project_name, &pool)?; idx.vacuum()?; } diff --git a/src/commands/scan.rs b/src/commands/scan.rs index afcc58b6..1df0023e 100644 --- a/src/commands/scan.rs +++ b/src/commands/scan.rs @@ -35,10 +35,9 @@ pub fn handle( ) -> Result<(), Box> { let scan_path = Path::new(path).canonicalize()?; let (project_name, db_path) = get_project_info(&scan_path, database_dir)?; - let diags: Vec; - if no_index { - diags = scan_filesystem(&scan_path, config).unwrap(); + let diags: Vec = if no_index { + scan_filesystem(&scan_path, config).unwrap() } else { if rebuild_index || !db_path.exists() { tracing::debug!("Scanning filesystem index filesystem"); @@ -46,12 +45,12 @@ pub fn handle( } let pool = Indexer::init(&db_path)?; - diags = scan_with_index_parallel(&project_name, pool, config)?; - } + scan_with_index_parallel(&project_name, pool, config)? + }; tracing::debug!("Found {:?} issues.", diags.len()); - if format == "console" || (format == "" && config.output.default_format == "console") { + if format == "console" || (format.is_empty() && config.output.default_format == "console") { tracing::debug!("Printing to console"); for d in &diags { let sev_str = match d.severity { diff --git a/src/database.rs b/src/database.rs index b2e1e014..57b22c61 100644 --- a/src/database.rs +++ b/src/database.rs @@ -59,12 +59,12 @@ pub mod index { let flags = OpenFlags::SQLITE_OPEN_READ_WRITE | OpenFlags::SQLITE_OPEN_CREATE | OpenFlags::SQLITE_OPEN_FULL_MUTEX; - let manager = SqliteConnectionManager::file(&database_path).with_flags(flags); + let manager = SqliteConnectionManager::file(database_path).with_flags(flags); let pool = Arc::new(Pool::new(manager)?); { let conn = pool.get()?; - conn.pragma_update(None, "journal_mode", &"WAL")?; + conn.pragma_update(None, "journal_mode", "WAL")?; conn.execute_batch(SCHEMA)?; } Ok(pool) @@ -81,14 +81,6 @@ pub mod index { // helper so code below can treat PooledConnection like &Connection fn c(&self) -> &Connection { self.conn.deref() } - /// Open (or create) the DB at `database_path` for the given project name. - // pub fn new(project: &str, database_path: &Path) -> Result> { - // let conn = Connection::open(database_path)?; - // conn.pragma_update(None, "journal_mode", &"WAL")?; - // conn.execute_batch(SCHEMA)?; - // Ok(Self { conn, project: project.to_owned() }) - // } - /// Return true when the file *content* or *mtime* changed since the last scan. pub fn should_scan(&self, path: &Path) -> Result> { let meta = fs::metadata(path)?;