From 8a4612f6bb48c8fad73143ebaa8408c01f68a69e Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Mon, 4 Feb 2019 16:09:51 +1300 Subject: [PATCH] Refactor build script As suggested in https://github.com/pingcap/raft-rs/pull/175 --- build.rs | 68 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/build.rs b/build.rs index 608596459..b8cefd86c 100644 --- a/build.rs +++ b/build.rs @@ -28,11 +28,6 @@ fn main() { return; } - let buf_lib = BufferLib::from_env_vars(); - if buf_lib == BufferLib::Prost { - unimplemented!("Prost support is not yet implemented"); - } - check_protoc_version(); let file_names: Vec<_> = read_dir("proto") @@ -50,26 +45,29 @@ fn main() { println!("cargo:rerun-if-changed={}", f); } - if buf_lib == BufferLib::Protobuf { - generate_protobuf_files(file_names); - } - - if buf_lib == BufferLib::Protobuf { - let mut mod_names: Vec<_> = read_dir("src/protobuf") - .expect("Couldn't read src directory") - .filter_map(|e| { - let file_name = e.expect("Couldn't list file").file_name(); - file_name - .to_string_lossy() - .split(".rs") - .next() - .map(|n| n.to_owned()) - }) - .collect(); - mod_names.sort(); - - replace_read_unknown_fields(&mod_names); - generate_protobuf_rs(&mod_names); + match BufferLib::from_env_vars() { + BufferLib::Protobuf => { + generate_protobuf_files(file_names); + + let mut mod_names: Vec<_> = read_dir("src/protobuf") + .expect("Couldn't read src directory") + .filter_map(|e| { + let file_name = e.expect("Couldn't list file").file_name(); + file_name + .to_string_lossy() + .split(".rs") + .next() + .map(|n| n.to_owned()) + }) + .collect(); + mod_names.sort(); + + replace_read_unknown_fields(&mod_names); + generate_protobuf_rs(&mod_names); + } + BufferLib::Prost => { + unimplemented!("Prost support is not yet implemented"); + } } } @@ -137,14 +135,18 @@ fn replace_read_unknown_fields(mod_names: &[String]) { .expect("Couldn't read source file"); } - let text = regex.replace_all( - &text, - "if $1 == ::protobuf::wire_format::WireTypeVarint {\ - $3 = $2.read_enum()?;\ - } else {\ - return ::std::result::Result::Err(::protobuf::rt::unexpected_wire_type(wire_type));\ - }", - ); + // FIXME Rustfmt bug in string literals + #[rustfmt::skip] + let text = { + regex.replace_all( + &text, + "if $1 == ::protobuf::wire_format::WireTypeVarint {\ + $3 = $2.read_enum()?;\ + } else {\ + return ::std::result::Result::Err(::protobuf::rt::unexpected_wire_type(wire_type));\ + }", + ) + }; let mut out = File::create(file_name).unwrap(); out.write_all(text.as_bytes()) .expect("Could not write source file");