Complete major sheet refactoring
Now using books etc is far easier
This commit is contained in:
116
src/main.rs
116
src/main.rs
@@ -13,10 +13,10 @@ use database::Database;
|
||||
use env_logger::Env;
|
||||
use log::{debug, error};
|
||||
use relm4::RelmApp;
|
||||
use sheet::Sheet;
|
||||
use sheet::{Pdf, Sheet};
|
||||
use walkdir::WalkDir;
|
||||
|
||||
use crate::ui::app::AppModel;
|
||||
use crate::ui::app::{AppInitData, AppModel};
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about)]
|
||||
@@ -36,38 +36,29 @@ async fn main() {
|
||||
let database = Database::setup(cli.directory.join("database.sqlite"))
|
||||
.await
|
||||
.unwrap();
|
||||
// database.insert_sheet(Sheet::new_debug()).await.unwrap();
|
||||
let sheets = database.fetch_all_sheets().await.unwrap();
|
||||
let orphan_files = database.fetch_all_orphan_files().await.unwrap();
|
||||
let sheets = sheet_dao::fetch_all_sheets(&database).await.unwrap();
|
||||
|
||||
debug!("Validating sheets from database...");
|
||||
let mut validation_result = validate_sheet_files(sheets, orphan_files, &cli.directory);
|
||||
let mut validation_result = validate_sheet_files(sheets, &cli.directory);
|
||||
debug!("{}", validation_result.get_stats()); // TODO: handle invalidated files
|
||||
for updated in validation_result.updated_sheets.iter() {
|
||||
database.update_sheet_path(updated).await.unwrap();
|
||||
}
|
||||
for updated in validation_result.updated_orphan_files.iter() {
|
||||
database.update_orphan_file_path(updated).await.unwrap();
|
||||
}
|
||||
|
||||
let mut orphans = validation_result.validated_orphan_files;
|
||||
orphans.append(&mut validation_result.updated_orphan_files);
|
||||
debug!("Inserting unassigned files into orphan table...");
|
||||
for unassigned in validation_result.unassigned_files {
|
||||
let mut orphan = OrphanFile::try_from(unassigned).unwrap();
|
||||
let id = database.insert_orphan_file(&orphan).await.unwrap();
|
||||
orphan.id = id;
|
||||
orphans.push(orphan);
|
||||
sheet_dao::update_sheet_path(&database, updated)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let mut sheets = validation_result.validated_sheets;
|
||||
sheets.append(&mut validation_result.updated_sheets);
|
||||
|
||||
let app_init_data = AppInitData {
|
||||
sheets,
|
||||
orphans,
|
||||
database,
|
||||
};
|
||||
debug!("Inserting unassigned files into orphan table...");
|
||||
for unassigned in validation_result.unassigned_files {
|
||||
let orphan = sheet_dao::insert_file_as_orphan(&database, unassigned)
|
||||
.await
|
||||
.unwrap();
|
||||
sheets.push(orphan);
|
||||
}
|
||||
|
||||
let app_init_data = AppInitData { sheets, database };
|
||||
|
||||
let app = RelmApp::new("de.frajul.sheet-organizer");
|
||||
// Pass empty command line args to allow my own parsing
|
||||
@@ -75,83 +66,44 @@ async fn main() {
|
||||
.run_async::<AppModel>(app_init_data);
|
||||
}
|
||||
|
||||
pub struct AppInitData {
|
||||
sheets: Vec<Sheet>,
|
||||
orphans: Vec<OrphanFile>,
|
||||
database: Database,
|
||||
}
|
||||
|
||||
pub struct FileValidationResult {
|
||||
validated_sheets: Vec<Sheet>,
|
||||
invalidated_sheets: Vec<Sheet>,
|
||||
updated_sheets: Vec<Sheet>,
|
||||
|
||||
validated_orphan_files: Vec<OrphanFile>,
|
||||
invalidated_orphan_files: Vec<OrphanFile>,
|
||||
updated_orphan_files: Vec<OrphanFile>,
|
||||
|
||||
unassigned_files: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
impl FileValidationResult {
|
||||
fn get_stats(&self) -> String {
|
||||
format!("Validated sheets: {}\nInvalidated sheets: {}\nUpdated sheets: {}\nValidated orphan_files: {}\nInvalidated orphan_files: {}\nUpdated orphan_files: {}\nUnassigned files: {}",
|
||||
format!("Validated sheets: {}\nInvalidated sheets: {}\nUpdated sheets: {}\nUnassigned files: {}",
|
||||
self.validated_sheets.len(), self.invalidated_sheets.len(), self.updated_sheets.len(),
|
||||
self.validated_orphan_files.len(), self.invalidated_orphan_files.len(), self.updated_orphan_files.len(), self.unassigned_files.len())
|
||||
self.unassigned_files.len())
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_sheet_files(
|
||||
sheets: Vec<Sheet>,
|
||||
orphan_files: Vec<OrphanFile>,
|
||||
dir: impl AsRef<Path>,
|
||||
) -> FileValidationResult {
|
||||
// TODO: fix duplication
|
||||
fn validate_sheet_files(sheets: Vec<Sheet>, dir: impl AsRef<Path>) -> FileValidationResult {
|
||||
let (validated_sheets, mut invalidated_sheets): (Vec<_>, Vec<_>) = sheets
|
||||
.into_iter()
|
||||
.partition(|sheet| sheet.validate_path(&sheet.path).unwrap_or(false));
|
||||
let (validated_orphan_files, mut invalidated_orphan_files): (Vec<_>, Vec<_>) =
|
||||
orphan_files.into_iter().partition(|orphan_file| {
|
||||
orphan_file
|
||||
.validate_path(&orphan_file.path)
|
||||
.unwrap_or(false)
|
||||
});
|
||||
.partition(|sheet| sheet.validate_own_path().unwrap_or(false));
|
||||
|
||||
let mut updated_sheets = Vec::new();
|
||||
let mut updated_orphan_files = Vec::new();
|
||||
let mut unassigned_files = Vec::new();
|
||||
|
||||
for pdf_file in WalkDir::new(dir)
|
||||
.into_iter()
|
||||
.filter_map(|e| e.ok())
|
||||
.filter(|file| file.file_type().is_file())
|
||||
.map(|file| file.into_path())
|
||||
.filter(|path| {
|
||||
path.extension()
|
||||
.map(|s| s.to_string_lossy().to_ascii_lowercase() == "pdf")
|
||||
.unwrap_or(false)
|
||||
})
|
||||
{
|
||||
// TODO: improve performance?
|
||||
for pdf_file in find_all_pdfs_in_directory_recursive(dir) {
|
||||
if let Some((i, _)) = invalidated_sheets
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find(|(_, sheet)| sheet.validate_path(&pdf_file).unwrap_or(false))
|
||||
{
|
||||
let mut sheet = invalidated_sheets.remove(i);
|
||||
sheet.path = pdf_file;
|
||||
let new_pdf = Pdf::try_from(pdf_file).unwrap();
|
||||
sheet.update_pdf_file(new_pdf);
|
||||
updated_sheets.push(sheet);
|
||||
} else if let Some((i, _)) = invalidated_orphan_files
|
||||
} else if !validated_sheets
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find(|(_, orphan_file)| orphan_file.validate_path(&pdf_file).unwrap_or(false))
|
||||
{
|
||||
let mut orphan_file = invalidated_orphan_files.remove(i);
|
||||
orphan_file.path = pdf_file;
|
||||
updated_orphan_files.push(orphan_file);
|
||||
} else if !validated_sheets.iter().any(|sheet| sheet.path == pdf_file)
|
||||
&& !validated_orphan_files
|
||||
.iter()
|
||||
.any(|orphan| orphan.path == pdf_file)
|
||||
.any(|sheet| sheet.pdf_path_equal(&pdf_file))
|
||||
{
|
||||
unassigned_files.push(pdf_file);
|
||||
}
|
||||
@@ -161,9 +113,19 @@ fn validate_sheet_files(
|
||||
validated_sheets,
|
||||
invalidated_sheets,
|
||||
updated_sheets,
|
||||
validated_orphan_files,
|
||||
invalidated_orphan_files,
|
||||
updated_orphan_files,
|
||||
unassigned_files,
|
||||
}
|
||||
}
|
||||
|
||||
fn find_all_pdfs_in_directory_recursive(dir: impl AsRef<Path>) -> impl Iterator<Item = PathBuf> {
|
||||
WalkDir::new(dir)
|
||||
.into_iter()
|
||||
.filter_map(|e| e.ok())
|
||||
.filter(|file| file.file_type().is_file())
|
||||
.map(|file| file.into_path())
|
||||
.filter(|path| {
|
||||
path.extension()
|
||||
.map(|s| s.to_string_lossy().to_ascii_lowercase() == "pdf")
|
||||
.unwrap_or(false)
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user