protoc-plugin-by-closure

Crates.ioprotoc-plugin-by-closure
lib.rsprotoc-plugin-by-closure
version0.1.3
sourcesrc
created_at2024-06-29 10:06:35.205254
updated_at2024-07-04 17:35:51.323344
descriptionUse Google Protocol Buffer compiler (`protoc`) with your closure code.
homepage
repositoryhttps://github.com/wada314/puroro
max_upload_size
id1287288
size25,054
(wada314)

documentation

README

A library for running protoc command with your own plugin code as a closure. When you develop your own protoc compiler plugin, normally you need to compile your code as an executable binary and run protoc command by yourself with passing the path to the binary. This crate provides a convenient replacement for that process: You just need to pass your code as a closure to this library, then it will run the protoc command as like as your closure is an executable binary.

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());

How it works

This crate is using the ipc-channel crate for the communication between the internal plugin binary and your closure code. When running the protoc command, this crate generates an one-time key for the IPC channel, and pass it to the internal plugin binary as a protoc argument. The internal plugin binary then creates the IPC channel with the key, and sends the whole input CodeGeneratorRequest bytes back to the caller process. The generated CodeGeneratorResponse bytes are sent back to the internal plugin binary in the same way, and the binary outputs that bytes to the stdout.

Features

  • on-memory: Enabled by default. Provides [ProtocOnMemory] struct which makes you to run the protoc command without touching the actual filesystem. Because this feature is using the tempfile crate, you can disable it if you don't need it.

Requirements

Nightly channel required.

This crate requires an unstable (as of 2024/06) cargo feature bindeps. This crate contains .cargo/config.toml file which enables this feature so you normally don't need to worry about it, but if you include this crate as a part of your Cargo workspace, then you need to create the .cargo/config.toml file with the following contents under your cargo workspace directory manually (see the official document for the reasons):

[unstable]
bindeps = true
Commit count: 1566

cargo fmt