mod error;
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, id: i64, pem: String) -> Result<(), Error> {
match self.index.borrow_mut().insert(id, pem) {
Some(_) => Err(Error::Overwrite(id)), 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) {
self.index.borrow_mut().clear()
}
}