https://github.com/plietar/rust-protobuf-macros
Easy protobuf in rust
https://github.com/plietar/rust-protobuf-macros
Last synced: about 1 year ago
JSON representation
Easy protobuf in rust
- Host: GitHub
- URL: https://github.com/plietar/rust-protobuf-macros
- Owner: plietar
- Created: 2015-04-24T16:36:15.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2017-02-09T01:07:45.000Z (over 9 years ago)
- Last Synced: 2024-04-14T19:43:42.513Z (about 2 years ago)
- Language: Rust
- Homepage:
- Size: 40 KB
- Stars: 30
- Watchers: 4
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# rust-protobuf-macros
Macros designed to make protobuf easier to use in rust.
The macros use [stepancheg/rust-protobuf](https://github.com/stepancheg/rust-protobuf),
but provide a simpler syntax to set and get fields from protobuf objects.
## Installation
### Rust nightly
Add to your `Cargo.toml`
```toml
[dependencies.protobuf]
git = "https://github.com/stepancheg/rust-protobuf.git"
[dependencies.protobuf_macros]
git = "https://github.com/plietar/rust-protobuf-macros.git"
```
Then enable it in your crate :
```rust
#![feature(plugin)]
#![plugin(protobuf_macros)]
```
### Rust stable
The plugin can be used on rust stable using [syntex](https://github.com/serde-rs/syntex) :
Add to your `Cargo.toml` :
```toml
[dependencies.protobuf]
git = "https://github.com/stepancheg/rust-protobuf.git"
[build-dependencies.protobuf_macros]
git = "https://github.com/plietar/rust-protobuf-macros.git"
```
And add a build script :
```rust
extern crate protobuf_macros;
use std::env;
use std::path::PathBuf;
fn main() {
let out = PathBuf::from(env::var("OUT_DIR").unwrap());
protobuf_macros::expand("src/lib.in.rs", &out.join("lib.rs")).unwrap();
}
```
## Usage
The examples use the following schema :
```protobuf
message Person {
required string name = 1;
required int32 id = 2;
repeated string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}
repeated PhoneNumber phone = 4;
message Job {
required string title = 1;
required string company = 2;
}
optional Job job = 5;
}
message AddressBook {
repeated Person person = 1;
}
```
### protobuf_init!
The `protobuf_init!` macro is used to fill an existing protobuf object.
```rust
let person = protobuf_init!(Person::new(), {
name: "Joe",
id: 42,
email => [
"joe@domain.com",
"joe@other.com"
],
phone => [
@{
number: "0123456789",
field_type: Person_PhoneType::HOME
},
@{
number: "9876543210",
field_type: Person_PhoneType::WORK
}
]
job => {
title: "Boss",
company: "Big Corp"
}
});
```
### protobuf_bind!
The `protobuf_bind!` macro is used to extract data from a protobuf object.
The variable names on the right are created by the macro using a let statement.
```rust
protobuf_bind!(person, {
name: person_name,
id: person_id,
email: emails,
phone: phone_numbers,
job => {
title: job_title,
company: company
}
});
```
Note that it is not possible to extract the repeated fields. Instead, you will get
a `RepeatedField` object. You can iterate on it, and call `protobuf_bind!` again
on its elements.
```rust
for phone in phone_numbers {
protobuf_bind!(phone, {
number: number,
field_type: phone_type,
});
}
```