remove useless format Bendable method, modify API exposition

This commit is contained in:
brevalferrari 2025-06-19 10:34:36 +02:00
parent 6bb627d77b
commit f72a30b972
Signed by: breval
GPG key ID: 913954DA013FAD4F
8 changed files with 4 additions and 56 deletions

View file

@ -37,7 +37,4 @@ impl Bendable for Bytes {
iter.for_each(|e| *e = f(Cow::Borrowed(e))); iter.for_each(|e| *e = f(Cow::Borrowed(e)));
self self
} }
fn format() -> crate::Format {
crate::Format::Binary
}
} }

View file

@ -92,10 +92,6 @@ impl Bendable for PdfDocument {
bookmarks, bookmarks,
} }
} }
fn format() -> crate::Format {
crate::Format::Archive
}
} }
#[cfg(test)] #[cfg(test)]

View file

@ -59,8 +59,4 @@ impl Bendable for ShivaDocument {
self.output_format, self.output_format,
) )
} }
fn format() -> crate::Format {
crate::Format::Doc
}
} }

View file

@ -46,8 +46,4 @@ impl Bendable for Font {
) )
.expect("coudn't get font back from bytes after map") .expect("coudn't get font back from bytes after map")
} }
fn format() -> crate::Format {
crate::Format::Font
}
} }

View file

