use crate::support::W;
use crate::{Error, Result};
use mlua::{IntoLua, Lua, Value};
pub fn process_lua_eval_result(_lua: &Lua, res: mlua::Result<Value>, script: &str) -> Result<Value> {
let res = match res {
Ok(res) => res,
Err(err) => return Err(Error::from_error_with_script(&err, script)),
};
let res = match res {
Value::Error(err) => {
return Err(Error::from_error_with_script(&err, script));
}
res => res,
};
Ok(res)
}
pub fn to_vec_of_strings(value: Value, err_prefix: &'static str) -> mlua::Result<Vec<String>> {
match value {
Value::String(lua_string) => {
let string_value = lua_string.to_str()?.to_string();
Ok(vec![string_value])
}
Value::Table(lua_table) => {
let mut result = Vec::new();
for pair in lua_table.sequence_values::<String>() {
match pair {
Ok(s) => result.push(s),
Err(_) => {
return Err(mlua::Error::FromLuaConversionError {
from: "table",
to: "Vec<String>".to_string(),
message: Some(format!("{err_prefix} - Table contains non-string values")),
});
}
}
}
Ok(result)
}
_ => Err(mlua::Error::FromLuaConversionError {
from: "unknown",
to: "Vec<String>".to_string(),
message: Some(format!("{err_prefix} - Expected a string or a list of strings")),
}),
}
}
pub fn get_value_prop_as_string(
value: Option<&mlua::Value>,
prop_name: &str,
err_prefix: &str,
) -> mlua::Result<Option<String>> {
let Some(value) = value else { return Ok(None) };
let table = value.as_table().ok_or_else(|| {
crate::Error::custom(format!(
"{err_prefix} - value should be of type lua table, but was of another type."
))
})?;
match table.get::<Option<Value>>(prop_name)? {
Some(Value::String(string)) => {
Ok(Some(string.to_string_lossy()))
}
Some(_other) => {
Err(crate::Error::custom("aip.file... options.base_dir must be of type string is present").into())
}
None => Ok(None),
}
}
impl IntoLua for W<&String> {
fn into_lua(self, lua: &mlua::Lua) -> mlua::Result<Value> {
Ok(Value::String(lua.create_string(self.0)?))
}
}
impl IntoLua for W<String> {
fn into_lua(self, lua: &mlua::Lua) -> mlua::Result<Value> {
Ok(Value::String(lua.create_string(&self.0)?))
}
}