ferrous_forge/
error.rs

1//! Error handling for Ferrous Forge
2//!
3//! This module provides a unified error handling system for all Ferrous Forge operations.
4
5
6/// Ferrous Forge specific errors
7#[derive(Debug, thiserror::Error)]
8pub enum Error {
9    /// IO errors (file operations, etc.)
10    #[error("IO error: {0}")]
11    Io(#[from] std::io::Error),
12
13    /// Configuration errors
14    #[error("Configuration error: {0}")]
15    Config(String),
16
17    /// Validation errors
18    #[error("Validation error: {0}")]
19    Validation(String),
20
21    /// Template errors
22    #[error("Template error: {0}")]
23    Template(String),
24
25    /// Update system errors
26    #[error("Update error: {0}")]
27    Update(String),
28
29    /// Standards enforcement errors
30    #[error("Standards violation: {0}")]
31    Standards(String),
32
33    /// CLI argument errors
34    #[error("CLI error: {0}")]
35    Cli(String),
36
37    /// External process errors
38    #[error("Process error: {0}")]
39    Process(String),
40
41    /// Serialization errors
42    #[error("Serialization error: {0}")]
43    Serialization(#[from] serde_json::Error),
44
45    /// TOML parsing errors
46    #[error("TOML error: {0}")]
47    Toml(#[from] toml::de::Error),
48
49    /// Network/HTTP errors
50    #[error("Network error: {0}")]
51    Network(#[from] reqwest::Error),
52
53    /// Semver parsing errors
54    #[error("Version error: {0}")]
55    Version(#[from] semver::Error),
56}
57
58/// Result type alias for Ferrous Forge operations
59pub type Result<T> = std::result::Result<T, Error>;
60
61impl Error {
62    /// Create a new configuration error
63    pub fn config(msg: impl Into<String>) -> Self {
64        Self::Config(msg.into())
65    }
66
67    /// Create a new validation error
68    pub fn validation(msg: impl Into<String>) -> Self {
69        Self::Validation(msg.into())
70    }
71
72    /// Create a new template error
73    pub fn template(msg: impl Into<String>) -> Self {
74        Self::Template(msg.into())
75    }
76
77    /// Create a new update error
78    pub fn update(msg: impl Into<String>) -> Self {
79        Self::Update(msg.into())
80    }
81
82    /// Create a new standards error
83    pub fn standards(msg: impl Into<String>) -> Self {
84        Self::Standards(msg.into())
85    }
86
87    /// Create a new CLI error
88    pub fn cli(msg: impl Into<String>) -> Self {
89        Self::Cli(msg.into())
90    }
91
92    /// Create a new process error
93    pub fn process(msg: impl Into<String>) -> Self {
94        Self::Process(msg.into())
95    }
96}
97
98/// Convert anyhow errors to our error type
99impl From<anyhow::Error> for Error {
100    fn from(err: anyhow::Error) -> Self {
101        Self::Process(err.to_string())
102    }
103}