1use ahash::AHashMap as HashMap;
10use ssi::dids::document::Document;
11use thiserror::Error;
12
13#[derive(Error, Debug)]
14pub enum DidExampleError {
15 #[error("Error parsing DID document: {0}")]
16 DocumentParseError(String),
17}
18
19#[derive(Clone, Default)]
20pub struct DiDExampleCache {
21 cache: HashMap<String, Document>,
22}
23
24impl DiDExampleCache {
25 fn from_string(document: String) -> Result<(String, Document), DidExampleError> {
26 let doc = Document::from_json(&document)
27 .map_err(|e| {
28 DidExampleError::DocumentParseError(format!("Couldn't parse Document String: {e}",))
29 })?
30 .into_document();
31
32 Ok((doc.id.to_string(), doc))
33 }
34
35 pub fn new() -> Self {
37 DiDExampleCache {
38 cache: HashMap::new(),
39 }
40 }
41
42 pub fn insert_from_string(&mut self, document: &str) -> Result<(), DidExampleError> {
45 let (id, doc) = DiDExampleCache::from_string(document.to_string())?;
46 self.cache.insert(id, doc);
47 Ok(())
48 }
49
50 pub fn get(&self, id: &str) -> Option<&Document> {
51 self.cache.get(id)
52 }
53}