1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- extern crate protobuf_codegen;
- extern crate protobuf_codegen_pure;
- use std::fs::{read_to_string, write};
- use std::path::Path;
- use protobuf_codegen_pure::parse_and_typecheck;
- use protobuf_codegen_pure::Customize;
- fn main() {
- let customizations = Customize {
- ..Default::default()
- };
- let lib_str = read_to_string("src/lib.rs").unwrap();
-
- for line in lib_str.lines() {
- if !line.starts_with("pub mod ") && !line.starts_with("mod ") {
- continue;
- }
- let len = line.len();
- let name;
- if line.starts_with("pub mod ") {
- name = &line[8..len - 1];
- } else {
- name = &line[4..len - 1];
- }
-
- let src_fname = &format!("proto/{}.proto", name);
- let dest_fname = &format!("src/{}.rs", name);
- let src = Path::new(src_fname);
- let dest = Path::new(dest_fname);
-
- let mut existing = "".to_string();
- if dest.exists() {
-
- existing = read_to_string(dest).unwrap().replace("\r\n", "\n");
- }
- println!("Regenerating {} from {}", dest.display(), src.display());
-
- let p = parse_and_typecheck(&[&Path::new("proto")], &[src]).expect("protoc");
-
-
- let result = protobuf_codegen::gen(&p.file_descriptors, &p.relative_paths, &customizations);
-
- let new = &result.first().unwrap().content;
-
- let new = std::str::from_utf8(&new).unwrap();
-
- if new != existing {
- write(dest, &new).unwrap();
- }
- }
- }
|