1 // TODO: move into separate crate
2 #![doc(hidden)]
3
4 use crate::descriptor::FileDescriptorProto;
5 use crate::plugin::*;
6 use crate::Message;
7 use std::io::stdin;
8 use std::io::stdout;
9 use std::str;
10
11 pub struct GenRequest<'a> {
12 pub file_descriptors: &'a [FileDescriptorProto],
13 pub files_to_generate: &'a [String],
14 pub parameter: &'a str,
15 }
16
17 pub struct GenResult {
18 pub name: String,
19 pub content: Vec<u8>,
20 }
21
plugin_main<F>(gen: F) where F: Fn(&[FileDescriptorProto], &[String]) -> Vec<GenResult>,22 pub fn plugin_main<F>(gen: F)
23 where
24 F: Fn(&[FileDescriptorProto], &[String]) -> Vec<GenResult>,
25 {
26 plugin_main_2(|r| gen(r.file_descriptors, r.files_to_generate))
27 }
28
plugin_main_2<F>(gen: F) where F: Fn(&GenRequest) -> Vec<GenResult>,29 pub fn plugin_main_2<F>(gen: F)
30 where
31 F: Fn(&GenRequest) -> Vec<GenResult>,
32 {
33 let req = CodeGeneratorRequest::parse_from_reader(&mut stdin()).unwrap();
34 let result = gen(&GenRequest {
35 file_descriptors: &req.get_proto_file(),
36 files_to_generate: &req.get_file_to_generate(),
37 parameter: req.get_parameter(),
38 });
39 let mut resp = CodeGeneratorResponse::new();
40 resp.set_file(
41 result
42 .iter()
43 .map(|file| {
44 let mut r = CodeGeneratorResponse_File::new();
45 r.set_name(file.name.to_string());
46 r.set_content(str::from_utf8(file.content.as_ref()).unwrap().to_string());
47 r
48 })
49 .collect(),
50 );
51 resp.write_to_writer(&mut stdout()).unwrap();
52 }
53