poem_openapi/types/external/
sqlx.rs

1use std::borrow::Cow;
2
3use serde_json::Value;
4
5use crate::{
6    registry::MetaSchemaRef,
7    types::{ParseError, ParseFromJSON, ParseResult, ToJSON, Type},
8};
9
10impl<T: Type> Type for sqlx::types::Json<T> {
11    const IS_REQUIRED: bool = Self::RawValueType::IS_REQUIRED;
12
13    type RawValueType = T;
14
15    type RawElementValueType = T::RawElementValueType;
16
17    fn name() -> Cow<'static, str> {
18        Self::RawValueType::name()
19    }
20
21    fn schema_ref() -> MetaSchemaRef {
22        Self::RawValueType::schema_ref()
23    }
24
25    fn as_raw_value(&self) -> Option<&Self::RawValueType> {
26        Some(&self.0)
27    }
28
29    fn raw_element_iter<'a>(
30        &'a self,
31    ) -> Box<dyn Iterator<Item = &'a Self::RawElementValueType> + 'a> {
32        self.0.raw_element_iter()
33    }
34}
35
36impl<T: ParseFromJSON> ParseFromJSON for sqlx::types::Json<T> {
37    fn parse_from_json(value: Option<Value>) -> ParseResult<Self> {
38        Self::RawValueType::parse_from_json(value)
39            .map(sqlx::types::Json)
40            .map_err(ParseError::propagate)
41    }
42}
43
44impl<T: ToJSON> ToJSON for sqlx::types::Json<T> {
45    fn to_json(&self) -> Option<Value> {
46        self.0.to_json()
47    }
48}