rust_rfc7807/traits.rs
1use crate::Problem;
2
3/// Convert a value into an RFC 7807 [`Problem`].
4///
5/// Implement this trait on your application's error types to enable
6/// automatic conversion into structured problem responses.
7///
8/// # Example
9///
10/// ```
11/// use rust_rfc7807::{IntoProblem, Problem};
12///
13/// enum AppError {
14/// UserNotFound(u64),
15/// Unauthorized,
16/// }
17///
18/// impl IntoProblem for AppError {
19/// fn into_problem(self) -> Problem {
20/// match self {
21/// AppError::UserNotFound(id) => Problem::not_found()
22/// .title("User not found")
23/// .detail(format!("No user with ID {id}"))
24/// .code("USER_NOT_FOUND"),
25/// AppError::Unauthorized => Problem::unauthorized()
26/// .title("Unauthorized")
27/// .code("UNAUTHORIZED"),
28/// }
29/// }
30/// }
31/// ```
32pub trait IntoProblem {
33 /// Convert this value into a [`Problem`] instance.
34 fn into_problem(self) -> Problem;
35}
36
37impl IntoProblem for Problem {
38 fn into_problem(self) -> Problem {
39 self
40 }
41}