Successfully implement orphan database inserting
This commit is contained in:
71
src/main.rs
71
src/main.rs
@@ -12,7 +12,7 @@ use database::Database;
|
||||
use env_logger::Env;
|
||||
use log::{debug, error};
|
||||
use relm4::RelmApp;
|
||||
use sheet::Sheet;
|
||||
use sheet::{OrphanFile, Sheet};
|
||||
use walkdir::WalkDir;
|
||||
|
||||
use crate::ui::app::AppModel;
|
||||
@@ -35,17 +35,41 @@ async fn main() {
|
||||
let database = Database::setup("./testdb.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();
|
||||
|
||||
debug!("Validating sheets from database...");
|
||||
let validation_result = validate_sheet_files(sheets, &cli.directory);
|
||||
debug!("{}", validation_result.get_stats());
|
||||
let mut validation_result = validate_sheet_files(sheets, orphan_files, &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;
|
||||
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);
|
||||
}
|
||||
|
||||
let mut sheets = validation_result.validated_sheets;
|
||||
sheets.append(&mut validation_result.updated_sheets);
|
||||
|
||||
let sheets_and_orphans = SheetsAndOrphans { sheets, orphans };
|
||||
|
||||
let app = RelmApp::new("de.frajul.sheet-organizer");
|
||||
// Pass empty command line args to allow my own parsing
|
||||
app.with_args(Vec::new()).run::<AppModel>(validation_result);
|
||||
app.with_args(Vec::new())
|
||||
.run::<AppModel>(sheets_and_orphans);
|
||||
}
|
||||
|
||||
pub struct SheetsAndOrphans {
|
||||
sheets: Vec<Sheet>,
|
||||
orphans: Vec<OrphanFile>,
|
||||
}
|
||||
|
||||
pub struct FileValidationResult {
|
||||
@@ -53,21 +77,39 @@ pub struct FileValidationResult {
|
||||
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: {}\nUnassigned files: {}", self.validated_sheets.len(), self.invalidated_sheets.len(), self.updated_sheets.len(), self.unassigned_files.len())
|
||||
format!("Validated sheets: {}\nInvalidated sheets: {}\nUpdated sheets: {}\nValidated orphan_files: {}\nInvalidated orphan_files: {}\nUpdated orphan_files: {}\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())
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_sheet_files(sheets: Vec<Sheet>, dir: impl AsRef<Path>) -> FileValidationResult {
|
||||
fn validate_sheet_files(
|
||||
sheets: Vec<Sheet>,
|
||||
orphan_files: Vec<OrphanFile>,
|
||||
dir: impl AsRef<Path>,
|
||||
) -> FileValidationResult {
|
||||
// TODO: fix duplication
|
||||
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)
|
||||
});
|
||||
|
||||
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)
|
||||
@@ -89,7 +131,19 @@ fn validate_sheet_files(sheets: Vec<Sheet>, dir: impl AsRef<Path>) -> FileValida
|
||||
let mut sheet = invalidated_sheets.remove(i);
|
||||
sheet.path = pdf_file;
|
||||
updated_sheets.push(sheet);
|
||||
} else {
|
||||
} else if let Some((i, _)) = invalidated_orphan_files
|
||||
.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)
|
||||
{
|
||||
unassigned_files.push(pdf_file);
|
||||
}
|
||||
}
|
||||
@@ -98,6 +152,9 @@ fn validate_sheet_files(sheets: Vec<Sheet>, dir: impl AsRef<Path>) -> FileValida
|
||||
validated_sheets,
|
||||
invalidated_sheets,
|
||||
updated_sheets,
|
||||
validated_orphan_files,
|
||||
invalidated_orphan_files,
|
||||
updated_orphan_files,
|
||||
unassigned_files,
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user