use std::path::PathBuf;
use thiserror::Error;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum CacheRootProblem {
NotAbsolute,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum CachePathProblem {
MissingParentDirectory,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum CacheDirectoryProblem {
Symlink,
NotDirectory,
WrongOwner,
GroupOrOtherAccessible,
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ThumbnailError {
#[error("invalid thumbnail URI identity: {reason}")]
#[non_exhaustive]
InvalidUriIdentity {
reason: &'static str,
},
#[error("invalid cache namespace: {reason}")]
#[non_exhaustive]
InvalidNamespace {
reason: &'static str,
},
#[error("cache root could not be resolved: {reason}")]
#[non_exhaustive]
CacheRootUnavailable {
reason: &'static str,
},
#[error("invalid cache root {path:?}: {problem:?}")]
#[non_exhaustive]
InvalidCacheRoot {
path: PathBuf,
problem: CacheRootProblem,
},
#[error("invalid cache path {path:?}: {problem:?}")]
#[non_exhaustive]
InvalidCachePath {
path: PathBuf,
problem: CachePathProblem,
},
#[error("insecure cache directory {path:?}: {problem:?}")]
#[non_exhaustive]
InsecureCacheDirectory {
path: PathBuf,
problem: CacheDirectoryProblem,
},
#[error("{context}: {source}")]
#[non_exhaustive]
Io {
context: &'static str,
path: Option<PathBuf>,
#[source]
source: std::io::Error,
},
#[error("png error: {message}")]
#[non_exhaustive]
Png {
message: String,
},
#[error("invalid thumbnail metadata: {reason}")]
#[non_exhaustive]
InvalidMetadata {
reason: &'static str,
},
#[error("unsupported rendered thumbnail: {reason}")]
#[non_exhaustive]
UnsupportedRenderedThumbnail {
reason: &'static str,
},
#[error("resource limit exceeded: {reason}")]
#[non_exhaustive]
ResourceLimitExceeded {
reason: &'static str,
},
#[error("refused to remove cache entry: {reason}")]
#[non_exhaustive]
UnsafeRemoval {
reason: &'static str,
},
}
impl ThumbnailError {
pub(crate) const fn invalid_uri(reason: &'static str) -> Self {
Self::InvalidUriIdentity { reason }
}
pub(crate) const fn invalid_namespace(reason: &'static str) -> Self {
Self::InvalidNamespace { reason }
}
pub(crate) const fn cache_root_unavailable(reason: &'static str) -> Self {
Self::CacheRootUnavailable { reason }
}
pub(crate) const fn invalid_metadata(reason: &'static str) -> Self {
Self::InvalidMetadata { reason }
}
pub(crate) const fn unsupported_rendered_thumbnail(reason: &'static str) -> Self {
Self::UnsupportedRenderedThumbnail { reason }
}
pub(crate) const fn resource_limit_exceeded(reason: &'static str) -> Self {
Self::ResourceLimitExceeded { reason }
}
pub(crate) const fn unsafe_removal(reason: &'static str) -> Self {
Self::UnsafeRemoval { reason }
}
pub(crate) fn png(message: impl Into<String>) -> Self {
Self::Png {
message: message.into(),
}
}
pub(crate) fn io(
context: &'static str,
path: impl Into<Option<PathBuf>>,
source: std::io::Error,
) -> Self {
Self::Io {
context,
path: path.into(),
source,
}
}
}
pub type Result<T, E = ThumbnailError> = std::result::Result<T, E>;