use thiserror::Error;
#[derive(Debug, Error)]
pub enum SarifError {
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("Validation error: {0}")]
Validation(#[from] ValidationError),
#[error("URI error: {0}")]
Uri(#[from] url::ParseError),
#[error("SARIF error: {message}")]
Custom { message: String },
}
#[derive(Debug, Error)]
pub enum ValidationError {
#[error("Missing required field: {field}")]
MissingRequiredField { field: String },
#[error("Invalid URI: {uri}")]
InvalidUri { uri: String },
#[error("Invalid version: {version}")]
InvalidVersion { version: String },
#[error("Invalid reference: {reference}")]
InvalidReference { reference: String },
#[error("Circular reference detected: {path}")]
CircularReference { path: String },
#[error("Inconsistent data: {message}")]
InconsistentData { message: String },
#[error("Schema validation error: {message}")]
SchemaValidation { message: String },
#[error("Validation error: {message}")]
Custom { message: String },
}
pub type SarifResult<T> = Result<T, SarifError>;
pub type ValidationResult<T> = Result<T, ValidationError>;
impl SarifError {
pub fn custom(message: impl Into<String>) -> Self {
Self::Custom {
message: message.into(),
}
}
pub fn is_json_error(&self) -> bool {
matches!(self, SarifError::Json(_))
}
pub fn is_io_error(&self) -> bool {
matches!(self, SarifError::Io(_))
}
pub fn is_validation_error(&self) -> bool {
matches!(self, SarifError::Validation(_))
}
}
impl ValidationError {
pub fn missing_field(field: impl Into<String>) -> Self {
Self::MissingRequiredField {
field: field.into(),
}
}
pub fn invalid_uri(uri: impl Into<String>) -> Self {
Self::InvalidUri { uri: uri.into() }
}
pub fn invalid_version(version: impl Into<String>) -> Self {
Self::InvalidVersion {
version: version.into(),
}
}
pub fn invalid_reference(reference: impl Into<String>) -> Self {
Self::InvalidReference {
reference: reference.into(),
}
}
pub fn circular_reference(path: impl Into<String>) -> Self {
Self::CircularReference { path: path.into() }
}
pub fn inconsistent_data(message: impl Into<String>) -> Self {
Self::InconsistentData {
message: message.into(),
}
}
pub fn schema_validation(message: impl Into<String>) -> Self {
Self::SchemaValidation {
message: message.into(),
}
}
pub fn custom(message: impl Into<String>) -> Self {
Self::Custom {
message: message.into(),
}
}
}