mod ctx;
pub use ctx::{HandlerArgs, HandlerCtx, NotifyError};
mod erased;
pub(crate) use erased::{BoxedIntoRoute, ErasedIntoRoute, MakeErasedHandler};
mod future;
pub use future::RouteFuture;
mod handler;
pub use handler::Handler;
pub(crate) use handler::HandlerInternal;
mod method;
pub(crate) use method::Method;
use serde_json::value::RawValue;
use std::{
convert::Infallible,
task::{Context, Poll},
};
use tower::{util::BoxCloneSyncService, Service, ServiceExt};
use crate::types::Response;
#[derive(Debug)]
pub(crate) struct Route(tower::util::BoxCloneSyncService<HandlerArgs, Box<RawValue>, Infallible>);
impl Route {
pub(crate) fn new<S>(inner: S) -> Self
where
S: Service<HandlerArgs, Response = Box<RawValue>, Error = Infallible>
+ Clone
+ Send
+ Sync
+ 'static,
S::Future: Send + 'static,
{
Self(BoxCloneSyncService::new(inner))
}
pub(crate) fn default_fallback() -> Self {
Self::new(tower::service_fn(|args: HandlerArgs| async {
let HandlerArgs { req, .. } = args;
let id = req.id_owned();
drop(req);
Ok(Response::method_not_found(id))
}))
}
pub(crate) fn oneshot_inner(&mut self, args: HandlerArgs) -> RouteFuture {
RouteFuture::new(self.0.clone().oneshot(args))
}
pub(crate) fn oneshot_inner_owned(self, args: HandlerArgs) -> RouteFuture {
RouteFuture::new(self.0.oneshot(args))
}
}
impl Clone for Route {
#[track_caller]
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl From<BoxCloneSyncService<HandlerArgs, Box<RawValue>, Infallible>> for Route {
fn from(inner: BoxCloneSyncService<HandlerArgs, Box<RawValue>, Infallible>) -> Self {
Self(inner)
}
}
impl Service<HandlerArgs> for Route {
type Response = Box<RawValue>;
type Error = Infallible;
type Future = RouteFuture;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, args: HandlerArgs) -> Self::Future {
self.oneshot_inner(args)
}
}