pub mod error;
pub use error::Error;
use std::{cell::RefCell, collections::HashMap};
pub struct Memory {
index: RefCell<HashMap<i64, String>>,
}
impl Memory {
pub fn new() -> Self {
Self {
index: RefCell::new(HashMap::new()),
}
}
pub fn add(&self, profile_identity_gemini_id: i64, pem: String) -> Result<(), Error> {
let mut index = self.index.borrow_mut();
if index.contains_key(&profile_identity_gemini_id) {
return Err(Error::Overwrite(profile_identity_gemini_id));
}
match index.insert(profile_identity_gemini_id, pem) {
Some(_) => Err(Error::Unexpected),
None => Ok(()),
}
}
pub fn get(&self, id: i64) -> Result<String, Error> {
match self.index.borrow().get(&id) {
Some(pem) => Ok(pem.clone()),
None => Err(Error::NotFound(id)),
}
}
pub fn clear(&self) -> Result<(), Error> {
let mut index = self.index.borrow_mut();
index.clear();
if index.is_empty() {
Ok(())
} else {
Err(Error::Clear)
}
}
}