architect_api_schema_builder/
code_gen.rs

1//! [2024-12-30] dkasten: copy of tonic-build/src/codegen.rs at
2//! https://github.com/hyperium/tonic/commit/aff1daf65d9a0d55b92719318eba2b5a4769c4e1
3
4use proc_macro2::TokenStream;
5use std::collections::HashSet;
6use tonic_build::{Attributes, Service};
7
8/// Builder for the generic code generation of server and clients.
9#[derive(Debug)]
10pub struct CodeGenBuilder {
11    emit_package: bool,
12    compile_well_known_types: bool,
13    attributes: Attributes,
14    build_transport: bool,
15    disable_comments: HashSet<String>,
16    use_arc_self: bool,
17    generate_default_stubs: bool,
18}
19
20impl CodeGenBuilder {
21    /// Create a new code gen builder with default options.
22    pub fn new() -> Self {
23        Default::default()
24    }
25
26    /// Enable code generation to emit the package name.
27    pub fn emit_package(&mut self, enable: bool) -> &mut Self {
28        self.emit_package = enable;
29        self
30    }
31
32    /// Attributes that will be added to `mod` and `struct` items.
33    ///
34    /// Reference [`Attributes`] for more information.
35    pub fn attributes(&mut self, attributes: Attributes) -> &mut Self {
36        self.attributes = attributes;
37        self
38    }
39
40    /// Enable transport code to be generated, this requires `tonic`'s `transport`
41    /// feature.
42    ///
43    /// This allows codegen level control of generating the transport code and
44    /// is a work around when other crates in a workspace enable this feature.
45    pub fn build_transport(&mut self, build_transport: bool) -> &mut Self {
46        self.build_transport = build_transport;
47        self
48    }
49
50    /// Enable compiling well knonw types, this will force codegen to not
51    /// use the well known types from `prost-types`.
52    pub fn compile_well_known_types(&mut self, enable: bool) -> &mut Self {
53        self.compile_well_known_types = enable;
54        self
55    }
56
57    /// Disable comments based on a proto path.
58    pub fn disable_comments(&mut self, disable_comments: HashSet<String>) -> &mut Self {
59        self.disable_comments = disable_comments;
60        self
61    }
62
63    /// Emit `Arc<Self>` instead of `&self` in servica trait.
64    pub fn use_arc_self(&mut self, enable: bool) -> &mut Self {
65        self.use_arc_self = enable;
66        self
67    }
68
69    /// Enable or disable returning automatic unimplemented gRPC error code for generated traits.
70    pub fn generate_default_stubs(&mut self, generate_default_stubs: bool) -> &mut Self {
71        self.generate_default_stubs = generate_default_stubs;
72        self
73    }
74
75    /// Generate server interface definition based on `Service`.
76    ///
77    /// This takes some `Service` and will generate a `TokenStream` that contains
78    /// a public module with the generated service definition.
79    pub fn generate_server_definition(
80        &self,
81        service: &impl Service,
82        rewrite_crate: &str,
83        proto_path: &str,
84    ) -> TokenStream {
85        crate::server::generate_definition(
86            service,
87            self.emit_package,
88            rewrite_crate,
89            proto_path,
90            self.compile_well_known_types,
91            &self.attributes,
92            &self.disable_comments,
93            self.use_arc_self,
94            self.generate_default_stubs,
95        )
96    }
97}
98
99impl Default for CodeGenBuilder {
100    fn default() -> Self {
101        Self {
102            emit_package: true,
103            compile_well_known_types: false,
104            attributes: Attributes::default(),
105            build_transport: true,
106            disable_comments: HashSet::default(),
107            use_arc_self: false,
108            generate_default_stubs: false,
109        }
110    }
111}