feat: support cbr reading
All checks were successful
Build and test / Tests (pull_request) Successful in 1m12s
Checking yaml / Run yamllint (pull_request) Successful in 4s
Build and test / Build AMD64 (pull_request) Successful in 1m3s
Build and test / Generate Documentation (pull_request) Successful in 58s
Checking Renovate configuration / validate (pull_request) Successful in 1m50s
Build and test / Clippy (pull_request) Successful in 1m2s

This commit is contained in:
Marc Plano-Lesay 2025-10-26 19:14:22 +11:00
parent a16ec085b1
commit 6379e8a56b
Signed by: kernald
GPG key ID: 66A41B08CC62A6CF
10 changed files with 216 additions and 20 deletions

23
tests/cbr_reader_tests.rs Normal file
View file

@ -0,0 +1,23 @@
use std::fs::File;
use std::io::Write;
use cbz2pdf::formats::cbr::CbrReader;
use cbz2pdf::formats::FormatReader;
// We cannot reliably create a RAR archive in tests (tools cannot create .rar),
// Instead, verify that the reader fails gracefully (returns an error) when given an invalid .cbr
// file.
#[test]
fn cbr_reader_errors_on_invalid_archive() {
let temp_dir = tempfile::tempdir().expect("create temp dir");
let cbr_path = temp_dir.path().join("invalid.cbr");
// Write some junk that is definitely not a RAR archive
let mut f = File::create(&cbr_path).expect("create cbr");
f.write_all(b"this is not a rar archive").unwrap();
let reader = CbrReader;
let res = reader.read(&cbr_path);
assert!(res.is_err(), "CbrReader should error on invalid archives");
}

View file

@ -7,8 +7,10 @@ use cbz2pdf::job::Job;
fn detect_from_path_recognizes_extensions() {
let cbz = PathBuf::from("/tmp/book.cbz");
let pdf = PathBuf::from("/tmp/book.pdf");
let cbr = PathBuf::from("/tmp/book.cbr");
assert_eq!(FormatId::detect_from_path(&cbz), Some(FormatId::Cbz));
assert_eq!(FormatId::detect_from_path(&pdf), Some(FormatId::Pdf));
assert_eq!(FormatId::detect_from_path(&cbr), Some(FormatId::Cbr));
assert_eq!(
FormatId::detect_from_path(&PathBuf::from("/tmp/book.txt")),
None
@ -24,11 +26,19 @@ fn job_new_sets_output_extension() {
let job2 = Job::new(
PathBuf::from("/tmp/book.pdf"),
outdir,
outdir.clone(),
FormatId::Pdf,
FormatId::Cbz,
);
assert!(job2.output_path.ends_with("book.cbz"));
let job3 = Job::new(
PathBuf::from("/tmp/book.cbz"),
outdir,
FormatId::Cbz,
FormatId::Cbr,
);
assert!(job3.output_path.ends_with("book.cbr"));
}
#[test]
@ -37,4 +47,6 @@ fn format_capabilities_consistent() {
assert!(FormatId::Cbz.can_write());
assert!(FormatId::Pdf.can_write());
assert!(FormatId::Pdf.can_read());
assert!(FormatId::Cbr.can_read());
assert!(!FormatId::Cbr.can_write());
}