//! The editor bundle's content-hashed file names, read from `assets/editor.manifest`. //! //! The build tool writes `editor-<16 hex>.js` and `editor-<16 hex>.wasm` into `assets/` //! and a manifest with two lines, `js=` and `wasm=`. /// The names of the current editor files, as served at the site root. #[derive(Debug, Clone, PartialEq, Eq)] pub struct EditorAssets { pub js: String, pub wasm: String, } /// The manifest, relative to the working directory like `public_dir`. const MANIFEST: &str = "assets/editor.manifest"; impl EditorAssets { /// Reads the manifest. Returns `None` when it is missing or malformed, logging why. pub fn load() -> Option { let text = match std::fs::read_to_string(MANIFEST) { Ok(text) => text, Err(err) => { eprintln!("editor assets: cannot read {MANIFEST}: {err}; editor not preloaded"); return None; } }; let mut js = None; let mut wasm = None; for line in text.lines() { match line.split_once('=') { Some(("js", name)) => js = Some(name.trim().to_owned()), Some(("wasm", name)) => wasm = Some(name.trim().to_owned()), _ => {} } } match (js, wasm) { (Some(js), Some(wasm)) if content_type(&js) == Some("text/javascript") && content_type(&wasm) == Some("application/wasm") => { Some(EditorAssets { js, wasm }) } _ => { eprintln!("editor assets: {MANIFEST} must name a hashed js and wasm file; editor not preloaded"); None } } } } /// The content type for a content-hashed editor file name (`editor-<16 hex>.js` or /// `editor-<16 hex>.wasm`), or `None` for any other name. pub fn content_type(name: &str) -> Option<&'static str> { let (stem, ext) = name.rsplit_once('.')?; let hash = stem.strip_prefix("editor-")?; if hash.len() != 16 || !hash.bytes().all(|b| b.is_ascii_hexdigit()) { return None; } match ext { "js" => Some("text/javascript"), "wasm" => Some("application/wasm"), _ => None, } }