A yet another protocol buffer compiler implementation for Rust language. This project is licensed under Apache 2.0 license.
This is not an officially supported Google product.
See puroro/src/lib.rs for more documents.
This library is under development and it is very possible to make breaking changes.
Currently this library only supports Rust nightly channel.
First, let's create a crate for your .pb files (and the generated .rs files). It is highly recommended to make a separated crate which only contains the .pb files and the generated .rs files.
```shell $ cargo new my-examples --lib $ cd my-examples
$ cargo install puroro-plugin --root=./
$ mkdir protos ```
As an example, let's make a simple proto file test1.proto
:
protobuf
syntax = "proto3";
package library;
message Book {
string title = 1;
uint32 num_pages = 2;
}
Note that the file names does not make any effect in the generated codes (For the generated code's filenames, the package names specified in the proto files are used).
Then edit the Cargo.toml
to add the dependency to puroro
library crate:
```toml [dependencies]
puroro = "0.4.1" ```
As a last step, create a file build.rs
into the crate root directory
with a contents like this (You can just copy-and-paste. Don't forget to
edit the "exe" extension for non-Windows users).
rust
use std::env;
use std::path::PathBuf;
use std::process::Command;
fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=protos/*.proto");
// ############ You may want to edit here for non-windows environment ############
let plugin_exe_path = ["bin", "puroro-plugin.exe"].iter().collect::<PathBuf>();
let output_rust_path = ["src"].iter().collect::<PathBuf>();
// ######### You may also want to edit here for finding protoc command ###########
let protoc_exe = env::var("PURORO_PROTOC_PATH").unwrap_or("protoc".to_string());
let protoc_status = Command::new(&protoc_exe)
.arg("protos/*.proto")
.arg(format!(
"--plugin=protoc-gen-rust={}",
plugin_exe_path.to_str().unwrap()
))
.arg(format!("--rust_out={}", output_rust_path.to_str().unwrap()))
.arg(format!("--proto_path={}", "./protos/"))
.arg("--experimental_allow_proto3_optional")
.status()
.unwrap();
if !protoc_status.success() {
println!("cargo:warning=Failed to run `protoc` command.");
panic!("Failed to run `protoc` command.")
}
}
Once you have finished these steps, the directory should be like this:
+ my-examples/
├ src/
│ └ (empty)
├ protos/
│ └ test1.proto
├ bin/
│ └ puroro-plugin.exe
├ cargo.toml
├ build.rs
├ (some other generated files)
Then run cargo build
command. If it successfully runs, then the generated
.rs
files will be generated under src/
directory and you can use it from
your own crate. Congraturations!
optional int32 foo = 1; [default=10]
)Vec
and String
allocation&[u8]
slice, without allocating any extra memories()
, which only returns default values(T, U)
::either::Either<T, U>
Option<T>
()
. Might be useful to make a minimum memory size struct when combined with (T, U)
message types.
Into<SomeIntType>
support for numerical fields
Clone
, Debug
, PartialEq
(currently we are just deriving those traits when defining a struct, but instead we should write a manual implementation which is only available when the field impls those traits).allocator_api
. Waiting for the String
support