haste_jwt/
sqlx.rs

1use crate::{ProjectId, ResourceId, TenantId, VersionId, VersionIdRef, scopes::Scopes};
2use sqlx::{Database, Decode, Encode, Postgres, postgres::PgArgumentBuffer};
3use std::{error::Error, io::Write};
4
5impl<'r, DB: Database> Decode<'r, DB> for TenantId
6where
7    &'r str: Decode<'r, DB>,
8{
9    fn decode(
10        value: <DB as Database>::ValueRef<'r>,
11    ) -> Result<TenantId, Box<dyn Error + 'static + Send + Sync>> {
12        let value = <&str as Decode<DB>>::decode(value)?;
13        Ok(TenantId::new(value.to_string()))
14    }
15}
16
17impl<'r> Encode<'r, Postgres> for TenantId {
18    fn encode_by_ref(
19        &self,
20        buf: &mut PgArgumentBuffer,
21    ) -> Result<sqlx::encode::IsNull, sqlx::error::BoxDynError> {
22        buf.write(self.as_ref().as_bytes())?;
23        Ok(sqlx::encode::IsNull::No)
24    }
25}
26
27impl sqlx::Type<Postgres> for TenantId {
28    fn type_info() -> sqlx::postgres::PgTypeInfo {
29        <&str as sqlx::Type<Postgres>>::type_info()
30    }
31}
32
33impl<'r, DB: Database> Decode<'r, DB> for ProjectId
34where
35    &'r str: Decode<'r, DB>,
36{
37    fn decode(
38        value: <DB as Database>::ValueRef<'r>,
39    ) -> Result<ProjectId, Box<dyn Error + 'static + Send + Sync>> {
40        let value = <&str as Decode<DB>>::decode(value)?;
41        Ok(ProjectId::new(value.to_string()))
42    }
43}
44impl<'r> Encode<'r, Postgres> for ProjectId {
45    fn encode_by_ref(
46        &self,
47        buf: &mut PgArgumentBuffer,
48    ) -> Result<sqlx::encode::IsNull, sqlx::error::BoxDynError> {
49        buf.write(self.as_ref().as_bytes())?;
50        Ok(sqlx::encode::IsNull::No)
51    }
52}
53
54impl sqlx::Type<Postgres> for ProjectId {
55    fn type_info() -> sqlx::postgres::PgTypeInfo {
56        <&str as sqlx::Type<Postgres>>::type_info()
57    }
58}
59
60impl<'r, DB: Database> Decode<'r, DB> for ResourceId
61where
62    &'r str: Decode<'r, DB>,
63{
64    fn decode(
65        value: <DB as Database>::ValueRef<'r>,
66    ) -> Result<ResourceId, Box<dyn Error + 'static + Send + Sync>> {
67        let value = <&str as Decode<DB>>::decode(value)?;
68        Ok(ResourceId::new(value.to_string()))
69    }
70}
71
72impl<'r> Encode<'r, Postgres> for ResourceId {
73    fn encode_by_ref(
74        &self,
75        buf: &mut PgArgumentBuffer,
76    ) -> Result<sqlx::encode::IsNull, sqlx::error::BoxDynError> {
77        buf.write(self.0.as_bytes())?;
78        Ok(sqlx::encode::IsNull::No)
79    }
80}
81
82impl<'r> Encode<'r, Postgres> for VersionIdRef<'r> {
83    fn encode_by_ref(
84        &self,
85        buf: &mut PgArgumentBuffer,
86    ) -> Result<sqlx::encode::IsNull, sqlx::error::BoxDynError> {
87        buf.write(self.0.as_bytes())?;
88        Ok(sqlx::encode::IsNull::No)
89    }
90}
91
92impl<'r> Encode<'r, Postgres> for VersionId {
93    fn encode_by_ref(
94        &self,
95        buf: &mut PgArgumentBuffer,
96    ) -> Result<sqlx::encode::IsNull, sqlx::error::BoxDynError> {
97        buf.write(self.0.as_bytes())?;
98        Ok(sqlx::encode::IsNull::No)
99    }
100}
101
102impl<'r, DB: Database> Decode<'r, DB> for VersionId
103where
104    &'r str: Decode<'r, DB>,
105{
106    fn decode(
107        value: <DB as Database>::ValueRef<'r>,
108    ) -> Result<VersionId, Box<dyn Error + 'static + Send + Sync>> {
109        let value = <&str as Decode<DB>>::decode(value)?;
110        Ok(VersionId::new(value.to_string()))
111    }
112}
113
114impl sqlx::Type<Postgres> for VersionId {
115    fn type_info() -> sqlx::postgres::PgTypeInfo {
116        <&str as sqlx::Type<Postgres>>::type_info()
117    }
118}
119
120impl<'r, DB: Database> Decode<'r, DB> for Scopes
121where
122    &'r str: Decode<'r, DB>,
123{
124    fn decode(
125        value: <DB as Database>::ValueRef<'r>,
126    ) -> Result<Scopes, Box<dyn Error + 'static + Send + Sync>> {
127        let value = <&str as Decode<DB>>::decode(value)?;
128        let scopes = Scopes::try_from(value)?;
129
130        Ok(scopes)
131    }
132}
133
134impl<'r> Encode<'r, Postgres> for Scopes {
135    fn encode_by_ref(
136        &self,
137        buf: &mut PgArgumentBuffer,
138    ) -> Result<sqlx::encode::IsNull, sqlx::error::BoxDynError> {
139        let scope_string = String::from(self.clone());
140        buf.write(scope_string.as_bytes())?;
141        Ok(sqlx::encode::IsNull::No)
142    }
143}
144
145impl sqlx::Type<Postgres> for Scopes {
146    fn type_info() -> sqlx::postgres::PgTypeInfo {
147        <&str as sqlx::Type<Postgres>>::type_info()
148    }
149}