chore: Fix feature flags and build docs (#311)

Signed-off-by: Lucio Franco <[email protected]>
This commit is contained in:
Lucio Franco
2020-03-30 12:02:10 -04:00
committed by GitHub
parent 59c7788846
commit 3cba9f5f6f
14 changed files with 258 additions and 247 deletions
+5 -4
View File
@@ -1,9 +1,12 @@
use super::schema::{Method, Service};
use super::{Method, Service};
use crate::{generate_doc_comments, naive_snake_case};
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
/// Generate service for client
/// Generate service for client.
///
/// This takes some `Service` and will generate a `TokenStream` that contains
/// a public module with the generated client.
pub fn generate<T: Service>(service: &T, proto_path: &str) -> TokenStream {
let service_ident = quote::format_ident!("{}Client", service.name());
let client_mod = quote::format_ident!("{}_client", naive_snake_case(&service.name()));
@@ -80,8 +83,6 @@ fn generate_methods<T: Service>(service: &T, proto_path: &str) -> TokenStream {
let mut stream = TokenStream::new();
for method in service.methods() {
use super::schema::Commentable;
let path = format!(
"/{}.{}/{}",
service.package(),
+61 -6
View File
@@ -55,23 +55,25 @@
#![doc(html_root_url = "https://docs.rs/tonic-build/0.1.0")]
#![doc(issue_tracker_base_url = "https://github.com/hyperium/tonic/issues/")]
#![doc(test(no_crate_inject, attr(deny(rust_2018_idioms))))]
#![cfg_attr(docsrs, feature(doc_cfg))]
use proc_macro2::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream};
use quote::TokenStreamExt;
/// Prost generator
#[cfg(feature = "prost")]
#[cfg_attr(docsrs, doc(cfg(feature = "prost")))]
mod prost;
#[cfg(feature = "prost")]
#[cfg_attr(docsrs, doc(cfg(feature = "prost")))]
pub use prost::{compile_protos, configure, Builder};
/// Traits to describe schema
pub mod schema;
#[cfg(feature = "rustfmt")]
#[cfg_attr(docsrs, doc(cfg(feature = "rustfmt")))]
use std::io::{self, Write};
#[cfg(feature = "rustfmt")]
#[cfg_attr(docsrs, doc(cfg(feature = "rustfmt")))]
use std::process::{exit, Command};
/// Service code generation for client
@@ -79,8 +81,63 @@ pub mod client;
/// Service code generation for Server
pub mod server;
/// Service generation trait.
///
/// This trait can be implemented and consumed
/// by `client::generate` and `server::generate`
/// to allow any codegen module to generate service
/// abstractions.
pub trait Service {
/// Path to the codec.
const CODEC_PATH: &'static str;
/// Comment type.
type Comment: AsRef<str>;
/// Method type.
type Method: Method;
/// Name of service.
fn name(&self) -> &str;
/// Package name of service.
fn package(&self) -> &str;
/// Identifier used to generate type name.
fn identifier(&self) -> &str;
/// Methods provided by service.
fn methods(&self) -> &[Self::Method];
/// Get comments about this item.
fn comment(&self) -> &[Self::Comment];
}
/// Method generation trait.
///
/// Each service contains a set of generic
/// `Methods`'s that will be used by codegen
/// to generate abstraction implementations for
/// the provided methods.
pub trait Method {
/// Path to the codec.
const CODEC_PATH: &'static str;
/// Comment type.
type Comment: AsRef<str>;
/// Name of method.
fn name(&self) -> &str;
/// Identifier used to generate type name.
fn identifier(&self) -> &str;
/// Method is streamed by client.
fn client_streaming(&self) -> bool;
/// Method is streamed by server.
fn server_streaming(&self) -> bool;
/// Get comments about this item.
fn comment(&self) -> &[Self::Comment];
/// Type name of request and response.
fn request_response_name(&self, proto_path: &str) -> (TokenStream, TokenStream);
}
/// Format files under the out_dir with rustfmt
#[cfg(feature = "rustfmt")]
#[cfg_attr(docsrs, doc(cfg(feature = "rustfmt")))]
pub fn fmt(out_dir: &str) {
let dir = std::fs::read_dir(out_dir).unwrap();
@@ -129,9 +186,7 @@ fn generate_doc_comment<S: AsRef<str>>(comment: S) -> TokenStream {
}
// Generate a larger doc comment composed of many lines of doc comments
fn generate_doc_comments<'a, T: AsRef<str> + 'a, C: IntoIterator<Item = &'a T>>(
comments: C,
) -> TokenStream {
fn generate_doc_comments<T: AsRef<str>>(comments: &[T]) -> TokenStream {
let mut stream = TokenStream::new();
for comment in comments {
+13 -19
View File
@@ -1,4 +1,4 @@
use super::{client, schema, server};
use super::{client, server};
use proc_macro2::TokenStream;
use prost_build::{Config, Method, Service};
use quote::ToTokens;
@@ -7,18 +7,11 @@ use std::path::{Path, PathBuf};
const PROST_CODEC_PATH: &'static str = "tonic::codec::ProstCodec";
impl schema::Commentable for Service {
type Comment = String;
fn comment(&self) -> &[Self::Comment] {
&self.comments.leading[..]
}
}
impl schema::Service for Service {
impl crate::Service for Service {
const CODEC_PATH: &'static str = PROST_CODEC_PATH;
type Method = Method;
type Comment = String;
fn name(&self) -> &str {
&self.name
@@ -32,21 +25,18 @@ impl schema::Service for Service {
&self.proto_name
}
fn comment(&self) -> &[Self::Comment] {
&self.comments.leading[..]
}
fn methods(&self) -> &[Self::Method] {
&self.methods[..]
}
}
impl schema::Commentable for Method {
type Comment = String;
fn comment(&self) -> &[Self::Comment] {
&self.comments.leading[..]
}
}
impl schema::Method for Method {
impl crate::Method for Method {
const CODEC_PATH: &'static str = PROST_CODEC_PATH;
type Comment = String;
fn name(&self) -> &str {
&self.name
@@ -64,6 +54,10 @@ impl schema::Method for Method {
self.server_streaming
}
fn comment(&self) -> &[Self::Comment] {
&self.comments.leading[..]
}
fn request_response_name(&self, proto_path: &str) -> (TokenStream, TokenStream) {
let request = if self.input_proto_type.starts_with(".google.protobuf")
|| self.input_type.starts_with("::")
-44
View File
@@ -1,44 +0,0 @@
use proc_macro2::TokenStream;
/// Item has comment
pub trait Commentable {
/// Comment type
type Comment: AsRef<str>;
/// Get comments about this item
fn comment(&self) -> &[Self::Comment];
}
/// Service
pub trait Service: Commentable {
/// Path to the codec
const CODEC_PATH: &'static str;
/// Method type
type Method: Method;
/// Name of service
fn name(&self) -> &str;
/// Package name of service
fn package(&self) -> &str;
/// Identifier used to generate type name
fn identifier(&self) -> &str;
/// Methods provided by service
fn methods(&self) -> &[Self::Method];
}
/// Method
pub trait Method: Commentable {
/// Path to the codec
const CODEC_PATH: &'static str;
/// Name of method
fn name(&self) -> &str;
/// Identifier used to generate type name
fn identifier(&self) -> &str;
/// Method is streamed by client
fn client_streaming(&self) -> bool;
/// Method is streamed by server
fn server_streaming(&self) -> bool;
/// Type name of request and response
fn request_response_name(&self, proto_path: &str) -> (TokenStream, TokenStream);
}
+5 -2
View File
@@ -1,10 +1,13 @@
use super::schema::{Commentable, Method, Service};
use super::{Method, Service};
use crate::{generate_doc_comment, generate_doc_comments, naive_snake_case};
use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{Ident, Lit, LitStr};
/// Generate service for Server
/// Generate service for Server.
///
/// This takes some `Service` and will generate a `TokenStream` that contains
/// a public module containing the server service and handler trait.
pub fn generate<T: Service>(service: &T, proto_path: &str) -> TokenStream {
let methods = generate_methods(service, proto_path);