Skip to content

Commit

Permalink
tonic-build: change codec_path from a constant to a method
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill committed May 4, 2022
1 parent 4533a6e commit d53520f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
8 changes: 4 additions & 4 deletions tonic-build/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ fn generate_unary<T: Method>(
compile_well_known_types: bool,
path: String,
) -> TokenStream {
let codec_name = syn::parse_str::<syn::Path>(T::CODEC_PATH).unwrap();
let codec_name = syn::parse_str::<syn::Path>(method.codec_path()).unwrap();
let ident = format_ident!("{}", method.name());
let (request, response) = method.request_response_name(proto_path, compile_well_known_types);

Expand All @@ -192,7 +192,7 @@ fn generate_server_streaming<T: Method>(
compile_well_known_types: bool,
path: String,
) -> TokenStream {
let codec_name = syn::parse_str::<syn::Path>(T::CODEC_PATH).unwrap();
let codec_name = syn::parse_str::<syn::Path>(method.codec_path()).unwrap();
let ident = format_ident!("{}", method.name());

let (request, response) = method.request_response_name(proto_path, compile_well_known_types);
Expand All @@ -218,7 +218,7 @@ fn generate_client_streaming<T: Method>(
compile_well_known_types: bool,
path: String,
) -> TokenStream {
let codec_name = syn::parse_str::<syn::Path>(T::CODEC_PATH).unwrap();
let codec_name = syn::parse_str::<syn::Path>(method.codec_path()).unwrap();
let ident = format_ident!("{}", method.name());

let (request, response) = method.request_response_name(proto_path, compile_well_known_types);
Expand All @@ -244,7 +244,7 @@ fn generate_streaming<T: Method>(
compile_well_known_types: bool,
path: String,
) -> TokenStream {
let codec_name = syn::parse_str::<syn::Path>(T::CODEC_PATH).unwrap();
let codec_name = syn::parse_str::<syn::Path>(method.codec_path()).unwrap();
let ident = format_ident!("{}", method.name());

let (request, response) = method.request_response_name(proto_path, compile_well_known_types);
Expand Down
7 changes: 2 additions & 5 deletions tonic-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ pub mod server;
/// 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>;

Expand All @@ -119,15 +116,15 @@ pub trait Service {
/// 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;
/// Path to the codec.
fn codec_path(&self) -> &str;
/// Method is streamed by client.
fn client_streaming(&self) -> bool;
/// Method is streamed by server.
Expand Down
15 changes: 9 additions & 6 deletions tonic-build/src/prost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ use super::{client, server, Attributes};
use proc_macro2::TokenStream;
use prost_build::{Config, Method, Service};
use quote::ToTokens;
use std::ffi::OsString;
use std::io;
use std::path::{Path, PathBuf};
use std::{
ffi::OsString,
io,
path::{Path, PathBuf},
};

/// Configure `tonic-build` code generation.
///
Expand Down Expand Up @@ -51,8 +53,6 @@ const PROST_CODEC_PATH: &str = "tonic::codec::ProstCodec";
const NON_PATH_TYPE_ALLOWLIST: &[&str] = &["()"];

impl crate::Service for Service {
const CODEC_PATH: &'static str = PROST_CODEC_PATH;

type Method = Method;
type Comment = String;

Expand All @@ -78,7 +78,6 @@ impl crate::Service for Service {
}

impl crate::Method for Method {
const CODEC_PATH: &'static str = PROST_CODEC_PATH;
type Comment = String;

fn name(&self) -> &str {
Expand All @@ -89,6 +88,10 @@ impl crate::Method for Method {
&self.proto_name
}

fn codec_path(&self) -> &str {
PROST_CODEC_PATH
}

fn client_streaming(&self) -> bool {
self.client_streaming
}
Expand Down
8 changes: 4 additions & 4 deletions tonic-build/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ fn generate_unary<T: Method>(
method_ident: Ident,
server_trait: Ident,
) -> TokenStream {
let codec_name = syn::parse_str::<syn::Path>(T::CODEC_PATH).unwrap();
let codec_name = syn::parse_str::<syn::Path>(method.codec_path()).unwrap();

let service_ident = quote::format_ident!("{}Svc", method.identifier());

Expand Down Expand Up @@ -415,7 +415,7 @@ fn generate_server_streaming<T: Method>(
method_ident: Ident,
server_trait: Ident,
) -> TokenStream {
let codec_name = syn::parse_str::<syn::Path>(T::CODEC_PATH).unwrap();
let codec_name = syn::parse_str::<syn::Path>(method.codec_path()).unwrap();

let service_ident = quote::format_ident!("{}Svc", method.identifier());

Expand Down Expand Up @@ -470,7 +470,7 @@ fn generate_client_streaming<T: Method>(
let service_ident = quote::format_ident!("{}Svc", method.identifier());

let (request, response) = method.request_response_name(proto_path, compile_well_known_types);
let codec_name = syn::parse_str::<syn::Path>(T::CODEC_PATH).unwrap();
let codec_name = syn::parse_str::<syn::Path>(method.codec_path()).unwrap();

quote! {
#[allow(non_camel_case_types)]
Expand Down Expand Up @@ -517,7 +517,7 @@ fn generate_streaming<T: Method>(
method_ident: Ident,
server_trait: Ident,
) -> TokenStream {
let codec_name = syn::parse_str::<syn::Path>(T::CODEC_PATH).unwrap();
let codec_name = syn::parse_str::<syn::Path>(method.codec_path()).unwrap();

let service_ident = quote::format_ident!("{}Svc", method.identifier());

Expand Down

0 comments on commit d53520f

Please sign in to comment.