pub struct FileModuleResolver { /* private fields */ }
Expand description

A module resolution service that loads module script files from the file system.

Caching

Resolved Modules are cached internally so script files are not reloaded and recompiled for subsequent requests.

Use clear_cache or clear_cache_for_path to clear the internal cache.

Namespace

When a function within a script file module is called, all functions defined within the same script are available, evan private ones. In other words, functions defined in a module script can always cross-call each other.

Example

use rhai::Engine;
use rhai::module_resolvers::FileModuleResolver;

// Create a new 'FileModuleResolver' loading scripts from the 'scripts' subdirectory
// with file extension '.x'.
let resolver = FileModuleResolver::new_with_path_and_extension("./scripts", "x");

let mut engine = Engine::new();

engine.set_module_resolver(resolver);

Implementations§

source§

impl FileModuleResolver

source

pub fn new() -> Self

Create a new FileModuleResolver with the current directory as base path.

The default extension is .rhai.

Example
use rhai::Engine;
use rhai::module_resolvers::FileModuleResolver;

// Create a new 'FileModuleResolver' loading scripts from the current directory
// with file extension '.rhai' (the default).
let resolver = FileModuleResolver::new();

let mut engine = Engine::new();
engine.set_module_resolver(resolver);
source

pub fn new_with_path(path: impl Into<PathBuf>) -> Self

Create a new FileModuleResolver with a specific base path.

The default extension is .rhai.

Example
use rhai::Engine;
use rhai::module_resolvers::FileModuleResolver;

// Create a new 'FileModuleResolver' loading scripts from the 'scripts' subdirectory
// with file extension '.rhai' (the default).
let resolver = FileModuleResolver::new_with_path("./scripts");

let mut engine = Engine::new();
engine.set_module_resolver(resolver);
source

pub fn new_with_extension(extension: impl Into<Identifier>) -> Self

Create a new FileModuleResolver with a file extension.

Example
use rhai::Engine;
use rhai::module_resolvers::FileModuleResolver;

// Create a new 'FileModuleResolver' loading scripts with file extension '.rhai' (the default).
let resolver = FileModuleResolver::new_with_extension("rhai");

let mut engine = Engine::new();
engine.set_module_resolver(resolver);
source

pub fn new_with_path_and_extension( path: impl Into<PathBuf>, extension: impl Into<Identifier> ) -> Self

Create a new FileModuleResolver with a specific base path and file extension.

Example
use rhai::Engine;
use rhai::module_resolvers::FileModuleResolver;

// Create a new 'FileModuleResolver' loading scripts from the 'scripts' subdirectory
// with file extension '.x'.
let resolver = FileModuleResolver::new_with_path_and_extension("./scripts", "x");

let mut engine = Engine::new();
engine.set_module_resolver(resolver);
source

pub fn base_path(&self) -> Option<&Path>

Get the base path for script files.

source

pub fn set_base_path(&mut self, path: impl Into<PathBuf>) -> &mut Self

Set the base path for script files.

source

pub fn extension(&self) -> &str

Get the script file extension.

source

pub fn set_extension(&mut self, extension: impl Into<Identifier>) -> &mut Self

Set the script file extension.

source

pub const fn scope(&self) -> &Scope<'_>

Get a reference to the file module resolver’s scope.

The scope is used for compiling module scripts.

source

pub fn set_scope(&mut self, scope: Scope<'static>)

Set the file module resolver’s scope.

The scope is used for compiling module scripts.

source

pub fn scope_mut(&mut self) -> &mut Scope<'static>

Get a mutable reference to the file module resolver’s scope.

The scope is used for compiling module scripts.

source

pub fn enable_cache(&mut self, enable: bool) -> &mut Self

Enable/disable the cache.

source

pub const fn is_cache_enabled(&self) -> bool

Is the cache enabled?

source

pub fn is_cached(&self, path: impl AsRef<Path>) -> bool

Is a particular path cached?

source

pub fn clear_cache(&mut self) -> &mut Self

Empty the internal cache.

source

pub fn clear_cache_for_path( &mut self, path: impl AsRef<Path> ) -> Option<Shared<Module>>

Remove the specified path from internal cache.

The next time this path is resolved, the script file will be loaded once again.

source

pub fn get_file_path(&self, path: &str, source_path: Option<&Path>) -> PathBuf

Construct a full file path.

Trait Implementations§

source§

impl Debug for FileModuleResolver

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for FileModuleResolver

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl ModuleResolver for FileModuleResolver

source§

fn resolve_ast( &self, engine: &Engine, source_path: Option<&str>, path: &str, pos: Position ) -> Option<Result<AST, Box<EvalAltResult>>>

Resolve an AST based on a path string.

The file system is accessed during each call; the internal cache is by-passed.

source§

fn resolve_raw( &self, engine: &Engine, global: &mut GlobalRuntimeState, scope: &mut Scope<'_>, path: &str, pos: Position ) -> Result<Shared<Module>, Box<EvalAltResult>>

Resolve a module based on a path string, given a GlobalRuntimeState and the current Scope. Read more
source§

fn resolve( &self, engine: &Engine, source: Option<&str>, path: &str, pos: Position ) -> Result<Shared<Module>, Box<EvalAltResult>>

Resolve a module based on a path string.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.