Files
craft-libs/crates/image-codecs/src/error.rs
T
hikari 102f7c6204 feat(image-codecs): let builds leave out codecs they never use
Every codec dispatched by decode_as_with and encode was linked into any
binary that calls them, so a program reading PNG and JPEG also carried
OpenEXR, WebP, the JPEG XL encoder and a dozen rarer codecs.

Each of those now sits behind a default Cargo feature: dds, dicom, dpx,
exr, fits, iff, jpegls, jxl-encode, pnm, tga, webp and xpm. The default
build is unchanged. With a feature off its codec, its dependency (exr,
image-webp, zune-jpegxl, the image crate's TGA decoder) and its
format-specific functions are not compiled in; the format is still
detected, caps reports it neither readable nor writable, and decoding,
encoding or probing it returns CodecError::Unsupported naming the
feature. SGI and Sun Raster now give that same named reason.
2026-11-24 10:24:42 +01:00

36 lines
1.4 KiB
Rust

use crate::format::Format;
/// Errors produced by this crate. Decoders never panic on malformed input;
/// they return [`CodecError::Malformed`] instead.
#[derive(Debug, thiserror::Error)]
pub enum CodecError {
#[error("unrecognized image format")]
UnknownFormat,
#[error("{format:?}: {reason}")]
Unsupported { format: Format, reason: String },
#[error("{format:?}: malformed data: {message}")]
Malformed { format: Format, message: String },
#[error("limit exceeded: {0}")]
LimitExceeded(String),
#[error("{format:?}: encode failed: {message}")]
Encode { format: Format, message: String },
#[error("invalid image: {0}")]
InvalidImage(String),
}
impl CodecError {
pub(crate) fn malformed(format: Format, e: impl std::fmt::Display) -> Self {
CodecError::Malformed { format, message: e.to_string() }
}
pub(crate) fn encode(format: Format, e: impl std::fmt::Display) -> Self {
CodecError::Encode { format, message: e.to_string() }
}
pub(crate) fn unsupported(format: Format, reason: impl Into<String>) -> Self {
CodecError::Unsupported { format, reason: reason.into() }
}
/// `format`'s codec is behind the Cargo feature `feature`, which this build leaves out.
pub(crate) fn feature_missing(format: Format, feature: &str) -> Self {
CodecError::unsupported(format, format!("this build leaves out the {feature} feature"))
}
}