From 96fc5038900509a6de11ec4ac3b1d3c57d03014b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dj8yf0=CE=BCl?= Date: Thu, 29 Jun 2023 15:56:35 +0300 Subject: [PATCH] refactor: move schema under `#[cfg(schema)]` in workspace --- .github/test.sh | 1 + borsh-derive/Cargo.toml | 6 +++++- borsh-derive/src/lib.rs | 2 ++ borsh/Cargo.toml | 5 +++-- borsh/src/lib.rs | 8 +++++++- borsh/tests/smoke.rs | 6 ++++-- borsh/tests/test_schema_enums.rs | 1 + borsh/tests/test_schema_nested.rs | 1 + borsh/tests/test_schema_primitives.rs | 1 + borsh/tests/test_schema_structs.rs | 1 + borsh/tests/test_schema_tuple.rs | 1 + 11 files changed, 27 insertions(+), 6 deletions(-) diff --git a/.github/test.sh b/.github/test.sh index ad25ce1fa..02832c7d3 100755 --- a/.github/test.sh +++ b/.github/test.sh @@ -6,6 +6,7 @@ pushd borsh cargo test --no-run cargo test cargo test --no-default-features +cargo test --no-default-features --features schema cargo test --no-default-features --features hashbrown,rc cargo test --features rc popd diff --git a/borsh-derive/Cargo.toml b/borsh-derive/Cargo.toml index 2a9412b87..c514b27eb 100644 --- a/borsh-derive/Cargo.toml +++ b/borsh-derive/Cargo.toml @@ -18,8 +18,12 @@ proc-macro = true [dependencies] borsh-derive-internal = { path = "../borsh-derive-internal", version = "0.11.0" } -borsh-schema-derive-internal = { path = "../borsh-schema-derive-internal", version = "0.11.0" } +borsh-schema-derive-internal = { path = "../borsh-schema-derive-internal", version = "0.11.0", optional = true } syn = { version = "2", features = ["full", "fold"] } proc-macro-crate = "1" proc-macro2 = "1" quote = "1" + +[features] +default = [] +schema = ["borsh-schema-derive-internal"] diff --git a/borsh-derive/src/lib.rs b/borsh-derive/src/lib.rs index b6c2f2e68..7b2b41d2c 100644 --- a/borsh-derive/src/lib.rs +++ b/borsh-derive/src/lib.rs @@ -6,6 +6,7 @@ use proc_macro_crate::FoundCrate; use syn::{Ident, ItemEnum, ItemStruct, ItemUnion}; use borsh_derive_internal::*; +#[cfg(feature = "schema")] use borsh_schema_derive_internal::*; #[proc_macro_derive(BorshSerialize, attributes(borsh_skip))] @@ -58,6 +59,7 @@ pub fn borsh_deserialize(input: TokenStream) -> TokenStream { }) } +#[cfg(feature = "schema")] #[proc_macro_derive(BorshSchema, attributes(borsh_skip))] pub fn borsh_schema(input: TokenStream) -> TokenStream { let name = &crate_name("borsh").unwrap(); diff --git a/borsh/Cargo.toml b/borsh/Cargo.toml index 7d48f9595..ed231b98b 100644 --- a/borsh/Cargo.toml +++ b/borsh/Cargo.toml @@ -21,7 +21,7 @@ path = "src/lib.rs" [[bin]] name = "generate_schema_schema" path = "src/generate_schema_schema.rs" -required-features = ["std"] +required-features = ["std", "schema"] [build-dependencies] cfg_aliases = "0.1.0" @@ -40,6 +40,7 @@ borsh = { path = ".", default_features = false, features = ["bytes", "bson"] } insta = "1.29.0" [features] -default = ["std"] +default = ["std", "schema"] +schema = ["borsh-derive/schema"] std = [] rc = [] diff --git a/borsh/src/lib.rs b/borsh/src/lib.rs index aaacd6c70..58f576d03 100644 --- a/borsh/src/lib.rs +++ b/borsh/src/lib.rs @@ -46,18 +46,24 @@ #[cfg(not(feature = "std"))] extern crate alloc; -pub use borsh_derive::{BorshDeserialize, BorshSchema, BorshSerialize}; +#[cfg(feature = "schema")] +pub use borsh_derive::BorshSchema; +pub use borsh_derive::{BorshDeserialize, BorshSerialize}; pub mod de; // See `hash_collections` alias definition in build.rs +#[cfg(feature = "schema")] pub mod schema; +#[cfg(feature = "schema")] pub mod schema_helpers; pub mod ser; pub use de::BorshDeserialize; pub use de::{from_reader, from_slice}; +#[cfg(feature = "schema")] pub use schema::BorshSchema; +#[cfg(feature = "schema")] pub use schema_helpers::{try_from_slice_with_schema, try_to_vec_with_schema}; pub use ser::helpers::{to_vec, to_writer}; pub use ser::BorshSerialize; diff --git a/borsh/tests/smoke.rs b/borsh/tests/smoke.rs index 61791e89b..85b18a02d 100644 --- a/borsh/tests/smoke.rs +++ b/borsh/tests/smoke.rs @@ -1,5 +1,4 @@ #![cfg_attr(not(feature = "std"), no_std)] -#![cfg(hash_collections)] // Smoke tests that ensure that we don't accidentally remove top-level // re-exports in a minor release. @@ -8,8 +7,11 @@ extern crate alloc; #[cfg(not(feature = "std"))] use alloc::vec; -use borsh::{self, from_slice, try_from_slice_with_schema, BorshSchema}; +use borsh::{self, from_slice}; +#[cfg(feature = "schema")] +use borsh::{try_from_slice_with_schema, BorshSchema}; +#[cfg(feature = "schema")] #[test] fn test_to_vec() { let value = 42u8; diff --git a/borsh/tests/test_schema_enums.rs b/borsh/tests/test_schema_enums.rs index 37794d4a7..61866a7b4 100644 --- a/borsh/tests/test_schema_enums.rs +++ b/borsh/tests/test_schema_enums.rs @@ -1,5 +1,6 @@ #![cfg_attr(not(feature = "std"), no_std)] #![allow(dead_code)] // Local structures do not have their fields used. +#![cfg(feature = "schema")] #[cfg(feature = "std")] use std::collections::BTreeMap; diff --git a/borsh/tests/test_schema_nested.rs b/borsh/tests/test_schema_nested.rs index c01be0d59..fe4f75bc3 100644 --- a/borsh/tests/test_schema_nested.rs +++ b/borsh/tests/test_schema_nested.rs @@ -1,6 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] #![cfg(hash_collections)] #![allow(dead_code)] // Local structures do not have their fields used. +#![cfg(feature = "schema")] use borsh::schema::*; #[cfg(feature = "hashbrown")] diff --git a/borsh/tests/test_schema_primitives.rs b/borsh/tests/test_schema_primitives.rs index 16e89e0d1..ff32b103e 100644 --- a/borsh/tests/test_schema_primitives.rs +++ b/borsh/tests/test_schema_primitives.rs @@ -1,5 +1,6 @@ #![cfg_attr(not(feature = "std"), no_std)] #![cfg(hash_collections)] +#![cfg(feature = "schema")] #[cfg(not(feature = "std"))] extern crate alloc; diff --git a/borsh/tests/test_schema_structs.rs b/borsh/tests/test_schema_structs.rs index 556e36cf3..e1c14ff0d 100644 --- a/borsh/tests/test_schema_structs.rs +++ b/borsh/tests/test_schema_structs.rs @@ -1,4 +1,5 @@ #![cfg_attr(not(feature = "std"), no_std)] +#![cfg(feature = "schema")] use borsh::schema::*; diff --git a/borsh/tests/test_schema_tuple.rs b/borsh/tests/test_schema_tuple.rs index 0d71c003c..a8673a981 100644 --- a/borsh/tests/test_schema_tuple.rs +++ b/borsh/tests/test_schema_tuple.rs @@ -1,4 +1,5 @@ #![cfg_attr(not(feature = "std"), no_std)] +#![cfg(feature = "schema")] #[cfg(feature = "std")] use std::collections::BTreeMap;