error_forge/lib.rs
1//! # Error Forge
2//!
3//! A high-performance, flexible Rust error framework for defining, formatting, chaining,
4//! and managing rich custom errors across large-scale applications.
5//!
6//! ## Overview
7//!
8//! Error Forge provides a comprehensive solution for error handling in Rust applications,
9//! focusing on performance, flexibility, and developer ergonomics. It enables defining
10//! structured error types with minimal boilerplate, making error handling more efficient
11//! and maintainable.
12//!
13//! ## Key Features
14//!
15//! - **`define_errors!` macro** - Create rich error enums with minimal boilerplate
16//! - **`#[derive(ModError)]` proc-macro** - "Lazy mode" errors with attribute-based configuration
17//! - **`group!` macro** - Compose multi-error enums with automatic `From` conversions
18//! - **Console theming** - ANSI-colored errors for CLI applications
19//! - **Built-in panic hook** - Enhanced panic formatting
20//! - **ForgeError trait** - Unified interface for error handling
21//! - **Serialization support** - Optional serde integration
22//!
23//! ## Quick Start
24//!
25//! ```ignore
26//! use error_forge::{define_errors, ForgeError};
27//!
28//! // Define our error type
29//! define_errors! {
30//! #[derive(Debug)]
31//! pub enum AppError {
32//! #[error(display = "Configuration error: {message}")]
33//! #[kind(Config, retryable = false, status = 500)]
34//! Config { message: String },
35//!
36//! #[error(display = "Database error: {message}")]
37//! #[kind(Database, retryable = true, status = 503)]
38//! Database { message: String },
39//! }
40//! }
41//!
42//! // Use the error type
43//! fn main() -> Result<(), error_forge::AppError> {
44//! if cfg!(debug_assertions) {
45//! return Err(error_forge::AppError::config("Missing configuration"));
46//! }
47//! Ok(())
48//! }
49//! ```
50//!
51//! ## Enhanced Error Reporting
52//!
53//! ```rust
54//! // Import the predefined AppError type from the library
55//! use error_forge::{console_theme::print_error, AppError};
56//!
57//! let error = AppError::config("Database connection failed");
58//! print_error(&error); // Displays a nicely formatted error message
59//! ```
60pub mod error;
61pub mod macros;
62pub mod group_macro;
63pub mod console_theme;
64pub mod context;
65pub mod registry;
66pub mod collector;
67pub mod logging;
68pub mod recovery;
69
70#[cfg(feature = "async")]
71pub mod async_error;
72#[cfg(feature = "async")]
73pub mod async_error_impl;
74
75// Re-export core types and traits
76pub use crate::error::{ForgeError, Result, AppError};
77pub use crate::console_theme::{ConsoleTheme, print_error, install_panic_hook};
78
79// Re-export context module
80pub use crate::context::{ContextError, ResultExt};
81
82// Re-export registry module
83pub use crate::registry::{WithErrorCode, CodedError, register_error_code, ErrorRegistry, ErrorCodeInfo};
84
85// Re-export collector module
86pub use crate::collector::{ErrorCollector, CollectError};
87
88// Re-export logging module
89pub use crate::logging::{ErrorLogger, register_logger, log_error, logger};
90
91// Re-export async module (when enabled)
92#[cfg(feature = "async")]
93pub use crate::async_error::{AsyncForgeError, AsyncResult};
94
95#[cfg(feature = "serde")]
96extern crate serde;
97
98// Re-export macros for convenient use
99#[allow(unused_imports)]
100pub use crate::macros::*;
101
102// Optional re-export of the proc macro
103#[cfg(feature = "derive")]
104pub use error_forge_derive::*;
105
106// Extension methods are implemented in error.rs
107
108// Extension methods are implemented in error.rs
109
110#[cfg(test)]
111mod tests {
112 use crate::ForgeError;
113 #[test]
114 fn it_works() {
115 assert_eq!(2 + 2, 4);
116 }
117
118 #[test]
119 fn test_error_display() {
120 let err = crate::error::AppError::config("Test error");
121 assert!(err.to_string().contains("Test error"));
122 }
123
124 #[test]
125 fn test_error_kind() {
126 let err = crate::error::AppError::config("Test error");
127 assert_eq!(err.kind(), "Config");
128 }
129}