From 73c0c9f9ad5fd50c173a3c7b8213d030fdb4e0e4 Mon Sep 17 00:00:00 2001 From: Zak Stucke Date: Fri, 16 Feb 2024 17:17:52 +0200 Subject: [PATCH] Adding features to docs --- .github/workflows/tests.yml | 4 ++-- dev_scripts/docs.sh | 2 +- rust/Cargo.lock | 1 + rust/Cargo.toml | 8 ++++++++ rust/bitbazaar/lib.rs | 2 ++ rust/build.rs | 14 ++++++++++++++ 6 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 rust/build.rs diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index b5ec752a..fc082da5 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -95,8 +95,8 @@ jobs: with: node-version: "20" - - name: Install rust toolchain - uses: dtolnay/rust-toolchain@stable + - name: Install rust toolchain (doc builds use nightly features) + uses: dtolnay/rust-toolchain@nightly - name: Install dependencies run: | diff --git a/dev_scripts/docs.sh b/dev_scripts/docs.sh index d04c7b11..2eda08bd 100755 --- a/dev_scripts/docs.sh +++ b/dev_scripts/docs.sh @@ -15,7 +15,7 @@ js_sub_build () { # Builds the nested js site: rust_sub_build () { - rm -rf ./docs/rust_ref && cargo doc --no-deps --manifest-path ./rust/Cargo.toml --target-dir ./docs/rust_ref + rm -rf ./docs/rust_ref && cargo +nightly doc --no-deps --manifest-path ./rust/Cargo.toml --target-dir ./docs/rust_ref --all-features } build () { diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 20138bdd..7503b983 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -204,6 +204,7 @@ dependencies = [ "redis", "regex", "rstest", + "rustc_version", "serde", "serde_json", "serial_test", diff --git a/rust/Cargo.toml b/rust/Cargo.toml index dcf19ef0..428ede3b 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -11,6 +11,14 @@ name = "bitbazaar" crate-type = ["lib"] path = "bitbazaar/lib.rs" +[build-dependencies] +rustc_version = "0.4.0" + +[package.metadata.docs.rs] +# We use feature(doc_auto_cfg) so show which features needed for which code. +# For the features to show in docs.rs, need to tell it to include them: +all-features = true + [features] cli = ['dep:normpath', 'dep:conch-parser', 'dep:homedir'] redis = ['dep:deadpool-redis', 'dep:redis', 'dep:sha1_smol'] diff --git a/rust/bitbazaar/lib.rs b/rust/bitbazaar/lib.rs index 86128abf..6d7eb2c6 100644 --- a/rust/bitbazaar/lib.rs +++ b/rust/bitbazaar/lib.rs @@ -1,5 +1,7 @@ #![warn(clippy::disallowed_types)] #![warn(missing_docs)] +// https://stackoverflow.com/questions/61417452/how-to-get-a-feature-requirement-tag-in-the-documentation-generated-by-cargo-do +#![cfg_attr(all(doc, CHANNEL_NIGHTLY), feature(doc_auto_cfg))] //! bitbazaar - An assortment of publicly available cross-language utilities useful to my projects. diff --git a/rust/build.rs b/rust/build.rs new file mode 100644 index 00000000..073426c9 --- /dev/null +++ b/rust/build.rs @@ -0,0 +1,14 @@ +use rustc_version::{version_meta, Channel}; + +// This build script sets the `cfg` flag `CHANNEL_STABLE`, `CHANNEL_BETA`, `CHANNEL_NIGHTLY` or `CHANNEL_DEV` depending on the release channel of the compiler being used. +// https://stackoverflow.com/questions/61417452/how-to-get-a-feature-requirement-tag-in-the-documentation-generated-by-cargo-do +fn main() { + // Set cfg flags depending on release channel + let channel = match version_meta().unwrap().channel { + Channel::Stable => "CHANNEL_STABLE", + Channel::Beta => "CHANNEL_BETA", + Channel::Nightly => "CHANNEL_NIGHTLY", + Channel::Dev => "CHANNEL_DEV", + }; + println!("cargo:rustc-cfg={}", channel) +}