protobuf_message_factory

Crates.ioprotobuf_message_factory
lib.rsprotobuf_message_factory
version0.1.3
sourcesrc
created_at2019-09-08 06:13:15.89575
updated_at2019-09-10 15:19:13.926407
descriptiongenerate a message factory to create message by message name
homepagehttps://github.com/egmkang/protobuf_message_factory
repositoryhttps://github.com/egmkang/protobuf_message_factory
max_upload_size
id163163
size11,415
egmkang wang (egmkang)

documentation

README

protobuf_message_factory

this repo provide you a way to generate a message factory to create a message instance by message name.

//use can do this in cpp
google::protobuf::Descriptor* desc =
    google::protobuf::DescriptorPool::generated_pool()
        ->FindMessageTypeByName("mypkg.MyType");
google::protobuf::Message* message =
    google::protobuf::MessageFactory::generated_factory()
        ->GetPrototype(desc)->New();
extern crate proto;

use proto::factory::*;

//now you can do this in rust
let desc = get_descriptor(&"mypkg.MyType".to_string()).unwrap();
let message = desc.new_instance();

API Docs: https://docs.rs/protobuf_message_factory

Usage

Step 1

create a project to generate proto

$ cargo new proto

Add this to Cargo.toml:

[dependencies]
protobuf = "2.8.0"

[build-dependencies]
protoc-rust = "2.8.0"
protobuf_message_factory = "0.1.3"

Step 2

add .proto file into src/

Step 3

add codes to build.rs

extern crate protobuf_message_factory;

use protobuf_message_factory::*;

...

fn main() {

    let proto_path = "src/";

    let proto_files = get_protos_info(proto_path);
    let proto_messages = get_proto_list(&proto_files);


    //!!!   this is importent.   !!!
    protoc_rust::run(protoc_rust::Args {
        out_dir: proto_path,
        input: &protos,
        includes: &[proto_path],
        customize: Customize {
          ..Default::default()
        },
    }).expect("protoc");

    //now generate factory codes
    generate_factory_file(proto_path, &proto_files);
}

Step 4

add proto deps into your project's toml

[dependencies]
proto = {version="^0", path="proto_path"}

step 1 create a proj named proto, replace proto_path into yours

License

MIT

Commit count: 5

cargo fmt