-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(build): Add
CodeGenBuilder
(#1154)
This commit adds a new `CodeGenBuilder` that replaces the client/server generate fn with a builder stlye that allows adding config items in an non-breaking way. This also deprecates both of the client/server generate fn in favor of the builder ones.
- Loading branch information
1 parent
542c5b3
commit c4525ba
Showing
7 changed files
with
175 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use std::collections::HashSet; | ||
|
||
use proc_macro2::TokenStream; | ||
|
||
use crate::{Attributes, Service}; | ||
|
||
/// Builder for the generic code generation of server and clients. | ||
#[derive(Debug)] | ||
pub struct CodeGen8uilder { | ||
emit_package: bool, | ||
compile_well_known_types: bool, | ||
attributes: Attributes, | ||
build_transport: bool, | ||
disable_comments: HashSet<String>, | ||
} | ||
|
||
impl CodeGen8uilder { | ||
/// Create a new code gen builder with default options. | ||
pub fn new() -> Self { | ||
Default::default() | ||
} | ||
|
||
/// Enable code generation to emit the package name. | ||
pub fn emit_package(&mut self, enable: bool) -> &mut Self { | ||
self.emit_package = enable; | ||
self | ||
} | ||
|
||
/// Attributes that will be added to `mod` and `struct` items. | ||
/// | ||
/// Reference [`Attributes`] for more information. | ||
pub fn attributes(&mut self, attributes: Attributes) -> &mut Self { | ||
self.attributes = attributes; | ||
self | ||
} | ||
|
||
/// Enable transport code to be generated, this requires `tonic`'s `transport` | ||
/// feature. | ||
/// | ||
/// This allows codegen level control of generating the transport code and | ||
/// is a work around when other crates in a workspace enable this feature. | ||
pub fn build_transport(&mut self, build_transport: bool) -> &mut Self { | ||
self.build_transport = build_transport; | ||
self | ||
} | ||
|
||
/// Enable compiling well knonw types, this will force codegen to not | ||
/// use the well known types from `prost-types`. | ||
pub fn compile_well_known_types(&mut self, enable: bool) -> &mut Self { | ||
self.compile_well_known_types = enable; | ||
self | ||
} | ||
|
||
/// Disable comments based on a proto path. | ||
pub fn disable_comments(&mut self, disable_comments: HashSet<String>) -> &mut Self { | ||
self.disable_comments = disable_comments; | ||
self | ||
} | ||
|
||
/// Generate client code based on `Service`. | ||
/// | ||
/// This takes some `Service` and will generate a `TokenStream` that contains | ||
/// a public module with the generated client. | ||
pub fn generate_client(&self, service: &impl Service, proto_path: &str) -> TokenStream { | ||
crate::client::generate_internal( | ||
service, | ||
self.emit_package, | ||
proto_path, | ||
self.compile_well_known_types, | ||
self.build_transport, | ||
&self.attributes, | ||
&self.disable_comments, | ||
) | ||
} | ||
|
||
/// Generate server code based on `Service`. | ||
/// | ||
/// This takes some `Service` and will generate a `TokenStream` that contains | ||
/// a public module with the generated client. | ||
pub fn generate_server(&self, service: &impl Service, proto_path: &str) -> TokenStream { | ||
crate::server::generate_internal( | ||
service, | ||
self.emit_package, | ||
proto_path, | ||
self.compile_well_known_types, | ||
&self.attributes, | ||
&self.disable_comments, | ||
) | ||
} | ||
} | ||
|
||
impl Default for CodeGen8uilder { | ||
fn default() -> Self { | ||
Self { | ||
emit_package: true, | ||
compile_well_known_types: false, | ||
attributes: Attributes::default(), | ||
build_transport: true, | ||
disable_comments: HashSet::default(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters