use std::{error::Error, fmt};
pub struct CompileResult {
pub success: bool,
pub compile_time_ms: u64,
}
impl CompileResult {
pub fn success(compile_time_ms: u64) -> Self {
Self { success: true, compile_time_ms }
}
pub fn failure(compile_time_ms: u64) -> Self {
Self { success: false, compile_time_ms }
}
}
#[derive(Debug)]
pub enum GatsbyError {
ConfigError {
message: String,
},
CompileError {
message: String,
},
IoError {
source: std::io::Error,
},
SerializeError {
source: serde_json::Error,
},
}
impl GatsbyError {
pub fn i18n_key(&self) -> &'static str {
match self {
GatsbyError::ConfigError { .. } => "gatsby.error.config",
GatsbyError::CompileError { .. } => "gatsby.error.compile",
GatsbyError::IoError { .. } => "gatsby.error.io",
GatsbyError::SerializeError { .. } => "gatsby.error.serialize",
}
}
}
impl fmt::Display for GatsbyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
GatsbyError::ConfigError { message } => write!(f, "Config error: {}", message),
GatsbyError::CompileError { message } => write!(f, "Compile error: {}", message),
GatsbyError::IoError { source } => write!(f, "IO error: {}", source),
GatsbyError::SerializeError { source } => write!(f, "Serialize error: {}", source),
}
}
}
impl Error for GatsbyError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
GatsbyError::IoError { source } => Some(source),
GatsbyError::SerializeError { source } => Some(source),
_ => None,
}
}
}
impl From<std::io::Error> for GatsbyError {
fn from(source: std::io::Error) -> Self {
GatsbyError::IoError { source }
}
}
impl From<serde_json::Error> for GatsbyError {
fn from(source: serde_json::Error) -> Self {
GatsbyError::SerializeError { source }
}
}
pub type Result<T> = std::result::Result<T, GatsbyError>;