ProtocOnMemory

Struct ProtocOnMemory 

Source
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

Source

pub fn new() -> Self

Creates a new ProtocOnMemory instance.

Source

pub fn protoc_path(self, path: impl Into<PathBuf>) -> Self

Sets the path to the protoc command. Default is "protoc".

Source

pub fn add_file(self, name: &str, content: &str) -> Self

Adds a (virtual) input proto file. Corresponds to the protoc command’s unnamed argument.

Source

pub fn add_files<I>(self, files: I) -> Self
where I: IntoIterator<Item = (String, String)>,

Adds (virtual) input proto files. Corresponds to the protoc command’s unnamed arguments.

Source

pub fn run<F>(self, timeout: Duration, func: F) -> Result<Vec<(String, String)>>
where F: FnOnce(&[u8]) -> Result<Vec<u8>, 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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.