From 8ac3a8ce8ba268a3261e23694b8b62afa6a3ae37 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Wed, 1 Nov 2023 16:45:29 +0100 Subject: [PATCH] implement an AnyCredentials --- src/login/mod.rs | 33 ++++++++++++++++----------------- src/mail/user.rs | 6 ++++-- src/storage/garage.rs | 3 ++- src/storage/in_memory.rs | 3 ++- src/storage/mod.rs | 14 ++++++-------- 5 files changed, 30 insertions(+), 29 deletions(-) diff --git a/src/login/mod.rs b/src/login/mod.rs index f403bcb..5bd976e 100644 --- a/src/login/mod.rs +++ b/src/login/mod.rs @@ -16,8 +16,6 @@ use rusoto_s3::S3Client; use crate::cryptoblob::*; use crate::storage::*; -use crate::storage::in_memory::MemTypes; -use crate::storage::garage::GrgTypes; /// The trait LoginProvider defines the interface for a login provider that allows /// to retrieve storage and cryptographic credentials for access to a user account @@ -32,19 +30,28 @@ pub trait LoginProvider { async fn public_login(&self, email: &str) -> Result; } -pub enum AnyCredentials { - InMemory(Credentials), - Garage(Credentials), -} - /// ArcLoginProvider is simply an alias on a structure that is used /// in many places in the code pub type ArcLoginProvider = Arc; +pub enum AnyCredentials { + InMemory(Credentials), + Garage(Credentials), +} +impl AnyCredentials where X: Sto +{ + fn to_gen(&self) -> Credentials { + match self { + Self::InMemory(u) => u, + Self::Garage(u) => u, + } + } +} + /// The struct Credentials represent all of the necessary information to interact /// with a user account's data after they are logged in. #[derive(Clone, Debug)] -pub struct Credentials { +pub struct Credentials { /// The storage credentials are used to authenticate access to the underlying storage (S3, K2V) pub storage: T::Builder, /// The cryptographic keys are used to encrypt and decrypt data stored in S3 and K2V @@ -114,7 +121,7 @@ impl Region { // ---- -/* + impl Credentials { pub fn k2v_client(&self) -> Result { self.storage.k2v_client() @@ -125,14 +132,6 @@ impl Credentials { pub fn bucket(&self) -> &str { self.storage.bucket.as_str() } -}*/ -impl From for Credentials { - fn from(ac: AnyCredentials) -> Self { - match ac { - AnyCredentials::InMemory(c) => c, - AnyCredentials::Garage(c) => c, - } - } } impl StorageCredentials { diff --git a/src/mail/user.rs b/src/mail/user.rs index 5523c2a..9d94563 100644 --- a/src/mail/user.rs +++ b/src/mail/user.rs @@ -30,9 +30,11 @@ pub const INBOX: &str = "INBOX"; const MAILBOX_LIST_PK: &str = "mailboxes"; const MAILBOX_LIST_SK: &str = "list"; -pub struct User { +use crate::storage::*; + +pub struct User { pub username: String, - pub creds: Credentials, + pub creds: Credentials, pub k2v: K2vClient, pub mailboxes: std::sync::Mutex>>, diff --git a/src/storage/garage.rs b/src/storage/garage.rs index 91c4fa2..b883623 100644 --- a/src/storage/garage.rs +++ b/src/storage/garage.rs @@ -6,7 +6,8 @@ pub struct GrgRef {} pub struct GrgValue {} pub struct GrgTypes {} -impl RowRealization for GrgTypes { +impl Sto for GrgTypes { + type Builder=GrgCreds; type Store=GrgStore; type Ref=GrgRef; type Value=GrgValue; diff --git a/src/storage/in_memory.rs b/src/storage/in_memory.rs index a2e9e96..56df266 100644 --- a/src/storage/in_memory.rs +++ b/src/storage/in_memory.rs @@ -6,7 +6,8 @@ pub struct MemRef {} pub struct MemValue {} pub struct MemTypes {} -impl RowRealization for MemTypes { +impl Sto for MemTypes { + type Builder=MemCreds; type Store=MemStore; type Ref=MemRef; type Value=MemValue; diff --git a/src/storage/mod.rs b/src/storage/mod.rs index bc26379..2e4f757 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -28,29 +28,27 @@ pub enum Error { Internal, } -pub trait RowRealization: Sized { - type Builder: RowBuilder; +pub trait Sto: Sized { + type Builder: RowStore; type Store: RowStore; type Ref: RowRef; type Value: RowValue; } -pub trait StorageEngine: RowRealization {} - // ------ Row Builder -pub trait RowBuilder +pub trait RowBuilder { fn row_store(&self) -> R::Store; } // ------ Row Store -pub trait RowStore +pub trait RowStore { fn new_row(&self, partition: &str, sort: &str) -> R::Ref; } // ------- Row Item -pub trait RowRef +pub trait RowRef { fn set_value(&self, content: Vec) -> R::Value; async fn fetch(&self) -> Result; @@ -58,7 +56,7 @@ pub trait RowRef async fn poll(&self) -> Result, Error>; } -pub trait RowValue +pub trait RowValue { fn to_ref(&self) -> R::Ref; fn content(&self) -> ConcurrentValues;