poem_openapi/payload/
binary.rs1use std::ops::{Deref, DerefMut};
2
3use bytes::Bytes;
4use poem::{Body, FromRequest, IntoResponse, Request, RequestBody, Response, Result};
5
6use crate::{
7 ApiResponse,
8 payload::{ParsePayload, Payload},
9 registry::{MetaMediaType, MetaResponse, MetaResponses, MetaSchema, MetaSchemaRef, Registry},
10};
11
12#[derive(Debug, Clone, Eq, PartialEq)]
71pub struct Binary<T>(pub T);
72
73impl<T> Deref for Binary<T> {
74 type Target = T;
75
76 fn deref(&self) -> &Self::Target {
77 &self.0
78 }
79}
80
81impl<T> DerefMut for Binary<T> {
82 fn deref_mut(&mut self) -> &mut Self::Target {
83 &mut self.0
84 }
85}
86
87impl<T: Send> Payload for Binary<T> {
88 const CONTENT_TYPE: &'static str = "application/octet-stream";
89
90 fn check_content_type(_content_type: &str) -> bool {
91 true
92 }
93
94 fn schema_ref() -> MetaSchemaRef {
95 MetaSchemaRef::Inline(Box::new(MetaSchema {
96 format: Some("binary"),
97 ..MetaSchema::new("string")
98 }))
99 }
100}
101
102impl ParsePayload for Binary<Vec<u8>> {
103 const IS_REQUIRED: bool = true;
104
105 async fn from_request(request: &Request, body: &mut RequestBody) -> Result<Self> {
106 Ok(Self(<Vec<u8>>::from_request(request, body).await?))
107 }
108}
109
110impl ParsePayload for Binary<Bytes> {
111 const IS_REQUIRED: bool = true;
112
113 async fn from_request(request: &Request, body: &mut RequestBody) -> Result<Self> {
114 Ok(Self(Bytes::from_request(request, body).await?))
115 }
116}
117
118impl ParsePayload for Binary<Body> {
119 const IS_REQUIRED: bool = true;
120
121 async fn from_request(request: &Request, body: &mut RequestBody) -> Result<Self> {
122 Ok(Self(Body::from_request(request, body).await?))
123 }
124}
125
126impl<T: Into<Body> + Send> IntoResponse for Binary<T> {
127 fn into_response(self) -> Response {
128 Response::builder()
129 .content_type(Self::CONTENT_TYPE)
130 .body(self.0.into())
131 }
132}
133
134impl<T: Into<Body> + Send> ApiResponse for Binary<T> {
135 fn meta() -> MetaResponses {
136 MetaResponses {
137 responses: vec![MetaResponse {
138 description: "",
139 status: Some(200),
140 status_range: None,
141 content: vec![MetaMediaType {
142 content_type: Self::CONTENT_TYPE,
143 schema: Self::schema_ref(),
144 }],
145 headers: vec![],
146 }],
147 }
148 }
149
150 fn register(_registry: &mut Registry) {}
151}
152
153impl_apirequest_for_payload!(Binary<Vec<u8>>);
154impl_apirequest_for_payload!(Binary<Bytes>);
155impl_apirequest_for_payload!(Binary<Body>);