Skip to main content

FileModuleResolver

Struct FileModuleResolver 

Source
pub struct FileModuleResolver { /* private fields */ }
Available on crate feature rhai only.
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() -> FileModuleResolver

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>) -> FileModuleResolver

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<SmartString<LazyCompact>>, ) -> FileModuleResolver

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<SmartString<LazyCompact>>, ) -> FileModuleResolver

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 FileModuleResolver

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<SmartString<LazyCompact>>, ) -> &mut FileModuleResolver

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 FileModuleResolver

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 FileModuleResolver

Empty the internal cache.

Source

pub fn clear_cache_for_path( &mut self, path: impl AsRef<Path>, ) -> Option<Arc<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<(), Error>

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

impl Default for FileModuleResolver

Source§

fn default() -> FileModuleResolver

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<Arc<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<Arc<Module>, Box<EvalAltResult>>

Resolve a module based on a path string.

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ServiceExt for T

Source§

fn map_response_body<F>(self, f: F) -> MapResponseBody<Self, F>
where Self: Sized,

Available on crate feature map-response-body only.
Apply a transformation to the response body. Read more
Source§

fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>
where Self: Sized,

Available on crate feature trace only.
High level tracing that classifies responses using HTTP status codes. Read more
Source§

fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>
where Self: Sized,

Available on crate feature trace only.
High level tracing that classifies responses using gRPC headers. Read more
Source§

fn follow_redirects(self) -> FollowRedirect<Self>
where Self: Sized,

Available on crate feature follow-redirect only.
Follow redirect resposes using the Standard policy. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more