use std::sync::PoisonError;
use thiserror::Error;
#[derive(Error, Debug, Clone)]
pub enum FontspectorError {
#[error("Error reading font file: {0}")]
FontRead(#[from] fontations::read::ReadError),
#[error("Error writing font file: {0}")]
FontWrite(#[from] fontations::write::error::Error),
#[error("Error building font file: {0}")]
FontBuild(#[from] fontations::write::BuilderError),
#[error("Error drawing glyph: {0}")]
Draw(#[from] fontations::skrifa::outline::DrawError),
#[error("Skipping check: {message} [{code}]")]
Skip {
code: &'static str,
message: &'static str,
},
#[error(
"Inappropriate file type: {filename} was not a valid ${expected} file: {more_details}"
)]
InappropriateFile {
expected: &'static str,
filename: String,
more_details: String,
},
#[error("Python error: {0}")]
Python(String),
#[error("Invalid UTF-8: {0}")]
InvalidUtf8(#[from] std::str::Utf8Error),
#[error("Invalid JSON: {0}")]
InvalidJson(#[from] std::sync::Arc<serde_json::Error>),
#[error("Error reading file: {0}")]
FileRead(#[from] std::sync::Arc<std::io::Error>),
#[error("A network error occurred: {0}")]
Network(String), #[error("Network access is disabled, but this check requires it")]
NetworkAccessDisabled,
#[error("Shaping engine error: {0}")]
Shaping(String),
#[error("Cache serialization/deserialization error: {0}")]
CacheSerialization(String),
#[error("Cache error: {0}")]
CachePoison(String),
#[error("Something went wrong: {0}")]
General(String),
#[error("Something went wrong while fixing: {0}")]
Fix(String),
}
impl From<std::string::FromUtf8Error> for FontspectorError {
fn from(err: std::string::FromUtf8Error) -> Self {
FontspectorError::InvalidUtf8(err.utf8_error())
}
}
impl From<serde_json::Error> for FontspectorError {
fn from(err: serde_json::Error) -> Self {
FontspectorError::InvalidJson(std::sync::Arc::new(err))
}
}
impl From<std::io::Error> for FontspectorError {
fn from(err: std::io::Error) -> Self {
FontspectorError::FileRead(std::sync::Arc::new(err))
}
}
impl<T> From<PoisonError<T>> for FontspectorError {
fn from(err: PoisonError<T>) -> Self {
FontspectorError::CachePoison(err.to_string())
}
}
impl FontspectorError {
pub fn skip(code: &'static str, message: &'static str) -> Self {
FontspectorError::Skip { code, message }
}
}