implement to_string method, prevent memory index overwrite on validation step

This commit is contained in:
yggverse 2024-11-23 17:38:59 +02:00
parent 58ed923bc7
commit cda94cba2e
4 changed files with 42 additions and 15 deletions

View File

@ -138,21 +138,21 @@ impl Gemini {
match option { match option {
// Activate identity for `auth_uri` // Activate identity for `auth_uri`
Some(profile_identity_gemini_id) => { Some(profile_identity_gemini_id) => {
profile if let Err(reason) = profile
.identity .identity
.gemini .gemini
.auth .auth
.apply(profile_identity_gemini_id, auth_url.as_str()) .apply(profile_identity_gemini_id, auth_url.as_str())
.unwrap(); {
todo!("{}", reason.to_string())
};
} }
// Remove all identity auths for `auth_uri` // Remove all identity auths for `auth_uri`
None => { None => {
profile if let Err(reason) = profile.identity.gemini.auth.remove(auth_url.as_str())
.identity {
.gemini todo!("{}", reason.to_string())
.auth };
.remove(auth_url.as_str())
.unwrap();
} }
} }

View File

@ -3,3 +3,12 @@ pub enum Error {
Database(sqlite::Error), Database(sqlite::Error),
Memory(super::memory::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()),
}
}
}

View File

@ -23,12 +23,17 @@ impl Memory {
/// Add new record with `url` as key and `profile_identity_gemini_id` as value /// Add new record with `url` as key and `profile_identity_gemini_id` as value
/// * validate record with same key does not exist yet /// * validate record with same key does not exist yet
pub fn add(&self, url: String, profile_identity_gemini_id: i64) -> Result<(), Error> { pub fn add(&self, url: String, profile_identity_gemini_id: i64) -> Result<(), Error> {
match self // Borrow shared index access
.index let mut index = self.index.borrow_mut();
.borrow_mut()
.insert(url, profile_identity_gemini_id) // Prevent existing key overwrite
{ if index.contains_key(&url) {
Some(key) => Err(Error::Overwrite(key)), // @TODO prevent? 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(()), None => Ok(()),
} }
} }

View File

@ -1,5 +1,18 @@
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
Clear, 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"),
}
}
} }