poem_openapi/types/
any.rs1use std::borrow::Cow;
2
3use serde::{de::DeserializeOwned, Deserialize, Deserializer, Serialize, Serializer};
4use serde_json::Value;
5
6use crate::{
7 registry::{MetaSchema, MetaSchemaRef},
8 types::{ParseError, ParseFromJSON, ParseFromXML, ParseResult, ToJSON, ToXML, Type},
9};
10
11#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
13pub struct Any<T>(pub T);
14
15impl<T: Send + Sync> Type for Any<T> {
16 const IS_REQUIRED: bool = true;
17
18 type RawValueType = T;
19
20 type RawElementValueType = T;
21
22 fn name() -> Cow<'static, str> {
23 "any".into()
24 }
25
26 fn schema_ref() -> MetaSchemaRef {
27 MetaSchemaRef::Inline(Box::new(MetaSchema::ANY))
28 }
29
30 fn as_raw_value(&self) -> Option<&Self::RawValueType> {
31 Some(&self.0)
32 }
33
34 fn raw_element_iter<'a>(
35 &'a self,
36 ) -> Box<dyn Iterator<Item = &'a Self::RawElementValueType> + 'a> {
37 Box::new(self.as_raw_value().into_iter())
38 }
39}
40
41impl<T: DeserializeOwned + Send + Sync> ParseFromJSON for Any<T> {
42 fn parse_from_json(value: Option<Value>) -> ParseResult<Self> {
43 Ok(Self(
44 serde_json::from_value(value.unwrap_or_default()).map_err(ParseError::custom)?,
45 ))
46 }
47}
48
49impl<T: Serialize + Send + Sync> ToJSON for Any<T> {
50 fn to_json(&self) -> Option<Value> {
51 Some(serde_json::to_value(&self.0).unwrap_or_default())
52 }
53}
54
55impl<T: DeserializeOwned + Send + Sync> ParseFromXML for Any<T> {
56 fn parse_from_xml(value: Option<Value>) -> ParseResult<Self> {
57 Ok(Self(
58 serde_json::from_value(value.unwrap_or_default()).map_err(ParseError::custom)?,
59 ))
60 }
61}
62
63impl<T: Serialize + Send + Sync> ToXML for Any<T> {
64 fn to_xml(&self) -> Option<Value> {
65 Some(serde_json::to_value(&self.0).unwrap_or_default())
66 }
67}
68
69impl<T: Serialize> Serialize for Any<T> {
70 #[inline]
71 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
72 where
73 S: Serializer,
74 {
75 self.0.serialize(serializer)
76 }
77}
78
79impl<'de, T: DeserializeOwned> Deserialize<'de> for Any<T> {
80 #[inline]
81 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
82 where
83 D: Deserializer<'de>,
84 {
85 T::deserialize(deserializer).map(Self)
86 }
87}
88
89impl Type for Value {
90 const IS_REQUIRED: bool = true;
91
92 type RawValueType = Self;
93
94 type RawElementValueType = Self;
95
96 fn name() -> Cow<'static, str> {
97 "any".into()
98 }
99
100 fn schema_ref() -> MetaSchemaRef {
101 MetaSchemaRef::Inline(Box::new(MetaSchema::ANY))
102 }
103
104 fn as_raw_value(&self) -> Option<&Self::RawValueType> {
105 Some(self)
106 }
107
108 fn raw_element_iter<'a>(
109 &'a self,
110 ) -> Box<dyn Iterator<Item = &'a Self::RawElementValueType> + 'a> {
111 Box::new(self.as_raw_value().into_iter())
112 }
113}
114
115impl ParseFromJSON for Value {
116 fn parse_from_json(value: Option<Value>) -> ParseResult<Self> {
117 Ok(value.unwrap_or_default())
118 }
119}
120
121impl ToJSON for Value {
122 fn to_json(&self) -> Option<Value> {
123 Some(self.clone())
124 }
125}
126
127impl ParseFromXML for Value {
128 fn parse_from_xml(value: Option<Value>) -> ParseResult<Self> {
129 Ok(value.unwrap_or_default())
130 }
131}
132
133impl ToXML for Value {
134 fn to_xml(&self) -> Option<Value> {
135 Some(self.clone())
136 }
137}