use std::path::PathBuf;
use thiserror::Error;
pub type AuditResult<T> = Result<T, AuditError>;
#[derive(Debug, Error, Clone)]
pub enum AuditError {
#[error("Audit section '{section}' is disabled in configuration")]
SectionDisabled {
section: String,
},
#[error("Audit analysis failed for section '{section}': {reason}")]
AnalysisFailed {
section: String,
reason: String,
},
#[error("Failed to generate audit report: {reason}")]
ReportGenerationFailed {
reason: String,
},
#[error("Invalid audit configuration: {reason}")]
InvalidConfig {
reason: String,
},
#[error("Package '{package}' not found in workspace")]
PackageNotFound {
package: String,
},
#[error("Failed to construct dependency graph: {reason}")]
DependencyGraphFailed {
reason: String,
},
#[error("Circular dependency detection failed: {reason}")]
CircularDependencyDetectionFailed {
reason: String,
},
#[error("Missing dependency analysis failed: {reason}")]
MissingDependencyAnalysisFailed {
reason: String,
},
#[error("Unused dependency analysis failed: {reason}")]
UnusedDependencyAnalysisFailed {
reason: String,
},
#[error("Version conflict detection failed: {reason}")]
VersionConflictDetectionFailed {
reason: String,
},
#[error("Breaking changes detection failed: {reason}")]
BreakingChangesDetectionFailed {
reason: String,
},
#[error("Upgrade detection failed: {reason}")]
UpgradeDetectionFailed {
reason: String,
},
#[error("Dependency categorization failed: {reason}")]
CategorizationFailed {
reason: String,
},
#[error("Health score calculation failed: {reason}")]
HealthScoreCalculationFailed {
reason: String,
},
#[error("Invalid severity level '{severity}': expected 'critical', 'warning', or 'info'")]
InvalidSeverity {
severity: String,
},
#[error("No issues found in audit (expected at least one in strict mode)")]
NoIssuesFound,
#[error("Workspace analysis failed: {reason}")]
WorkspaceAnalysisFailed {
reason: String,
},
#[error("Filesystem error at '{path}': {reason}")]
FileSystemError {
path: PathBuf,
reason: String,
},
#[error("Git operation failed during audit: {operation} - {reason}")]
GitError {
operation: String,
reason: String,
},
#[error("Unsupported report format: {format}")]
UnsupportedFormat {
format: String,
},
#[error("Failed to export report to '{path}': {reason}")]
ExportFailed {
path: PathBuf,
reason: String,
},
#[error("Audit operation timed out after {duration_secs} seconds")]
Timeout {
duration_secs: u64,
},
#[error("Registry communication failed during audit: {reason}")]
RegistryError {
reason: String,
},
#[error("Data inconsistency detected: {reason}")]
DataInconsistency {
reason: String,
},
#[error(
"Audit threshold exceeded: {threshold_type} limit of {limit} exceeded with {actual} issues"
)]
ThresholdExceeded {
threshold_type: String,
limit: usize,
actual: usize,
},
#[error("Invalid workspace root '{path}': {reason}")]
InvalidWorkspaceRoot {
path: PathBuf,
reason: String,
},
}
impl AsRef<str> for AuditError {
fn as_ref(&self) -> &str {
match self {
Self::SectionDisabled { .. } => "audit section disabled",
Self::AnalysisFailed { .. } => "audit analysis failed",
Self::ReportGenerationFailed { .. } => "report generation failed",
Self::InvalidConfig { .. } => "invalid configuration",
Self::PackageNotFound { .. } => "package not found",
Self::DependencyGraphFailed { .. } => "dependency graph failed",
Self::CircularDependencyDetectionFailed { .. } => {
"circular dependency detection failed"
}
Self::MissingDependencyAnalysisFailed { .. } => "missing dependency analysis failed",
Self::UnusedDependencyAnalysisFailed { .. } => "unused dependency analysis failed",
Self::VersionConflictDetectionFailed { .. } => "version conflict detection failed",
Self::BreakingChangesDetectionFailed { .. } => "breaking changes detection failed",
Self::UpgradeDetectionFailed { .. } => "upgrade detection failed",
Self::CategorizationFailed { .. } => "categorization failed",
Self::HealthScoreCalculationFailed { .. } => "health score calculation failed",
Self::InvalidSeverity { .. } => "invalid severity",
Self::NoIssuesFound => "no issues found",
Self::WorkspaceAnalysisFailed { .. } => "workspace analysis failed",
Self::FileSystemError { .. } => "filesystem error",
Self::GitError { .. } => "git error",
Self::UnsupportedFormat { .. } => "unsupported format",
Self::ExportFailed { .. } => "export failed",
Self::Timeout { .. } => "timeout",
Self::RegistryError { .. } => "registry error",
Self::DataInconsistency { .. } => "data inconsistency",
Self::ThresholdExceeded { .. } => "threshold exceeded",
Self::InvalidWorkspaceRoot { .. } => "invalid workspace root",
}
}
}
impl AuditError {
#[must_use]
pub fn is_transient(&self) -> bool {
matches!(
self,
Self::FileSystemError { .. }
| Self::GitError { .. }
| Self::RegistryError { .. }
| Self::Timeout { .. }
)
}
#[must_use]
pub fn is_fatal(&self) -> bool {
matches!(
self,
Self::InvalidConfig { .. }
| Self::InvalidWorkspaceRoot { .. }
| Self::WorkspaceAnalysisFailed { .. }
| Self::DataInconsistency { .. }
)
}
#[must_use]
pub fn is_dependency_related(&self) -> bool {
matches!(
self,
Self::DependencyGraphFailed { .. }
| Self::CircularDependencyDetectionFailed { .. }
| Self::MissingDependencyAnalysisFailed { .. }
| Self::UnusedDependencyAnalysisFailed { .. }
| Self::VersionConflictDetectionFailed { .. }
| Self::CategorizationFailed { .. }
)
}
}