pub struct Protoc { /* private fields */ }
Expand description
A convenient wrapper for running protoc command with your own plugin code as a closure.
See the crate level documentation for the basic explanation.
§Example
use protoc_plugin_by_closure::Protoc;
use std::time::Duration;
Protoc::new()
.proto_file("my_protobuf_file.proto")
.proto_file("my_protobuf_file2.proto")
.proto_path("path/to/my/input_proto_dir/")
.out_dir("path/to/my/output_dir/")
.run(Duration::from_sec(3), |request_bytes| {
// Your plugin logic here, which takes the CodeGeneratorRequest bytes
// and returns the Result of CodeGeneratorResponse bytes.
})
.unwrap();
// The generated file names depend on your plugin logic and the contents of
// the input proto files, but typically they will be like this:
assert!(std::path::Path("path/to/my/output_dir/my_protobuf_file.rs").exists());
assert!(std::path::Path("path/to/my/output_dir/my_protobuf_file2.rs").exists());
Implementations§
Source§impl Protoc
impl Protoc
Sourcepub fn protoc_path(self, path: impl Into<PathBuf>) -> Self
pub fn protoc_path(self, path: impl Into<PathBuf>) -> Self
Sets the path to the protoc
command. Default is "protoc"
.
Sourcepub fn out_dir(self, path: impl Into<PathBuf>) -> Self
pub fn out_dir(self, path: impl Into<PathBuf>) -> Self
Sets the output directory for the generated files. Corresponds to --rust_out
option of protoc
.
Sourcepub fn proto_file(self, path: impl Into<PathBuf>) -> Self
pub fn proto_file(self, path: impl Into<PathBuf>) -> Self
Sets the path to the input proto file. Corresponds to the unnamed argument of protoc
.
Sourcepub fn proto_files<I>(self, paths: I) -> Self
pub fn proto_files<I>(self, paths: I) -> Self
Sets the paths to the input proto files. Corresponds to the unnamed arguments of protoc
.
Sourcepub fn proto_path(self, path: impl Into<PathBuf>) -> Self
pub fn proto_path(self, path: impl Into<PathBuf>) -> Self
Sets the path to the input proto file directory. Corresponds to --proto_path
option of protoc
.
Sourcepub fn run<F>(self, timeout: Duration, body: F) -> Result<()>
pub fn run<F>(self, timeout: Duration, body: F) -> Result<()>
Runs the protoc
command with the given closure as a plugin code.
The body
param can be any FnOnce
closure which takes the encoded CodeGeneratorRequest
bytes
and returns the Result
of encoded CodeGeneratorResponse
bytes.
Set the timeout
to the maximum duration of the protoc
command execution.