chore: refactor ahead of supporting more conversion types
All checks were successful
Checking Renovate configuration / validate (pull_request) Successful in 1m5s
Build and test / Clippy (pull_request) Successful in 3m25s
Checking yaml / Run yamllint (pull_request) Successful in 11s
Build and test / Tests (pull_request) Successful in 4m12s
Build and test / Build AMD64 (pull_request) Successful in 4m12s
Build and test / Generate Documentation (pull_request) Successful in 3m52s

This commit is contained in:
Marc Plano-Lesay 2025-10-10 16:34:52 +11:00
parent 48b560d85e
commit 034f0b142c
Signed by: kernald
GPG key ID: 66A41B08CC62A6CF
12 changed files with 468 additions and 150 deletions

74
src/job.rs Normal file
View file

@ -0,0 +1,74 @@
use std::path::PathBuf;
use std::time::Duration;
use anyhow::Result;
use indicatif::{ProgressBar, ProgressStyle};
use rayon::prelude::*;
use crate::formats::cbz::CbzReader;
use crate::formats::pdf::PdfWriter;
use crate::formats::{FormatId, FormatReader, FormatWriter};
#[derive(Debug, Clone)]
pub struct Job {
pub from: FormatId,
pub to: FormatId,
pub input_path: PathBuf,
pub output_path: PathBuf,
}
impl Job {
pub fn new(input_path: PathBuf, output_dir: PathBuf, from: FormatId, to: FormatId) -> Self {
let mut output_path = output_dir.join(input_path.file_name().unwrap());
match to {
FormatId::Pdf => output_path.set_extension("pdf"),
FormatId::Cbz => output_path.set_extension("cbz"),
};
Self {
from,
to,
input_path,
output_path,
}
}
}
fn get_reader(format: FormatId) -> Box<dyn FormatReader> {
match format {
FormatId::Cbz => Box::new(CbzReader),
// Placeholder for future formats
FormatId::Pdf => unimplemented!("Reading PDF not implemented"),
}
}
fn get_writer(format: FormatId) -> Box<dyn FormatWriter> {
match format {
FormatId::Pdf => Box::new(PdfWriter),
// Placeholder for future formats
FormatId::Cbz => unimplemented!("Writing CBZ not implemented"),
}
}
pub fn process_jobs(jobs: Vec<Job>) -> Result<()> {
let pb = ProgressBar::new(jobs.len() as u64);
pb.enable_steady_tick(Duration::from_millis(300));
pb.set_style(ProgressStyle::with_template(
"[{elapsed_precise}] {wide_bar} {pos:>7}/{len:7} {msg}",
)?);
jobs.par_iter().for_each(|job| {
// Build the pipeline for each job
let reader = get_reader(job.from);
let writer = get_writer(job.to);
let doc = reader.read(&job.input_path).expect("Failed to read input");
writer
.write(&doc, &job.output_path)
.expect("Failed to write output");
pb.inc(1);
});
pb.finish();
Ok(())
}