Skip to main content

llm_config_devtools/
error.rs

1//! Error types for the devtools crate.
2//!
3//! This module provides comprehensive error handling for all scanning operations.
4
5use std::path::PathBuf;
6use thiserror::Error;
7
8/// Result type alias for devtools operations.
9pub type Result<T> = std::result::Result<T, DevtoolsError>;
10
11/// Main error type for devtools operations.
12#[derive(Error, Debug)]
13pub enum DevtoolsError {
14    /// IO error occurred during file operations.
15    #[error("IO error: {0}")]
16    Io(#[from] std::io::Error),
17
18    /// Cargo command execution failed.
19    #[error("Cargo command failed: {0}")]
20    CargoExecution(String),
21
22    /// Failed to parse cargo metadata.
23    #[error("Failed to parse cargo metadata: {0}")]
24    CargoMetadata(#[from] cargo_metadata::Error),
25
26    /// Clippy execution failed.
27    #[error("Clippy execution failed: {0}")]
28    ClippyFailed(String),
29
30    /// Pattern compilation failed.
31    #[error("Pattern compilation failed: {0}")]
32    PatternError(#[from] regex::Error),
33
34    /// Project root not found or invalid.
35    #[error("Invalid project root: {path}")]
36    InvalidProjectRoot {
37        /// The invalid path
38        path: PathBuf
39    },
40
41    /// No Cargo.toml found in project.
42    #[error("No Cargo.toml found at: {path}")]
43    NoCargoToml {
44        /// The path where Cargo.toml was expected
45        path: PathBuf
46    },
47
48    /// Serialization error.
49    #[error("Serialization error: {0}")]
50    Serialization(String),
51
52    /// Deserialization error.
53    #[error("Deserialization error: {0}")]
54    Deserialization(String),
55
56    /// Report generation failed.
57    #[error("Report generation failed: {0}")]
58    ReportGeneration(String),
59
60    /// Git repository error.
61    #[error("Git error: {0}")]
62    Git(#[from] git2::Error),
63
64    /// No findings to report.
65    #[error("No security findings to report")]
66    NoFindings,
67
68    /// Configuration error.
69    #[error("Configuration error: {0}")]
70    Config(String),
71
72    /// Ignore/walkdir error.
73    #[error("File walking error: {0}")]
74    Ignore(String),
75
76    /// Other error.
77    #[error("{0}")]
78    Other(String),
79}
80
81impl From<ignore::Error> for DevtoolsError {
82    fn from(err: ignore::Error) -> Self {
83        DevtoolsError::Ignore(err.to_string())
84    }
85}
86
87impl From<serde_json::Error> for DevtoolsError {
88    fn from(err: serde_json::Error) -> Self {
89        DevtoolsError::Serialization(err.to_string())
90    }
91}
92
93impl From<serde_yaml::Error> for DevtoolsError {
94    fn from(err: serde_yaml::Error) -> Self {
95        DevtoolsError::Serialization(err.to_string())
96    }
97}
98
99#[cfg(test)]
100mod tests {
101    use super::*;
102
103    #[test]
104    fn test_error_display() {
105        let err = DevtoolsError::CargoExecution("test error".to_string());
106        assert_eq!(err.to_string(), "Cargo command failed: test error");
107    }
108
109    #[test]
110    fn test_invalid_project_root() {
111        let err = DevtoolsError::InvalidProjectRoot {
112            path: PathBuf::from("/invalid/path"),
113        };
114        assert!(err.to_string().contains("/invalid/path"));
115    }
116}