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.
29///
30/// # Implementing for Custom Types
31///
32/// If you need to use a custom type as error context, you have two options:
33///
34/// 1. Use the [`impl_error_context!`](crate::impl_error_context) macro for types implementing `Display`:
35/// ```ignore
36/// impl_error_context!(MyCustomError);
37/// ```
38///
39/// 2. Implement the trait manually:
40/// ```
41/// use error_rail::{traits::IntoErrorContext, ErrorContext};
42///
43/// struct MyContext { user_id: u64 }
44///
45/// impl IntoErrorContext for MyContext {
46/// fn into_error_context(self) -> ErrorContext {
47/// ErrorContext::metadata("user_id", self.user_id.to_string())
48/// }
49/// }
50/// ```
51#[diagnostic::on_unimplemented(
52 message = "`{Self}` cannot be used as error context",
53 label = "this type does not implement `IntoErrorContext`",
54 note = "implement `IntoErrorContext` manually or use `impl_error_context!({Self})` macro",
55 note = "see: https://docs.rs/error-rail/latest/error_rail/macro.impl_error_context.html"
56)]
57pub trait IntoErrorContext {
58 /// Converts `self` into an [`ErrorContext`].
59 fn into_error_context(self) -> ErrorContext;
60}
61
62impl IntoErrorContext for String {
63 /// Converts an owned `String` into a message context.
64 #[inline]
65 fn into_error_context(self) -> ErrorContext {
66 ErrorContext::new(self)
67 }
68}
69
70impl IntoErrorContext for &'static str {
71 /// Converts a static string slice into a message context.
72 #[inline]
73 fn into_error_context(self) -> ErrorContext {
74 ErrorContext::new(self)
75 }
76}
77
78impl IntoErrorContext for Cow<'static, str> {
79 /// Converts a Cow string into a message context.
80 #[inline]
81 fn into_error_context(self) -> ErrorContext {
82 ErrorContext::new(self)
83 }
84}
85
86impl IntoErrorContext for ErrorContext {
87 /// Identity conversion for `ErrorContext` (no-op).
88 #[inline]
89 fn into_error_context(self) -> ErrorContext {
90 self
91 }
92}