From e04d2fd1f95039d3715f41a470dc2cf7dfaccf39 Mon Sep 17 00:00:00 2001 From: dzmitry-lahoda Date: Thu, 7 Sep 2023 19:43:35 +0100 Subject: [PATCH 1/4] schemars --- bounded-collections/Cargo.toml | 2 ++ bounded-collections/src/bounded_vec.rs | 1 + 2 files changed, 3 insertions(+) diff --git a/bounded-collections/Cargo.toml b/bounded-collections/Cargo.toml index 17a9ecb4..726769f3 100644 --- a/bounded-collections/Cargo.toml +++ b/bounded-collections/Cargo.toml @@ -13,12 +13,14 @@ serde = { version = "1.0.101", default-features = false, optional = true, featur codec = { version = "3.3.0", default-features = false, features = ["max-encoded-len"], package = "parity-scale-codec" } scale-info = { version = ">=1.0, <3", features = ["derive"], default-features = false } log = { version = "0.4.17", default-features = false } +schemars = { version = "0.8.13", default-features = true, optional = true } [dev-dependencies] serde_json = "1.0.41" [features] default = ["std"] +schema = ["schemars", "std"] std = [ "log/std", "codec/std", diff --git a/bounded-collections/src/bounded_vec.rs b/bounded-collections/src/bounded_vec.rs index cd26afb1..e458acce 100644 --- a/bounded-collections/src/bounded_vec.rs +++ b/bounded-collections/src/bounded_vec.rs @@ -43,6 +43,7 @@ use serde::{ #[cfg_attr(feature = "serde", derive(Serialize), serde(transparent))] #[derive(Encode, scale_info::TypeInfo)] #[scale_info(skip_type_params(S))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct BoundedVec(pub(super) Vec, #[cfg_attr(feature = "serde", serde(skip_serializing))] PhantomData); /// Create an object through truncation. From 52c7e8fc53cb0524c1f3813ca4c0ee99416229db Mon Sep 17 00:00:00 2001 From: dzmitry-lahoda Date: Mon, 9 Oct 2023 12:49:13 +0100 Subject: [PATCH 2/4] comment --- bounded-collections/Cargo.toml | 2 +- build-targets-check.sh | 14 ++++++++++++++ primitive-types/Cargo.toml | 2 ++ primitive-types/src/lib.rs | 15 +++++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100755 build-targets-check.sh diff --git a/bounded-collections/Cargo.toml b/bounded-collections/Cargo.toml index 726769f3..ddb261da 100644 --- a/bounded-collections/Cargo.toml +++ b/bounded-collections/Cargo.toml @@ -20,7 +20,7 @@ serde_json = "1.0.41" [features] default = ["std"] -schema = ["schemars", "std"] +json-schema = ["dep:schemars"] std = [ "log/std", "codec/std", diff --git a/build-targets-check.sh b/build-targets-check.sh new file mode 100755 index 00000000..71e985e7 --- /dev/null +++ b/build-targets-check.sh @@ -0,0 +1,14 @@ +#!/bin/sh +# checks combination features are working in real no std and in std other features working for basic types + +# uses build instead of check as sometimes some issues are not caught by check +cargo_deep_check() { + cargo build --locked --no-default-features $@ +} + +cargo_deep_check --target thumbv7em-none-eabi --package primitive-types --features=codec,serde_no_std,byteorder,rustc-hex,codec +cargo_deep_check --target wasm32-unknown-unknown --package primitive-types --features=codec,serde_no_std,byteorder,rustc-hex,codec,num-traits +cargo_deep_check --package primitive-types --features=std,serde,json-schema,byteorder,rustc-hex,codec +cargo_deep_check --target thumbv7em-none-eabi --package bounded-collections +cargo_deep_check --package primitive-types --features=json-schema +cargo_deep_check --target wasm32-unknown-unknown --package bounded-collections \ No newline at end of file diff --git a/primitive-types/Cargo.toml b/primitive-types/Cargo.toml index 7aa3d2e9..63b39869 100644 --- a/primitive-types/Cargo.toml +++ b/primitive-types/Cargo.toml @@ -16,6 +16,7 @@ impl-codec = { version = "0.6.0", path = "impls/codec", default-features = false impl-num-traits = { version = "0.1.0", path = "impls/num-traits", default-features = false, optional = true } impl-rlp = { version = "0.3", path = "impls/rlp", default-features = false, optional = true } scale-info-crate = { package = "scale-info", version = ">=0.9, <3", features = ["derive"], default-features = false, optional = true } +schemars = { version = "0.8.13", default-features = true, optional = true } [dev-dependencies] num-traits = "0.2" @@ -26,6 +27,7 @@ std = ["uint/std", "fixed-hash/std", "impl-codec?/std"] byteorder = ["fixed-hash/byteorder"] rustc-hex = ["fixed-hash/rustc-hex"] serde = ["std", "impl-serde", "impl-serde/std"] +json-schema = ["dep:schemars"] serde_no_std = ["impl-serde"] codec = ["impl-codec"] scale-info = ["codec", "scale-info-crate"] diff --git a/primitive-types/src/lib.rs b/primitive-types/src/lib.rs index dd372a9e..e971f8e6 100644 --- a/primitive-types/src/lib.rs +++ b/primitive-types/src/lib.rs @@ -105,6 +105,21 @@ mod serde { impl_fixed_hash_serde!(H768, 96); } +#[cfg(all(feature = "std", feature = "json-schema"))] +mod json_schema { + use super::*; + + impl schemars::JsonSchema for H160 { + fn schema_name() -> String { + "0xPrefixedHexString".to_string() + } + + fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { + String::json_schema(gen) + } + } +} + #[cfg(feature = "impl-codec")] mod codec { use super::*; From db97e88283f15c11a848448440705c65dd2a56ec Mon Sep 17 00:00:00 2001 From: dzmitry-lahoda Date: Mon, 9 Oct 2023 15:51:04 +0100 Subject: [PATCH 3/4] clean up, aligned version of schema with CosmWasm for now --- bounded-collections/Cargo.toml | 2 +- build-targets-check.sh | 14 -------------- primitive-types/Cargo.toml | 2 +- primitive-types/src/lib.rs | 2 ++ 4 files changed, 4 insertions(+), 16 deletions(-) delete mode 100755 build-targets-check.sh diff --git a/bounded-collections/Cargo.toml b/bounded-collections/Cargo.toml index ddb261da..fb14da38 100644 --- a/bounded-collections/Cargo.toml +++ b/bounded-collections/Cargo.toml @@ -13,7 +13,7 @@ serde = { version = "1.0.101", default-features = false, optional = true, featur codec = { version = "3.3.0", default-features = false, features = ["max-encoded-len"], package = "parity-scale-codec" } scale-info = { version = ">=1.0, <3", features = ["derive"], default-features = false } log = { version = "0.4.17", default-features = false } -schemars = { version = "0.8.13", default-features = true, optional = true } +schemars = { version = ">=0.8.12", default-features = true, optional = true } [dev-dependencies] serde_json = "1.0.41" diff --git a/build-targets-check.sh b/build-targets-check.sh deleted file mode 100755 index 71e985e7..00000000 --- a/build-targets-check.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/sh -# checks combination features are working in real no std and in std other features working for basic types - -# uses build instead of check as sometimes some issues are not caught by check -cargo_deep_check() { - cargo build --locked --no-default-features $@ -} - -cargo_deep_check --target thumbv7em-none-eabi --package primitive-types --features=codec,serde_no_std,byteorder,rustc-hex,codec -cargo_deep_check --target wasm32-unknown-unknown --package primitive-types --features=codec,serde_no_std,byteorder,rustc-hex,codec,num-traits -cargo_deep_check --package primitive-types --features=std,serde,json-schema,byteorder,rustc-hex,codec -cargo_deep_check --target thumbv7em-none-eabi --package bounded-collections -cargo_deep_check --package primitive-types --features=json-schema -cargo_deep_check --target wasm32-unknown-unknown --package bounded-collections \ No newline at end of file diff --git a/primitive-types/Cargo.toml b/primitive-types/Cargo.toml index 63b39869..d642effc 100644 --- a/primitive-types/Cargo.toml +++ b/primitive-types/Cargo.toml @@ -16,7 +16,7 @@ impl-codec = { version = "0.6.0", path = "impls/codec", default-features = false impl-num-traits = { version = "0.1.0", path = "impls/num-traits", default-features = false, optional = true } impl-rlp = { version = "0.3", path = "impls/rlp", default-features = false, optional = true } scale-info-crate = { package = "scale-info", version = ">=0.9, <3", features = ["derive"], default-features = false, optional = true } -schemars = { version = "0.8.13", default-features = true, optional = true } +schemars = { version = ">=0.8.12", default-features = true, optional = true } [dev-dependencies] num-traits = "0.2" diff --git a/primitive-types/src/lib.rs b/primitive-types/src/lib.rs index e971f8e6..d80a312f 100644 --- a/primitive-types/src/lib.rs +++ b/primitive-types/src/lib.rs @@ -105,6 +105,8 @@ mod serde { impl_fixed_hash_serde!(H768, 96); } +// true that no need std, but need to do no_std alloc than, so simplified for now +// also no macro, but easy to create #[cfg(all(feature = "std", feature = "json-schema"))] mod json_schema { use super::*; From 34b692518330c499f90a1d4df95b11bfbb61a7c6 Mon Sep 17 00:00:00 2001 From: dzmitry-lahoda Date: Mon, 9 Oct 2023 20:26:48 +0100 Subject: [PATCH 4/4] Update bounded-collections/src/bounded_vec.rs Co-authored-by: ordian --- bounded-collections/src/bounded_vec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bounded-collections/src/bounded_vec.rs b/bounded-collections/src/bounded_vec.rs index e458acce..3d426995 100644 --- a/bounded-collections/src/bounded_vec.rs +++ b/bounded-collections/src/bounded_vec.rs @@ -43,7 +43,7 @@ use serde::{ #[cfg_attr(feature = "serde", derive(Serialize), serde(transparent))] #[derive(Encode, scale_info::TypeInfo)] #[scale_info(skip_type_params(S))] -#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))] pub struct BoundedVec(pub(super) Vec, #[cfg_attr(feature = "serde", serde(skip_serializing))] PhantomData); /// Create an object through truncation.