From 70483827062b3217fcccfb107b1f6edd115e4106 Mon Sep 17 00:00:00 2001 From: Niklas Adolfsson Date: Fri, 29 Dec 2023 16:57:10 +0100 Subject: [PATCH 1/8] generate docs for feature-gated items on crates.io --- codegen/Cargo.toml | 7 +++++++ codegen/src/lib.rs | 2 ++ lightclient/Cargo.toml | 8 ++++++++ lightclient/src/lib.rs | 3 +++ signer/Cargo.toml | 8 ++++++++ signer/src/lib.rs | 4 ++++ signer/src/sr25519.rs | 1 + subxt/Cargo.toml | 7 +++++++ subxt/src/backend/rpc/mod.rs | 1 + subxt/src/backend/rpc/rpc_client.rs | 1 + subxt/src/client/light_client/builder.rs | 2 ++ subxt/src/client/mod.rs | 2 ++ subxt/src/client/online_client.rs | 1 + subxt/src/config/mod.rs | 1 + subxt/src/error/mod.rs | 2 ++ subxt/src/lib.rs | 4 ++++ subxt/src/tx/mod.rs | 1 + subxt/src/tx/signer.rs | 1 + 18 files changed, 56 insertions(+) diff --git a/codegen/Cargo.toml b/codegen/Cargo.toml index aae5ffec34..9a03fcac27 100644 --- a/codegen/Cargo.toml +++ b/codegen/Cargo.toml @@ -40,3 +40,10 @@ bitvec = { workspace = true } scale-info = { workspace = true, features = ["bit-vec"] } pretty_assertions = { workspace = true } frame-metadata = { workspace = true } + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = ["--cfg", "docsrs"] + +[package.metadata.playground] +all-features = true \ No newline at end of file diff --git a/codegen/src/lib.rs b/codegen/src/lib.rs index e319b1aaff..62400b0372 100644 --- a/codegen/src/lib.rs +++ b/codegen/src/lib.rs @@ -7,6 +7,7 @@ //! be used directly if preferable. #![deny(unused_crate_dependencies, missing_docs)] +#![cfg_attr(docsrs, feature(doc_cfg))] mod api; mod ir; @@ -18,6 +19,7 @@ pub mod error; // macro and CLI tool, so they only live here because this is a common // crate that both depend on. #[cfg(feature = "fetch-metadata")] +#[cfg_attr(docsrs, doc(cfg(feature = "fetch-metadata")))] pub mod fetch_metadata; #[cfg(feature = "web")] diff --git a/lightclient/Cargo.toml b/lightclient/Cargo.toml index 1cc3b2310b..9033a8859c 100644 --- a/lightclient/Cargo.toml +++ b/lightclient/Cargo.toml @@ -80,3 +80,11 @@ pin-project = { workspace = true, optional = true } # Included if "web" feature is enabled, to enable its js feature. getrandom = { workspace = true, optional = true } + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = ["--cfg", "docsrs"] + +[package.metadata.playground] +all-features = true + diff --git a/lightclient/src/lib.rs b/lightclient/src/lib.rs index ee3f727e26..34800fe4f0 100644 --- a/lightclient/src/lib.rs +++ b/lightclient/src/lib.rs @@ -10,6 +10,8 @@ //! //! This leverages the smoldot crate to connect to the chain. +#![cfg_attr(docsrs, feature(doc_cfg))] + #[cfg(any( all(feature = "web", feature = "native"), not(any(feature = "web", feature = "native")) @@ -35,6 +37,7 @@ pub mod smoldot { }; #[cfg(feature = "native")] + #[cfg_attr(docsrs, doc(cfg(feature = "native")))] pub use smoldot_light::platform::default::DefaultPlatform; } diff --git a/signer/Cargo.toml b/signer/Cargo.toml index 11f6e5efdd..41098770a3 100644 --- a/signer/Cargo.toml +++ b/signer/Cargo.toml @@ -58,3 +58,11 @@ sp-keyring = { workspace = true } [package.metadata.cargo-machete] ignored = ["getrandom"] + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = ["--cfg", "docsrs"] + +[package.metadata.playground] +all-features = true + diff --git a/signer/src/lib.rs b/signer/src/lib.rs index 77cf5b3d41..2c79d6a1ec 100644 --- a/signer/src/lib.rs +++ b/signer/src/lib.rs @@ -13,16 +13,20 @@ //! Enable the `subxt` feature to enable use of this [`sr25519::Keypair`] in signing //! subxt transactions for chains supporting sr25519 signatures. +#![cfg_attr(docsrs, feature(doc_cfg))] + #[macro_use] mod utils; mod crypto; // An sr25519 key pair implementation. #[cfg(feature = "sr25519")] +#[cfg_attr(docsrs, doc(cfg(feature = "sr25519")))] pub mod sr25519; // An ecdsa key pair implementation. #[cfg(feature = "ecdsa")] +#[cfg_attr(docsrs, doc(cfg(feature = "ecdsa")))] pub mod ecdsa; // Re-export useful bits and pieces for generating a Pair from a phrase, diff --git a/signer/src/sr25519.rs b/signer/src/sr25519.rs index d3172e519b..01e6cc84b1 100644 --- a/signer/src/sr25519.rs +++ b/signer/src/sr25519.rs @@ -245,6 +245,7 @@ pub mod dev { // Make `Keypair` usable to sign transactions in Subxt. This is optional so that // `subxt-signer` can be used entirely independently of Subxt. #[cfg(feature = "subxt")] +#[cfg_attr(docsrs, doc(cfg(feature = "subxt")))] mod subxt_compat { use super::*; diff --git a/subxt/Cargo.toml b/subxt/Cargo.toml index cc8ff5820c..bb3b9d60cb 100644 --- a/subxt/Cargo.toml +++ b/subxt/Cargo.toml @@ -122,3 +122,10 @@ required-features = ["unstable-light-client", "jsonrpsee"] name = "light_client_parachains" path = "examples/light_client_parachains.rs" required-features = ["unstable-light-client", "jsonrpsee", "native"] + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = ["--cfg", "docsrs"] + +[package.metadata.playground] +all-features = true \ No newline at end of file diff --git a/subxt/src/backend/rpc/mod.rs b/subxt/src/backend/rpc/mod.rs index 0368a4a5c5..693d96a3a4 100644 --- a/subxt/src/backend/rpc/mod.rs +++ b/subxt/src/backend/rpc/mod.rs @@ -57,6 +57,7 @@ #![allow(clippy::module_inception)] #[cfg(feature = "jsonrpsee")] +#[cfg_attr(docsrs, doc(cfg(feature = "jsonrpsee")))] mod jsonrpsee_impl; mod rpc_client; diff --git a/subxt/src/backend/rpc/rpc_client.rs b/subxt/src/backend/rpc/rpc_client.rs index 0e4bb62940..7f6d978340 100644 --- a/subxt/src/backend/rpc/rpc_client.rs +++ b/subxt/src/backend/rpc/rpc_client.rs @@ -19,6 +19,7 @@ pub struct RpcClient { impl RpcClient { #[cfg(feature = "jsonrpsee")] + #[cfg_attr(docsrs, doc(cfg(feature = "jsonrpsee")))] /// Create a default RPC client pointed at some URL, currently based on [`jsonrpsee`]. pub async fn from_url>(url: U) -> Result { let client = jsonrpsee_helpers::client(url.as_ref()) diff --git a/subxt/src/client/light_client/builder.rs b/subxt/src/client/light_client/builder.rs index da784e5cb8..023eff1f8b 100644 --- a/subxt/src/client/light_client/builder.rs +++ b/subxt/src/client/light_client/builder.rs @@ -100,6 +100,7 @@ impl LightClientBuilder { /// If smoldot panics, then the promise created will be leaked. For more details, see /// https://docs.rs/wasm-bindgen-futures/latest/wasm_bindgen_futures/fn.future_to_promise.html. #[cfg(feature = "jsonrpsee")] + #[cfg_attr(docsrs, doc(cfg(feature = "jsonrpsee")))] pub async fn build_from_url>(self, url: Url) -> Result, Error> { let chain_spec = fetch_url(url.as_ref()).await?; @@ -245,6 +246,7 @@ async fn fetch_url(url: impl AsRef) -> Result { } #[cfg(all(feature = "jsonrpsee", feature = "native"))] +#[cfg_attr(docsrs, doc(cfg(all(feature = "jsonrpsee", feature = "native"))))] mod jsonrpsee_helpers { use crate::error::{Error, LightClientError}; pub use jsonrpsee::{ diff --git a/subxt/src/client/mod.rs b/subxt/src/client/mod.rs index dbfbdd67d4..cfaa5e4aae 100644 --- a/subxt/src/client/mod.rs +++ b/subxt/src/client/mod.rs @@ -12,6 +12,7 @@ mod offline_client; mod online_client; #[cfg(feature = "unstable-light-client")] +#[cfg_attr(docsrs, doc(cfg(feature = "unstable-light-client")))] mod light_client; pub use offline_client::{OfflineClient, OfflineClientT}; @@ -20,6 +21,7 @@ pub use online_client::{ }; #[cfg(feature = "unstable-light-client")] +#[cfg_attr(docsrs, doc(cfg(feature = "unstable-light-client")))] pub use light_client::{ LightClient, LightClientBuilder, LightClientError, RawLightClient, RawLightClientBuilder, }; diff --git a/subxt/src/client/online_client.rs b/subxt/src/client/online_client.rs index 338643ebea..646f1dce46 100644 --- a/subxt/src/client/online_client.rs +++ b/subxt/src/client/online_client.rs @@ -56,6 +56,7 @@ impl std::fmt::Debug for OnlineClient { // The default constructors assume Jsonrpsee. #[cfg(feature = "jsonrpsee")] +#[cfg_attr(docsrs, doc(cfg(feature = "jsonrpsee")))] impl OnlineClient { /// Construct a new [`OnlineClient`] using default settings which /// point to a locally running node on `ws://127.0.0.1:9944`. diff --git a/subxt/src/config/mod.rs b/subxt/src/config/mod.rs index 32101d9b72..68e8b3774c 100644 --- a/subxt/src/config/mod.rs +++ b/subxt/src/config/mod.rs @@ -127,6 +127,7 @@ pub trait Header: Sized + Encode + Decode { /// implement subxt's Hasher and Header traits for some substrate structs #[cfg(feature = "substrate-compat")] +#[cfg_attr(docsrs, doc(cfg(feature = "substrate-compat")))] mod substrate_impls { use super::*; diff --git a/subxt/src/error/mod.rs b/subxt/src/error/mod.rs index a3666b1b68..7df87497f7 100644 --- a/subxt/src/error/mod.rs +++ b/subxt/src/error/mod.rs @@ -9,6 +9,7 @@ mod dispatch_error; use core::fmt::Debug; #[cfg(feature = "unstable-light-client")] +#[cfg_attr(docsrs, doc(cfg(feature = "unstable-light-client")))] pub use crate::client::LightClientError; // Re-export dispatch error types: @@ -73,6 +74,7 @@ pub enum Error { Unknown(Vec), /// Light client error. #[cfg(feature = "unstable-light-client")] + #[cfg_attr(docsrs, doc(cfg(feature = "unstable-light-client")))] #[error("An error occurred but it could not be decoded: {0}")] LightClient(#[from] LightClientError), /// Other error. diff --git a/subxt/src/lib.rs b/subxt/src/lib.rs index 939400c5e7..dd11d4795e 100644 --- a/subxt/src/lib.rs +++ b/subxt/src/lib.rs @@ -10,6 +10,8 @@ //! //! Take a look at [the Subxt guide](book) to learn more about how to use Subxt. +#![cfg_attr(docsrs, feature(doc_cfg))] + #[cfg(any( all(feature = "web", feature = "native"), not(any(feature = "web", feature = "native")) @@ -74,8 +76,10 @@ pub mod ext { pub use scale_encode; pub use scale_value; #[cfg(feature = "substrate-compat")] + #[cfg_attr(docsrs, doc(cfg(feature = "substrate-compat")))] pub use sp_core; #[cfg(feature = "substrate-compat")] + #[cfg_attr(docsrs, doc(cfg(feature = "substrate-compat")))] pub use sp_runtime; } diff --git a/subxt/src/tx/mod.rs b/subxt/src/tx/mod.rs index 039db376bf..bc76d08bfa 100644 --- a/subxt/src/tx/mod.rs +++ b/subxt/src/tx/mod.rs @@ -17,6 +17,7 @@ mod tx_progress; // The PairSigner impl currently relies on Substrate bits and pieces, so make it an optional // feature if we want to avoid needing sp_core and sp_runtime. #[cfg(feature = "substrate-compat")] +#[cfg_attr(docsrs, doc(cfg(feature = "substrate-compat")))] pub use self::signer::PairSigner; pub use self::{ diff --git a/subxt/src/tx/signer.rs b/subxt/src/tx/signer.rs index b007cbb256..fc74a185cc 100644 --- a/subxt/src/tx/signer.rs +++ b/subxt/src/tx/signer.rs @@ -25,6 +25,7 @@ pub trait Signer { } #[cfg(feature = "substrate-compat")] +#[cfg_attr(docsrs, doc(cfg(feature = "substrate-compat")))] pub use pair_signer::PairSigner; // A signer suitable for substrate based chains. This provides compatibility with Substrate From b294ec55b81052ab9136018d78a29c898b0686fb Mon Sep 17 00:00:00 2001 From: Niklas Adolfsson Date: Fri, 29 Dec 2023 17:09:51 +0100 Subject: [PATCH 2/8] add newline in Cargo.toml --- codegen/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codegen/Cargo.toml b/codegen/Cargo.toml index 9a03fcac27..e9969d8cb3 100644 --- a/codegen/Cargo.toml +++ b/codegen/Cargo.toml @@ -46,4 +46,4 @@ all-features = true rustdoc-args = ["--cfg", "docsrs"] [package.metadata.playground] -all-features = true \ No newline at end of file +all-features = true From e815d4c0afca0fa4b40204509119559d8976d248 Mon Sep 17 00:00:00 2001 From: Niklas Adolfsson Date: Fri, 29 Dec 2023 17:12:27 +0100 Subject: [PATCH 3/8] unify newlines --- codegen/Cargo.toml | 2 +- lightclient/Cargo.toml | 3 +-- signer/Cargo.toml | 1 - 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/codegen/Cargo.toml b/codegen/Cargo.toml index e9969d8cb3..9a03fcac27 100644 --- a/codegen/Cargo.toml +++ b/codegen/Cargo.toml @@ -46,4 +46,4 @@ all-features = true rustdoc-args = ["--cfg", "docsrs"] [package.metadata.playground] -all-features = true +all-features = true \ No newline at end of file diff --git a/lightclient/Cargo.toml b/lightclient/Cargo.toml index 9033a8859c..8a0e6b10f3 100644 --- a/lightclient/Cargo.toml +++ b/lightclient/Cargo.toml @@ -86,5 +86,4 @@ all-features = true rustdoc-args = ["--cfg", "docsrs"] [package.metadata.playground] -all-features = true - +all-features = true \ No newline at end of file diff --git a/signer/Cargo.toml b/signer/Cargo.toml index 41098770a3..86812d6a60 100644 --- a/signer/Cargo.toml +++ b/signer/Cargo.toml @@ -65,4 +65,3 @@ rustdoc-args = ["--cfg", "docsrs"] [package.metadata.playground] all-features = true - From 2cc5d2f82f660993f82bcbd56819f6b6b0d1878d Mon Sep 17 00:00:00 2001 From: Niklas Adolfsson Date: Tue, 2 Jan 2024 10:32:55 +0100 Subject: [PATCH 4/8] fix clippy --- codegen/src/types/mod.rs | 2 +- lightclient/src/platform/mod.rs | 2 +- subxt/src/backend/rpc/rpc_client.rs | 5 +---- testing/integration-tests/src/full_client/blocks/mod.rs | 2 +- testing/test-runtime/build.rs | 5 +---- 5 files changed, 5 insertions(+), 11 deletions(-) diff --git a/codegen/src/types/mod.rs b/codegen/src/types/mod.rs index 338da6109d..2052ac2e76 100644 --- a/codegen/src/types/mod.rs +++ b/codegen/src/types/mod.rs @@ -21,7 +21,7 @@ use crate::error::CodegenError; pub use self::{ composite_def::{CompositeDef, CompositeDefFieldType, CompositeDefFields}, derives::{Derives, DerivesRegistry}, - substitutes::{AbsolutePath, TypeSubstitutes}, + substitutes::TypeSubstitutes, type_def::TypeDefGen, type_def_params::TypeDefParameters, type_path::{TypeParameter, TypePath, TypePathType}, diff --git a/lightclient/src/platform/mod.rs b/lightclient/src/platform/mod.rs index bfba17c029..7a1182da66 100644 --- a/lightclient/src/platform/mod.rs +++ b/lightclient/src/platform/mod.rs @@ -11,7 +11,7 @@ mod wasm_platform; #[cfg(feature = "web")] mod wasm_socket; -pub use helpers::{build_platform, PlatformType}; +pub use helpers::build_platform; #[cfg(feature = "native")] mod helpers { diff --git a/subxt/src/backend/rpc/rpc_client.rs b/subxt/src/backend/rpc/rpc_client.rs index 7f6d978340..8f56e93746 100644 --- a/subxt/src/backend/rpc/rpc_client.rs +++ b/subxt/src/backend/rpc/rpc_client.rs @@ -231,10 +231,7 @@ impl Stream for RpcSubscription { mod jsonrpsee_helpers { pub use jsonrpsee::{ client_transport::ws::{Receiver, Sender, Url, WsTransportClientBuilder}, - core::{ - client::{Client, ClientBuilder}, - Error, - }, + core::{client::Client, Error}, }; /// Build WS RPC client from URL diff --git a/testing/integration-tests/src/full_client/blocks/mod.rs b/testing/integration-tests/src/full_client/blocks/mod.rs index efba9f0a14..0cb40404b7 100644 --- a/testing/integration-tests/src/full_client/blocks/mod.rs +++ b/testing/integration-tests/src/full_client/blocks/mod.rs @@ -214,7 +214,7 @@ async fn fetch_block_and_decode_extrinsic_details() { .collect::>(); // All blocks contain a timestamp; check this first: - let timestamp = block_extrinsics.get(0).unwrap(); + let timestamp = block_extrinsics.first().unwrap(); timestamp.as_root_extrinsic::().unwrap(); timestamp .as_extrinsic::() diff --git a/testing/test-runtime/build.rs b/testing/test-runtime/build.rs index 23643e491d..9a6ae962b6 100644 --- a/testing/test-runtime/build.rs +++ b/testing/test-runtime/build.rs @@ -103,10 +103,7 @@ async fn run() { mod client { pub use jsonrpsee::{ client_transport::ws::{Receiver, Sender, Url, WsTransportClientBuilder}, - core::{ - client::{Client, ClientBuilder}, - Error, - }, + core::{client::Client, Error}, }; pub use jsonrpsee::core::{client::ClientT, rpc_params}; From 9f1886d0a10dffd95be483c3ed632143bbd78438 Mon Sep 17 00:00:00 2001 From: Niklas Adolfsson Date: Tue, 9 Jan 2024 15:31:19 +0100 Subject: [PATCH 5/8] introduce macros for features --- codegen/Cargo.toml | 4 +-- lightclient/Cargo.toml | 4 +-- signer/Cargo.toml | 4 +-- subxt/Cargo.toml | 4 +-- subxt/src/backend/rpc/mod.rs | 6 ++-- subxt/src/client/light_client/builder.rs | 45 ++++++++++++------------ subxt/src/client/mod.rs | 16 ++++----- subxt/src/config/mod.rs | 41 ++++++++++----------- subxt/src/error/mod.rs | 6 ++-- subxt/src/lib.rs | 15 ++++---- subxt/src/tx/mod.rs | 8 +++-- subxt/src/tx/signer.rs | 7 ++-- 12 files changed, 83 insertions(+), 77 deletions(-) diff --git a/codegen/Cargo.toml b/codegen/Cargo.toml index 9a03fcac27..f8ff7a76af 100644 --- a/codegen/Cargo.toml +++ b/codegen/Cargo.toml @@ -42,8 +42,8 @@ pretty_assertions = { workspace = true } frame-metadata = { workspace = true } [package.metadata.docs.rs] -all-features = true +defalt-features = true rustdoc-args = ["--cfg", "docsrs"] [package.metadata.playground] -all-features = true \ No newline at end of file +defalt-features = true diff --git a/lightclient/Cargo.toml b/lightclient/Cargo.toml index 8a0e6b10f3..eb079b3e90 100644 --- a/lightclient/Cargo.toml +++ b/lightclient/Cargo.toml @@ -82,8 +82,8 @@ pin-project = { workspace = true, optional = true } getrandom = { workspace = true, optional = true } [package.metadata.docs.rs] -all-features = true +defalt-features = true rustdoc-args = ["--cfg", "docsrs"] [package.metadata.playground] -all-features = true \ No newline at end of file +defalt-features = true diff --git a/signer/Cargo.toml b/signer/Cargo.toml index 86812d6a60..226237c315 100644 --- a/signer/Cargo.toml +++ b/signer/Cargo.toml @@ -60,8 +60,8 @@ sp-keyring = { workspace = true } ignored = ["getrandom"] [package.metadata.docs.rs] -all-features = true +defalt-features = true rustdoc-args = ["--cfg", "docsrs"] [package.metadata.playground] -all-features = true +defalt-features = true diff --git a/subxt/Cargo.toml b/subxt/Cargo.toml index bb3b9d60cb..2920b3f7d8 100644 --- a/subxt/Cargo.toml +++ b/subxt/Cargo.toml @@ -124,8 +124,8 @@ path = "examples/light_client_parachains.rs" required-features = ["unstable-light-client", "jsonrpsee", "native"] [package.metadata.docs.rs] -all-features = true +defalt-features = true rustdoc-args = ["--cfg", "docsrs"] [package.metadata.playground] -all-features = true \ No newline at end of file +defalt-features = true diff --git a/subxt/src/backend/rpc/mod.rs b/subxt/src/backend/rpc/mod.rs index 693d96a3a4..12910939e7 100644 --- a/subxt/src/backend/rpc/mod.rs +++ b/subxt/src/backend/rpc/mod.rs @@ -56,9 +56,9 @@ // with other file names for their types. #![allow(clippy::module_inception)] -#[cfg(feature = "jsonrpsee")] -#[cfg_attr(docsrs, doc(cfg(feature = "jsonrpsee")))] -mod jsonrpsee_impl; +crate::macros::cfg_jsonrpsee! { + mod jsonrpsee_impl; +} mod rpc_client; mod rpc_client_t; diff --git a/subxt/src/client/light_client/builder.rs b/subxt/src/client/light_client/builder.rs index f7d5a2d883..b3180976c4 100644 --- a/subxt/src/client/light_client/builder.rs +++ b/subxt/src/client/light_client/builder.rs @@ -5,6 +5,7 @@ use super::{rpc::LightClientRpc, LightClient, LightClientError}; use crate::backend::rpc::RpcClient; use crate::client::RawLightClient; +use crate::macros::cfg_jsonrpsee; use crate::{config::Config, error::Error, OnlineClient}; use std::num::NonZeroU32; use subxt_lightclient::{smoldot, AddedChain}; @@ -245,35 +246,35 @@ async fn fetch_url(url: impl AsRef) -> Result { .map_err(|err| Error::Rpc(crate::error::RpcError::ClientError(Box::new(err)))) } -#[cfg(all(feature = "jsonrpsee", feature = "native"))] -#[cfg_attr(docsrs, doc(cfg(all(feature = "jsonrpsee", feature = "native"))))] -mod jsonrpsee_helpers { - use crate::error::{Error, LightClientError}; - pub use jsonrpsee::{ - client_transport::ws::{Receiver, Sender, Url, WsTransportClientBuilder}, - core::client::Client, - }; +cfg_jsonrpsee_native! { + mod jsonrpsee_helpers { + use crate::error::{Error, LightClientError}; + pub use jsonrpsee::{ + client_transport::ws::{Receiver, Sender, Url, WsTransportClientBuilder}, + core::client::Client, + }; - /// Build WS RPC client from URL - pub async fn client(url: &str) -> Result { - let url = Url::parse(url).map_err(|_| Error::LightClient(LightClientError::InvalidUrl))?; + /// Build WS RPC client from URL + pub async fn client(url: &str) -> Result { + let url = Url::parse(url).map_err(|_| Error::LightClient(LightClientError::InvalidUrl))?; - if url.scheme() != "ws" && url.scheme() != "wss" { - return Err(Error::LightClient(LightClientError::InvalidScheme)); - } + if url.scheme() != "ws" && url.scheme() != "wss" { + return Err(Error::LightClient(LightClientError::InvalidScheme)); + } - let (sender, receiver) = ws_transport(url).await?; + let (sender, receiver) = ws_transport(url).await?; - Ok(Client::builder() + Ok(Client::builder() .max_buffer_capacity_per_subscription(4096) .build_with_tokio(sender, receiver)) - } + } - async fn ws_transport(url: Url) -> Result<(Sender, Receiver), Error> { - WsTransportClientBuilder::default() - .build(url) - .await - .map_err(|_| Error::LightClient(LightClientError::Handshake)) + async fn ws_transport(url: Url) -> Result<(Sender, Receiver), Error> { + WsTransportClientBuilder::default() + .build(url) + .await + .map_err(|_| Error::LightClient(LightClientError::Handshake)) + } } } diff --git a/subxt/src/client/mod.rs b/subxt/src/client/mod.rs index cfaa5e4aae..c764af4b59 100644 --- a/subxt/src/client/mod.rs +++ b/subxt/src/client/mod.rs @@ -11,17 +11,15 @@ mod offline_client; mod online_client; -#[cfg(feature = "unstable-light-client")] -#[cfg_attr(docsrs, doc(cfg(feature = "unstable-light-client")))] -mod light_client; +crate::macros::cfg_unstable_light_client! { + mod light_client; + + pub use light_client::{ + LightClient, LightClientBuilder, LightClientError, RawLightClient, RawLightClientBuilder, + }; +} pub use offline_client::{OfflineClient, OfflineClientT}; pub use online_client::{ ClientRuntimeUpdater, OnlineClient, OnlineClientT, RuntimeUpdaterStream, Update, UpgradeError, }; - -#[cfg(feature = "unstable-light-client")] -#[cfg_attr(docsrs, doc(cfg(feature = "unstable-light-client")))] -pub use light_client::{ - LightClient, LightClientBuilder, LightClientError, RawLightClient, RawLightClientBuilder, -}; diff --git a/subxt/src/config/mod.rs b/subxt/src/config/mod.rs index 68e8b3774c..ddfa2466c7 100644 --- a/subxt/src/config/mod.rs +++ b/subxt/src/config/mod.rs @@ -15,6 +15,7 @@ pub mod polkadot; pub mod signed_extensions; pub mod substrate; +use crate::macros::cfg_substrate_compat; use codec::{Decode, Encode}; use core::fmt::Debug; use scale_decode::DecodeAsType; @@ -125,29 +126,29 @@ pub trait Header: Sized + Encode + Decode { } } -/// implement subxt's Hasher and Header traits for some substrate structs -#[cfg(feature = "substrate-compat")] -#[cfg_attr(docsrs, doc(cfg(feature = "substrate-compat")))] -mod substrate_impls { - use super::*; - - impl Header for T - where - ::Number: Into, - { - type Number = T::Number; - type Hasher = T::Hashing; - - fn number(&self) -> Self::Number { - *self.number() +cfg_substrate_compat! { + /// implement subxt's Hasher and Header traits for some substrate structs + mod substrate_impls { + use super::*; + + impl Header for T + where + ::Number: Into, + { + type Number = T::Number; + type Hasher = T::Hashing; + + fn number(&self) -> Self::Number { + *self.number() + } } - } - impl Hasher for T { - type Output = T::Output; + impl Hasher for T { + type Output = T::Output; - fn hash(s: &[u8]) -> Self::Output { - ::hash(s) + fn hash(s: &[u8]) -> Self::Output { + ::hash(s) + } } } } diff --git a/subxt/src/error/mod.rs b/subxt/src/error/mod.rs index 7df87497f7..3e44c5c42e 100644 --- a/subxt/src/error/mod.rs +++ b/subxt/src/error/mod.rs @@ -8,9 +8,9 @@ mod dispatch_error; use core::fmt::Debug; -#[cfg(feature = "unstable-light-client")] -#[cfg_attr(docsrs, doc(cfg(feature = "unstable-light-client")))] -pub use crate::client::LightClientError; +crate::macros::cfg_unstable_light_client! { + pub use crate::client::LightClientError; +} // Re-export dispatch error types: pub use dispatch_error::{ diff --git a/subxt/src/lib.rs b/subxt/src/lib.rs index dd11d4795e..4dd2edea93 100644 --- a/subxt/src/lib.rs +++ b/subxt/src/lib.rs @@ -57,6 +57,10 @@ pub mod storage; pub mod tx; pub mod utils; +// Macros useful below, but not to be exposed outside of the crate. +#[macro_use] +mod macros; + // Expose a few of the most common types at root, // but leave most types behind their respective modules. pub use crate::{ @@ -75,12 +79,11 @@ pub mod ext { pub use scale_decode; pub use scale_encode; pub use scale_value; - #[cfg(feature = "substrate-compat")] - #[cfg_attr(docsrs, doc(cfg(feature = "substrate-compat")))] - pub use sp_core; - #[cfg(feature = "substrate-compat")] - #[cfg_attr(docsrs, doc(cfg(feature = "substrate-compat")))] - pub use sp_runtime; + + cfg_substrate_compat! { + pub use sp_runtime; + pub use sp_core; + } } /// Generate a strongly typed API for interacting with a Substrate runtime from its metadata. diff --git a/subxt/src/tx/mod.rs b/subxt/src/tx/mod.rs index bc76d08bfa..0bb2d9f3eb 100644 --- a/subxt/src/tx/mod.rs +++ b/subxt/src/tx/mod.rs @@ -9,6 +9,8 @@ //! additional and signed extra parameters are used when constructing an extrinsic, and is a part //! of the chain configuration (see [`crate::config::Config`]). +use crate::macros::cfg_substrate_compat; + mod signer; mod tx_client; mod tx_payload; @@ -16,9 +18,9 @@ mod tx_progress; // The PairSigner impl currently relies on Substrate bits and pieces, so make it an optional // feature if we want to avoid needing sp_core and sp_runtime. -#[cfg(feature = "substrate-compat")] -#[cfg_attr(docsrs, doc(cfg(feature = "substrate-compat")))] -pub use self::signer::PairSigner; +cfg_substrate_compat! { + pub use self::signer::PairSigner; +} pub use self::{ signer::Signer, diff --git a/subxt/src/tx/signer.rs b/subxt/src/tx/signer.rs index fc74a185cc..444aa46477 100644 --- a/subxt/src/tx/signer.rs +++ b/subxt/src/tx/signer.rs @@ -5,6 +5,7 @@ //! A library to **sub**mit e**xt**rinsics to a //! [substrate](https://github.com/paritytech/substrate) node via RPC. +use crate::macros::cfg_substrate_compat; use crate::Config; /// Signing transactions requires a [`Signer`]. This is responsible for @@ -24,9 +25,9 @@ pub trait Signer { fn sign(&self, signer_payload: &[u8]) -> T::Signature; } -#[cfg(feature = "substrate-compat")] -#[cfg_attr(docsrs, doc(cfg(feature = "substrate-compat")))] -pub use pair_signer::PairSigner; +cfg_substrate_compat! { + pub use pair_signer::PairSigner; +} // A signer suitable for substrate based chains. This provides compatibility with Substrate // packages like sp_keyring and such, and so relies on sp_core and sp_runtime to be included. From 162d260dc1d75a91d33b97e4e1d87ade324549d3 Mon Sep 17 00:00:00 2001 From: Niklas Adolfsson Date: Tue, 9 Jan 2024 15:35:08 +0100 Subject: [PATCH 6/8] commit missing file --- subxt/src/macros.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 subxt/src/macros.rs diff --git a/subxt/src/macros.rs b/subxt/src/macros.rs new file mode 100644 index 0000000000..309b084b27 --- /dev/null +++ b/subxt/src/macros.rs @@ -0,0 +1,49 @@ +// Copyright 2019-2023 Parity Technologies (UK) Ltd. +// This file is dual-licensed as Apache-2.0 or GPL-3.0. +// see LICENSE for license details. + +macro_rules! cfg_feature { + ($feature:literal, $($item:item)*) => { + $( + #[cfg(feature = $feature)] + #[cfg_attr(docsrs, doc(cfg(feature = $feature)))] + $item + )* + } +} + +macro_rules! cfg_substrate_compat { + ($($item:item)*) => { + crate::macros::cfg_feature!("substrate-compat", $($item)*); + }; +} + +macro_rules! cfg_unstable_light_client { + ($($item:item)*) => { + crate::macros::cfg_feature!("unstable-light-client", $($item)*); + }; +} + +macro_rules! cfg_jsonrpsee { + ($($item:item)*) => { + crate::macros::cfg_feature!("jsonrpsee", $($item)*); + }; +} + +#[allow(unused)] +macro_rules! cfg_jsonrpsee_native { + ($($item:item)*) => { + $( + #[cfg(all(feature = "jsonrpsee", feature = "native"))] + #[cfg_attr(docsrs, doc(cfg(all(feature = "jsonrpsee", feature = "native"))))] + $item + )* + } +} + +pub(crate) use cfg_feature; +pub(crate) use cfg_jsonrpsee; +#[allow(unused)] +pub(crate) use cfg_jsonrpsee_native; +pub(crate) use cfg_substrate_compat; +pub(crate) use cfg_unstable_light_client; From 79e6fcdc07daffe9016be18ab8674634018e010d Mon Sep 17 00:00:00 2001 From: Niklas Adolfsson Date: Tue, 9 Jan 2024 15:38:27 +0100 Subject: [PATCH 7/8] Update subxt/src/lib.rs --- subxt/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/subxt/src/lib.rs b/subxt/src/lib.rs index 4dd2edea93..2a510a52ee 100644 --- a/subxt/src/lib.rs +++ b/subxt/src/lib.rs @@ -57,7 +57,7 @@ pub mod storage; pub mod tx; pub mod utils; -// Macros useful below, but not to be exposed outside of the crate. +// Internal helper macros #[macro_use] mod macros; From dc233047371caaa3bc76c89a639ddc1f0149c826 Mon Sep 17 00:00:00 2001 From: Niklas Adolfsson Date: Tue, 9 Jan 2024 16:02:36 +0100 Subject: [PATCH 8/8] make it compile --- codegen/Cargo.toml | 4 +-- subxt/Cargo.toml | 4 +-- subxt/src/client/light_client/builder.rs | 37 ++++++++++++------------ subxt/src/macros.rs | 20 +++++++++---- 4 files changed, 37 insertions(+), 28 deletions(-) diff --git a/codegen/Cargo.toml b/codegen/Cargo.toml index f8ff7a76af..7efa3f577b 100644 --- a/codegen/Cargo.toml +++ b/codegen/Cargo.toml @@ -5,14 +5,12 @@ authors.workspace = true edition.workspace = true rust-version.workspace = true publish = true - license.workspace = true repository.workspace = true documentation = "https://docs.rs/subxt-codegen" homepage.workspace = true description = "Generate an API for interacting with a substrate node from FRAME metadata" - [features] default = [] fetch-metadata = ["dep:jsonrpsee", "dep:tokio", "dep:frame-metadata"] @@ -42,7 +40,7 @@ pretty_assertions = { workspace = true } frame-metadata = { workspace = true } [package.metadata.docs.rs] -defalt-features = true +features = ["fetch-metadata"] rustdoc-args = ["--cfg", "docsrs"] [package.metadata.playground] diff --git a/subxt/Cargo.toml b/subxt/Cargo.toml index 2920b3f7d8..3f2d8a5754 100644 --- a/subxt/Cargo.toml +++ b/subxt/Cargo.toml @@ -124,8 +124,8 @@ path = "examples/light_client_parachains.rs" required-features = ["unstable-light-client", "jsonrpsee", "native"] [package.metadata.docs.rs] -defalt-features = true +features = ["default", "substrate-compat", "unstable-light-client"] rustdoc-args = ["--cfg", "docsrs"] [package.metadata.playground] -defalt-features = true +features = ["default", "substrate-compat", "unstable-light-client"] diff --git a/subxt/src/client/light_client/builder.rs b/subxt/src/client/light_client/builder.rs index b3180976c4..b0068990db 100644 --- a/subxt/src/client/light_client/builder.rs +++ b/subxt/src/client/light_client/builder.rs @@ -5,7 +5,7 @@ use super::{rpc::LightClientRpc, LightClient, LightClientError}; use crate::backend::rpc::RpcClient; use crate::client::RawLightClient; -use crate::macros::cfg_jsonrpsee; +use crate::macros::{cfg_jsonrpsee_native, cfg_jsonrpsee_web}; use crate::{config::Config, error::Error, OnlineClient}; use std::num::NonZeroU32; use subxt_lightclient::{smoldot, AddedChain}; @@ -278,22 +278,23 @@ cfg_jsonrpsee_native! { } } -#[cfg(all(feature = "jsonrpsee", feature = "web"))] -mod jsonrpsee_helpers { - use crate::error::{Error, LightClientError}; - pub use jsonrpsee::{ - client_transport::web, - core::client::{Client, ClientBuilder}, - }; - - /// Build web RPC client from URL - pub async fn client(url: &str) -> Result { - let (sender, receiver) = web::connect(url) - .await - .map_err(|_| Error::LightClient(LightClientError::Handshake))?; - - Ok(ClientBuilder::default() - .max_buffer_capacity_per_subscription(4096) - .build_with_wasm(sender, receiver)) +cfg_jsonrpsee_web! { + mod jsonrpsee_helpers { + use crate::error::{Error, LightClientError}; + pub use jsonrpsee::{ + client_transport::web, + core::client::{Client, ClientBuilder}, + }; + + /// Build web RPC client from URL + pub async fn client(url: &str) -> Result { + let (sender, receiver) = web::connect(url) + .await + .map_err(|_| Error::LightClient(LightClientError::Handshake))?; + + Ok(ClientBuilder::default() + .max_buffer_capacity_per_subscription(4096) + .build_with_wasm(sender, receiver)) + } } } diff --git a/subxt/src/macros.rs b/subxt/src/macros.rs index 309b084b27..a8d86ada69 100644 --- a/subxt/src/macros.rs +++ b/subxt/src/macros.rs @@ -41,9 +41,19 @@ macro_rules! cfg_jsonrpsee_native { } } -pub(crate) use cfg_feature; -pub(crate) use cfg_jsonrpsee; #[allow(unused)] -pub(crate) use cfg_jsonrpsee_native; -pub(crate) use cfg_substrate_compat; -pub(crate) use cfg_unstable_light_client; +macro_rules! cfg_jsonrpsee_web { + ($($item:item)*) => { + $( + #[cfg(all(feature = "jsonrpsee", feature = "web"))] + #[cfg_attr(docsrs, doc(cfg(all(feature = "jsonrpsee", feature = "web"))))] + $item + )* + } +} + +pub(crate) use {cfg_feature, cfg_jsonrpsee, cfg_substrate_compat, cfg_unstable_light_client}; + +// Only used by light-client. +#[allow(unused)] +pub(crate) use {cfg_jsonrpsee_native, cfg_jsonrpsee_web};