From cda94cba2e20c201a8a21e01f794b9febd7a6200 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sat, 23 Nov 2024 17:38:59 +0200 Subject: [PATCH] implement to_string method, prevent memory index overwrite on validation step --- .../browser/window/tab/item/identity/gemini.rs | 16 ++++++++-------- src/profile/identity/gemini/auth/error.rs | 9 +++++++++ src/profile/identity/gemini/auth/memory.rs | 17 +++++++++++------ .../identity/gemini/auth/memory/error.rs | 15 ++++++++++++++- 4 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/app/browser/window/tab/item/identity/gemini.rs b/src/app/browser/window/tab/item/identity/gemini.rs index bf8bdf5f..fca6f0ee 100644 --- a/src/app/browser/window/tab/item/identity/gemini.rs +++ b/src/app/browser/window/tab/item/identity/gemini.rs @@ -138,21 +138,21 @@ impl Gemini { match option { // Activate identity for `auth_uri` Some(profile_identity_gemini_id) => { - profile + if let Err(reason) = profile .identity .gemini .auth .apply(profile_identity_gemini_id, auth_url.as_str()) - .unwrap(); + { + todo!("{}", reason.to_string()) + }; } // Remove all identity auths for `auth_uri` None => { - profile - .identity - .gemini - .auth - .remove(auth_url.as_str()) - .unwrap(); + if let Err(reason) = profile.identity.gemini.auth.remove(auth_url.as_str()) + { + todo!("{}", reason.to_string()) + }; } } diff --git a/src/profile/identity/gemini/auth/error.rs b/src/profile/identity/gemini/auth/error.rs index 6e329b69..6b1c83ba 100644 --- a/src/profile/identity/gemini/auth/error.rs +++ b/src/profile/identity/gemini/auth/error.rs @@ -3,3 +3,12 @@ pub enum Error { Database(sqlite::Error), Memory(super::memory::Error), } + +impl Error { + pub fn to_string(&self) -> String { + match self { + Self::Database(reason) => format!("Database error: {}", reason.to_string()), + Self::Memory(reason) => format!("Memory error: {}", reason.to_string()), + } + } +} diff --git a/src/profile/identity/gemini/auth/memory.rs b/src/profile/identity/gemini/auth/memory.rs index ecf4a260..f586e945 100644 --- a/src/profile/identity/gemini/auth/memory.rs +++ b/src/profile/identity/gemini/auth/memory.rs @@ -23,12 +23,17 @@ impl Memory { /// Add new record with `url` as key and `profile_identity_gemini_id` as value /// * validate record with same key does not exist yet pub fn add(&self, url: String, profile_identity_gemini_id: i64) -> Result<(), Error> { - match self - .index - .borrow_mut() - .insert(url, profile_identity_gemini_id) - { - Some(key) => Err(Error::Overwrite(key)), // @TODO prevent? + // Borrow shared index access + let mut index = self.index.borrow_mut(); + + // Prevent existing key overwrite + if index.contains_key(&url) { + return Err(Error::Overwrite(url)); + } + + // Slot should be free, let check it twice + match index.insert(url, profile_identity_gemini_id) { + Some(_) => return Err(Error::Unexpected), None => Ok(()), } } diff --git a/src/profile/identity/gemini/auth/memory/error.rs b/src/profile/identity/gemini/auth/memory/error.rs index a9689656..895e86cc 100644 --- a/src/profile/identity/gemini/auth/memory/error.rs +++ b/src/profile/identity/gemini/auth/memory/error.rs @@ -1,5 +1,18 @@ #[derive(Debug)] pub enum Error { Clear, - Overwrite(i64), + Overwrite(String), + Unexpected, +} + +impl Error { + pub fn to_string(&self) -> String { + match self { + Self::Clear => format!("Could not cleanup memory index"), + Self::Overwrite(key) => { + format!("Overwrite attempt for existing record `{key}`") + } + Self::Unexpected => format!("Unexpected error"), + } + } }