Skip to content

Commit

Permalink
fix: make parse_spanned take a table instead of a string
Browse files Browse the repository at this point in the history
  • Loading branch information
vhyrro committed Jun 27, 2024
1 parent 9d0f129 commit fa78930
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ mlua = { version = "0.9.6", features = ["module", "serialize"] }
serde = "1.0"
toml_edit = "0.22.9"
toml = "0.8.12"
itertools = "0.13.0"
20 changes: 12 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use mlua::{ExternalError, Lua, LuaSerdeExt, Result, Value};
use itertools::Itertools;
use mlua::{ExternalError, Lua, LuaSerdeExt, Result, Table, Value};
use std::cell::RefCell;
use std::rc::Rc;
use toml_edit::{DocumentMut, ImDocument, Key};
Expand Down Expand Up @@ -191,21 +192,24 @@ pub fn toml_edit(lua: &'static Lua) -> Result<mlua::Table> {
spanned.set(
"span_of",
lua.create_function(
move |lua: &Lua, (path, selector): (mlua::String, Value)| {
move |lua: &Lua, (path, selector): (Table, Value)| {
let initial_key = Key::new("");
let fullpath = path.clone().sequence_values::<String>().map(|val| val.unwrap()).join(".");

let (key, item) = path.to_str().unwrap().split('.').try_fold(
let (key, item) = path.sequence_values::<String>().try_fold(
(&initial_key, document.as_item()),
|acc: (&Key, &toml_edit::Item), next_path| {
acc.1.as_table().unwrap().get_key_value(next_path).ok_or(
let next_path = next_path?;

acc.1.as_table().unwrap().get_key_value(next_path.as_str()).ok_or(
mlua::Error::BadArgument {
to: Some("span_of".into()),
pos: 1,
name: Some("path".into()),
cause: format!(
"key '{}' does not exist in the TOML. The full path that was provided: {}",
next_path,
path.to_str().unwrap()
fullpath
)
.into_lua_err()
.into(),
Expand Down Expand Up @@ -233,13 +237,13 @@ pub fn toml_edit(lua: &'static Lua) -> Result<mlua::Table> {
name: Some("selector".into()),
cause: format!(
"value {} is not an array (required for numerical selectors)",
path.to_str().unwrap()
fullpath
)
.into_lua_err()
.into(),
}
)?
.get(num as usize)
.get((num - 1) as usize)
.ok_or(
mlua::Error::BadArgument {
to: Some("span_of".into()),
Expand All @@ -249,7 +253,7 @@ pub fn toml_edit(lua: &'static Lua) -> Result<mlua::Table> {
"array '{}' does not have a value at index {}. The full path that was provided: {}",
key,
num,
path.to_str().unwrap()
fullpath
)
.into_lua_err()
.into(),
Expand Down

0 comments on commit fa78930

Please sign in to comment.