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//! fn main() {
58//! let error = AppError::config("Database connection failed");
59//! print_error(&error); // Displays a nicely formatted error message
60//! }
61//! ```
62pub mod error;
63pub mod macros;
64pub mod group_macro;
65pub mod console_theme;
66
67// Re-export core types and traits
68pub use crate::error::{ForgeError, Result, AppError};
69pub use crate::console_theme::{ConsoleTheme, print_error, install_panic_hook};
70
71#[cfg(feature = "serde")]
72extern crate serde;
73
74// Re-export macros for convenient use
75#[allow(unused_imports)]
76pub use crate::group_macro::*;
77
78// Optional re-export of the proc macro
79#[cfg(feature = "derive")]
80pub use error_forge_derive::*;
81
82#[cfg(test)]
83mod tests {
84 use crate::ForgeError;
85 #[test]
86 fn it_works() {
87 assert_eq!(2 + 2, 4);
88 }
89
90 #[test]
91 fn test_error_display() {
92 let err = crate::error::AppError::config("Test error");
93 assert!(err.to_string().contains("Test error"));
94 }
95
96 #[test]
97 fn test_error_kind() {
98 let err = crate::error::AppError::config("Test error");
99 assert_eq!(err.kind(), "Config");
100 }
101}