Skip to content

Commit

Permalink
Merge pull request #2 from gaku-sei/improve-eco-cbz-further
Browse files Browse the repository at this point in the history
Simplify the eco-cbz crate further
  • Loading branch information
gaku-sei authored Oct 30, 2023
2 parents 51921f9 + ebeb74e commit a503c90
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 30 deletions.
46 changes: 18 additions & 28 deletions eco-cbz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use std::{
fs::{File, OpenOptions},
io::{self, Cursor, Read, Seek, Write},
marker::PhantomData,
path::Path,
result,
};
Expand Down Expand Up @@ -85,17 +84,13 @@ impl<'a> From<CbzFile<'a>> for ZipFile<'a> {
}

#[derive(Debug)]
pub struct CbzReader<'a, R> {
pub struct CbzReader<R> {
archive: ZipArchive<R>,
_lifetime: PhantomData<&'a ()>,
}

impl<'a, R> CbzReader<'a, R> {
impl<R> CbzReader<R> {
pub fn new(archive: ZipArchive<R>) -> Self {
Self {
archive,
_lifetime: PhantomData,
}
Self { archive }
}

pub fn archive(&self) -> &ZipArchive<R> {
Expand All @@ -107,7 +102,7 @@ impl<'a, R> CbzReader<'a, R> {
}
}

impl<'a, R> CbzReader<'a, R>
impl<R> CbzReader<R>
where
R: Read + Seek,
{
Expand Down Expand Up @@ -179,7 +174,7 @@ where
}
}

impl<'a> CbzReader<'a, File> {
impl CbzReader<File> {
/// Creates `CbzReader` from a path
///
/// ## Errors
Expand All @@ -192,7 +187,7 @@ impl<'a> CbzReader<'a, File> {
}
}

impl<'a, 'b> CbzReader<'a, Cursor<&'b [u8]>> {
impl<'b> CbzReader<Cursor<&'b [u8]>> {
/// Creates `CbzReader` from a bytes slice
///
/// ## Errors
Expand All @@ -205,7 +200,7 @@ impl<'a, 'b> CbzReader<'a, Cursor<&'b [u8]>> {
}
}

impl<'a> CbzReader<'a, Cursor<Vec<u8>>> {
impl CbzReader<Cursor<Vec<u8>>> {
/// Creates `CbzReader` from bytes
///
/// ## Errors
Expand All @@ -218,34 +213,29 @@ impl<'a> CbzReader<'a, Cursor<Vec<u8>>> {
}
}

impl<'a, R> From<ZipArchive<R>> for CbzReader<'a, R> {
impl<R> From<ZipArchive<R>> for CbzReader<R> {
fn from(archive: ZipArchive<R>) -> Self {
Self::new(archive)
}
}

impl<'a, R> From<CbzReader<'a, R>> for ZipArchive<R> {
fn from(cbz: CbzReader<'a, R>) -> Self {
impl<R> From<CbzReader<R>> for ZipArchive<R> {
fn from(cbz: CbzReader<R>) -> Self {
cbz.archive
}
}

pub struct CbzWriter<'a, W: Write + Seek> {
pub struct CbzWriter<W: Write + Seek> {
archive: ZipWriter<W>,
size: usize,
_lifetime: PhantomData<&'a ()>,
}

impl<'a, W> CbzWriter<'a, W>
impl<W> CbzWriter<W>
where
W: Write + Seek,
{
pub fn new(archive: ZipWriter<W>) -> Self {
Self {
archive,
size: 0,
_lifetime: PhantomData,
}
Self { archive, size: 0 }
}

/// Creates a `CbzWriter` from a `Write`
Expand Down Expand Up @@ -341,7 +331,7 @@ where
}
}

impl<'a> CbzWriter<'a, Cursor<Vec<u8>>> {
impl CbzWriter<Cursor<Vec<u8>>> {
/// ## Errors
///
/// Same errors as the underlying `ZipWriter::finish` method
Expand Down Expand Up @@ -374,13 +364,13 @@ impl<'a> CbzWriter<'a, Cursor<Vec<u8>>> {
}
}

impl<'a> Default for CbzWriter<'a, Cursor<Vec<u8>>> {
impl Default for CbzWriter<Cursor<Vec<u8>>> {
fn default() -> Self {
Self::from_writer(Cursor::new(Vec::new()))
}
}

impl<'a, W> From<ZipWriter<W>> for CbzWriter<'a, W>
impl<W> From<ZipWriter<W>> for CbzWriter<W>
where
W: Write + Seek,
{
Expand All @@ -389,11 +379,11 @@ where
}
}

impl<'a, W> From<CbzWriter<'a, W>> for ZipWriter<W>
impl<W> From<CbzWriter<W>> for ZipWriter<W>
where
W: Write + Seek,
{
fn from(cbz: CbzWriter<'a, W>) -> Self {
fn from(cbz: CbzWriter<W>) -> Self {
cbz.archive
}
}
4 changes: 2 additions & 2 deletions eco-pack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ pub fn get_images_from_glob(glob_expr: impl AsRef<str>) -> Result<Vec<Image>> {
}

#[allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]
pub fn pack_imgs_to_cbz<'a>(
pub fn pack_imgs_to_cbz(
imgs: Vec<Image>,
contrast: Option<f32>,
brightness: Option<i32>,
blur: Option<f32>,
autosplit: bool,
reading_order: ReadingOrder,
) -> Result<CbzWriter<'a, Cursor<Vec<u8>>>> {
) -> Result<CbzWriter<Cursor<Vec<u8>>>> {
let mut cbz_writer = CbzWriter::default();
for mut img in imgs {
if let Some(contrast) = contrast {
Expand Down

0 comments on commit a503c90

Please sign in to comment.