es_entity/context/
sqlx.rs

1use sqlx::{
2    Postgres,
3    postgres::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueRef},
4};
5
6use super::ContextData;
7
8impl sqlx::Type<Postgres> for ContextData {
9    fn type_info() -> PgTypeInfo {
10        <serde_json::Value as sqlx::Type<Postgres>>::type_info()
11    }
12}
13
14impl<'q> sqlx::Encode<'q, Postgres> for ContextData {
15    fn encode_by_ref(
16        &self,
17        buf: &mut PgArgumentBuffer,
18    ) -> Result<sqlx::encode::IsNull, Box<dyn std::error::Error + Send + Sync + 'static>> {
19        let json_value = serde_json::to_value(&self.0)?;
20        <serde_json::Value as sqlx::Encode<Postgres>>::encode_by_ref(&json_value, buf)
21    }
22}
23
24impl<'r> sqlx::Decode<'r, Postgres> for ContextData {
25    fn decode(
26        value: PgValueRef<'r>,
27    ) -> Result<Self, Box<dyn std::error::Error + 'static + Send + Sync>> {
28        let json_value = <serde_json::Value as sqlx::Decode<Postgres>>::decode(value)?;
29        let res: ContextData = serde_json::from_value(json_value)?;
30        Ok(res)
31    }
32}
33
34impl PgHasArrayType for ContextData {
35    fn array_type_info() -> PgTypeInfo {
36        <serde_json::Value as sqlx::postgres::PgHasArrayType>::array_type_info()
37    }
38}