diff options
Diffstat (limited to 'src/handlers/mod.rs')
| -rw-r--r-- | src/handlers/mod.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs new file mode 100644 index 0000000..800b61e --- /dev/null +++ b/src/handlers/mod.rs @@ -0,0 +1,38 @@ +use mlua::{FromLua, Value}; + +use crate::{handlers::staticfile::StaticFile, request::Request, response::Response}; + +mod staticfile; + +pub(super) trait Handle<T> { + async fn handle(&self, request: Request<T>) -> Result<Response, Error>; +} + +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error("unsupported method")] + Unsupported, + + #[error("static file handler error: {0}")] + StaticFile(#[from] staticfile::Error), +} + +#[derive(Debug, Clone)] +pub enum Handlers { + StaticFile(StaticFile), +} + +impl FromLua for Handlers { + fn from_lua(value: Value, lua: &mlua::Lua) -> mlua::Result<Self> { + match value { + Value::Table(table) => match table.get::<String>("handler")?.as_str() { + "staticfile" => Ok(Self::StaticFile(StaticFile::from_lua( + Value::Table(table.clone()), + lua, + )?)), + _ => Err(mlua::Error::runtime("unknown handler")), + }, + _ => Err(mlua::Error::runtime("expected table")), + } + } +} |
