use ssi::dids::document::Document;
use std::collections::HashMap;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum DidExampleError {
#[error("Error parsing DID document: {0}")]
DocumentParseError(String),
}
#[derive(Clone, Default)]
pub struct DiDExampleCache {
cache: HashMap<String, Document>,
}
impl DiDExampleCache {
fn from_string(document: String) -> Result<(String, Document), DidExampleError> {
let doc = Document::from_json(&document)
.map_err(|e| {
DidExampleError::DocumentParseError(format!(
"Couldn't parse Document String: {}",
e
))
})?
.into_document();
Ok((doc.id.to_string(), doc))
}
pub fn new() -> Self {
DiDExampleCache {
cache: HashMap::new(),
}
}
pub fn insert_from_string(&mut self, document: &str) -> Result<(), DidExampleError> {
let (id, doc) = DiDExampleCache::from_string(document.to_string())?;
self.cache.insert(id, doc);
Ok(())
}
pub fn get(&self, id: &str) -> Option<&Document> {
self.cache.get(id)
}
}