#![warn(clippy::pedantic)]
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
pub type ViteManifest<S = String> = HashMap<S, ManifestChunk<S>>;
pub type DynamicManifestChunk = ManifestChunk<String, Vec<String>, Vec<u8>>;
pub type StaticManifestChunk = ManifestChunk<&'static str, &'static [&'static str], &'static [u8]>;
#[derive(Serialize, Deserialize, Clone, Copy, Default, Debug, PartialEq, Eq, Hash)]
#[serde(rename_all = "camelCase")]
pub struct ManifestChunk<S = String, Vs = Vec<S>, Vu8 = Vec<u8>> {
#[serde(skip)]
pub key: S,
#[serde(skip)]
pub contents: Vu8,
#[serde(skip)]
pub hash: S,
#[serde(skip)]
pub mime_type: S,
pub src: Option<S>,
pub file: S,
#[serde(default)]
pub css: Vs,
#[serde(default)]
pub assets: Vs,
#[serde(default)]
pub is_entry: bool,
pub name: Option<S>,
#[serde(default)]
pub is_dynamic_entry: bool,
#[serde(default)]
pub imports: Vs,
#[serde(default)]
pub dynamic_imports: Vs,
}
pub trait Manifest<'a> {
fn manifest_key_output_pairs(&self) -> &'a [(&'a str, &'a str)];
fn manifest_chunks(&self) -> &'a [(&'a str, &'a StaticManifestChunk)];
fn boxed(&self) -> Box<dyn Manifest<'a>>;
fn resolve_output(&self, key: &str) -> Option<&'a str> {
let pairs = self.manifest_key_output_pairs();
match pairs.binary_search_by_key(&key, |&(this_key, _)| this_key) {
Ok(index) => Some(pairs[index].1),
Err(_) => None,
}
}
fn chunk(&self, output: &str) -> Option<&'a StaticManifestChunk> {
let chunks = self.manifest_chunks();
match chunks.binary_search_by_key(&output, |&(this_output, _)| this_output) {
Ok(index) => Some(chunks[index].1),
Err(_) => None,
}
}
fn chunk_by_key(&self, output: &str) -> Option<&'a StaticManifestChunk> {
match self.resolve_output(output) {
Some(output) => self.chunk(output),
None => None,
}
}
}