use thiserror::Error;
#[derive(Error, Debug)]
pub enum AssetError {
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("Index serialization error: {0}")]
Serialization(#[from] serde_json::Error),
#[error("Configuration error: {0}")]
Config(String),
#[error("Asset not found: {0}")]
NotFound(String),
#[error("Cache directory error: {0}")]
CacheDir(String),
#[error("Asset index mutex poisoned: {0}")]
Poisoned(String),
#[error(transparent)]
Core(#[from] devboy_core::Error),
}
pub type Result<T> = std::result::Result<T, AssetError>;
impl AssetError {
pub fn config(msg: impl Into<String>) -> Self {
AssetError::Config(msg.into())
}
pub fn cache_dir(msg: impl Into<String>) -> Self {
AssetError::CacheDir(msg.into())
}
pub fn not_found(id: impl Into<String>) -> Self {
AssetError::NotFound(id.into())
}
pub fn poisoned(msg: impl Into<String>) -> Self {
AssetError::Poisoned(msg.into())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn constructor_helpers() {
let e = AssetError::config("bad value");
assert!(matches!(e, AssetError::Config(_)));
assert!(format!("{e}").contains("bad value"));
let e = AssetError::cache_dir("no perms");
assert!(matches!(e, AssetError::CacheDir(_)));
let e = AssetError::not_found("asset-1");
assert!(matches!(e, AssetError::NotFound(_)));
}
#[test]
fn io_error_is_convertible() {
let io = std::io::Error::new(std::io::ErrorKind::NotFound, "x");
let e: AssetError = io.into();
assert!(matches!(e, AssetError::Io(_)));
}
}