//! handler.rs

use crate::IntoResponse;
use hyper::{Body, Request, Response};
use std::future::Future;

/// Handler trait - 用于处理 HTTP 请求
pub trait Handler<Args> {
	type Future: Future<Output = Response<Body>> + Send;
	fn call(&self, args: Args) -> Self::Future;
}

// 为零参数 handler 实现
impl<F, Fut, Res> Handler<()> for F
where
	F: Fn() -> Fut + Send + Sync + 'static,
	Fut: Future<Output = Res> + Send + 'static,
	Res: IntoResponse,
{
	type Future = Pin<Box<dyn Future<Output = Response<Body>> + Send>>;

	fn call(&self, _args: ()) -> Self::Future {
		let fut = (self)();
		Box::pin(async move { fut.await.into_response() })
	}
}

// 为带 Request 参数的 handler 实现
impl<F, Fut, Res> Handler<Request<Body>> for F
where
	F: Fn(Request<Body>) -> Fut + Send + Sync + 'static,
	Fut: Future<Output = Res> + Send + 'static,
	Res: IntoResponse,
{
	type Future = Pin<Box<dyn Future<Output = Response<Body>> + Send>>;

	fn call(&self, args: Request<Body>) -> Self::Future {
		let fut = (self)(args);
		Box::pin(async move { fut.await.into_response() })
	}
}

// 添加需要的导入
use std::pin::Pin;
