use crate::error::{HandlerError, Result};
use crate::types::{HandlerContext, HandlerResult};
#[cfg(feature = "kotobas")]
pub mod kotobas_integration {
use super::*;
use kotoba_kotobas::http_parser::{HttpConfig, HttpRouteConfig, HttpMethod};
pub struct KotobasHttpHandler {
config: HttpConfig,
}
impl KotobasHttpHandler {
pub fn new(config_content: &str) -> Result<Self> {
let config: HttpConfig = serde_json::from_str(config_content)
.map_err(|e| HandlerError::Parse(format!("Failed to parse HTTP config: {}", e)))?;
Ok(Self { config })
}
pub fn find_route(&self, method: &str, path: &str) -> Option<&HttpRouteConfig> {
let request_method = match method {
"GET" => HttpMethod::GET,
"POST" => HttpMethod::POST,
"PUT" => HttpMethod::PUT,
"DELETE" => HttpMethod::DELETE,
"PATCH" => HttpMethod::PATCH,
"OPTIONS" => HttpMethod::OPTIONS,
"HEAD" => HttpMethod::HEAD,
_ => return None,
};
self.config.routes.iter()
.find(|route| route.method == request_method && route.path == path)
}
pub fn get_middleware(&self, route: &HttpRouteConfig) -> Vec<String> {
route.middleware.clone()
}
}
}
#[cfg(feature = "kotobas")]
pub struct IntegratedHandler {
kotobas_handler: kotobas_integration::KotobasHttpHandler,
}
#[cfg(feature = "kotobas")]
impl IntegratedHandler {
pub fn new(kotobas_config: &str) -> Result<Self> {
Ok(Self {
kotobas_handler: kotobas_integration::KotobasHttpHandler::new(kotobas_config)?,
})
}
pub async fn process_request(&mut self, context: HandlerContext, _content: Option<&str>) -> Result<String> {
if let Some(route) = self.kotobas_handler.find_route(&context.method, &context.path) {
let middleware = self.kotobas_handler.get_middleware(route);
Ok(format!(
"Route matched: {} {} -> Handler: {}",
route.method.as_ref(),
route.path,
route.handler
))
} else {
Ok("No route matched".to_string())
}
}
}
#[cfg(feature = "kotobas")]
pub fn create_handler(content: &str, _context: &HandlerContext) -> Result<Box<dyn HandlerTrait>> {
if serde_json::from_str::<serde_json::Value>(content).is_ok() {
let handler = kotobas_integration::KotobasHttpHandler::new(content)?;
Ok(Box::new(KotobasWrapper(handler)))
} else {
Err(HandlerError::Parse("Unsupported content format - only JSON/Kotobas supported".to_string()))
}
}
pub trait HandlerTrait {
fn process(&mut self, context: HandlerContext) -> Result<String>;
}
#[cfg(feature = "kotobas")]
struct KotobasWrapper(kotobas_integration::KotobasHttpHandler);
#[cfg(feature = "kotobas")]
impl HandlerTrait for KotobasWrapper {
fn process(&mut self, context: HandlerContext) -> Result<String> {
match self.0.find_route(&context.method, &context.path) {
Some(route) => Ok(format!(
"Kotobas route: {} {} -> {}",
route.method.as_ref(),
route.path,
route.handler
)),
None => Ok("No Kotobas route matched".to_string()),
}
}
}