1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#[cfg(feature = "cb7")]
#[cfg_attr(docsrs, doc(cfg(feature = "cb7")))]
pub mod cb7;
#[cfg(feature = "cbt")]
#[cfg_attr(docsrs, doc(cfg(feature = "cbt")))]
pub mod cbt;
#[cfg(feature = "cbz")]
#[cfg_attr(docsrs, doc(cfg(feature = "cbz")))]
pub mod cbz;
use std::{io::Read, path::Path};
use image::open;
/// Abstraction trait for writing comic book files
pub trait ComicBookWriter {
type Error;
/// Add a page at the Comic book archive.
///
/// The additional [`image::ImageFormat`] argument is the initial format the image. (Since we can't extract format via [`image::DynamicImage`]).
/// There are some edge cases where you might need this. (Like handling GIF images for example)
///
/// If the `comicinfo` feature flag is enabled,
/// all writers should normally provide the [`Image`](crate::comicinfo::comic_page_info::ComicPageInfo::image),
/// [`ImageWidth`](crate::comicinfo::comic_page_info::ComicPageInfo::image_width),
/// [`ImageHeight`](crate::comicinfo::comic_page_info::ComicPageInfo::image_height) attributes on `ComicInfo` `Page` elements.
/// The [`DoublePage`](crate::comicinfo::comic_page_info::ComicPageInfo::double_page) attributes depends on writers implementation
///
fn add_page(
&mut self,
image: image::DynamicImage,
_format: Option<image::ImageFormat>,
) -> Result<(), Self::Error>;
/// Add an additional file to the archive
///
/// You might need if you want
///
fn add_file<P, R>(&mut self, path: P, file: R) -> Result<(), Self::Error>
where
P: AsRef<Path>,
R: Read;
/// Similiar to [`Self::add_page`] but will allow you to specify some `ComixInfo.xml` page type, and bookmark.
#[cfg(feature = "comicinfo")]
#[cfg_attr(docsrs, doc(feature = "comicinfo"))]
fn add_page_with_metadata(
&mut self,
image: image::DynamicImage,
_format: Option<image::ImageFormat>,
_page_type: Vec<crate::comicinfo::comic_page_type::ComicPageType>,
_bookmark: Option<String>,
) -> Result<(), Self::Error> {
self.add_page(image, _format)
}
}
/// Some utilities extension methods for [`ComicBookWriter`]
pub trait ComicBookWriterExt: ComicBookWriter {
fn add_page_from_path<P>(&mut self, path: P) -> Result<(), Self::Error>
where
P: AsRef<Path>,
Self::Error: From<image::ImageError>,
{
self.add_page(
open(path.as_ref())?,
image::ImageFormat::from_path(path).ok(),
)
}
}
impl<C> ComicBookWriterExt for C where C: ComicBookWriter {}