Code Monkey home page Code Monkey logo

rust-protobuf-macros's Introduction

rust-protobuf-macros

Macros designed to make protobuf easier to use in rust.

The macros use stepancheg/rust-protobuf, but provide a simpler syntax to set and get fields from protobuf objects.

Installation

Rust nightly

Add to your Cargo.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 :

#![feature(plugin)]
#![plugin(protobuf_macros)]

Rust stable

The plugin can be used on rust stable using syntex :

Add to your Cargo.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 :

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 :

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.

let person = protobuf_init!(Person::new(), {
    name: "Joe",
    id: 42,
    email => [
        "[email protected]",
        "[email protected]"
    ],
    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.

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.

for phone in phone_numbers {
    protobuf_bind!(phone, {
        number: number,
        field_type: phone_type,
    });
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.