1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
pub(crate) mod sql_db;
use async_trait::async_trait;
use sqlx::{Postgres, Sqlite};
use std::sync::Arc;
use uuid::Uuid;
use crate::{
Address, AddressType, AddressValidation, Credential, CredentialId, CredentialType, Identity,
IdentityId,
};
use self::sql_db::SqlClient;
#[derive(Debug, thiserror::Error, derive_more::Display)]
pub enum StoreError {
SqlClientError(#[from] sqlx::Error),
SerdeError(#[from] serde_json::Error),
ParseError(#[from] strum::ParseError),
StoreValueCannotBeParsedInvariant,
IdempotentCheckAlreadyExists,
}
#[async_trait]
pub trait Store: Send + Sync {
fn get_type(&self) -> StoreType;
}
pub enum StoreType {
Postgres { c: Arc<SqlClient<Postgres>> },
Sqlite { c: Arc<SqlClient<Sqlite>> },
}
#[async_trait]
pub(crate) trait Storable<'a> {
type Item;
type Lens;
async fn write(&self, store: Arc<dyn Store>) -> Result<(), StoreError>;
async fn find(
store: Arc<dyn Store>,
lens: &'a Self::Lens,
) -> Result<Vec<Self::Item>, StoreError>;
}
pub(crate) trait Lens {}
pub(crate) struct AddressLens<'a> {
pub id: Option<&'a Uuid>,
pub t: Option<&'a AddressType>,
}
impl<'a> Lens for AddressLens<'a> {}
pub(crate) struct AddressValidationLens<'a> {
pub id: Option<&'a Uuid>,
}
impl<'a> Lens for AddressValidationLens<'a> {}
pub(crate) struct IdentityLens<'a> {
pub id: Option<&'a Uuid>,
pub address_type: Option<&'a AddressType>,
pub validated_address: Option<bool>,
}
impl<'a> Lens for IdentityLens<'a> {}
pub(crate) struct CredentialLens<'a> {
pub id: Option<CredentialId>,
pub identity_id: Option<IdentityId>,
pub t: Option<&'a CredentialType>,
}
impl<'a> Lens for CredentialLens<'a> {}
#[async_trait]
impl<'a> Storable<'a> for Address {
type Item = Address;
type Lens = AddressLens<'a>;
async fn write(&self, store: Arc<dyn Store>) -> Result<(), StoreError> {
match store.get_type() {
StoreType::Postgres { c } => c.write_address(self).await?,
StoreType::Sqlite { c } => c.write_address(self).await?,
}
Ok(())
}
async fn find(
store: Arc<dyn Store>,
lens: &'a Self::Lens,
) -> Result<Vec<Self::Item>, StoreError> {
let typ = lens.t.map(|at| at.to_string());
let typ = typ.as_deref();
let val = lens.t.and_then(|at| at.get_value());
let val = val.as_deref();
Ok(match store.get_type() {
StoreType::Postgres { c } => c.find_address(lens.id, typ, val).await?,
StoreType::Sqlite { c } => c.find_address(lens.id, typ, val).await?,
})
}
}
#[async_trait]
impl<'a> Storable<'a> for AddressValidation {
type Item = AddressValidation;
type Lens = AddressValidationLens<'a>;
async fn write(&self, store: Arc<dyn Store>) -> Result<(), StoreError> {
match store.get_type() {
StoreType::Sqlite { c } => c.write_address_validation(self).await?,
StoreType::Postgres { c } => c.write_address_validation(self).await?,
}
Ok(())
}
async fn find(
store: Arc<dyn Store>,
lens: &'a Self::Lens,
) -> Result<Vec<Self::Item>, StoreError> {
Ok(match store.get_type() {
StoreType::Postgres { c } => c.find_address_validation(lens.id).await?,
StoreType::Sqlite { c } => c.find_address_validation(lens.id).await?,
})
}
}
#[async_trait]
impl<'a> Storable<'a> for Identity {
type Item = Identity;
type Lens = IdentityLens<'a>;
async fn write(&self, store: Arc<dyn Store>) -> Result<(), StoreError> {
match store.get_type() {
StoreType::Postgres { c } => c.write_identity(self).await?,
StoreType::Sqlite { c } => c.write_identity(self).await?,
}
Ok(())
}
async fn find(
store: Arc<dyn Store>,
lens: &'a Self::Lens,
) -> Result<Vec<Self::Item>, StoreError> {
let val = lens.address_type.and_then(|at| at.get_value());
let val = val.as_deref();
Ok(match store.get_type() {
StoreType::Postgres { c } => {
c.find_identity(lens.id, val, lens.validated_address)
.await?
}
StoreType::Sqlite { c } => {
c.find_identity(lens.id, val, lens.validated_address)
.await?
}
})
}
}
#[async_trait]
impl<'a> Storable<'a> for Credential {
type Item = Credential;
type Lens = CredentialLens<'a>;
async fn write(&self, store: Arc<dyn Store>) -> Result<(), StoreError> {
match store.get_type() {
StoreType::Postgres { c } => c.write_credential(self).await?,
StoreType::Sqlite { c } => c.write_credential(self).await?,
}
Ok(())
}
async fn find(
store: Arc<dyn Store>,
lens: &'a Self::Lens,
) -> Result<Vec<Self::Item>, StoreError> {
Ok(match store.get_type() {
StoreType::Postgres { c } => {
c.find_credential(lens.id, lens.identity_id, lens.t).await?
}
StoreType::Sqlite { c } => c.find_credential(lens.id, lens.identity_id, lens.t).await?,
})
}
}
|