modkit/result.rs
1//! Ergonomic result types for API handlers
2//!
3//! This module provides type aliases and conversions to make error handling
4//! in HTTP handlers more concise and uniform.
5
6use crate::api::problem::Problem;
7
8/// Standard result type for API operations
9///
10/// Use this throughout your handlers for consistent error handling:
11///
12/// ```ignore
13/// async fn handler() -> ApiResult<Json<User>> {
14/// let user = fetch_user().await?; // auto-converts errors to Problem
15/// Ok(Json(user))
16/// }
17/// ```
18///
19/// The `?` operator automatically converts any error implementing
20/// `Into<Problem>` (including `modkit_odata::Error`) into a Problem.
21/// Problem implements `IntoResponse`, so Axum will automatically convert it
22/// to an HTTP response when returned from a handler.
23pub type ApiResult<T = ()> = Result<T, Problem>;
24
25#[cfg(test)]
26#[cfg_attr(coverage_nightly, coverage(off))]
27mod tests {
28 use super::*;
29
30 #[test]
31 fn test_api_result_ok() {
32 let result: ApiResult<i32> = Ok(42);
33 assert!(result.is_ok());
34 }
35
36 #[test]
37 fn test_api_result_err() {
38 use http::StatusCode;
39
40 let result: ApiResult<i32> = Err(Problem::new(
41 StatusCode::BAD_REQUEST,
42 "Bad Request",
43 "Invalid input",
44 ));
45 assert!(result.is_err());
46 }
47}