error_rail/traits/
into_error_context.rs

1//! Trait for converting types into structured error context.
2//!
3//! This trait provides a unified interface for types that can be converted into
4//! [`ErrorContext`], enabling flexible context attachment in error handling pipelines.
5//!
6//! # Implementations
7//!
8//! The trait is implemented for common types:
9//! - `String` - Converts to `ErrorContext::Message`
10//! - `&str` - Converts to `ErrorContext::Message`
11//! - `ErrorContext` - Identity conversion (no-op)
12//!
13//! # Examples
14//!
15//! ```
16//! use error_rail::{traits::IntoErrorContext, ErrorContext};
17//!
18//! let ctx1 = "simple message".into_error_context();
19//! let ctx2 = String::from("owned message").into_error_context();
20//! let ctx3 = ErrorContext::tag("network").into_error_context();
21//! ```
22use crate::types::error_context::ErrorContext;
23
24/// Converts a type into an [`ErrorContext`] for error annotation.
25///
26/// This trait is used throughout the error handling pipeline to accept
27/// flexible context types when building composable errors.
28pub trait IntoErrorContext {
29    /// Converts `self` into an [`ErrorContext`].
30    fn into_error_context(self) -> ErrorContext;
31}
32
33impl IntoErrorContext for String {
34    /// Converts an owned `String` into a message context.
35    #[inline]
36    fn into_error_context(self) -> ErrorContext {
37        ErrorContext::new(self)
38    }
39}
40
41impl IntoErrorContext for &str {
42    /// Converts a string slice into a message context.
43    #[inline]
44    fn into_error_context(self) -> ErrorContext {
45        ErrorContext::new(self)
46    }
47}
48
49impl IntoErrorContext for ErrorContext {
50    /// Identity conversion for `ErrorContext` (no-op).
51    #[inline]
52    fn into_error_context(self) -> ErrorContext {
53        self
54    }
55}