Skip to content

Commit

Permalink
refactor!: do not error when a key is not found, simply return nil
Browse files Browse the repository at this point in the history
  • Loading branch information
vhyrro committed Sep 3, 2023
1 parent 3952fff commit 4214ec6
Showing 1 changed file with 34 additions and 33 deletions.
67 changes: 34 additions & 33 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,47 +28,48 @@ where
item.as_table_like()
.unwrap()
.get(payload.1.to_str()?)
.expect("Key not found!")
.to_owned()
.map(ToOwned::to_owned)
} else {
toml_edit::Item::Value(
item.as_array()
.unwrap()
// Subtract one to account for one-based indexing.
.get(payload.1.to_str()?.parse::<usize>().unwrap() - 1)
.expect("Index out of bounds!")
.to_owned(),
)
item.as_array()
.unwrap()
// Subtract one to account for one-based indexing.
.get(payload.1.to_str()?.parse::<usize>().unwrap() - 1)
.map(ToOwned::to_owned)
.map(toml_edit::Item::Value)
};

match item {
toml_edit::Item::None => Ok(mlua::Value::Nil),
toml_edit::Item::Value(ref val) => match val {
toml_edit::Value::String(str) => lua.to_value(str.value()),
toml_edit::Value::Integer(int) => lua.to_value(int.value()),
toml_edit::Value::Float(float) => lua.to_value(float.value()),
toml_edit::Value::Boolean(bool) => lua.to_value(bool.value()),
toml_edit::Value::Array(_) => {
let ret = lua.create_table()?;
ret.set("__entry", AnyUserData::wrap(item))?;
ret.set_metatable(Some(mt_clone.clone()));
Ok(mlua::Value::Table(ret))
}
toml_edit::Value::InlineTable(_) => {
if let Some(value) = item {
match value {
toml_edit::Item::None => Ok(mlua::Value::Nil),
toml_edit::Item::Value(ref val) => match val {
toml_edit::Value::String(str) => lua.to_value(str.value()),
toml_edit::Value::Integer(int) => lua.to_value(int.value()),
toml_edit::Value::Float(float) => lua.to_value(float.value()),
toml_edit::Value::Boolean(bool) => lua.to_value(bool.value()),
toml_edit::Value::Array(_) => {
let ret = lua.create_table()?;
ret.set("__entry", AnyUserData::wrap(value))?;
ret.set_metatable(Some(mt_clone.clone()));
Ok(mlua::Value::Table(ret))
}
toml_edit::Value::InlineTable(_) => {
let ret = lua.create_table()?;
ret.set("__entry", AnyUserData::wrap(value))?;
ret.set_metatable(Some(mt_clone.clone()));
Ok(mlua::Value::Table(ret))
}
_ => unimplemented!(),
},
toml_edit::Item::Table(_) => {
let ret = lua.create_table()?;
ret.set("__entry", AnyUserData::wrap(item))?;
ret.set("__entry", AnyUserData::wrap(value))?;
ret.set_metatable(Some(mt_clone.clone()));
Ok(mlua::Value::Table(ret))
}
_ => unimplemented!(),
},
toml_edit::Item::Table(_) => {
let ret = lua.create_table()?;
ret.set("__entry", AnyUserData::wrap(item))?;
ret.set_metatable(Some(mt_clone.clone()));
Ok(mlua::Value::Table(ret))
toml_edit::Item::ArrayOfTables(_) => unimplemented!(),
}
toml_edit::Item::ArrayOfTables(_) => unimplemented!(),
} else {
Ok(mlua::Value::Nil)
}
})?,
)?;
Expand Down

0 comments on commit 4214ec6

Please sign in to comment.