Skip to content

Commit

Permalink
Rename the feature flags and make code generation opt-in
Browse files Browse the repository at this point in the history
  • Loading branch information
nrc committed Feb 25, 2019
1 parent 480fcca commit 7539110
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ name = "kvproto"
path = "src/lib.rs"

[features]
default = ["proto-buf"]
proto-buf = ["protobuf"]
prost-buf = ["prost", "prost-derive", "bytes"]
default = ["lib-rust-protobuf"]
lib-rust-protobuf = ["protobuf"]
lib-prost = ["prost", "prost-derive", "bytes"]
regenerate = []

[dependencies]
protobuf = { version = "~2.0", optional = true }
Expand Down
20 changes: 15 additions & 5 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,21 @@

use regex::Regex;
use std::env;
use std::fs::read_dir;
use std::fs::File;
use std::fs::{read_dir, remove_file};
use std::io::{Read, Write};
use std::process::Command;

fn main() {
// This build script creates files in the `src` directory. Since that is
// outside Cargo's OUT_DIR it will cause an error when this crate is used
// as a dependency. Therefore, the user must opt-in to regenerating the
// Rust files.
if env::var_os("CARGO_FEATURE_REGENERATE").is_none() {
println!("cargo:rerun-if-changed=build.rs");
return;
}

let buf_lib = BufferLib::from_env_vars();
if buf_lib == BufferLib::Prost {
unimplemented!("Prost support is not yet implemented");
Expand Down Expand Up @@ -46,7 +55,7 @@ fn main() {
}

if buf_lib == BufferLib::Protobuf {
let mod_names: Vec<_> = read_dir("src/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();
Expand All @@ -58,6 +67,7 @@ fn main() {
})
.collect();
mod_names.sort();

replace_read_unknown_fields(&mod_names);
generate_protobuf_rs(&mod_names);
}
Expand All @@ -72,11 +82,11 @@ enum BufferLib {
impl BufferLib {
fn from_env_vars() -> BufferLib {
match (
env::var_os("CARGO_FEATURE_PROST_BUF"),
env::var_os("CARGO_FEATURE_PROTO_BUF"),
env::var_os("CARGO_FEATURE_LIB_PROST"),
env::var_os("CARGO_FEATURE_LIB_RUST_PROTOBUF"),
) {
(Some(_), Some(_)) | (None, None) => {
panic!("You must use exactly one of `proto-buf` and `prost-buf` features")
panic!("You must use exactly one of `lib-rust-protobuf` and `prost-buf` features")
}
(Some(_), _) => BufferLib::Prost,
(_, Some(_)) => BufferLib::Protobuf,
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7539110

Please sign in to comment.