From 49b9c675abd2be96a8426038c128bb46acc76240 Mon Sep 17 00:00:00 2001 From: Brent Gardner Date: Fri, 23 Sep 2022 05:35:48 -0700 Subject: [PATCH] Fix docs.rs (#3580) * I think this will fix docs.rs but no idea how to test * Generate to correct location * Clippy * Alphabetize * Feature -> cfg * New clippy is more demanding --- datafusion/proto/Cargo.toml | 3 +++ datafusion/proto/build.rs | 30 +++++++++++++++------------ datafusion/proto/src/generated/mod.rs | 13 +++++++----- datafusion/proto/src/lib.rs | 3 --- 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/datafusion/proto/Cargo.toml b/datafusion/proto/Cargo.toml index 8720ee70b186..adab4b356977 100644 --- a/datafusion/proto/Cargo.toml +++ b/datafusion/proto/Cargo.toml @@ -28,6 +28,9 @@ keywords = ["arrow", "query", "sql"] edition = "2021" rust-version = "1.62" +[package.metadata.docs.rs] +rustc-args = ["--cfg", "docsrs"] + [lib] name = "datafusion_proto" path = "src/lib.rs" diff --git a/datafusion/proto/build.rs b/datafusion/proto/build.rs index 38ae9a962f77..3efd71350300 100644 --- a/datafusion/proto/build.rs +++ b/datafusion/proto/build.rs @@ -28,12 +28,11 @@ fn main() -> Result<(), String> { Ok(()) } -#[cfg(feature = "json")] fn build() -> Result<(), String> { use std::io::Write; let out = std::path::PathBuf::from( - std::env::var("OUT_DIR").expect("Cannot find OUT_DIR environment vairable"), + std::env::var("OUT_DIR").expect("Cannot find OUT_DIR environment variable"), ); let descriptor_path = out.join("proto_descriptor.bin"); @@ -44,9 +43,11 @@ fn build() -> Result<(), String> { .compile_protos(&["proto/datafusion.proto"], &["proto"]) .map_err(|e| format!("protobuf compilation failed: {}", e))?; + #[cfg(feature = "json")] let descriptor_set = std::fs::read(&descriptor_path) .expect(&*format!("Cannot read {:?}", &descriptor_path)); + #[cfg(feature = "json")] pbjson_build::Builder::new() .register_descriptors(&descriptor_set) .expect(&*format!( @@ -58,22 +59,25 @@ fn build() -> Result<(), String> { // .serde.rs is not a valid package name, so append to datafusion.rs so we can treat it normally let proto = std::fs::read_to_string(out.join("datafusion.rs")).unwrap(); + + #[cfg(feature = "json")] let json = std::fs::read_to_string(out.join("datafusion.serde.rs")).unwrap(); + + #[cfg(feature = "docsrs")] + let path = out.join("datafusion.rs"); + #[cfg(not(feature = "docsrs"))] + let path = "src/generated/datafusion.rs"; + let mut file = std::fs::OpenOptions::new() .write(true) + .truncate(true) .create(true) - .open("src/generated/datafusion_json.rs") + .open(path) .unwrap(); - file.write(proto.as_str().as_ref()).unwrap(); - file.write(json.as_str().as_ref()).unwrap(); + file.write_all(proto.as_str().as_ref()).unwrap(); - Ok(()) -} + #[cfg(feature = "json")] + file.write_all(json.as_str().as_ref()).unwrap(); -#[cfg(not(feature = "json"))] -fn build() -> Result<(), String> { - prost_build::Config::new() - .out_dir("src/generated") - .compile_protos(&["proto/datafusion.proto"], &["proto"]) - .map_err(|e| format!("protobuf compilation failed: {}", e)) + Ok(()) } diff --git a/datafusion/proto/src/generated/mod.rs b/datafusion/proto/src/generated/mod.rs index f3154b265711..bf58e9687d79 100644 --- a/datafusion/proto/src/generated/mod.rs +++ b/datafusion/proto/src/generated/mod.rs @@ -15,13 +15,16 @@ // specific language governing permissions and limitations // under the License. -// include the generated protobuf source as a submodule #[allow(clippy::all)] #[rustfmt::skip] -#[cfg(not(feature = "json"))] +#[cfg(not(docsrs))] pub mod datafusion; +#[cfg(docsrs)] #[allow(clippy::all)] -#[rustfmt::skip] -#[cfg(feature = "json")] -pub mod datafusion_json; +pub mod datafusion { + include!(concat!(env!("OUT_DIR"), "/datafusion.rs")); + + #[cfg(feature = "json")] + include!(concat!(env!("OUT_DIR"), "/datafusion.serde.rs")); +} diff --git a/datafusion/proto/src/lib.rs b/datafusion/proto/src/lib.rs index 4dc496932fe2..feefbe27d4d9 100644 --- a/datafusion/proto/src/lib.rs +++ b/datafusion/proto/src/lib.rs @@ -25,10 +25,7 @@ pub mod generated; pub mod logical_plan; pub mod to_proto; -#[cfg(not(feature = "json"))] pub use generated::datafusion as protobuf; -#[cfg(feature = "json")] -pub use generated::datafusion_json as protobuf; #[cfg(doctest)] doc_comment::doctest!("../README.md", readme_example_test);