poem_openapi/
macros.rs

1#[macro_export]
2/// This macro implements ApiExtractor for your type, with additional bounds
3/// if you want to.
4macro_rules! impl_apirequest_for_payload {
5    ($ty:ty) => {
6        impl_apirequest_for_payload!($ty,);
7    };
8
9    ($ty:ty, $($bounds:tt)*) => {
10        impl<'a, $($bounds)*> $crate::ApiExtractor<'a> for $ty {
11            const TYPES: &'static [$crate::ApiExtractorType] = &[$crate::ApiExtractorType::RequestObject];
12
13            type ParamType = ();
14            type ParamRawType = ();
15
16            fn register(registry: &mut $crate::registry::Registry) {
17                <Self as $crate::payload::Payload>::register(registry);
18            }
19
20            fn request_meta() -> Option<$crate::registry::MetaRequest> {
21                Some($crate::registry::MetaRequest {
22                    description: None,
23                    content: vec![$crate::registry::MetaMediaType {
24                        content_type: <Self as $crate::payload::Payload>::CONTENT_TYPE,
25                        schema: <Self as $crate::payload::Payload>::schema_ref(),
26                    }],
27                    required: <Self as $crate::payload::ParsePayload>::IS_REQUIRED,
28                })
29            }
30
31            async fn from_request(
32                request: &'a poem::Request,
33                body: &mut poem::RequestBody,
34                _param_opts: $crate::ExtractParamOptions<Self::ParamType>,
35            ) -> poem::Result<Self> {
36                match request.content_type() {
37                    Some(content_type) => {
38                        if <$ty>::check_content_type(content_type) {
39                            <Self as $crate::payload::ParsePayload>::from_request(request, body).await
40                        } else {
41                            return Err($crate::error::ContentTypeError::NotSupported {
42                                content_type: content_type.to_string(),
43                            }.into());
44
45                        }
46                    }
47                    None => Err($crate::error::ContentTypeError::ExpectContentType.into()),
48                }
49            }
50        }
51    };
52}