use std::sync::Arc;
use thiserror::Error;
use super::source_file::SourceFile;
#[derive(Debug, Error)]
pub enum GrimoireCssError {
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Regex error: {0}")]
Regex(#[from] regex::Error),
#[error("Serialization/Deserialization error: {0}")]
Serde(#[from] serde_json::Error),
#[error("Invalid spell format: {message}")]
InvalidSpellFormat {
message: String,
span: (usize, usize),
label: String,
help: Option<String>,
source_file: Option<Arc<SourceFile>>,
},
#[error("Invalid input: {0}")]
InvalidInput(String),
#[error("Invalid path: {0}")]
InvalidPath(String),
#[error("Glob pattern error: {0}")]
GlobPatternError(String),
#[error("Runtime error: {0}")]
RuntimeError(String),
#[error("CSS Optimization failed: {0}")]
OptimizationError(String),
#[error("{message}")]
CompileError {
message: String,
span: (usize, usize),
label: String,
help: Option<String>,
source_file: Option<Arc<SourceFile>>,
},
}
impl GrimoireCssError {
pub fn with_source(self, source: Arc<SourceFile>) -> Self {
match self {
GrimoireCssError::InvalidSpellFormat {
message,
span,
label,
help,
source_file: existing,
} => GrimoireCssError::InvalidSpellFormat {
message,
span,
label,
help,
source_file: existing.or(Some(source)),
},
GrimoireCssError::CompileError {
message,
span,
label,
help,
source_file: existing,
} => GrimoireCssError::CompileError {
message,
span,
label,
help,
source_file: existing.or(Some(source)),
},
other => other,
}
}
pub fn source(&self) -> Option<&Arc<SourceFile>> {
match self {
GrimoireCssError::InvalidSpellFormat { source_file, .. } => source_file.as_ref(),
GrimoireCssError::CompileError { source_file, .. } => source_file.as_ref(),
_ => None,
}
}
}