use martin_tile_utils::Format;
use crate::tiles::neighbourhood::NeighbourhoodError;
#[derive(thiserror::Error, Debug)]
#[non_exhaustive]
pub enum HillshadeError {
#[error(
"The centre normal tile was fetched but could not be decoded as an image. \
The upstream source served malformed data for this tile."
)]
CorruptCentreTile,
#[error(
"Baked hillshade is {actual} bytes but its {side}x{side} dimensions require {expected}"
)]
MalformedBake {
actual: usize,
expected: usize,
side: u32,
},
#[error(
"Hillshade cannot be encoded as {0}. Supported formats are png, webp, and jxl, \
all lossless. A hillshade is multiplied over the basemap, so a lossy \
codec would show visible blotches on flat terrain."
)]
UnsupportedFormat(Format),
#[error("Failed to encode the baked hillshade as {format}: {source}")]
Encoding {
format: Format,
source: image::ImageError,
},
}
impl From<NeighbourhoodError> for HillshadeError {
fn from(value: NeighbourhoodError) -> Self {
match value {
NeighbourhoodError::CorruptCentreTile => Self::CorruptCentreTile,
}
}
}
impl crate::Classify for HillshadeError {
fn kind(&self) -> crate::ErrorKind {
use crate::ErrorKind::{Internal, InvalidInput, Unavailable};
match self {
Self::CorruptCentreTile => Unavailable,
Self::UnsupportedFormat(_) => InvalidInput,
Self::MalformedBake { .. } | Self::Encoding { .. } => Internal,
}
}
}