Skip to main content

covy_core/
error.rs

1use std::path::PathBuf;
2
3use thiserror::Error;
4
5#[derive(Debug, Error)]
6pub enum CovyError {
7    #[error("Failed to read {path}: {source}")]
8    Io {
9        path: PathBuf,
10        source: std::io::Error,
11    },
12
13    #[error("IO error: {0}")]
14    IoRaw(#[from] std::io::Error),
15
16    #[error("Failed to parse {format} coverage: {detail}")]
17    Parse { format: String, detail: String },
18
19    #[error("XML parse error: {0}")]
20    Xml(String),
21
22    #[error("Unknown coverage format for {path} (use --format to specify)")]
23    UnknownFormat { path: String },
24
25    #[error("Git is not installed or not found in PATH")]
26    GitNotFound,
27
28    #[error("Git error: {0}")]
29    Git(String),
30
31    #[error("Coverage file is empty: {path}")]
32    EmptyInput { path: String },
33
34    #[error("Config error: {0}")]
35    Config(String),
36
37    #[error("Cache error: {0}")]
38    Cache(String),
39
40    #[error("Path mapping failed: no match for {0}")]
41    PathMapping(String),
42
43    #[error("{0}")]
44    Other(String),
45}
46
47impl CovyError {
48    /// Return a user-friendly hint for this error, if any.
49    pub fn hint(&self) -> Option<&'static str> {
50        match self {
51            CovyError::UnknownFormat { .. } => {
52                Some("Supported formats: lcov, cobertura, jacoco, gocov, llvm-cov")
53            }
54            CovyError::GitNotFound => Some("Install git or ensure it is available in your PATH"),
55            CovyError::EmptyInput { .. } => {
56                Some("Check that your test runner generated coverage output")
57            }
58            _ => None,
59        }
60    }
61}
62
63impl From<toml::de::Error> for CovyError {
64    fn from(e: toml::de::Error) -> Self {
65        CovyError::Config(e.to_string())
66    }
67}