Skip to content

Commit

Permalink
Merge pull request #1 from gaku-sei/improve-eco-cbz
Browse files Browse the repository at this point in the history
Improve and simplify the eco-cbz crate
  • Loading branch information
gaku-sei authored Oct 30, 2023
2 parents 9a18518 + fb01997 commit 51921f9
Show file tree
Hide file tree
Showing 17 changed files with 236 additions and 525 deletions.
5 changes: 1 addition & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ anyhow = "1.0.71"
async-recursion = "1.0.4"
async-trait = "0.1.68"
base64 = "0.21.2"
bytes = "1.4.0"
camino = "1.1.4"
clap = { version = "4.3.5", features = ["derive"] }
cli-table = "0.4.7"
Expand Down
2 changes: 1 addition & 1 deletion eco-cbz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ edition.workspace = true
rust-version.workspace = true

[dependencies]
bytes.workspace = true
clap = { workspace = true, optional = true }
camino.workspace = true
image.workspace = true
sanitize-filename.workspace = true
thiserror.workspace = true
tracing.workspace = true
zip.workspace = true
Expand Down
53 changes: 26 additions & 27 deletions eco-cbz/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ use std::{
};

use image::{io::Reader as ImageReader, DynamicImage, ImageFormat};
use tracing::debug;

use crate::{CbzWrite, CbzWriterInsertionBuilder, Result};
use crate::{Error, Result};

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
pub enum ReadingOrder {
Rtl,
Expand Down Expand Up @@ -51,7 +50,7 @@ impl Image {
/// ## Errors
///
/// Fails if the image format can't be guessed or the image can't be decoded
pub fn from_reader(reader: impl BufRead + Seek) -> Result<Self> {
pub fn try_from_reader(reader: impl BufRead + Seek) -> Result<Self> {
let reader = ImageReader::new(reader).with_guessed_format()?;
let format = reader.format();
Ok(Self {
Expand All @@ -64,13 +63,7 @@ impl Image {
///
/// Fails if the image format can't be guessed or the image can't be decoded
pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
let buf_reader = Cursor::new(bytes);
let reader = ImageReader::new(buf_reader).with_guessed_format()?;
let format = reader.format();
Ok(Self {
dynamic_image: reader.decode()?,
format,
})
Self::try_from(bytes)
}

fn from_dynamic_image(dynamic_image: DynamicImage, format: Option<ImageFormat>) -> Self {
Expand Down Expand Up @@ -131,26 +124,32 @@ impl Image {
}
}

#[must_use]
pub fn dynamic(&self) -> &DynamicImage {
&self.dynamic_image
}

#[must_use]
pub fn format(&self) -> Option<ImageFormat> {
self.format
}

pub fn set_format(&mut self, format: ImageFormat) -> &Self {
self.format = Some(format);
self
}
}

#[allow(clippy::missing_errors_doc)]
pub fn insert_into_cbz_writer(
self,
cbz_writer: &mut impl CbzWrite,
name: impl AsRef<str>,
) -> Result<()> {
let mut out = Cursor::new(Vec::new());
let format = self.format.unwrap_or(ImageFormat::Png);
self.dynamic_image.write_to(&mut out, format)?;
let insertion = CbzWriterInsertionBuilder::from_extension(format.extensions_str()[0])
.set_bytes_ref(out.get_ref())
.build_custom_str(name.as_ref())?;
cbz_writer.insert_custom_str(insertion)?;
debug!("inserted page into zip");

Ok(())
impl TryFrom<&[u8]> for Image {
type Error = Error;

fn try_from(bytes: &[u8]) -> std::result::Result<Self, Self::Error> {
let buf_reader = Cursor::new(bytes);
let reader = ImageReader::new(buf_reader).with_guessed_format()?;
let format = reader.format();
Ok(Self {
dynamic_image: reader.decode()?,
format,
})
}
}
Loading

0 comments on commit 51921f9

Please sign in to comment.