use std::future::Future;
use bytes::Bytes;
use crate::handler::HandlerError;
pub use crate::layer::ErasedCall;
use crate::layer::{LayerFn, Next, ServiceBody};
use crate::service::{HandlerService, Operation, OperationContract};
pub fn unary(local_name: &str, contract: OperationContract, call: ErasedCall) -> HandlerService {
service(local_name, Operation::Unary, contract, call)
}
pub fn streaming(
local_name: &str,
contract: OperationContract,
call: ErasedCall,
) -> HandlerService {
service(local_name, Operation::Streaming, contract, call)
}
fn service(
local_name: &str,
operation: Operation,
contract: OperationContract,
call: ErasedCall,
) -> HandlerService {
HandlerService::declare(local_name, None, None, operation, contract, move |_| {
Ok(call)
})
}
pub fn layer<F, Fut>(call: F) -> LayerFn<F>
where
F: Fn(http::Request<Bytes>, Next) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<http::Response<ServiceBody>, HandlerError>> + Send + 'static,
{
crate::layer::layer_fn(call)
}
pub fn peer_layer<F, Fut>(call: F) -> crate::peer::PeerLayerFn<F>
where
F: Fn(crate::peer::PeerRequest, crate::peer::PeerNext) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<crate::peer::PeerRequest, HandlerError>> + Send + 'static,
{
crate::peer::peer_layer_fn(call)
}