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 {
// 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())
};
}
}

View File

@ -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()),
}
}
}

View File

@ -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(()),
}
}

View File

@ -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"),
}
}
}