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 function | Conversion |
|---|---|
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
impl AssetResolver
Sourcepub fn new(root: impl Into<PathBuf>) -> Result<Self, InitError>
pub fn new(root: impl Into<PathBuf>) -> Result<Self, InitError>
Build an AssetResolver backed by the real filesystem.
Sourcepub fn with_sandbox(sandbox: impl SandboxedFs + 'static) -> Self
pub fn with_sandbox(sandbox: impl SandboxedFs + 'static) -> Self
Inject an arbitrary SandboxedFs implementation.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for AssetResolver
impl !RefUnwindSafe for AssetResolver
impl Send for AssetResolver
impl Sync for AssetResolver
impl Unpin for AssetResolver
impl UnsafeUnpin for AssetResolver
impl !UnwindSafe for AssetResolver
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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