1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use std::io::stdin;
use std::io::stdout;
use std::str;
use plugin::*;
use protobuf::parse_from_reader;
use protobuf::Message;
use protobuf::descriptor::FileDescriptorProto;


pub struct GenResult {
    pub name: String,
    pub content: Vec<u8>,
}

pub fn plugin_main(
    gen: fn(file_descriptors: &[FileDescriptorProto], files_to_generate: &[String])
        -> Vec<GenResult>,
) {
    let req = parse_from_reader::<CodeGeneratorRequest>(&mut stdin()).unwrap();
    let result = gen(req.get_proto_file(), req.get_file_to_generate());
    let mut resp = CodeGeneratorResponse::new();
    resp.set_file(
        result
            .iter()
            .map(|file| {
                let mut r = CodeGeneratorResponse_File::new();
                r.set_name(file.name.to_string());
                r.set_content(str::from_utf8(file.content.as_ref()).unwrap().to_string());
                r
            })
            .collect(),
    );
    resp.write_to_writer(&mut stdout()).unwrap();
}