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::alloc_type::{Cow, String};
23use crate::types::error_context::ErrorContext;
24
25/// Converts a type into an [`ErrorContext`] for error annotation.
26///
27/// This trait is used throughout the error handling pipeline to accept
28/// flexible context types when building composable errors.
29pub trait IntoErrorContext {
30 /// Converts `self` into an [`ErrorContext`].
31 fn into_error_context(self) -> ErrorContext;
32}
33
34impl IntoErrorContext for String {
35 /// Converts an owned `String` into a message context.
36 #[inline]
37 fn into_error_context(self) -> ErrorContext {
38 ErrorContext::new(self)
39 }
40}
41
42impl IntoErrorContext for &'static str {
43 /// Converts a static string slice into a message context.
44 #[inline]
45 fn into_error_context(self) -> ErrorContext {
46 ErrorContext::new(self)
47 }
48}
49
50impl IntoErrorContext for Cow<'static, str> {
51 /// Converts a Cow string into a message context.
52 #[inline]
53 fn into_error_context(self) -> ErrorContext {
54 ErrorContext::new(self)
55 }
56}
57
58impl IntoErrorContext for ErrorContext {
59 /// Identity conversion for `ErrorContext` (no-op).
60 #[inline]
61 fn into_error_context(self) -> ErrorContext {
62 self
63 }
64}