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