use eyre::Result;
use tiny_http::{Request, ResponseBox};
pub enum HttpHandlerResult {
Response(ResponseBox),
Error(String),
NotHandled,
}
impl From<Result<ResponseBox>> for HttpHandlerResult {
fn from(r: Result<ResponseBox>) -> Self {
match r {
Ok(response) => HttpHandlerResult::Response(response),
Err(e) => HttpHandlerResult::Error(e.to_string()),
}
}
}
pub trait HttpHandler: Send {
fn handle_request(&self, request: &mut Request) -> HttpHandlerResult;
}
#[cfg(test)]
mod tests {
use tiny_http::ResponseBox;
use super::HttpHandlerResult;
impl HttpHandlerResult {
pub fn expect(self, m: &'static str) -> ResponseBox {
match self {
HttpHandlerResult::Response(response) => response,
HttpHandlerResult::Error(e) => panic!("{}: HttpHandlerResult::Error({})", m, e),
HttpHandlerResult::NotHandled => panic!("{}: HttpHandlerResult::Nothandled", m),
}
}
}
}