use crate::{AssetEncoding, CertifiedAssetResponse, RequestKey};
use ic_http_certification::HttpResponse;
use std::collections::{hash_map::Iter, HashMap};
pub trait AssetMap<'content> {
fn get(
&self,
path: impl Into<String>,
encoding: Option<AssetEncoding>,
starting_range: Option<usize>,
) -> Option<&HttpResponse<'content>>;
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
fn iter(&'content self) -> AssetMapIterator<'content>;
}
impl<'content> AssetMap<'content> for HashMap<RequestKey, CertifiedAssetResponse<'content>> {
fn get(
&self,
path: impl Into<String>,
encoding: Option<AssetEncoding>,
range_begin: Option<usize>,
) -> Option<&HttpResponse<'content>> {
let req_key = RequestKey::new(path, encoding.map(|e| e.to_string()), range_begin);
self.get(&req_key).map(|e| &e.response)
}
fn len(&self) -> usize {
self.len()
}
fn iter(&'content self) -> AssetMapIterator<'content> {
AssetMapIterator { inner: self.iter() }
}
}
#[derive(Debug)]
pub struct AssetMapIterator<'content> {
inner: Iter<'content, RequestKey, CertifiedAssetResponse<'content>>,
}
impl<'content> Iterator for AssetMapIterator<'content> {
type Item = (
(&'content str, Option<&'content str>, Option<usize>),
&'content HttpResponse<'content>,
);
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|(key, asset)| {
(
(key.path.as_str(), key.encoding.as_deref(), key.range_begin),
&asset.response,
)
})
}
}