polyoxide_core/macros.rs
1//! Macros for reducing boilerplate in API client implementations
2
3/// Implements standard error conversions for API error wrapper types
4///
5/// This macro generates `From` implementations for `reqwest::Error` and `url::ParseError`
6/// that wrap them in the `Api` variant of the error type.
7///
8/// # Example
9///
10/// ```ignore
11/// use polyoxide_core::{ApiError, RequestError};
12/// use thiserror::Error;
13///
14/// #[derive(Error, Debug)]
15/// pub enum MyApiError {
16/// #[error(transparent)]
17/// Api(#[from] ApiError),
18/// }
19///
20/// impl RequestError for MyApiError {
21/// async fn from_response(response: reqwest::Response) -> Self {
22/// Self::Api(ApiError::from_response(response).await)
23/// }
24/// }
25///
26/// // Instead of writing these manually:
27/// // impl From<reqwest::Error> for MyApiError { ... }
28/// // impl From<url::ParseError> for MyApiError { ... }
29///
30/// // Use the macro:
31/// polyoxide_core::impl_api_error_conversions!(MyApiError);
32/// ```
33#[macro_export]
34macro_rules! impl_api_error_conversions {
35 ($error_type:ty) => {
36 impl From<reqwest::Error> for $error_type {
37 fn from(err: reqwest::Error) -> Self {
38 Self::Api($crate::ApiError::Network(err))
39 }
40 }
41
42 impl From<url::ParseError> for $error_type {
43 fn from(err: url::ParseError) -> Self {
44 Self::Api($crate::ApiError::Url(err))
45 }
46 }
47 };
48}