Skip to content

Commit

Permalink
feat: add better error handling for __index metamethod
Browse files Browse the repository at this point in the history
  • Loading branch information
vhyrro committed Sep 15, 2023
1 parent ff3f32d commit 1cc2d41
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mlua::{AnyUserData, ExternalError, Lua, LuaSerdeExt, Result};
use mlua::{ExternalError, Lua, LuaSerdeExt, Result};
use std::cell::RefCell;
use std::rc::Rc;
use toml_edit::Document;
Expand All @@ -10,7 +10,6 @@ where
'lua: 'static,
{
let table = lua.create_table()?;
table.set("__entry", AnyUserData::wrap(Rc::clone(&document)))?;
table.set("__path", Vec::<String>::new())?;

let metatable = lua.create_table()?;
Expand All @@ -25,16 +24,30 @@ where

// TODO: Don't error on an invalid path
let binding = index_document_copy.borrow();
let entry = path
.clone()
.into_iter()
.fold(binding.as_item(), |entry, next_key| {
let entry = path.clone().into_iter().try_fold(
binding.as_item(),
|entry, next_key: String| {
entry
.as_table()
.expect("Unwrapping to table failed")
.ok_or_else(|| {
mlua::Error::RuntimeError(
"attempt to index '".to_string()
+ next_key.as_str()
+ "' (a "
+ entry.type_name()
+ " value)",
)
})?
.get(next_key.as_str())
.unwrap()
});
.ok_or_else(|| {
mlua::Error::RuntimeError(
"attempt to index '".to_string()
+ next_key.as_str()
+ "' (a nil value)",
)
})
},
)?;

match entry {
toml_edit::Item::Table(_) => {
Expand Down Expand Up @@ -76,6 +89,7 @@ where
.get_mut(next_key.as_str())
.unwrap()
});

*entry = match value {
mlua::Value::Nil => toml_edit::Item::None,
mlua::Value::String(str) => toml_edit::value(str.to_str()?.to_string()),
Expand Down

0 comments on commit 1cc2d41

Please sign in to comment.