pub trait ModuleLoader: Debug {
// Required method
fn load(
&mut self,
referrer: &Module,
spec: &str,
ctx: &Context,
) -> Result<Module>;
// Provided methods
fn insert(&mut self, name: String, module: Module) { ... }
fn get(&self, name: &str) -> Option<Module> { ... }
fn keys(&self) -> Vec<String> { ... }
fn resolve_referrer(&self, referrer: &Module, spec: &str) -> Result<PathBuf> { ... }
}Expand description
Trait that defines the interface for a module loader.
Required Methods§
Provided Methods§
Sourcefn insert(&mut self, name: String, module: Module)
fn insert(&mut self, name: String, module: Module)
Insert a module into the loader’s cache if loader handles caching.
This function is a no-op for loaders that do not cache modules.
§Arguments
name- The name of the module to insert into the cache.module- The module to insert into the cache.
Sourcefn get(&self, name: &str) -> Option<Module>
fn get(&self, name: &str) -> Option<Module>
Get a module from the cache if the loader caches modules.
This function returns None for loaders that do not cache modules.
§Arguments
name- The name of the module to get from the cache.
Sourcefn keys(&self) -> Vec<String>
fn keys(&self) -> Vec<String>
Returns all the keys in the cache.
This function returns an empty vector for loaders that do not cache modules.
§Returns
A vector of strings representing the keys in the cache.
Sourcefn resolve_referrer(&self, referrer: &Module, spec: &str) -> Result<PathBuf>
fn resolve_referrer(&self, referrer: &Module, spec: &str) -> Result<PathBuf>
Resolves the path of a referenced module based on the referrer module’s path and the provided specification.
§Arguments
referrer- A reference to theModulethat provides the context for resolving the path.spec- A string slice that represents the specification of the path to resolve.
§Returns
A Result<PathBuf>, where the Ok variant contains the resolved path, and the Err variant
contains an error if the operation fails (e.g., if the referrer path has no parent).
§Panics
This function will panic if the referrer module’s path has no parent directory.