use crate::error::{Error, Result};
use crate::types::{ProtectionContext, ProtectionLevel, ProtectionWarning, VerificationStatus};
use image::DynamicImage;
fn join_err(e: tokio::task::JoinError) -> Error {
if e.is_cancelled() {
Error::Task(format!("cancelled: {e}"))
} else if e.is_panic() {
Error::Task(format!("panicked: {e}"))
} else {
Error::Task(format!("{e}"))
}
}
#[must_use = "the protected image should be saved or used"]
pub async fn process_image_async(
img: DynamicImage,
level: ProtectionLevel,
ctx: ProtectionContext,
) -> Result<DynamicImage> {
tokio::task::spawn_blocking(move || crate::process_image(img, level, &ctx))
.await
.map_err(join_err)?
}
#[must_use = "the protected image bytes should be saved or used"]
pub async fn process_image_bytes_async(
img_bytes: Vec<u8>,
level: ProtectionLevel,
ctx: ProtectionContext,
) -> Result<Vec<u8>> {
tokio::task::spawn_blocking(move || crate::process_image_bytes(&img_bytes, level, &ctx))
.await
.map_err(join_err)?
}
#[must_use = "the protected image bytes and warnings should be used"]
pub async fn process_image_bytes_with_warnings_async(
img_bytes: Vec<u8>,
level: ProtectionLevel,
ctx: ProtectionContext,
) -> Result<(Vec<u8>, Vec<ProtectionWarning>)> {
tokio::task::spawn_blocking(move || {
crate::process_image_bytes_with_warnings(&img_bytes, level, &ctx)
})
.await
.map_err(join_err)?
}
#[must_use = "the protected images should be saved or used"]
pub async fn process_images_parallel_async(
images: Vec<DynamicImage>,
level: ProtectionLevel,
ctx: ProtectionContext,
) -> Result<Vec<DynamicImage>> {
tokio::task::spawn_blocking(move || crate::process_images_parallel(&images, level, &ctx))
.await
.map_err(join_err)?
}
#[must_use = "the protected image bytes should be saved or used"]
pub async fn process_images_bytes_parallel_async(
images: Vec<Vec<u8>>,
level: ProtectionLevel,
ctx: ProtectionContext,
) -> Result<Vec<Vec<u8>>> {
tokio::task::spawn_blocking(move || crate::process_images_bytes_parallel(&images, level, &ctx))
.await
.map_err(join_err)?
}
#[must_use = "the verification result should be checked"]
pub async fn verify_image_bytes_async(
img_bytes: Vec<u8>,
mac_key: Vec<u8>,
) -> Result<VerificationStatus> {
tokio::task::spawn_blocking(move || crate::verify_image_bytes(&img_bytes, &mac_key))
.await
.map_err(join_err)
}