json_ld_core/loader/
map.rs

1use super::{Loader, RemoteDocument};
2use crate::{LoadError, LoadingResult};
3use iref::{Iri, IriBuf};
4use std::collections::{BTreeMap, HashMap};
5
6/// Error returned using [`HashMap`] or [`BTreeMap`] as a [`Loader`] with the
7/// requested document is not found.
8#[derive(Debug, thiserror::Error)]
9#[error("document not found")]
10pub struct EntryNotFound;
11
12impl Loader for HashMap<IriBuf, RemoteDocument> {
13	async fn load(&self, url: &Iri) -> LoadingResult<IriBuf> {
14		match self.get(url) {
15			Some(document) => Ok(document.clone()),
16			None => Err(LoadError::new(url.to_owned(), EntryNotFound)),
17		}
18	}
19}
20
21impl Loader for BTreeMap<IriBuf, RemoteDocument> {
22	async fn load(&self, url: &Iri) -> LoadingResult<IriBuf> {
23		match self.get(url) {
24			Some(document) => Ok(document.clone()),
25			None => Err(LoadError::new(url.to_owned(), EntryNotFound)),
26		}
27	}
28}