poem_openapi/payload/
yaml.rs1use std::ops::{Deref, DerefMut};
2
3use poem::{FromRequest, IntoResponse, Request, RequestBody, Response, Result};
4use serde_json::Value;
5
6use crate::{
7 error::ParseRequestPayloadError,
8 payload::{ParsePayload, Payload},
9 registry::{MetaMediaType, MetaResponse, MetaResponses, MetaSchemaRef, Registry},
10 types::{ParseFromYAML, ToYAML, Type},
11 ApiResponse,
12};
13
14#[derive(Debug, Clone, Eq, PartialEq)]
16pub struct Yaml<T>(pub T);
17
18impl<T> Deref for Yaml<T> {
19 type Target = T;
20
21 fn deref(&self) -> &Self::Target {
22 &self.0
23 }
24}
25
26impl<T> DerefMut for Yaml<T> {
27 fn deref_mut(&mut self) -> &mut Self::Target {
28 &mut self.0
29 }
30}
31
32impl<T: Type> Payload for Yaml<T> {
33 const CONTENT_TYPE: &'static str = "application/yaml; charset=utf-8";
34
35 fn check_content_type(content_type: &str) -> bool {
36 matches!(content_type.parse::<mime::Mime>(), Ok(content_type) if content_type.type_() == "application"
37 && (content_type.subtype() == "yaml"
38 || content_type
39 .suffix()
40 .is_some_and(|v| v == "yaml")))
41 }
42
43 fn schema_ref() -> MetaSchemaRef {
44 T::schema_ref()
45 }
46
47 #[allow(unused_variables)]
48 fn register(registry: &mut Registry) {
49 T::register(registry);
50 }
51}
52
53impl<T: ParseFromYAML> ParsePayload for Yaml<T> {
54 const IS_REQUIRED: bool = true;
55
56 async fn from_request(request: &Request, body: &mut RequestBody) -> Result<Self> {
57 let data = Vec::<u8>::from_request(request, body).await?;
58 let value = if data.is_empty() {
59 Value::Null
60 } else {
61 serde_yaml::from_slice(&data).map_err(|err| ParseRequestPayloadError {
62 reason: err.to_string(),
63 })?
64 };
65
66 let value = T::parse_from_yaml(Some(value)).map_err(|err| ParseRequestPayloadError {
67 reason: err.into_message(),
68 })?;
69 Ok(Self(value))
70 }
71}
72
73impl<T: ToYAML> IntoResponse for Yaml<T> {
74 fn into_response(self) -> Response {
75 poem::web::Yaml(self.0.to_yaml()).into_response()
76 }
77}
78
79impl<T: ToYAML> ApiResponse for Yaml<T> {
80 fn meta() -> MetaResponses {
81 MetaResponses {
82 responses: vec![MetaResponse {
83 description: "",
84 status: Some(200),
85 status_range: None,
86 content: vec![MetaMediaType {
87 content_type: Self::CONTENT_TYPE,
88 schema: Self::schema_ref(),
89 }],
90 headers: vec![],
91 }],
92 }
93 }
94
95 fn register(registry: &mut Registry) {
96 T::register(registry);
97 }
98}
99
100impl_apirequest_for_payload!(Yaml<T>, T: ParseFromYAML);