Clean up tonic-build
This commit is contained in:
+13
-17
@@ -8,18 +8,16 @@ pub(crate) fn generate(service: &Service, proto: &str) -> TokenStream {
|
||||
let methods = generate_methods(service, proto);
|
||||
|
||||
quote! {
|
||||
use tonic::_codegen::*;
|
||||
|
||||
pub struct #service_ident <T> {
|
||||
pub struct #service_ident<T> {
|
||||
inner: tonic::client::Grpc<T>,
|
||||
}
|
||||
|
||||
impl<T> #service_ident <T>
|
||||
impl<T> #service_ident<T>
|
||||
where T: tonic::client::GrpcService<tonic::BoxBody>,
|
||||
T::ResponseBody: tonic::body::Body + tonic::_codegen::HttpBody + Send + 'static,
|
||||
T::Error: Into<tonic::error::Error>,
|
||||
<T::ResponseBody as tonic::_codegen::HttpBody>::Error: Into<tonic::error::Error> + Send,
|
||||
<T::ResponseBody as tonic::_codegen::HttpBody>::Data: Into<bytes::Bytes> + Send, {
|
||||
T::ResponseBody: Body + HttpBody + Send + 'static,
|
||||
T::Error: Into<StdError>,
|
||||
<T::ResponseBody as HttpBody>::Error: Into<StdError> + Send,
|
||||
<T::ResponseBody as HttpBody>::Data: Into<bytes::Bytes> + Send, {
|
||||
pub fn new(inner: T) -> Self {
|
||||
let inner = tonic::client::Grpc::new(inner);
|
||||
Self { inner }
|
||||
@@ -34,7 +32,7 @@ pub(crate) fn generate(service: &Service, proto: &str) -> TokenStream {
|
||||
#methods
|
||||
}
|
||||
|
||||
impl<T: Clone> Clone for #service_ident <T> {
|
||||
impl<T: Clone> Clone for #service_ident<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
inner: self.inner.clone(),
|
||||
@@ -72,7 +70,7 @@ fn generate_unary(method: &Method, proto: &str, path: String) -> TokenStream {
|
||||
let response: Path = syn::parse_str(&format!("{}::{}", proto, method.output_type)).unwrap();
|
||||
|
||||
quote! {
|
||||
pub async fn #ident (&mut self, request: tonic::Request<#request>)
|
||||
pub async fn #ident(&mut self, request: tonic::Request<#request>)
|
||||
-> Result<tonic::Response<#response>, tonic::Status> {
|
||||
self.ready().await?;
|
||||
let codec = tonic::codec::ProstCodec::new();
|
||||
@@ -88,7 +86,7 @@ fn generate_server_streaming(method: &Method, proto: &str, path: String) -> Toke
|
||||
let response: Path = syn::parse_str(&format!("{}::{}", proto, method.output_type)).unwrap();
|
||||
|
||||
quote! {
|
||||
pub async fn #ident (&mut self, request: tonic::Request<#request>)
|
||||
pub async fn #ident(&mut self, request: tonic::Request<#request>)
|
||||
-> Result<tonic::Response<tonic::codec::Streaming<#response>>, tonic::Status> {
|
||||
self.ready().await?;
|
||||
let codec = tonic::codec::ProstCodec::new();
|
||||
@@ -104,14 +102,13 @@ fn generate_client_streaming(method: &Method, proto: &str, path: String) -> Toke
|
||||
let response: Path = syn::parse_str(&format!("{}::{}", proto, method.output_type)).unwrap();
|
||||
|
||||
quote! {
|
||||
pub async fn #ident <S>(&mut self, request: tonic::Request<S>)
|
||||
pub async fn #ident<S>(&mut self, request: tonic::Request<S>)
|
||||
-> Result<tonic::Response<#response>, tonic::Status>
|
||||
where S: tonic::_codegen::Stream<Item = Result<#request, tonic::Status>> + Send + 'static,
|
||||
where S: Stream<Item = Result<#request, tonic::Status>> + Send + 'static,
|
||||
{
|
||||
self.ready().await?;
|
||||
let codec = tonic::codec::ProstCodec::new();
|
||||
let path = http::uri::PathAndQuery::from_static(#path);
|
||||
let request = request.map(|s| Box::pin(s));
|
||||
self.inner.client_streaming(request, path, codec).await
|
||||
}
|
||||
}
|
||||
@@ -123,14 +120,13 @@ fn generate_streaming(method: &Method, proto: &str, path: String) -> TokenStream
|
||||
let response: Path = syn::parse_str(&format!("{}::{}", proto, method.output_type)).unwrap();
|
||||
|
||||
quote! {
|
||||
pub async fn #ident <S>(&mut self, request: tonic::Request<S>)
|
||||
pub async fn #ident<S>(&mut self, request: tonic::Request<S>)
|
||||
-> Result<tonic::Response<tonic::codec::Streaming<#response>>, tonic::Status>
|
||||
where S: tonic::_codegen::Stream<Item = Result<#request, tonic::Status>> + Send + 'static,
|
||||
where S: Stream<Item = Result<#request, tonic::Status>> + Send + 'static,
|
||||
{
|
||||
self.ready().await?;
|
||||
let codec = tonic::codec::ProstCodec::new();
|
||||
let path = http::uri::PathAndQuery::from_static(#path);
|
||||
let request = request.map(|s| Box::pin(s));
|
||||
self.inner.streaming(request, path, codec).await
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,18 @@
|
||||
//! `tonic-build` compiles `proto` files via `prost` and generates service stubs
|
||||
//! and proto definitiones for use with `tonic`.
|
||||
//!
|
||||
//! # Examples
|
||||
//!
|
||||
//! ```rust,no_run
|
||||
//! fn main() {
|
||||
//! tonic_build::compile_protos(
|
||||
//! &["proto/helloworld/helloworld.proto"],
|
||||
//! &["proto/helloworld"],
|
||||
//! "helloworld",
|
||||
//! )
|
||||
//! .unwrap();
|
||||
//! }
|
||||
|
||||
use proc_macro2::TokenStream;
|
||||
use prost_build::Config;
|
||||
use std::{io, path, path::Path, process::Command};
|
||||
@@ -72,12 +87,14 @@ impl prost_build::ServiceGenerator for ServiceGenerator {
|
||||
let service = quote::quote! {
|
||||
pub mod client {
|
||||
#![allow(unused_variables, dead_code, missing_docs)]
|
||||
use tonic::codegen::*;
|
||||
|
||||
#clients
|
||||
}
|
||||
|
||||
pub mod server {
|
||||
#![allow(unused_variables, dead_code, missing_docs)]
|
||||
use tonic::codegen::*;
|
||||
|
||||
#servers
|
||||
}
|
||||
|
||||
+32
-37
@@ -12,37 +12,34 @@ pub(crate) fn generate(service: &Service, proto_path: &str) -> TokenStream {
|
||||
let generated_trait = generate_trait(service, proto_path, server_trait.clone());
|
||||
|
||||
quote! {
|
||||
use tonic::_codegen::*;
|
||||
|
||||
#generated_trait
|
||||
|
||||
// TODO: impl debug
|
||||
#[derive(Clone)]
|
||||
pub struct #server_make_service <T: #server_trait > {
|
||||
inner: std::sync::Arc<T>,
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct #server_make_service<T: #server_trait> {
|
||||
inner: Arc<T>,
|
||||
}
|
||||
|
||||
// TODO: impl debug
|
||||
pub struct #server_service <T: #server_trait > {
|
||||
inner: std::sync::Arc<T>,
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct #server_service<T: #server_trait> {
|
||||
inner: Arc<T>,
|
||||
}
|
||||
|
||||
impl<T: #server_trait > #server_make_service <T> {
|
||||
impl<T: #server_trait> #server_make_service<T> {
|
||||
pub fn new(inner: T) -> Self {
|
||||
let inner = std::sync::Arc::new(inner);
|
||||
let inner = Arc::new(inner);
|
||||
Self { inner }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: #server_trait > #server_service <T> {
|
||||
pub fn new(inner: std::sync::Arc<T>) -> Self {
|
||||
impl<T: #server_trait> #server_service<T> {
|
||||
pub fn new(inner: Arc<T>) -> Self {
|
||||
Self { inner }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: #server_trait , R> Service<R> for #server_make_service <T> {
|
||||
type Response = #server_service <T>;
|
||||
type Error = tonic::error::Never;
|
||||
impl<T: #server_trait, R> Service<R> for #server_make_service<T> {
|
||||
type Response = #server_service<T>;
|
||||
type Error = Never;
|
||||
type Future = Ready<Result<Self::Response, Self::Error>>;
|
||||
|
||||
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
@@ -50,25 +47,26 @@ pub(crate) fn generate(service: &Service, proto_path: &str) -> TokenStream {
|
||||
}
|
||||
|
||||
fn call(&mut self, _: R) -> Self::Future {
|
||||
ok(#server_service ::new(self.inner.clone()))
|
||||
ok(#server_service::new(self.inner.clone()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: #server_trait > Service<http::Request<tonic::_codegen::HyperBody>> for #server_service <T> {
|
||||
impl<T: #server_trait> Service<http::Request<HyperBody>> for #server_service<T> {
|
||||
type Response = http::Response<tonic::BoxBody>;
|
||||
type Error = tonic::error::Never;
|
||||
type Error = Never;
|
||||
type Future = BoxFuture<Self::Response, Self::Error>;
|
||||
|
||||
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
|
||||
fn call(&mut self, req: http::Request<tonic::_codegen::HyperBody>) -> Self::Future {
|
||||
fn call(&mut self, req: http::Request<HyperBody>) -> Self::Future {
|
||||
let inner = self.inner.clone();
|
||||
|
||||
match req.uri().path() {
|
||||
#methods
|
||||
|
||||
// TODO: implement grpc unimplemented for server
|
||||
_ => unimplemented!("use grpc unimplemented"),
|
||||
}
|
||||
}
|
||||
@@ -83,7 +81,6 @@ fn generate_trait(service: &Service, proto_path: &str, server_trait: Ident) -> T
|
||||
#[async_trait]
|
||||
pub trait #server_trait : Send + Sync + 'static {
|
||||
#methods
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -101,13 +98,13 @@ fn generate_trait_methods(service: &Service, proto_path: &str) -> TokenStream {
|
||||
let method = match (method.client_streaming, method.server_streaming) {
|
||||
(false, false) => {
|
||||
quote! {
|
||||
async fn #name (&self, request: tonic::Request<#req_message>)
|
||||
async fn #name(&self, request: tonic::Request<#req_message>)
|
||||
-> Result<tonic::Response<#res_message>, tonic::Status>;
|
||||
}
|
||||
}
|
||||
(true, false) => {
|
||||
quote! {
|
||||
async fn #name (&self, request: tonic::Request<tonic::Streaming<#req_message>>)
|
||||
async fn #name(&self, request: tonic::Request<tonic::Streaming<#req_message>>)
|
||||
-> Result<tonic::Response<#res_message>, tonic::Status>;
|
||||
}
|
||||
}
|
||||
@@ -115,9 +112,9 @@ fn generate_trait_methods(service: &Service, proto_path: &str) -> TokenStream {
|
||||
let stream = quote::format_ident!("{}Stream", method.proto_name);
|
||||
|
||||
quote! {
|
||||
type #stream: Stream<Item = Result<#res_message, tonic::Status>> + Unpin + Send + 'static;
|
||||
type #stream: Stream<Item = Result<#res_message, tonic::Status>> + Send + 'static;
|
||||
|
||||
async fn #name (&self, request: tonic::Request<#req_message>)
|
||||
async fn #name(&self, request: tonic::Request<#req_message>)
|
||||
-> Result<tonic::Response<Self::#stream>, tonic::Status>;
|
||||
}
|
||||
}
|
||||
@@ -125,9 +122,9 @@ fn generate_trait_methods(service: &Service, proto_path: &str) -> TokenStream {
|
||||
let stream = quote::format_ident!("{}Stream", method.proto_name);
|
||||
|
||||
quote! {
|
||||
type #stream: Stream<Item = Result<#res_message, tonic::Status>> + Unpin + Send + 'static;
|
||||
type #stream: Stream<Item = Result<#res_message, tonic::Status>> + Send + 'static;
|
||||
|
||||
async fn #name (&self, request: tonic::Request<tonic::Streaming<#req_message>>)
|
||||
async fn #name(&self, request: tonic::Request<tonic::Streaming<#req_message>>)
|
||||
-> Result<tonic::Response<Self::#stream>, tonic::Status>;
|
||||
}
|
||||
}
|
||||
@@ -188,9 +185,9 @@ fn generate_unary(
|
||||
syn::parse_str(&format!("{}::{}", proto_path, method.output_type)).unwrap();
|
||||
|
||||
quote! {
|
||||
struct #service_ident <T: #server_trait >(pub std::sync::Arc<T>);
|
||||
struct #service_ident<T: #server_trait >(pub Arc<T>);
|
||||
|
||||
impl<T: #server_trait > tonic::server::UnaryService<#request> for #service_ident <T> {
|
||||
impl<T: #server_trait> tonic::server::UnaryService<#request> for #service_ident<T> {
|
||||
type Response = #response;
|
||||
type Future = BoxFuture<tonic::Response<Self::Response>, tonic::Status>;
|
||||
|
||||
@@ -231,12 +228,10 @@ fn generate_server_streaming(
|
||||
|
||||
let response_stream = quote::format_ident!("{}Stream", method.proto_name);
|
||||
|
||||
// TODO: parse response stream type, if it is a concrete type then use that
|
||||
// as the ResponseStream type, if it is a impl Trait then we need to box.
|
||||
quote! {
|
||||
struct #service_ident <T: #server_trait >(pub std::sync::Arc<T>);
|
||||
struct #service_ident<T: #server_trait >(pub Arc<T>);
|
||||
|
||||
impl<T: #server_trait > tonic::server::ServerStreamingService<#request> for #service_ident <T> {
|
||||
impl<T: #server_trait> tonic::server::ServerStreamingService<#request> for #service_ident<T> {
|
||||
type Response = #response;
|
||||
type ResponseStream = T::#response_stream;
|
||||
type Future = BoxFuture<tonic::Response<Self::ResponseStream>, tonic::Status>;
|
||||
@@ -277,9 +272,9 @@ fn generate_client_streaming(
|
||||
syn::parse_str(&format!("{}::{}", proto_path, method.output_type)).unwrap();
|
||||
|
||||
quote! {
|
||||
struct #service_ident<T: #server_trait >(pub std::sync::Arc<T>);
|
||||
struct #service_ident<T: #server_trait >(pub Arc<T>);
|
||||
|
||||
impl<T: #server_trait> tonic::server::ClientStreamingService<#request> for #service_ident <T>
|
||||
impl<T: #server_trait> tonic::server::ClientStreamingService<#request> for #service_ident<T>
|
||||
{
|
||||
type Response = #response;
|
||||
type Future = BoxFuture<tonic::Response<Self::Response>, tonic::Status>;
|
||||
@@ -322,9 +317,9 @@ fn generate_streaming(
|
||||
let response_stream = quote::format_ident!("{}Stream", method.proto_name);
|
||||
|
||||
quote! {
|
||||
struct #service_ident<T: #server_trait>(pub std::sync::Arc<T>);
|
||||
struct #service_ident<T: #server_trait>(pub Arc<T>);
|
||||
|
||||
impl<T: #server_trait> tonic::server::StreamingService<#request> for #service_ident <T>
|
||||
impl<T: #server_trait> tonic::server::StreamingService<#request> for #service_ident<T>
|
||||
{
|
||||
type Response = #response;
|
||||
type ResponseStream = T::#response_stream;
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
//! Codegen exports used by `tonic-build`.
|
||||
|
||||
pub use async_trait::async_trait;
|
||||
pub use futures_core::Stream;
|
||||
pub use futures_util::future::{ok, poll_fn, Ready};
|
||||
|
||||
pub use http_body::Body as HttpBody;
|
||||
pub use std::future::Future;
|
||||
pub use std::pin::Pin;
|
||||
pub use std::sync::Arc;
|
||||
pub use std::task::{Context, Poll};
|
||||
pub use tower_service::Service;
|
||||
pub type StdError = Box<dyn std::error::Error + Send + Sync + 'static>;
|
||||
pub use crate::body::Body;
|
||||
|
||||
#[cfg(feature = "transport")]
|
||||
pub use hyper::Body as HyperBody;
|
||||
|
||||
pub type BoxFuture<T, E> = self::Pin<Box<dyn self::Future<Output = Result<T, E>> + Send + 'static>>;
|
||||
pub type BoxStream<T> =
|
||||
self::Pin<Box<dyn futures_core::Stream<Item = Result<T, crate::Status>> + Send + 'static>>;
|
||||
|
||||
pub mod http {
|
||||
pub use http::*;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Never {}
|
||||
|
||||
impl std::fmt::Display for Never {
|
||||
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match *self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for Never {}
|
||||
@@ -1,14 +0,0 @@
|
||||
use std::fmt;
|
||||
|
||||
pub type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Never {}
|
||||
|
||||
impl fmt::Display for Never {
|
||||
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match *self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for Never {}
|
||||
+2
-25
@@ -33,8 +33,6 @@
|
||||
pub mod body;
|
||||
pub mod client;
|
||||
pub mod codec;
|
||||
#[doc(hidden)]
|
||||
pub mod error;
|
||||
pub mod metadata;
|
||||
pub mod server;
|
||||
|
||||
@@ -56,28 +54,7 @@ pub use status::{Code, Status};
|
||||
#[doc(inline)]
|
||||
pub use transport::{Channel, Server};
|
||||
|
||||
pub(crate) use error::Error;
|
||||
pub(crate) type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||
|
||||
#[doc(hidden)]
|
||||
pub mod _codegen {
|
||||
pub use async_trait::async_trait;
|
||||
pub use futures_core::Stream;
|
||||
pub use futures_util::future::{ok, poll_fn, Ready};
|
||||
pub use http_body::Body as HttpBody;
|
||||
pub use std::future::Future;
|
||||
pub use std::pin::Pin;
|
||||
pub use std::task::{Context, Poll};
|
||||
pub use tower_service::Service;
|
||||
|
||||
#[cfg(feature = "transport")]
|
||||
pub use hyper::Body as HyperBody;
|
||||
|
||||
pub type BoxFuture<T, E> =
|
||||
self::Pin<Box<dyn self::Future<Output = Result<T, E>> + Send + 'static>>;
|
||||
pub type BoxStream<T> =
|
||||
self::Pin<Box<dyn futures_core::Stream<Item = Result<T, crate::Status>> + Send + 'static>>;
|
||||
|
||||
pub mod http {
|
||||
pub use http::*;
|
||||
}
|
||||
}
|
||||
pub mod codegen;
|
||||
|
||||
+1
-1
@@ -420,7 +420,7 @@ impl From<i32> for Code {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::error::Error;
|
||||
use crate::Error;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Nested(Error);
|
||||
|
||||
Reference in New Issue
Block a user