summaryrefslogtreecommitdiff
path: root/src/handlers/mod.rs
blob: 800b61ef5b14fb744ee5697fd5cb6421f197dbab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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")),
        }
    }
}