core_crypto_macros/entity_derive/
derive_impl.rs

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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use crate::entity_derive::{IdColumnType, IdTransformation, KeyStoreEntityFlattened};
use quote::quote;

impl quote::ToTokens for KeyStoreEntityFlattened {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        let entity_base_impl = self.entity_base_impl();
        let entity_generic_impl = self.entity_generic_impl();
        let entity_wasm_impl = self.entity_wasm_impl();
        let entity_transaction_ext_impl = self.entity_transaction_ext_impl();
        tokens.extend(quote! {
            #entity_base_impl
            #entity_generic_impl
            #entity_wasm_impl
            #entity_transaction_ext_impl
        });
    }
}
impl KeyStoreEntityFlattened {
    fn entity_base_impl(&self) -> proc_macro2::TokenStream {
        let Self {
            collection_name,
            struct_name,
            ..
        } = self;

        // Identical for both wasm and non-wasm
        quote! {
            #[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]
            #[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
            impl crate::entities::EntityBase for #struct_name {
                type ConnectionType = crate::connection::KeystoreDatabaseConnection;
                type AutoGeneratedFields = ();
                const COLLECTION_NAME: &'static str = #collection_name;

                fn to_missing_key_err_kind() -> crate::MissingKeyErrorKind {
                    crate::MissingKeyErrorKind::#struct_name
                }

                fn to_transaction_entity(self) -> crate::transaction::dynamic_dispatch::Entity {
                    crate::transaction::dynamic_dispatch::Entity::#struct_name(self)
                }
            }
        }
    }

    fn entity_generic_impl(&self) -> proc_macro2::TokenStream {
        let Self {
            collection_name,
            struct_name,
            id,
            id_type,
            id_name,
            id_transformation,
            blob_columns,
            blob_column_names,
            all_columns,
            optional_blob_columns,
            optional_blob_column_names,
            ..
        } = self;

        let string_id_conversion = (*id_type == IdColumnType::String).then(|| {
            quote! { let #id: String = id.try_into()?; }
        });

        let id_to_byte_slice = match id_type {
            IdColumnType::String => quote! {self.#id.as_bytes() },
            IdColumnType::Bytes => quote! { &self.#id.as_slice() },
        };

        let id_field_find_one = match id_type {
            IdColumnType::String => quote! { #id, },
            IdColumnType::Bytes => quote! { #id: id.to_bytes(), },
        };

        let id_slice = match id_type {
            IdColumnType::String => quote! { #id.as_str() },
            IdColumnType::Bytes => quote! { #id.as_slice() },
        };

        let id_input_transformed = match id_transformation {
            Some(IdTransformation::Hex) => quote! { id.as_hex_string() },
            Some(IdTransformation::Sha256) => todo!(),
            None => id_slice,
        };

        let destructure_row = match id_transformation {
            Some(IdTransformation::Hex) => quote! { let (rowid, #id): (_, String) = row?; },
            Some(IdTransformation::Sha256) => todo!(),
            None => quote! { let (rowid, #id) = row?; },
        };

        let id_from_transformed = match id_transformation {
            Some(IdTransformation::Hex) => {
                quote! { let #id = <Self as crate::entities::EntityIdStringExt>::id_from_hex(&#id)?; }
            }
            Some(IdTransformation::Sha256) => todo!(),
            None => quote! {},
        };

        let find_all_query = format!("SELECT rowid, {id_name} FROM {collection_name} ");

        let find_one_query = format!("SELECT rowid FROM {collection_name} WHERE {id_name} = ?");

        let count_query = format!("SELECT COUNT(*) FROM {collection_name}");

        quote! {
            #[cfg(not(target_family = "wasm"))]
            #[async_trait::async_trait]
            impl crate::entities::Entity for #struct_name {
                fn id_raw(&self) -> &[u8] {
                    #id_to_byte_slice
                }

                async fn find_all(
                    conn: &mut Self::ConnectionType,
                    params: crate::entities::EntityFindParams,
                ) -> crate::CryptoKeystoreResult<Vec<Self>> {
                    let mut conn = conn.conn().await;
                    let transaction = conn.transaction()?;
                    let query = #find_all_query.to_string() + &params.to_sql();

                    let mut stmt = transaction.prepare_cached(&query)?;
                    let mut rows = stmt.query_map([], |r| Ok((r.get(0)?, r.get(1)?)))?;
                    use std::io::Read as _;
                    rows.map(|row| {
                        #destructure_row
                        #id_from_transformed

                        #(
                            let mut blob = transaction.blob_open(rusqlite::DatabaseName::Main, #collection_name, #blob_column_names, rowid, true)?;
                            let mut #blob_columns = Vec::with_capacity(blob.len());
                            blob.read_to_end(&mut #blob_columns)?;
                            blob.close()?;
                        )*

                        #(
                            let mut #optional_blob_columns = None;
                            if let Ok(mut blob) =
                                transaction.blob_open(rusqlite::DatabaseName::Main, #collection_name, #optional_blob_column_names, rowid, true)
                            {
                                if !blob.is_empty() {
                                    let mut blob_data = Vec::with_capacity(blob.len());
                                    blob.read_to_end(&mut blob_data)?;
                                    #optional_blob_columns.replace(blob_data);
                                }
                                blob.close()?;
                            }
                        )*

                        Ok(Self { #id
                            #(
                            , #all_columns
                            )*
                        })
                    }).collect()
                }

                async fn find_one(
                    conn: &mut Self::ConnectionType,
                    id: &crate::entities::StringEntityId,
                ) -> crate::CryptoKeystoreResult<Option<Self>> {
                    let mut conn = conn.conn().await;
                    let transaction = conn.transaction()?;
                    use rusqlite::OptionalExtension as _;

                   #string_id_conversion

                    let mut rowid: Option<i64> = transaction
                        .query_row(&#find_one_query, [#id_input_transformed], |r| {
                            r.get::<_, i64>(0)
                        })
                        .optional()?;

                    use std::io::Read as _;
                    if let Some(rowid) = rowid.take() {
                        #(
                            let mut blob = transaction.blob_open(rusqlite::DatabaseName::Main, #collection_name, #blob_column_names, rowid, true)?;
                            let mut #blob_columns = Vec::with_capacity(blob.len());
                            blob.read_to_end(&mut #blob_columns)?;
                            blob.close()?;
                        )*

                        #(
                            let mut #optional_blob_columns = None;
                            if let Ok(mut blob) =
                                transaction.blob_open(rusqlite::DatabaseName::Main, #collection_name, #optional_blob_column_names, rowid, true)
                            {
                                if !blob.is_empty() {
                                    let mut blob_data = Vec::with_capacity(blob.len());
                                    blob.read_to_end(&mut blob_data)?;
                                    #optional_blob_columns.replace(blob_data);
                                }
                                blob.close()?;
                            }
                        )*

                        Ok(Some(Self {
                            #id_field_find_one
                            #(
                                #blob_columns,
                            )*
                            #(
                                #optional_blob_columns,
                            )*
                        }))
                    } else {
                        Ok(None)
                    }
                }

                async fn count(conn: &mut Self::ConnectionType) -> crate::CryptoKeystoreResult<usize> {
                    let conn = conn.conn().await;
                    conn.query_row(&#count_query, [], |r| r.get(0)).map_err(Into::into)
                }
            }
        }
    }

    fn entity_wasm_impl(&self) -> proc_macro2::TokenStream {
        let Self {
            collection_name,
            struct_name,
            id,
            id_type,
            blob_columns,
            ..
        } = self;

        let id_to_byte_slice = match id_type {
            IdColumnType::String => quote! {self.#id.as_bytes() },
            IdColumnType::Bytes => quote! { self.#id.as_slice() },
        };

        quote! {
            #[cfg(target_family = "wasm")]
            #[async_trait::async_trait(?Send)]
            impl crate::entities::Entity for #struct_name {
                fn id_raw(&self) -> &[u8] {
                    #id_to_byte_slice
                }

                async fn find_all(conn: &mut Self::ConnectionType, params: crate::entities::EntityFindParams) ->  crate::CryptoKeystoreResult<Vec<Self>> {
                    let storage = conn.storage();
                    storage.get_all(#collection_name, Some(params)).await
                }

                async fn find_one(conn: &mut Self::ConnectionType, id: &crate::entities::StringEntityId) ->  crate::CryptoKeystoreResult<Option<Self>> {
                    conn.storage().get(#collection_name, id.as_slice()).await
                }

                async fn count(conn: &mut Self::ConnectionType) ->  crate::CryptoKeystoreResult<usize> {
                    conn.storage().count(#collection_name).await
                }

                fn encrypt(&mut self, cipher: &aes_gcm::Aes256Gcm) -> crate::CryptoKeystoreResult<()> {
                    use crate::connection::DatabaseConnection as _;
                    #(
                        self.#blob_columns = self.encrypt_data(cipher, self.#blob_columns.as_slice())?;
                        Self::ConnectionType::check_buffer_size(self.#blob_columns.len())?;
                    )*
                    Ok(())
                }

                fn decrypt(&mut self, cipher: &aes_gcm::Aes256Gcm) -> crate::CryptoKeystoreResult<()> {
                    #(
                        self.#blob_columns = self.decrypt_data(cipher, self.#blob_columns.as_slice())?;
                    )*
                    Ok(())
                }
            }
        }
    }

    fn entity_transaction_ext_impl(&self) -> proc_macro2::TokenStream {
        let Self {
            collection_name,
            struct_name,
            id,
            id_name,
            all_columns,
            all_column_names,
            blob_columns,
            blob_column_names,
            optional_blob_columns,
            optional_blob_column_names,
            id_transformation,
            no_upsert,
            id_type,
            ..
        } = self;

        let upsert_pairs: Vec<_> = all_column_names
            .iter()
            .map(|col| format! { "{col} = excluded.{col}"})
            .collect();
        let upsert_postfix = (!no_upsert)
            // UPSERT (ON CONFLICT DO UPDATE) with RETURNING to get the rowid
            .then(|| format!(" ON CONFLICT({id_name}) DO UPDATE SET {}", upsert_pairs.join(", ")))
            .unwrap_or_default();

        let column_list = all_columns
            .iter()
            .map(ToString::to_string)
            .collect::<Vec<_>>()
            .join(", ");

        let import_id_string_ext = match id_transformation {
            Some(IdTransformation::Hex) => quote! { use crate::entities::EntityIdStringExt as _; },
            Some(IdTransformation::Sha256) => todo!(),
            None => quote! {},
        };

        let upsert_query = format!(
            "INSERT INTO {collection_name} ({id_name}, {column_list}) VALUES (?{}){upsert_postfix} RETURNING rowid",
            ", ?".repeat(self.all_columns.len()),
        );

        let self_id_transformed = match id_transformation {
            Some(IdTransformation::Hex) => quote! { self.id_hex() },
            Some(IdTransformation::Sha256) => todo!(),
            None => quote! { self.#id },
        };

        let delete_query = format!("DELETE FROM {collection_name} WHERE {id_name} = ?");

        let id_slice_delete = match id_type {
            IdColumnType::String => quote! { id.try_as_str()? },
            IdColumnType::Bytes => quote! { id.as_slice() },
        };

        let id_input_transformed_delete = match id_transformation {
            Some(IdTransformation::Hex) => quote! { id.as_hex_string() },
            Some(IdTransformation::Sha256) => todo!(),
            None => id_slice_delete,
        };

        quote! {
            #[cfg(target_family = "wasm")]
            #[async_trait::async_trait(?Send)]
            impl crate::entities::EntityTransactionExt for #struct_name {}

            #[cfg(not(target_family = "wasm"))]
            #[async_trait::async_trait]
            impl crate::entities::EntityTransactionExt for #struct_name {
                async fn save(&self, transaction: &crate::connection::TransactionWrapper<'_>) -> crate::CryptoKeystoreResult<()> {
                    use crate::entities::EntityBase as _;
                    use rusqlite::ToSql as _;
                    use crate::connection::DatabaseConnection as _;

                    #(
                        crate::connection::KeystoreDatabaseConnection::check_buffer_size(self.#blob_columns.len())?;
                    )*
                    #(
                      crate::connection::KeystoreDatabaseConnection::check_buffer_size(
                            self.#optional_blob_columns.as_ref().map(|v| v.len()).unwrap_or_default()
                      )?;
                    )*

                    #import_id_string_ext

                    let sql = #upsert_query;

                    let rowid_result: Result<i64, rusqlite::Error> =
                        transaction.query_row(&sql, [
                        #self_id_transformed.to_sql()?
                        #(
                            ,
                            rusqlite::blob::ZeroBlob(self.#blob_columns.len() as i32).to_sql()?
                        )*
                        #(
                            ,
                            rusqlite::blob::ZeroBlob(self.#optional_blob_columns.as_ref().map(|v| v.len() as i32).unwrap_or_default()).to_sql()?
                        )*
                    ], |r| r.get(0));

                    use std::io::Write as _;
                    match rowid_result {
                        Ok(rowid) => {
                            #(
                                let mut blob = transaction.blob_open(
                                    rusqlite::DatabaseName::Main,
                                    #collection_name,
                                    #blob_column_names,
                                    rowid,
                                    false,
                                )?;

                                blob.write_all(&self.#blob_columns)?;
                                blob.close()?;
                            )*

                            #(
                                let mut blob = transaction.blob_open(rusqlite::DatabaseName::Main, #collection_name, #optional_blob_column_names, rowid, false)?;
                                if let Some(#optional_blob_columns) = self.#optional_blob_columns.as_ref() {
                                    blob.write_all(#optional_blob_columns)?;
                                }
                                blob.close()?;
                            )*

                            Ok(())
                        }
                        Err(rusqlite::Error::SqliteFailure(e, _)) if e.extended_code == rusqlite::ffi::SQLITE_CONSTRAINT_UNIQUE => {
                            Err(crate::CryptoKeystoreError::AlreadyExists)
                        }
                        Err(e) => Err(e.into()),
                    }
                }

                async fn delete_fail_on_missing_id(
                    transaction: &crate::connection::TransactionWrapper<'_>,
                    id: crate::entities::StringEntityId<'_>,
                ) -> crate::CryptoKeystoreResult<()> {
                    use crate::entities::EntityBase as _;
                    let deleted = transaction.execute(&#delete_query, [#id_input_transformed_delete])?;

                    if deleted > 0 {
                        Ok(())
                    } else {
                        Err(Self::to_missing_key_err_kind().into())
                    }
                }
            }
        }
    }
}