Giter Club home page Giter Club 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,
    });
}

rust-protobuf-macros's People

Contributors

herrernst avatar lalitmaganti avatar lawrencejones avatar plietar avatar sl33nyc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

rust-protobuf-macros's Issues

Error: mismatched types with Rust nightly

Running Rust nightly:

$ rustc --version
rustc 1.7.0-dev (a79008f4f 2016-01-07)
$ cargo build

Output:

<std macros>:3:1: 3:39 error: mismatched types:
 expected `()`,
    found `core::result::Result<_, _>`
(expected (),
    found enum `core::result::Result`) [E0308]
<std macros>:3 $ crate:: result:: Result:: Ok ( val ) => val , $ crate:: result:: Result::
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/joerg/.cargo/git/checkouts/rust-protobuf-macros-0155ebac4efea846/master/src/util.rs:157:17: 157:42 note: in this expansion of try! (defined in <std macros>)

I don't know much about Rust (yet), so I cannot help here.

unresolved import `rustc::plugin::Registry`. Could not find `plugin` in `rustc`

I tried to compile librespot directly on raspberry pi with rustc 1.6.0-dev (e4066f5f4 2015-12-03) from the prebuilt Rust binaries available at https://github.com/warricksothr/RustBuild and got this:

/home/user/.cargo/git/checkouts/rust-protobuf-macros-eb90ab0bc4dc65d2/master/src/lib.rs:10:5: 10:28 error: unresolved import rustc::plugin::Registry. Could not find `plugin` in `rustc` [E0432]
/home/user/.cargo/git/checkouts/rust-protobuf-macros-eb90ab0bc4dc65d2/master/src/lib.rs:10 use rustc::plugin::Registry;
                                                                                               ^~~~~~~~~~~~~~~~~~~~~~~

Probably isn't specific to ARM, also happened to other users: plietar/librespot#11 https://github.com/sashahilton00/spotify-connect-resources/issues/2#issuecomment-164013366

error: mismatched types:

Hi,

I am trying to compile librespot on my Raspberry Pi 2 running Raspbian Jessie using the rust binaries from here: https://github.com/warricksothr/RustBuild and it fails with the following errors

``` /root/.cargo/git/checkouts/rust-protobuf-macros-eb90ab0bc4dc65d2/master/src/protobuf_init.rs:49:5:`

49:20 error: mismatched types:
expected syntax::ptr::P<syntax::codemap::Spanned<syntax::ast::StmtKind>>,
found syntax::codemap::Spanned<syntax::ast::StmtKind>
(expected struct syntax::ptr::P,
found struct syntax::codemap::Spanned) [E0308]
/root/.cargo/git/checkouts/rust-protobuf-macros-eb90ab0bc4dc65d2/master/src/protobuf_init.rs:49 cx.stmt_expr(e)
^~~~~~~~~~~~~~~
/root/.cargo/git/checkouts/rust-protobuf-macros-eb90ab0bc4dc65d2/master/src/protobuf_init.rs:49:5: 49:20 help: run rustc --explain E0308 to see a detailed explanation
/root/.cargo/git/checkouts/rust-protobuf-macros-eb90ab0bc4dc65d2/master/src/protobuf_init.rs:73:28: 73:72 error: mismatched types:
expected syntax::codemap::Spanned<syntax::ast::StmtKind>,
found syntax::ptr::P<syntax::codemap::Spanned<syntax::ast::StmtKind>>
(expected struct syntax::codemap::Spanned,
found struct syntax::ptr::P) [E0308]
/root/.cargo/git/checkouts/rust-protobuf-macros-eb90ab0bc4dc65d2/master/src/protobuf_init.rs:73 stmts.push(emit_repeated(cx, sp, v, e_repeated.clone()))
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/root/.cargo/git/checkouts/rust-protobuf-macros-eb90ab0bc4dc65d2/master/src/protobuf_init.rs:73:28: 73:72 help: run rustc --explain E0308 to see a detailed explanation
/root/.cargo/git/checkouts/rust-protobuf-macros-eb90ab0bc4dc65d2/master/src/protobuf_bind.rs:78:17: 78:48 error: mismatched types:
expected syntax::codemap::Spanned<syntax::ast::StmtKind>,
found syntax::ptr::P<syntax::codemap::Spanned<syntax::ast::StmtKind>>
(expected struct syntax::codemap::Spanned,
found struct syntax::ptr::P) [E0308]
/root/.cargo/git/checkouts/rust-protobuf-macros-eb90ab0bc4dc65d2/master/src/protobuf_bind.rs:78 cx.stmt_let_pat(sp, pat, value)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/root/.cargo/git/checkouts/rust-protobuf-macros-eb90ab0bc4dc65d2/master/src/protobuf_bind.rs:78:17: 78:48 help: run rustc --explain E0308 to see a detailed explanation
error: aborting due to 3 previous errors`


My version of rustc is the following one: rustc 1.8.0-dev (f9543a9b7 2016-02-15)
Does it mean that the issue comes from rust-protobuf-macros ?

Regards,

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.