rquickjs_extension/loader/
loader.rs1use std::collections::HashMap;
2
3use rquickjs::{loader::Loader, Ctx, Error, Module, Result};
4
5use super::ModuleLoadFn;
6
7pub struct ModuleLoader {
10 modules: HashMap<&'static str, ModuleLoadFn>,
11}
12
13impl ModuleLoader {
14 pub(crate) fn new(modules: HashMap<&'static str, ModuleLoadFn>) -> Self {
15 Self { modules }
16 }
17}
18
19impl Loader for ModuleLoader {
20 fn load<'js>(&mut self, ctx: &Ctx<'js>, path: &str) -> Result<Module<'js>> {
21 let load = self
22 .modules
23 .remove(path)
24 .ok_or_else(|| Error::new_loading(path))?;
25
26 (load)(ctx.clone(), Vec::from(path))
27 }
28}