Implement sheet path validation

This commit is contained in:
2024-02-02 17:27:24 +01:00
parent 0addc7c4b9
commit 4f65715967
5 changed files with 99 additions and 31 deletions

View File

@@ -3,16 +3,21 @@ mod mcdu;
mod sheet;
mod sheet_listing;
use std::{path::PathBuf, process};
use std::{
path::{Path, PathBuf},
process,
};
use clap::Parser;
use database::Database;
use env_logger::Env;
use gtk::prelude::*;
use log::error;
use log::{debug, error};
use mcdu::McduModel;
use relm4::prelude::*;
use sheet::Sheet;
use sheet_listing::{SheetListingInput, SheetListingModel};
use walkdir::WalkDir;
struct AppModel {
mcdu: Controller<McduModel>,
@@ -103,7 +108,6 @@ struct Cli {
#[tokio::main]
async fn main() {
// dotenvy::dotenv().unwrap();
env_logger::Builder::from_env(Env::default().default_filter_or("debug")).init();
let cli = Cli::parse();
if !cli.directory.is_dir() {
@@ -111,11 +115,72 @@ async fn main() {
process::exit(1);
}
let database = Database::setup("./testdb.db").await.unwrap();
let database = Database::setup("./testdb.sqlite").await.unwrap();
// database.insert_sheet(Sheet::new_debug()).await.unwrap();
let sheets = database.fetch_all_sheets().await.unwrap();
debug!("Validating sheets from database...");
let validation_result = validate_sheet_files(sheets, &cli.directory);
debug!("{}", validation_result.get_stats());
for updated in validation_result.updated_sheets {
database.update_sheet_path(updated).await.unwrap();
}
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>(cli.directory);
}
struct FileValidationResult {
validated_sheets: Vec<Sheet>,
invalidated_sheets: Vec<Sheet>,
updated_sheets: Vec<Sheet>,
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())
}
}
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 mut updated_sheets = 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)
})
{
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.to_str().unwrap().to_string();
updated_sheets.push(sheet);
} else {
unassigned_files.push(pdf_file);
}
}
FileValidationResult {
validated_sheets,
invalidated_sheets,
updated_sheets,
unassigned_files,
}
}