Skip to main content

AssetResolver

Struct AssetResolver 

Source
pub struct AssetResolver { /* private fields */ }
Expand description

Resolver that registers parsers by extension and auto-converts non-Lua resources.

Parsers are registered per extension via .parser(). For unregistered extensions, no I/O is performed and None is returned.

Filenames are treated literally (no dot-to-path conversion).

§Built-in parsers

Factory functionConversion
json_parser()Parse with serde_json -> Lua Table
text_parser()Return as-is as Lua String

§Examples

use mlua_pkg::resolvers::{AssetResolver, json_parser, text_parser};

let resolver = AssetResolver::new("./assets")?
    .parser("json", json_parser())
    .parser("sql", text_parser())
    .parser("css", text_parser());

Custom parsers can also be registered as closures:

use mlua_pkg::resolvers::{AssetResolver, json_parser};

let resolver = AssetResolver::new("./assets")?
    .parser("json", json_parser())
    .parser("csv", |lua, content| {
        // Split by lines and convert to a Lua table
        let t = lua.create_table()?;
        for (i, line) in content.lines().enumerate() {
            t.set(i + 1, lua.create_string(line)?)?;
        }
        Ok(mlua::Value::Table(t))
    });

I/O goes through the SandboxedFs trait. Use with_sandbox to inject test mocks or alternative backends.

§Design decision: why extension keys are String

Parser registration uses HashMap<String, BoxFn>. String keys are chosen over enums to prioritize extensibility (Open/Closed), allowing users to freely register custom parsers for any extension.

Impact of a typo: parsers.get(ext) returns None -> safely falls through to the next Resolver. No panic/UB occurs. Setup code is small, so typos surface immediately in tests.

§Errors

new() returns InitError::RootNotFound if the root does not exist.

Implementations§

Source§

impl AssetResolver

Source

pub fn new(root: impl Into<PathBuf>) -> Result<Self, InitError>

Build an AssetResolver backed by the real filesystem.

Source

pub fn with_sandbox(sandbox: impl SandboxedFs + 'static) -> Self

Inject an arbitrary SandboxedFs implementation.

Source

pub fn parser( self, ext: impl Into<String>, f: impl Fn(&Lua, &str) -> Result<Value> + Send + Sync + 'static, ) -> Self

Register a parser for an extension. Duplicate extensions are overwritten.

Trait Implementations§

Source§

impl Resolver for AssetResolver

Source§

fn resolve(&self, lua: &Lua, name: &str) -> Option<Result<Value>>

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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 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<T> MaybeSend for T