From 3b363b2a7803564231e001c215ab427c99c9435b Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Thu, 2 Nov 2023 12:18:43 +0100 Subject: [PATCH] implement equality+cmp for builders based on url --- src/login/mod.rs | 13 +++++------ src/mail/user.rs | 3 ++- src/storage/garage.rs | 8 +++++-- src/storage/in_memory.rs | 8 +++++-- src/storage/mod.rs | 47 +++++++++++++++++++++++----------------- 5 files changed, 46 insertions(+), 33 deletions(-) diff --git a/src/login/mod.rs b/src/login/mod.rs index afade28..f4bf4d2 100644 --- a/src/login/mod.rs +++ b/src/login/mod.rs @@ -39,7 +39,7 @@ pub type ArcLoginProvider = Arc; #[derive(Clone, Debug)] pub struct Credentials { /// The storage credentials are used to authenticate access to the underlying storage (S3, K2V) - pub storage: Engine, + pub storage: Builders, /// The cryptographic keys are used to encrypt and decrypt data stored in S3 and K2V pub keys: CryptoKeys, } @@ -109,14 +109,11 @@ impl Region { impl Credentials { - pub fn k2v_client(&self) -> Result { - self.storage.row.row_store() + pub fn row_client(&self) -> Result { + Ok(self.storage.row_store()?) } - pub fn s3_client(&self) -> Result { - self.storage.s3_client() - } - pub fn bucket(&self) -> &str { - self.storage.bucket.as_str() + pub fn blob_client(&self) -> Result { + Ok(self.storage.blob_store()?) } } diff --git a/src/mail/user.rs b/src/mail/user.rs index 5523c2a..2104455 100644 --- a/src/mail/user.rs +++ b/src/mail/user.rs @@ -13,6 +13,7 @@ use crate::mail::incoming::incoming_mail_watch_process; use crate::mail::mailbox::Mailbox; use crate::mail::uidindex::ImapUidvalidity; use crate::mail::unique_ident::{gen_ident, UniqueIdent}; +use crate::storage; use crate::time::now_msec; pub const MAILBOX_HIERARCHY_DELIMITER: char = '.'; @@ -455,6 +456,6 @@ enum CreatedMailbox { // ---- User cache ---- lazy_static! { - static ref USER_CACHE: std::sync::Mutex>> = + static ref USER_CACHE: std::sync::Mutex>> = std::sync::Mutex::new(HashMap::new()); } diff --git a/src/storage/garage.rs b/src/storage/garage.rs index dfee88d..6dea00c 100644 --- a/src/storage/garage.rs +++ b/src/storage/garage.rs @@ -1,12 +1,12 @@ use crate::storage::*; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Hash)] pub struct GrgCreds {} pub struct GrgStore {} pub struct GrgRef {} pub struct GrgValue {} -impl IBuilder for GrgCreds { +impl IBuilders for GrgCreds { fn row_store(&self) -> Result { unimplemented!(); } @@ -14,6 +14,10 @@ impl IBuilder for GrgCreds { fn blob_store(&self) -> Result { unimplemented!(); } + + fn url(&self) -> &str { + return "grg://unimplemented;" + } } impl IRowStore for GrgStore { diff --git a/src/storage/in_memory.rs b/src/storage/in_memory.rs index 80e7fdf..5cc8ef8 100644 --- a/src/storage/in_memory.rs +++ b/src/storage/in_memory.rs @@ -1,13 +1,13 @@ use futures::FutureExt; use crate::storage::*; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Hash)] pub struct FullMem {} pub struct MemStore {} pub struct MemRef {} pub struct MemValue {} -impl IBuilder for FullMem { +impl IBuilders for FullMem { fn row_store(&self) -> Result { unimplemented!(); } @@ -15,6 +15,10 @@ impl IBuilder for FullMem { fn blob_store(&self) -> Result { unimplemented!(); } + + fn url(&self) -> &str { + return "mem://unimplemented;" + } } impl IRowStore for MemStore { diff --git a/src/storage/mod.rs b/src/storage/mod.rs index a2bdd43..0939463 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -8,6 +8,7 @@ * into the object system so it is not exposed. */ +use std::hash::{Hash, Hasher}; use futures::future::BoxFuture; pub mod in_memory; @@ -40,32 +41,38 @@ impl std::fmt::Display for StorageError { } impl std::error::Error for StorageError {} -pub struct Engine { - pub bucket: String, - pub builders: Builder, -} -impl Clone for Engine { - fn clone(&self) -> Self { - Engine { - bucket: "test".into(), - builders: Box::new(in_memory::FullMem{}) - } - } -} -impl std::fmt::Debug for Engine { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Engine").field("bucket", &self.bucket).finish() - } -} - // Utils pub type AsyncResult<'a, T> = BoxFuture<'a, Result>; -pub trait IBuilder { +// ----- Builders +pub trait IBuilders { fn row_store(&self) -> Result; fn blob_store(&self) -> Result; + fn url(&self) -> &str; +} +pub type Builders = Box; +impl Clone for Builders { + fn clone(&self) -> Self { + // @FIXME write a real implementation with a box_clone function + Box::new(in_memory::FullMem{}) + } +} +impl std::fmt::Debug for Builders { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("aerogramme::storage::Builder") + } +} +impl PartialEq for Builders { + fn eq(&self, other: &Self) -> bool { + self.url() == other.url() + } +} +impl Eq for Builders {} +impl Hash for Builders { + fn hash(&self, state: &mut H) { + self.url().hash(state); + } } -pub type Builder = Box; // ------ Row pub trait IRowStore