use std::collections::HashMap;
use percent_encoding::{AsciiSet, CONTROLS, PercentEncode, utf8_percent_encode};
use crate::{AssetBundle, AssetId, Manifest};
#[derive(Debug, Clone)]
pub struct BundledAsset {
file: String,
content_type: String,
}
impl BundledAsset {
#[must_use]
pub fn name(&self) -> &str {
&self.file
}
pub(crate) fn encoded_name(&self) -> PercentEncode<'_> {
utf8_percent_encode(self.name(), FILENAME)
}
#[must_use]
pub fn content_type(&self) -> &str {
&self.content_type
}
}
const FILENAME: &AsciiSet = &CONTROLS
.add(b' ')
.add(b'"')
.add(b'#')
.add(b'%')
.add(b'(')
.add(b')')
.add(b'/')
.add(b'<')
.add(b'>')
.add(b'?')
.add(b'`')
.add(b'{')
.add(b'}')
.add(b'\\');
#[derive(Debug, Default, Clone)]
pub struct AssetCatalog {
bundled_assets: HashMap<AssetId, BundledAsset>,
}
impl AssetCatalog {
#[must_use]
pub fn get(&self, id: AssetId) -> Option<&BundledAsset> {
self.bundled_assets.get(&id)
}
pub fn assets(&self) -> impl Iterator<Item = &BundledAsset> {
self.bundled_assets.values()
}
}
impl From<AssetBundle> for AssetCatalog {
fn from(bundle: AssetBundle) -> Self {
bundle.catalog
}
}
impl From<Manifest> for AssetCatalog {
fn from(manifest: Manifest) -> Self {
Self {
bundled_assets: manifest
.assets
.into_iter()
.map(|entry| {
(
entry.id,
BundledAsset {
file: entry.file,
content_type: entry.content_type,
},
)
})
.collect(),
}
}
}