ic_asset_certification/
asset_map.rs1use crate::{AssetEncoding, CertifiedAssetResponse, RequestKey};
2use ic_http_certification::HttpResponse;
3use std::collections::{hash_map::Iter, HashMap};
4
5pub trait AssetMap<'content> {
7 fn get(
22 &self,
23 path: impl Into<String>,
24 encoding: Option<AssetEncoding>,
25 starting_range: Option<usize>,
26 ) -> Option<&HttpResponse<'content>>;
27
28 fn len(&self) -> usize;
30
31 fn is_empty(&self) -> bool {
33 self.len() == 0
34 }
35
36 fn iter(&'content self) -> AssetMapIterator<'content>;
38}
39
40impl<'content> AssetMap<'content> for HashMap<RequestKey, CertifiedAssetResponse<'content>> {
41 fn get(
42 &self,
43 path: impl Into<String>,
44 encoding: Option<AssetEncoding>,
45 range_begin: Option<usize>,
46 ) -> Option<&HttpResponse<'content>> {
47 let req_key = RequestKey::new(path, encoding.map(|e| e.to_string()), range_begin);
48
49 self.get(&req_key).map(|e| &e.response)
50 }
51
52 fn len(&self) -> usize {
53 self.len()
54 }
55
56 fn iter(&'content self) -> AssetMapIterator<'content> {
57 AssetMapIterator { inner: self.iter() }
58 }
59}
60
61#[derive(Debug)]
63pub struct AssetMapIterator<'content> {
64 inner: Iter<'content, RequestKey, CertifiedAssetResponse<'content>>,
65}
66
67impl<'content> Iterator for AssetMapIterator<'content> {
68 type Item = (
69 (&'content str, Option<&'content str>, Option<usize>),
70 &'content HttpResponse<'content>,
71 );
72
73 fn next(&mut self) -> Option<Self::Item> {
74 self.inner.next().map(|(key, asset)| {
75 (
76 (key.path.as_str(), key.encoding.as_deref(), key.range_begin),
77 &asset.response,
78 )
79 })
80 }
81}