mod lua; mod staticfile; use mlua::{FromLua, Value}; use crate::{ handler::{lua::LuaResponse, staticfile::StaticFile}, request::Request, response::Response, }; pub trait Handle { fn handle(self, request: Request) -> impl Future>; } #[derive(Debug, thiserror::Error)] pub enum Error { #[error("unsupported method")] Unsupported, #[error("static file handler error: {0}")] StaticFile(#[from] staticfile::Error), } #[derive(Debug)] pub enum Handler { StaticFile(StaticFile), Lua(LuaResponse), } impl FromLua for Handler { fn from_lua(value: Value, lua: &mlua::Lua) -> mlua::Result { match value { Value::Table(table) => match table.get::("handler")?.as_str() { "staticfile" => Ok(Self::StaticFile(StaticFile::from_lua( Value::Table(table.clone()), lua, )?)), "lua" => Ok(Self::Lua(LuaResponse::from_lua( Value::Table(table.clone()), lua, )?)), _ => Err(mlua::Error::runtime("unknown handler")), }, _ => Err(mlua::Error::runtime("expected table")), } } }