Skip to main content

cargo_truce/
error.rs

1//! Typed error enum for cargo-truce.
2//!
3//! Callers pattern-match on the failure mode (missing tool, codesign
4//! failure, manifest parse error, etc.) instead of string-grepping a
5//! `Box<dyn Error>` Display output. The catch-all `Other` variant
6//! carries stringly-typed errors from `Err("...".into())` sites that
7//! haven't been migrated to a named variant; `From<String>` and
8//! `From<&str>` conversions keep `?` propagation transparent.
9
10use std::io;
11
12#[derive(Debug, thiserror::Error)]
13#[non_exhaustive]
14pub enum CargoTruceError {
15    #[error("I/O error: {0}")]
16    Io(#[from] io::Error),
17
18    #[error("TOML parse error: {0}")]
19    Toml(#[from] toml::de::Error),
20
21    #[error("codesign failed: {0}")]
22    Codesign(String),
23
24    #[error("required tool not found: {0}")]
25    MissingTool(String),
26
27    #[error("{0}")]
28    Other(String),
29}
30
31impl From<String> for CargoTruceError {
32    fn from(s: String) -> Self {
33        Self::Other(s)
34    }
35}
36
37impl From<&str> for CargoTruceError {
38    fn from(s: &str) -> Self {
39        Self::Other(s.to_string())
40    }
41}
42
43impl From<Box<dyn std::error::Error>> for CargoTruceError {
44    fn from(e: Box<dyn std::error::Error>) -> Self {
45        Self::Other(e.to_string())
46    }
47}
48
49impl From<Box<dyn std::error::Error + Send + Sync>> for CargoTruceError {
50    fn from(e: Box<dyn std::error::Error + Send + Sync>) -> Self {
51        Self::Other(e.to_string())
52    }
53}