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
				
			
		
		
	
	
				
					
				
			
		
			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:
		
							parent
							
								
									48b560d85e
								
							
						
					
					
						commit
						034f0b142c
					
				
					 12 changed files with 468 additions and 150 deletions
				
			
		
							
								
								
									
										74
									
								
								src/job.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								src/job.rs
									
										
									
									
									
										Normal 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(())
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue