Trait prost_build::ServiceGenerator
source · [−]pub trait ServiceGenerator {
fn generate(&mut self, service: Service, buf: &mut String);
fn finalize(&mut self, _buf: &mut String) { ... }
fn finalize_package(&mut self, _package: &str, _buf: &mut String) { ... }
}
Expand description
A service generator takes a service descriptor and generates Rust code.
ServiceGenerator
can be used to generate application-specific interfaces
or implementations for Protobuf service definitions.
Service generators are registered with a code generator using the
Config::service_generator
method.
A viable scenario is that an RPC framework provides a service generator. It generates a trait describing methods of the service and some glue code to call the methods of the trait, defining details like how errors are handled or if it is asynchronous. Then the user provides an implementation of the generated trait in the application code and plugs it into the framework.
Such framework isn’t part of Prost at present.
Required Methods
Provided Methods
Finalizes the generation process.
In case there’s something that needs to be output at the end of the generation process, it
goes here. Similar to generate
, the output should be appended to
buf
.
An example can be a module or other thing that needs to appear just once, not for each service generated.
This still can be called multiple times in a lifetime of the service generator, because it
is called once per .proto
file.
The default implementation is empty and does nothing.
fn finalize_package(&mut self, _package: &str, _buf: &mut String)
fn finalize_package(&mut self, _package: &str, _buf: &mut String)
Finalizes the generation process for an entire protobuf package.
This differs from finalize
by where (and how often) it is called
during the service generator life cycle. This method is called once per protobuf package,
making it ideal for grouping services within a single package spread across multiple
.proto
files.
The default implementation is empty and does nothing.