Protoc

Struct Protoc 

Source
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

Source

pub fn new() -> Self

Creates a new Protoc 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 out_dir(self, path: impl Into<PathBuf>) -> Self

Sets the output directory for the generated files. Corresponds to --rust_out option of protoc.

Source

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.

Source

pub fn proto_files<I>(self, paths: I) -> Self
where I: IntoIterator, I::Item: Into<PathBuf>,

Sets the paths to the input proto files. Corresponds to the unnamed arguments of protoc.

Source

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.

Source

pub fn run<F>(self, timeout: Duration, body: F) -> Result<()>
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§

§

impl Freeze for Protoc

§

impl RefUnwindSafe for Protoc

§

impl Send for Protoc

§

impl Sync for Protoc

§

impl Unpin for Protoc

§

impl UnwindSafe for Protoc

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.