1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use super::{Loader, RemoteDocument};
use crate::{LoadError, LoadingResult};
use iref::{Iri, IriBuf};
use std::collections::{BTreeMap, HashMap};

/// Error returned using [`HashMap`] or [`BTreeMap`] as a [`Loader`] with the
/// requested document is not found.
#[derive(Debug, thiserror::Error)]
#[error("document not found")]
pub struct EntryNotFound;

impl Loader for HashMap<IriBuf, RemoteDocument> {
	async fn load(&self, url: &Iri) -> LoadingResult<IriBuf> {
		match self.get(url) {
			Some(document) => Ok(document.clone()),
			None => Err(LoadError::new(url.to_owned(), EntryNotFound)),
		}
	}
}

impl Loader for BTreeMap<IriBuf, RemoteDocument> {
	async fn load(&self, url: &Iri) -> LoadingResult<IriBuf> {
		match self.get(url) {
			Some(document) => Ok(document.clone()),
			None => Err(LoadError::new(url.to_owned(), EntryNotFound)),
		}
	}
}