#[cfg(feature = "image")]
use image::ImageError;
use thiserror::Error;
use crate::math::sampling::SamplingError;
#[derive(Debug, Error)]
pub enum Error {
#[error("Image data is empty: no pixels to process")]
EmptyImageData,
#[error("Image data is invalid: contains invalid pixel data")]
InvalidImageData,
#[error("Palette extraction process failed with error: {details}")]
PaletteExtractionError {
details: String,
},
#[error("Swatch selection process failed with error: {cause}")]
SwatchSelectionError {
#[from]
cause: SamplingError,
},
#[error("Unsupported algorithm specified: '{name}'")]
UnsupportedAlgorithm {
name: String,
},
#[error("Unsupported theme specified: '{name}'")]
UnsupportedTheme {
name: String,
},
#[cfg(feature = "image")]
#[error("Image loading process failed with error: {cause}")]
ImageLoadError {
#[from]
cause: ImageError,
},
#[cfg(feature = "image")]
#[error("Image format or color type is not supported")]
UnsupportedImageFormat,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_empty_image_data() {
let actual = Error::EmptyImageData;
assert_eq!(
actual.to_string(),
"Image data is empty: no pixels to process"
);
}
#[test]
fn test_invalid_image_data() {
let actual = Error::InvalidImageData;
assert_eq!(
actual.to_string(),
"Image data is invalid: contains invalid pixel data"
);
}
#[test]
fn test_palette_extraction_error() {
let actual = Error::PaletteExtractionError {
details: "Details about the failure.".to_string(),
};
assert_eq!(
actual.to_string(),
"Palette extraction process failed with error: Details about the failure."
);
}
#[test]
fn test_swatches_selection_error() {
let cause = SamplingError::InvalidDiversity;
let actual = Error::SwatchSelectionError { cause };
assert_eq!(
actual.to_string(),
"Swatch selection process failed with error: Invalid diversity: Diversity score must be between 0.0 and 1.0."
);
}
#[test]
fn test_unsupported_algorithm() {
let actual = Error::UnsupportedAlgorithm {
name: "xmeans".to_string(),
};
assert_eq!(
actual.to_string(),
"Unsupported algorithm specified: 'xmeans'"
);
}
#[test]
fn test_unsupported_theme() {
let actual = Error::UnsupportedTheme {
name: "pastel".to_string(),
};
assert_eq!(actual.to_string(), "Unsupported theme specified: 'pastel'");
}
#[test]
#[cfg(feature = "image")]
fn test_image_load_error() {
let cause = ImageError::IoError(std::io::Error::from(std::io::ErrorKind::NotFound));
let error = Error::ImageLoadError { cause };
let actual = error.to_string();
assert_eq!(
actual,
"Image loading process failed with error: entity not found"
);
}
#[test]
#[cfg(feature = "image")]
fn test_unsupported_image() {
let actual = Error::UnsupportedImageFormat;
assert_eq!(
actual.to_string(),
"Image format or color type is not supported"
);
}
}