@ -15,7 +15,7 @@ use num::{
use rayon::iter::ParallelIterator; use rayon::iter::ParallelIterator;
use thiserror::Error; use thiserror::Error;
use crate::{Bendable, Format, FromDataBytes, IntoDataBytes, TryFromDataBytes}; use crate::{Bendable, FromDataBytes, IntoDataBytes, TryFromDataBytes};
impl<P: Pixel> IntoDataBytes for ImageBuffer<P, Vec<P::Subpixel>> impl<P: Pixel> IntoDataBytes for ImageBuffer<P, Vec<P::Subpixel>>
where where
@ -188,9 +188,6 @@ where
iter.for_each(|p| *p = f(Cow::Borrowed(p))); iter.for_each(|p| *p = f(Cow::Borrowed(p)));
self self
} }
fn format() -> crate::Format {
Format::Image
}
} }
impl IntoDataBytes for DynamicImage { impl IntoDataBytes for DynamicImage {

View file

@ -13,7 +13,7 @@ pub mod txt;
pub type Bytes = Vec<u8>; pub type Bytes = Vec<u8>;
mod dynamic { pub mod dynamic {
#[cfg(feature = "text")] #[cfg(feature = "text")]
use std::string::FromUtf8Error; use std::string::FromUtf8Error;
use std::{ use std::{
@ -40,53 +40,33 @@ mod dynamic {
use cfg_if::cfg_if; use cfg_if::cfg_if;
#[cfg(feature = "fonts")] #[cfg(feature = "fonts")]
use font_kit::error::FontLoadingError; use font_kit::error::FontLoadingError;
pub use infer::MatcherType; pub use infer::*;
#[cfg(feature = "documents")] #[cfg(feature = "documents")]
use printpdf::PdfDocument; use printpdf::PdfDocument;
#[cfg(feature = "documents")] #[cfg(feature = "documents")]
use shiva::core::{bytes, Document, DocumentType}; use shiva::core::{bytes, Document, DocumentType};
use strum::{AsRefStr, EnumDiscriminants, EnumString};
use thiserror::Error; use thiserror::Error;
#[derive(EnumDiscriminants)]
#[strum_discriminants(name(Format), derive(EnumString, AsRefStr))]
pub enum DynamicBendable<'a> { pub enum DynamicBendable<'a> {
#[cfg(feature = "pictures")] #[cfg(feature = "pictures")]
#[strum_discriminants(strum(serialize = "img", serialize = "image"))]
Image(DynamicImage), Image(DynamicImage),
#[cfg(feature = "binary")] #[cfg(feature = "binary")]
#[strum_discriminants(strum(serialize = "bin", serialize = "binary"))]
Binary(Bytes), Binary(Bytes),
#[cfg(feature = "music")] #[cfg(feature = "music")]
#[strum_discriminants(strum(serialize = "snd", serialize = "sound"))]
Sound(Audio), Sound(Audio),
#[cfg(feature = "text")] #[cfg(feature = "text")]
#[strum_discriminants(strum(serialize = "txt", serialize = "text"))]
Text(Text<'a>), Text(Text<'a>),
#[cfg(not(feature = "text"))] #[cfg(not(feature = "text"))]
Phantom(PhantomData<&'a ()>), Phantom(PhantomData<&'a ()>),
#[cfg(feature = "documents")] #[cfg(feature = "documents")]
#[strum_discriminants(strum(serialize = "doc", serialize = "document"))]
Doc(ShivaDocument), Doc(ShivaDocument),
#[cfg(feature = "documents")] #[cfg(feature = "documents")]
#[strum_discriminants(strum(serialize = "arc", serialize = "archive"))]
Archive(PdfDocument), Archive(PdfDocument),
Meta, Meta,
#[cfg(feature = "fonts")] #[cfg(feature = "fonts")]
#[strum_discriminants(strum(serialize = "fnt", serialize = "font"))]
Font(FontKitFont), Font(FontKitFont),
} }
#[cfg(feature = "pictures")]
#[cfg(test)]
#[test]
fn format_str() {
use std::str::FromStr;
assert_eq!("image", Format::Image.as_ref());
assert_eq!(Ok(Format::Image), Format::from_str("img"));
}
#[cfg(feature = "shiva")] #[cfg(feature = "shiva")]
#[derive(Debug, Error)] #[derive(Debug, Error)]
#[error("extension is unknown by Shiva")] #[error("extension is unknown by Shiva")]
@ -165,15 +145,11 @@ mod dynamic {
} }
self self
} }
fn format() -> Format {
Format::Meta
}
} }
pub type DynamicResult = Result<Option<DynamicBendable<'static>>, OpenError>; pub type DynamicResult = Result<Option<DynamicBendable<'static>>, OpenError>;
fn guess(t: Option<infer::Type>, bytes: Bytes) -> DynamicResult { pub fn guess(t: Option<infer::Type>, bytes: Bytes) -> DynamicResult {
use MatcherType::*; use MatcherType::*;
t.map(|t| (t.matcher_type(), t.extension())) t.map(|t| (t.matcher_type(), t.extension()))
.map( .map(
@ -244,8 +220,6 @@ mod dynamic {
use std::{borrow::Cow, convert::Infallible}; use std::{borrow::Cow, convert::Infallible};
pub use dynamic::*;
pub trait Bendable: TryFromDataBytes + IntoDataBytes { pub trait Bendable: TryFromDataBytes + IntoDataBytes {
type Unit; type Unit;
fn bend_into<T: TryFromDataBytes + IntoDataBytes>( fn bend_into<T: TryFromDataBytes + IntoDataBytes>(
@ -263,7 +237,6 @@ pub trait Bendable: TryFromDataBytes + IntoDataBytes {
Self::try_from_data_bytes(b.into_data_bytes(), format, crop) Self::try_from_data_bytes(b.into_data_bytes(), format, crop)
} }
fn map<F: Fn(Cow<Self::Unit>) -> Self::Unit + Sync>(self, f: F) -> Self; fn map<F: Fn(Cow<Self::Unit>) -> Self::Unit + Sync>(self, f: F) -> Self;
fn format() -> Format;
} }
pub trait IntoDataBytes: Sized { pub trait IntoDataBytes: Sized {

View file

@ -93,8 +93,5 @@ if #[cfg(feature = "rayon")] {
self.0.iter_mut().for_each(|e| *e = f(Cow::Borrowed(e))); self.0.iter_mut().for_each(|e| *e = f(Cow::Borrowed(e)));
self self
} }
fn format() -> crate::dynamic::Format {
crate::Format::Sound
}
} }
}} }}

View file

@ -31,8 +31,4 @@ impl Bendable for Text<'_> {
.collect::<String>() .collect::<String>()
.into() .into()
} }
fn format() -> crate::Format {
crate::Format::Text
}
} }