#[macro_export]
#[deprecated(
since = "0.4.0",
note = "Use `generate_svc!` instead for type-safe API"
)]
macro_rules! generate {
($prefix:literal, $type: ident) => {
use ::wiremock_grpc::tonic::{
codegen::{http, Body, StdError},
Code,
};
use std::{
net::SocketAddr,
ops::{Deref, DerefMut},
task::Poll,
};
use wiremock_grpc::*;
#[doc = $prefix]
#[doc = stringify!($type)]
#[derive(Clone)]
pub struct $type(pub(crate) GrpcServer);
impl Deref for $type {
type Target = GrpcServer;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for $type {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<B> tonic::codegen::Service<tonic::codegen::http::Request<B>> for $type
where
B: ::wiremock_grpc::http_body::Body + Send + 'static,
B::Error: Into<tonic::codegen::StdError> + Send + 'static,
{
type Response = tonic::codegen::http::Response<tonic::body::Body>;
type Error = std::convert::Infallible;
type Future = tonic::codegen::BoxFuture<Self::Response, Self::Error>;
fn poll_ready(
&mut self,
_cx: &mut std::task::Context<'_>,
) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: tonic::codegen::http::Request<B>) -> Self::Future {
self.0.handle_request(req)
}
}
impl tonic::server::NamedService for $type {
const NAME: &'static str = $prefix;
}
impl $type {
pub async fn start_default() -> Self {
let port = GrpcServer::find_unused_port()
.await
.expect("Unable to find an open port");
Self(GrpcServer::new(port)).start_internal().await
}
pub async fn start(port: u16) -> Self {
Self(GrpcServer::new(port)).start_internal().await
}
pub async fn start_with_addr(addr: SocketAddr) -> Self {
Self(GrpcServer::with_addr(addr)).start_internal().await
}
async fn start_internal(&mut self) -> Self {
let address = self.address().clone();
let thread = tokio::spawn(
tonic::transport::Server::builder()
.add_service(self.clone())
.serve(address),
);
self._start(thread).await;
self.to_owned()
}
}
};
}