cbz2pdf/src/formats/mod.rs
Marc Plano-Lesay b35ccbe271
All checks were successful
Build and test / Clippy (pull_request) Successful in 44s
Build and test / Tests (pull_request) Successful in 48s
Checking yaml / Run yamllint (pull_request) Successful in 5s
Checking Renovate configuration / validate (pull_request) Successful in 1m4s
Build and test / Build AMD64 (pull_request) Successful in 49s
Build and test / Generate Documentation (pull_request) Successful in 59s
feat: implement cbz writing and pdf reading
2025-10-13 16:57:14 +11:00

60 lines
1.3 KiB
Rust

use std::ffi::OsStr;
use std::path::Path;
use anyhow::Result;
use crate::model::Document;
pub mod cbz;
pub mod pdf;
use cbz::{CbzReader, CbzWriter};
use pdf::{PdfReader, PdfWriter};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FormatId {
Cbz,
Pdf,
}
impl FormatId {
#[allow(dead_code)]
pub fn can_read(self) -> bool {
get_reader(self).is_some()
}
#[allow(dead_code)]
pub fn can_write(self) -> bool {
get_writer(self).is_some()
}
pub fn detect_from_path(path: &Path) -> Option<FormatId> {
match path.extension().and_then(OsStr::to_str) {
Some("cbz") => Some(FormatId::Cbz),
Some("pdf") => Some(FormatId::Pdf),
_ => None,
}
}
}
pub trait FormatReader: Send + Sync {
fn read(&self, input: &Path) -> Result<Document>;
}
pub trait FormatWriter: Send + Sync {
fn write(&self, doc: &Document, output: &Path) -> Result<()>;
}
pub fn get_reader(format: FormatId) -> Option<Box<dyn FormatReader>> {
match format {
FormatId::Cbz => Some(Box::new(CbzReader)),
FormatId::Pdf => Some(Box::new(PdfReader)),
}
}
pub fn get_writer(format: FormatId) -> Option<Box<dyn FormatWriter>> {
match format {
FormatId::Pdf => Some(Box::new(PdfWriter)),
FormatId::Cbz => Some(Box::new(CbzWriter)),
}
}