pub struct ProtocOnMemory { /* private fields */ }
Expand description
A variant of Protoc
which you can run the protoc
command without touching the actual filesystem.
Instead of using the actual filesystem, you can pass the name-value pairs of proto files to this struct, and it returns the generated files as name-value pairs.
This is useful when you want to test your plugin code, or when you want to implement your procedual macro which generates the inlined generated code.
See the crate level documentation or Protoc
for the basic explanations.
§Example
use protoc_plugin_by_closure::ProtocOnMemory;
use std::time::Duration;
let result_files = Protoc::new()
.add_file("my_protobuf_file.proto", r#"
syntax = "proto3";
package my_package;
message MyMessage {
string name = 1;
}"#)
.add_file("another/path/to/my_protobuf_file2.proto", r#"
syntax = "proto3";
package my_package2;
message MyMessage2 {
string name2 = 2;
}"#)
.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 filenames depend on your plugin logic, but typically they will be like this:
assert!(result_files.iter().any(|(name, _)| name == "my_package.rs"));
assert!(result_files.iter().any(|(name, _)| name == "my_package2.rs"));
Implementations§
Source§impl ProtocOnMemory
impl ProtocOnMemory
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 add_file(self, name: &str, content: &str) -> Self
pub fn add_file(self, name: &str, content: &str) -> Self
Adds a (virtual) input proto file. Corresponds to the protoc
command’s unnamed argument.
Sourcepub fn add_files<I>(self, files: I) -> Self
pub fn add_files<I>(self, files: I) -> Self
Adds (virtual) input proto files. Corresponds to the protoc
command’s unnamed arguments.
Sourcepub fn run<F>(self, timeout: Duration, func: F) -> Result<Vec<(String, String)>>
pub fn run<F>(self, timeout: Duration, func: F) -> Result<Vec<(String, String)>>
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.