mirror of
https://github.com/PR0M3TH3AN/Marlin.git
synced 2025-09-09 07:38:50 +00:00
Format codebase with rustfmt
This commit is contained in:
@@ -1,21 +1,21 @@
|
||||
//! Database abstraction for Marlin
|
||||
//!
|
||||
//!
|
||||
//! This module provides a database abstraction layer that wraps the SQLite connection
|
||||
//! and provides methods for common database operations.
|
||||
|
||||
use anyhow::Result;
|
||||
use rusqlite::Connection;
|
||||
use std::path::PathBuf;
|
||||
use anyhow::Result;
|
||||
|
||||
/// Options for indexing files
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct IndexOptions {
|
||||
/// Only update files marked as dirty
|
||||
pub dirty_only: bool,
|
||||
|
||||
|
||||
/// Index file contents (not just metadata)
|
||||
pub index_contents: bool,
|
||||
|
||||
|
||||
/// Maximum file size to index (in bytes)
|
||||
pub max_size: Option<u64>,
|
||||
}
|
||||
@@ -41,32 +41,34 @@ impl Database {
|
||||
pub fn new(conn: Connection) -> Self {
|
||||
Self { conn }
|
||||
}
|
||||
|
||||
|
||||
/// Get a reference to the underlying connection
|
||||
pub fn conn(&self) -> &Connection {
|
||||
&self.conn
|
||||
}
|
||||
|
||||
|
||||
/// Get a mutable reference to the underlying connection
|
||||
pub fn conn_mut(&mut self) -> &mut Connection {
|
||||
&mut self.conn
|
||||
}
|
||||
|
||||
|
||||
/// Index one or more files
|
||||
pub fn index_files(&mut self, paths: &[PathBuf], _options: &IndexOptions) -> Result<usize> {
|
||||
// In a real implementation, this would index the files
|
||||
// For now, we just return the number of files "indexed"
|
||||
if paths.is_empty() { // Add a branch for coverage
|
||||
if paths.is_empty() {
|
||||
// Add a branch for coverage
|
||||
return Ok(0);
|
||||
}
|
||||
Ok(paths.len())
|
||||
}
|
||||
|
||||
|
||||
/// Remove files from the index
|
||||
pub fn remove_files(&mut self, paths: &[PathBuf]) -> Result<usize> {
|
||||
// In a real implementation, this would remove the files
|
||||
// For now, we just return the number of files "removed"
|
||||
if paths.is_empty() { // Add a branch for coverage
|
||||
if paths.is_empty() {
|
||||
// Add a branch for coverage
|
||||
return Ok(0);
|
||||
}
|
||||
Ok(paths.len())
|
||||
@@ -77,8 +79,8 @@ impl Database {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::db::open as open_marlin_db; // Use your project's DB open function
|
||||
use tempfile::tempdir;
|
||||
use std::fs::File;
|
||||
use tempfile::tempdir;
|
||||
|
||||
fn setup_db() -> Database {
|
||||
let conn = open_marlin_db(":memory:").expect("Failed to open in-memory DB");
|
||||
@@ -102,7 +104,7 @@ mod tests {
|
||||
|
||||
let paths = vec![file1.to_path_buf()];
|
||||
let options = IndexOptions::default();
|
||||
|
||||
|
||||
assert_eq!(db.index_files(&paths, &options).unwrap(), 1);
|
||||
assert_eq!(db.index_files(&[], &options).unwrap(), 0); // Test empty case
|
||||
}
|
||||
@@ -115,7 +117,7 @@ mod tests {
|
||||
File::create(&file1).unwrap(); // File doesn't need to be in DB for this stub
|
||||
|
||||
let paths = vec![file1.to_path_buf()];
|
||||
|
||||
|
||||
assert_eq!(db.remove_files(&paths).unwrap(), 1);
|
||||
assert_eq!(db.remove_files(&[]).unwrap(), 0); // Test empty case
|
||||
}
|
||||
|
@@ -9,27 +9,38 @@ use std::{
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use std::result::Result as StdResult;
|
||||
use anyhow::{Context, Result};
|
||||
use chrono::Local;
|
||||
use rusqlite::{
|
||||
backup::{Backup, StepResult},
|
||||
params,
|
||||
Connection,
|
||||
OpenFlags,
|
||||
OptionalExtension,
|
||||
TransactionBehavior,
|
||||
params, Connection, OpenFlags, OptionalExtension, TransactionBehavior,
|
||||
};
|
||||
use std::result::Result as StdResult;
|
||||
use tracing::{debug, info, warn};
|
||||
|
||||
/* ─── embedded migrations ─────────────────────────────────────────── */
|
||||
|
||||
const MIGRATIONS: &[(&str, &str)] = &[
|
||||
("0001_initial_schema.sql", include_str!("migrations/0001_initial_schema.sql")),
|
||||
("0002_update_fts_and_triggers.sql", include_str!("migrations/0002_update_fts_and_triggers.sql")),
|
||||
("0003_create_links_collections_views.sql", include_str!("migrations/0003_create_links_collections_views.sql")),
|
||||
("0004_fix_hierarchical_tags_fts.sql", include_str!("migrations/0004_fix_hierarchical_tags_fts.sql")),
|
||||
("0005_add_dirty_table.sql", include_str!("migrations/0005_add_dirty_table.sql")),
|
||||
(
|
||||
"0001_initial_schema.sql",
|
||||
include_str!("migrations/0001_initial_schema.sql"),
|
||||
),
|
||||
(
|
||||
"0002_update_fts_and_triggers.sql",
|
||||
include_str!("migrations/0002_update_fts_and_triggers.sql"),
|
||||
),
|
||||
(
|
||||
"0003_create_links_collections_views.sql",
|
||||
include_str!("migrations/0003_create_links_collections_views.sql"),
|
||||
),
|
||||
(
|
||||
"0004_fix_hierarchical_tags_fts.sql",
|
||||
include_str!("migrations/0004_fix_hierarchical_tags_fts.sql"),
|
||||
),
|
||||
(
|
||||
"0005_add_dirty_table.sql",
|
||||
include_str!("migrations/0005_add_dirty_table.sql"),
|
||||
),
|
||||
];
|
||||
|
||||
/* ─── connection bootstrap ────────────────────────────────────────── */
|
||||
@@ -237,10 +248,7 @@ pub fn list_links(
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
pub fn find_backlinks(
|
||||
conn: &Connection,
|
||||
pattern: &str,
|
||||
) -> Result<Vec<(String, Option<String>)>> {
|
||||
pub fn find_backlinks(conn: &Connection, pattern: &str) -> Result<Vec<(String, Option<String>)>> {
|
||||
let like = pattern.replace('*', "%");
|
||||
|
||||
let mut stmt = conn.prepare(
|
||||
@@ -318,11 +326,9 @@ pub fn list_views(conn: &Connection) -> Result<Vec<(String, String)>> {
|
||||
}
|
||||
|
||||
pub fn view_query(conn: &Connection, name: &str) -> Result<String> {
|
||||
conn.query_row(
|
||||
"SELECT query FROM views WHERE name = ?1",
|
||||
[name],
|
||||
|r| r.get::<_, String>(0),
|
||||
)
|
||||
conn.query_row("SELECT query FROM views WHERE name = ?1", [name], |r| {
|
||||
r.get::<_, String>(0)
|
||||
})
|
||||
.context(format!("no view called '{}'", name))
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user