lua: create LuaQueryResult for simplying implementation of queries and filters
This commit is contained in:
parent
8072ec1c7e
commit
cb3c3a601f
|
@ -41,3 +41,99 @@ impl LuaQuery {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum LuaQueryResult {
|
||||
None,
|
||||
AlwaysNone,
|
||||
FilterPass,
|
||||
FilterDeny,
|
||||
Some(mlua::Value),
|
||||
}
|
||||
|
||||
impl mlua::IntoLua for LuaQueryResult {
|
||||
fn into_lua(self, lua: &mlua::Lua) -> mlua::Result<mlua::Value> {
|
||||
let t = lua.create_table()?;
|
||||
t.set("enum_ty", "query_result")?;
|
||||
|
||||
match self {
|
||||
LuaQueryResult::None => {
|
||||
t.set("result", "none")?;
|
||||
},
|
||||
LuaQueryResult::AlwaysNone => {
|
||||
t.set("result", "always_none")?;
|
||||
},
|
||||
LuaQueryResult::FilterPass => {
|
||||
t.set("result", "filter_pass")?;
|
||||
},
|
||||
LuaQueryResult::FilterDeny => {
|
||||
t.set("result", "filter_deny")?;
|
||||
},
|
||||
LuaQueryResult::Some(value) => {
|
||||
t.set("result", "some")?;
|
||||
t.set("val", value)?;
|
||||
},
|
||||
}
|
||||
|
||||
t.into_lua(lua)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn from_lua_error_query_result(ty: &'static str, msg: &str) -> mlua::Error {
|
||||
mlua::Error::FromLuaConversionError {
|
||||
from: ty,
|
||||
to: "QueryResult".into(),
|
||||
message: Some(msg.into()),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn malformed_table_error_query_result(ty: &'static str, missing_field: &str) -> mlua::Error {
|
||||
mlua::Error::FromLuaConversionError {
|
||||
from: ty,
|
||||
to: "QueryResult".into(),
|
||||
message: Some(format!("malformed table, cannot convert, failed to get field '{}'", missing_field)),
|
||||
}
|
||||
}
|
||||
|
||||
impl mlua::FromLua for LuaQueryResult {
|
||||
fn from_lua(value: mlua::Value, _: &mlua::Lua) -> mlua::Result<Self> {
|
||||
let ty = value.type_name();
|
||||
let table = value.as_table()
|
||||
.ok_or(from_lua_error_query_result(ty, "expected Table"))?;
|
||||
|
||||
let var_name: String = table.get("enum_ty")
|
||||
.map_err(|_| malformed_table_error_query_result(ty, "enum_ty"))?;
|
||||
if var_name != "query_result" {
|
||||
return Err(mlua::Error::FromLuaConversionError {
|
||||
from: ty,
|
||||
to: "QueryResult".into(),
|
||||
message: Some(format!("mismatched enum_ty: '{}'", var_name)),
|
||||
});
|
||||
}
|
||||
|
||||
let result: String = table.get("result")
|
||||
.map_err(|_| malformed_table_error_query_result(ty, "result"))?;
|
||||
let result_str = result.as_str();
|
||||
|
||||
match result_str {
|
||||
"none" => Ok(Self::None),
|
||||
"always_none" => Ok(Self::AlwaysNone),
|
||||
"filter_pass" => Ok(Self::FilterPass),
|
||||
"filter_deny" => Ok(Self::FilterDeny),
|
||||
"value" => {
|
||||
let val: mlua::Value = table.get("val")
|
||||
.map_err(|_| malformed_table_error_query_result(ty, "val"))?;
|
||||
Ok(Self::Some(val))
|
||||
},
|
||||
_ => {
|
||||
Err(mlua::Error::FromLuaConversionError {
|
||||
from: ty,
|
||||
to: "QueryResult".into(),
|
||||
message: Some(format!("unknown result type: '{}'", result_str)),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue