Skip to content

Commit

Permalink
refactor: move schema under #[cfg(schema)] in workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
dj8yf0μl committed Jun 29, 2023
1 parent 130d605 commit 96fc503
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion borsh-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
2 changes: 2 additions & 0 deletions borsh-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions borsh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 = []
8 changes: 7 additions & 1 deletion borsh/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions borsh/tests/smoke.rs
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions borsh/tests/test_schema_enums.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
1 change: 1 addition & 0 deletions borsh/tests/test_schema_nested.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down
1 change: 1 addition & 0 deletions borsh/tests/test_schema_primitives.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg(hash_collections)]
#![cfg(feature = "schema")]

#[cfg(not(feature = "std"))]
extern crate alloc;
Expand Down
1 change: 1 addition & 0 deletions borsh/tests/test_schema_structs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg(feature = "schema")]

use borsh::schema::*;

Expand Down
1 change: 1 addition & 0 deletions borsh/tests/test_schema_tuple.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg(feature = "schema")]

#[cfg(feature = "std")]
use std::collections::BTreeMap;
Expand Down

0 comments on commit 96fc503

Please sign in to comment.