sarif_rust 0.3.0

A comprehensive Rust library for parsing, generating, and manipulating SARIF (Static Analysis Results Interchange Format) v2.1.0 files
Documentation
//! Error types for SARIF parsing and validation
//!
//! This module defines error types used throughout the SARIF library.

use thiserror::Error;

/// Main error type for SARIF operations
#[derive(Debug, Error)]
pub enum SarifError {
    /// JSON parsing or serialization error
    #[error("JSON error: {0}")]
    Json(#[from] serde_json::Error),

    /// I/O error when reading or writing files
    #[error("I/O error: {0}")]
    Io(#[from] std::io::Error),

    /// Validation error
    #[error("Validation error: {0}")]
    Validation(#[from] ValidationError),

    /// URI parsing error
    #[error("URI error: {0}")]
    Uri(#[from] url::ParseError),

    /// Custom error with message
    #[error("SARIF error: {message}")]
    Custom { message: String },
}

/// Validation error types
#[derive(Debug, Error)]
pub enum ValidationError {
    /// Required field is missing
    #[error("Missing required field: {field}")]
    MissingRequiredField { field: String },

    /// Invalid URI format
    #[error("Invalid URI: {uri}")]
    InvalidUri { uri: String },

    /// Invalid version string
    #[error("Invalid version: {version}")]
    InvalidVersion { version: String },

    /// Invalid reference (e.g., index out of bounds)
    #[error("Invalid reference: {reference}")]
    InvalidReference { reference: String },

    /// Circular reference detected
    #[error("Circular reference detected: {path}")]
    CircularReference { path: String },

    /// Inconsistent data
    #[error("Inconsistent data: {message}")]
    InconsistentData { message: String },

    /// Schema validation error
    #[error("Schema validation error: {message}")]
    SchemaValidation { message: String },

    /// Custom validation error
    #[error("Validation error: {message}")]
    Custom { message: String },
}

/// Result type for SARIF operations
pub type SarifResult<T> = Result<T, SarifError>;

/// Result type for validation operations
pub type ValidationResult<T> = Result<T, ValidationError>;

impl SarifError {
    /// Create a custom error with a message
    pub fn custom(message: impl Into<String>) -> Self {
        Self::Custom {
            message: message.into(),
        }
    }

    /// Check if this is a JSON error
    pub fn is_json_error(&self) -> bool {
        matches!(self, SarifError::Json(_))
    }

    /// Check if this is an I/O error
    pub fn is_io_error(&self) -> bool {
        matches!(self, SarifError::Io(_))
    }

    /// Check if this is a validation error
    pub fn is_validation_error(&self) -> bool {
        matches!(self, SarifError::Validation(_))
    }
}

impl ValidationError {
    /// Create a missing field error
    pub fn missing_field(field: impl Into<String>) -> Self {
        Self::MissingRequiredField {
            field: field.into(),
        }
    }

    /// Create an invalid URI error
    pub fn invalid_uri(uri: impl Into<String>) -> Self {
        Self::InvalidUri { uri: uri.into() }
    }

    /// Create an invalid version error
    pub fn invalid_version(version: impl Into<String>) -> Self {
        Self::InvalidVersion {
            version: version.into(),
        }
    }

    /// Create an invalid reference error
    pub fn invalid_reference(reference: impl Into<String>) -> Self {
        Self::InvalidReference {
            reference: reference.into(),
        }
    }

    /// Create a circular reference error
    pub fn circular_reference(path: impl Into<String>) -> Self {
        Self::CircularReference { path: path.into() }
    }

    /// Create an inconsistent data error
    pub fn inconsistent_data(message: impl Into<String>) -> Self {
        Self::InconsistentData {
            message: message.into(),
        }
    }

    /// Create a schema validation error
    pub fn schema_validation(message: impl Into<String>) -> Self {
        Self::SchemaValidation {
            message: message.into(),
        }
    }

    /// Create a custom validation error
    pub fn custom(message: impl Into<String>) -> Self {
        Self::Custom {
            message: message.into(),
        }
    }
}