diff --git a/Cargo-minimal.lock b/Cargo-minimal.lock index 522683348..c8fcbe8fb 100644 --- a/Cargo-minimal.lock +++ b/Cargo-minimal.lock @@ -46,7 +46,6 @@ dependencies = [ "mutagen", "secp256k1", "serde", - "serde_derive", "serde_json", "serde_test", ] @@ -311,23 +310,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc6ab463ae35acccb5cba66c0084c985257b797d288b6050cc2f6ac1b266cb78" dependencies = [ "dyn-clone", - "schemars_derive", "serde", "serde_json", ] -[[package]] -name = "schemars_derive" -version = "0.8.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "902fdfbcf871ae8f653bddf4b2c05905ddaabc08f69d32a915787e3be0d31356" -dependencies = [ - "proc-macro2", - "quote", - "serde_derive_internals", - "syn", -] - [[package]] name = "secp256k1" version = "0.28.0" @@ -386,17 +372,6 @@ dependencies = [ "syn", ] -[[package]] -name = "serde_derive_internals" -version = "0.25.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1dbab34ca63057a1f15280bdf3c39f2b1eb1b54c17e98360e511637aef7418c6" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "serde_json" version = "1.0.68" diff --git a/Cargo-recent.lock b/Cargo-recent.lock index 27677e11c..c6af81cf5 100644 --- a/Cargo-recent.lock +++ b/Cargo-recent.lock @@ -45,7 +45,6 @@ dependencies = [ "mutagen", "secp256k1", "serde", - "serde_derive", "serde_json", "serde_test", ] @@ -300,23 +299,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "02c613288622e5f0c3fdc5dbd4db1c5fbe752746b1d1a56a0630b78fd00de44f" dependencies = [ "dyn-clone", - "schemars_derive", "serde", "serde_json", ] -[[package]] -name = "schemars_derive" -version = "0.8.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "109da1e6b197438deb6db99952990c7f959572794b80ff93707d55a232545e7c" -dependencies = [ - "proc-macro2", - "quote", - "serde_derive_internals", - "syn", -] - [[package]] name = "secp256k1" version = "0.28.0" @@ -375,17 +361,6 @@ dependencies = [ "syn", ] -[[package]] -name = "serde_derive_internals" -version = "0.26.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85bf8229e7920a9f636479437026331ce11aa132b4dde37d121944a44d6e5f3c" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "serde_json" version = "1.0.96" diff --git a/bitcoin/Cargo.toml b/bitcoin/Cargo.toml index 0b9933cce..aa9f61591 100644 --- a/bitcoin/Cargo.toml +++ b/bitcoin/Cargo.toml @@ -53,7 +53,6 @@ actual-serde = { package = "serde", version = "1.0.103", default-features = fals [dev-dependencies] serde_json = "1.0.0" serde_test = "1.0.19" -serde_derive = "1.0.103" bincode = "1.3.1" [target.'cfg(mutate)'.dev-dependencies] diff --git a/bitcoin/src/consensus/serde.rs b/bitcoin/src/consensus/serde.rs index 1b2019965..9073a32a8 100644 --- a/bitcoin/src/consensus/serde.rs +++ b/bitcoin/src/consensus/serde.rs @@ -465,9 +465,9 @@ impl>> io::Read for IterReader(T); +#[cfg(feature = "schemars")] +impl schemars::JsonSchema for Hmac { + fn is_referenceable() -> bool { ::is_referenceable() } + + fn schema_name() -> std::string::String { ::schema_name() } + + fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { + ::json_schema(gen) + } +} + impl str::FromStr for Hmac { type Err = ::Err; fn from_str(s: &str) -> Result { Ok(Hmac(str::FromStr::from_str(s)?)) } diff --git a/hashes/src/internal_macros.rs b/hashes/src/internal_macros.rs index 32a52dbf7..bb99febcd 100644 --- a/hashes/src/internal_macros.rs +++ b/hashes/src/internal_macros.rs @@ -196,14 +196,11 @@ pub(crate) use hash_trait_impls; /// The `from_engine` free-standing function is still required with this macro. See the doc of /// [`hash_trait_impls`]. macro_rules! hash_type { - ($bits:expr, $reverse:expr, $doc:literal, $schemars:literal) => { + ($bits:expr, $reverse:expr, $doc:literal) => { #[doc = $doc] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] - #[cfg_attr(feature = "schemars", derive(crate::schemars::JsonSchema))] #[repr(transparent)] - pub struct Hash( - #[cfg_attr(feature = "schemars", schemars(schema_with = $schemars))] [u8; $bits / 8], - ); + pub struct Hash([u8; $bits / 8]); impl Hash { fn internal_new(arr: [u8; $bits / 8]) -> Self { Hash(arr) } @@ -211,6 +208,22 @@ macro_rules! hash_type { fn internal_engine() -> HashEngine { Default::default() } } + #[cfg(feature = "schemars")] + impl schemars::JsonSchema for Hash { + fn schema_name() -> String { "Hash".to_owned() } + + fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { + let len = $bits / 8; + let mut schema: schemars::schema::SchemaObject = ::json_schema(gen).into(); + schema.string = Some(Box::new(schemars::schema::StringValidation { + max_length: Some(len * 2), + min_length: Some(len * 2), + pattern: Some("[0-9a-fA-F]+".to_owned()), + })); + schema.into() + } + } + crate::internal_macros::hash_trait_impls!($bits, $reverse); }; } diff --git a/hashes/src/ripemd160.rs b/hashes/src/ripemd160.rs index d97b3bbaf..e0bebe496 100644 --- a/hashes/src/ripemd160.rs +++ b/hashes/src/ripemd160.rs @@ -13,8 +13,7 @@ use crate::{FromSliceError, HashEngine as _}; crate::internal_macros::hash_type! { 160, false, - "Output of the RIPEMD160 hash function.", - "crate::util::json_hex_string::len_20" + "Output of the RIPEMD160 hash function." } #[cfg(not(hashes_fuzz))] diff --git a/hashes/src/sha1.rs b/hashes/src/sha1.rs index a638ebc39..79eab8f56 100644 --- a/hashes/src/sha1.rs +++ b/hashes/src/sha1.rs @@ -13,8 +13,7 @@ use crate::{FromSliceError, HashEngine as _}; crate::internal_macros::hash_type! { 160, false, - "Output of the SHA1 hash function.", - "crate::util::json_hex_string::len_20" + "Output of the SHA1 hash function." } fn from_engine(mut e: HashEngine) -> Hash { diff --git a/hashes/src/sha256.rs b/hashes/src/sha256.rs index 3a8718d50..4267c66a3 100644 --- a/hashes/src/sha256.rs +++ b/hashes/src/sha256.rs @@ -17,8 +17,7 @@ use crate::{hex, sha256d, FromSliceError, HashEngine as _}; crate::internal_macros::hash_type! { 256, false, - "Output of the SHA256 hash function.", - "crate::util::json_hex_string::len_32" + "Output of the SHA256 hash function." } #[cfg(not(hashes_fuzz))] diff --git a/hashes/src/sha256d.rs b/hashes/src/sha256d.rs index 86cff70a6..4fb813bda 100644 --- a/hashes/src/sha256d.rs +++ b/hashes/src/sha256d.rs @@ -12,8 +12,7 @@ use crate::{sha256, FromSliceError}; crate::internal_macros::hash_type! { 256, true, - "Output of the SHA256d hash function.", - "crate::util::json_hex_string::len_32" + "Output of the SHA256d hash function." } type HashEngine = sha256::HashEngine; diff --git a/hashes/src/sha256t.rs b/hashes/src/sha256t.rs index 6b4847a99..774dfdc87 100644 --- a/hashes/src/sha256t.rs +++ b/hashes/src/sha256t.rs @@ -27,7 +27,13 @@ impl schemars::JsonSchema for Hash { fn schema_name() -> String { "Hash".to_owned() } fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { - crate::util::json_hex_string::len_32(gen) + let mut schema: schemars::schema::SchemaObject = ::json_schema(gen).into(); + schema.string = Some(Box::new(schemars::schema::StringValidation { + max_length: Some(32 * 2), + min_length: Some(32 * 2), + pattern: Some("[0-9a-fA-F]+".to_owned()), + })); + schema.into() } } @@ -169,7 +175,6 @@ mod tests { ]; #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Default, Hash)] - #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] pub struct TestHashTag; impl sha256t::Tag for TestHashTag { @@ -180,6 +185,21 @@ mod tests { } } + #[cfg(feature = "schemars")] + impl schemars::JsonSchema for TestHashTag { + fn schema_name() -> String { "Hash".to_owned() } + + fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { + let mut schema: schemars::schema::SchemaObject = ::json_schema(gen).into(); + schema.string = Some(Box::new(schemars::schema::StringValidation { + max_length: Some(64 * 2), + min_length: Some(64 * 2), + pattern: Some("[0-9a-fA-F]+".to_owned()), + })); + schema.into() + } + } + /// A hash tagged with `$name`. #[cfg(feature = "alloc")] pub type TestHash = sha256t::Hash; diff --git a/hashes/src/sha512.rs b/hashes/src/sha512.rs index f7fff5437..1b380b8f4 100644 --- a/hashes/src/sha512.rs +++ b/hashes/src/sha512.rs @@ -13,8 +13,7 @@ use crate::{FromSliceError, HashEngine as _}; crate::internal_macros::hash_type! { 512, false, - "Output of the SHA512 hash function.", - "crate::util::json_hex_string::len_64" + "Output of the SHA512 hash function." } #[cfg(not(hashes_fuzz))] diff --git a/hashes/src/sha512_256.rs b/hashes/src/sha512_256.rs index 61ce8fe36..c0d4529bd 100644 --- a/hashes/src/sha512_256.rs +++ b/hashes/src/sha512_256.rs @@ -16,8 +16,7 @@ use crate::{sha512, FromSliceError}; crate::internal_macros::hash_type! { 256, false, - "Output of the SHA512/256 hash function.\n\nSHA512/256 is a hash function that uses the sha512 alogrithm but it truncates the output to 256 bits. It has different initial constants than sha512 so it produces an entirely different hash compared to sha512. More information at . ", - "crate::util::json_hex_string::len_32" + "Output of the SHA512/256 hash function.\n\nSHA512/256 is a hash function that uses the sha512 alogrithm but it truncates the output to 256 bits. It has different initial constants than sha512 so it produces an entirely different hash compared to sha512. More information at ." } fn from_engine(e: HashEngine) -> Hash { diff --git a/hashes/src/siphash24.rs b/hashes/src/siphash24.rs index 86981afe2..c81335de6 100644 --- a/hashes/src/siphash24.rs +++ b/hashes/src/siphash24.rs @@ -12,8 +12,7 @@ use crate::{FromSliceError, Hash as _, HashEngine as _}; crate::internal_macros::hash_type! { 64, false, - "Output of the SipHash24 hash function.", - "crate::util::json_hex_string::len_8" + "Output of the SipHash24 hash function." } #[cfg(not(hashes_fuzz))] diff --git a/hashes/src/util.rs b/hashes/src/util.rs index 2a5756ada..393718c9b 100644 --- a/hashes/src/util.rs +++ b/hashes/src/util.rs @@ -386,30 +386,6 @@ macro_rules! hash_newtype_known_attrs { ($($ignore:tt)*) => {}; } -#[cfg(feature = "schemars")] -pub mod json_hex_string { - use schemars::gen::SchemaGenerator; - use schemars::schema::{Schema, SchemaObject}; - use schemars::JsonSchema; - macro_rules! define_custom_hex { - ($name:ident, $len:expr) => { - pub fn $name(gen: &mut SchemaGenerator) -> Schema { - let mut schema: SchemaObject = ::json_schema(gen).into(); - schema.string = Some(Box::new(schemars::schema::StringValidation { - max_length: Some($len * 2), - min_length: Some($len * 2), - pattern: Some("[0-9a-fA-F]+".to_owned()), - })); - schema.into() - } - }; - } - define_custom_hex!(len_8, 8); - define_custom_hex!(len_20, 20); - define_custom_hex!(len_32, 32); - define_custom_hex!(len_64, 64); -} - #[cfg(test)] mod test { use crate::{sha256, Hash};