Merge rust-bitcoin/rust-bitcoin#2093: Manually implement `JsonSchema`

75c490c60f hashes: Remove default features from schemars dep (Tobin C. Harding)
1105876423 Remove whitespace character from string (Tobin C. Harding)
a6d7d542ab bitcoin:: Remove dev dependency serde_derive (Tobin C. Harding)

Pull request description:

  Done while investigating removal of `serde_derive` dependency.

  - Patch 1: Do trivial dev-dep removal
  - Patch 2: Manually implement `JsonSchema` and remove default dependencies from "schemars" dependency (transitively depends on `serde_derive`)

ACKs for top commit:
  apoelstra:
    ACK 75c490c60f

Tree-SHA512: aab5bd622a76fc24259933af2f20f863d20c8ccf6e69e68246c374266c540e483ced8a769532582a184b922996857db7320a6b08ae9b5b95503eac752ef9d301
This commit is contained in:
Andrew Poelstra 2023-11-22 14:58:44 +00:00
commit ba318f167a
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
18 changed files with 61 additions and 106 deletions

View File

@ -46,7 +46,6 @@ dependencies = [
"mutagen", "mutagen",
"secp256k1", "secp256k1",
"serde", "serde",
"serde_derive",
"serde_json", "serde_json",
"serde_test", "serde_test",
] ]
@ -311,23 +310,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc6ab463ae35acccb5cba66c0084c985257b797d288b6050cc2f6ac1b266cb78" checksum = "bc6ab463ae35acccb5cba66c0084c985257b797d288b6050cc2f6ac1b266cb78"
dependencies = [ dependencies = [
"dyn-clone", "dyn-clone",
"schemars_derive",
"serde", "serde",
"serde_json", "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]] [[package]]
name = "secp256k1" name = "secp256k1"
version = "0.28.0" version = "0.28.0"
@ -386,17 +372,6 @@ dependencies = [
"syn", "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]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.68" version = "1.0.68"

View File

@ -45,7 +45,6 @@ dependencies = [
"mutagen", "mutagen",
"secp256k1", "secp256k1",
"serde", "serde",
"serde_derive",
"serde_json", "serde_json",
"serde_test", "serde_test",
] ]
@ -300,23 +299,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02c613288622e5f0c3fdc5dbd4db1c5fbe752746b1d1a56a0630b78fd00de44f" checksum = "02c613288622e5f0c3fdc5dbd4db1c5fbe752746b1d1a56a0630b78fd00de44f"
dependencies = [ dependencies = [
"dyn-clone", "dyn-clone",
"schemars_derive",
"serde", "serde",
"serde_json", "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]] [[package]]
name = "secp256k1" name = "secp256k1"
version = "0.28.0" version = "0.28.0"
@ -375,17 +361,6 @@ dependencies = [
"syn", "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]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.96" version = "1.0.96"

View File

@ -53,7 +53,6 @@ actual-serde = { package = "serde", version = "1.0.103", default-features = fals
[dev-dependencies] [dev-dependencies]
serde_json = "1.0.0" serde_json = "1.0.0"
serde_test = "1.0.19" serde_test = "1.0.19"
serde_derive = "1.0.103"
bincode = "1.3.1" bincode = "1.3.1"
[target.'cfg(mutate)'.dev-dependencies] [target.'cfg(mutate)'.dev-dependencies]

View File

@ -465,9 +465,9 @@ impl<E: fmt::Debug, I: Iterator<Item = Result<u8, E>>> io::Read for IterReader<E
/// To (de)serialize a field using consensus encoding you can write e.g.: /// To (de)serialize a field using consensus encoding you can write e.g.:
/// ///
/// ``` /// ```
/// # use actual_serde::{Serialize, Deserialize};
/// use bitcoin::Transaction; /// use bitcoin::Transaction;
/// use bitcoin::consensus; /// use bitcoin::consensus;
/// use serde_derive::{Serialize, Deserialize};
/// ///
/// #[derive(Serialize, Deserialize)] /// #[derive(Serialize, Deserialize)]
/// # #[serde(crate = "actual_serde")] /// # #[serde(crate = "actual_serde")]

View File

@ -13,10 +13,7 @@ if cargo --version | grep ${MSRV}; then
cargo update -p quote --precise 1.0.30 cargo update -p quote --precise 1.0.30
cargo update -p proc-macro2 --precise 1.0.63 cargo update -p proc-macro2 --precise 1.0.63
cargo update -p serde_test --precise 1.0.175 cargo update -p serde_test --precise 1.0.175
# Have to pin this so we can pin `schemars_derive`
cargo update -p schemars --precise 0.8.12 cargo update -p schemars --precise 0.8.12
# schemars_derive 0.8.13 uses edition 2021
cargo update -p schemars_derive --precise 0.8.12
# byteorder 1.5.0 uses edition 2021 # byteorder 1.5.0 uses edition 2021
cargo update -p byteorder --precise 1.4.3 cargo update -p byteorder --precise 1.4.3

View File

@ -30,8 +30,7 @@ rustdoc-args = ["--cfg", "docsrs"]
hex = { package = "hex-conservative", version = "0.1.1", default-features = false } hex = { package = "hex-conservative", version = "0.1.1", default-features = false }
bitcoin-io = { version = "0.1", default-features = false, optional = true } bitcoin-io = { version = "0.1", default-features = false, optional = true }
schemars = { version = "0.8.3", default-features = false, optional = true }
schemars = { version = "0.8.3", optional = true }
# Only enable this if you explicitly do not want to use "std", otherwise enable "serde-std". # Only enable this if you explicitly do not want to use "std", otherwise enable "serde-std".
serde = { version = "1.0", default-features = false, optional = true } serde = { version = "1.0", default-features = false, optional = true }

View File

@ -17,8 +17,7 @@ use crate::{ripemd160, sha256, FromSliceError};
crate::internal_macros::hash_type! { crate::internal_macros::hash_type! {
160, 160,
false, false,
"Output of the Bitcoin HASH160 hash function. (RIPEMD160(SHA256))", "Output of the Bitcoin HASH160 hash function. (RIPEMD160(SHA256))"
"crate::util::json_hex_string::len_20"
} }
type HashEngine = sha256::HashEngine; type HashEngine = sha256::HashEngine;

View File

@ -17,11 +17,20 @@ use crate::{FromSliceError, Hash, HashEngine};
/// A hash computed from a RFC 2104 HMAC. Parameterized by the underlying hash function. /// A hash computed from a RFC 2104 HMAC. Parameterized by the underlying hash function.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "schemars", schemars(transparent))]
#[repr(transparent)] #[repr(transparent)]
pub struct Hmac<T: Hash>(T); pub struct Hmac<T: Hash>(T);
#[cfg(feature = "schemars")]
impl<T: Hash + schemars::JsonSchema> schemars::JsonSchema for Hmac<T> {
fn is_referenceable() -> bool { <T as schemars::JsonSchema>::is_referenceable() }
fn schema_name() -> std::string::String { <T as schemars::JsonSchema>::schema_name() }
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
<T as schemars::JsonSchema>::json_schema(gen)
}
}
impl<T: Hash + str::FromStr> str::FromStr for Hmac<T> { impl<T: Hash + str::FromStr> str::FromStr for Hmac<T> {
type Err = <T as str::FromStr>::Err; type Err = <T as str::FromStr>::Err;
fn from_str(s: &str) -> Result<Self, Self::Err> { Ok(Hmac(str::FromStr::from_str(s)?)) } fn from_str(s: &str) -> Result<Self, Self::Err> { Ok(Hmac(str::FromStr::from_str(s)?)) }

View File

@ -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 /// The `from_engine` free-standing function is still required with this macro. See the doc of
/// [`hash_trait_impls`]. /// [`hash_trait_impls`].
macro_rules! hash_type { macro_rules! hash_type {
($bits:expr, $reverse:expr, $doc:literal, $schemars:literal) => { ($bits:expr, $reverse:expr, $doc:literal) => {
#[doc = $doc] #[doc = $doc]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "schemars", derive(crate::schemars::JsonSchema))]
#[repr(transparent)] #[repr(transparent)]
pub struct Hash( pub struct Hash([u8; $bits / 8]);
#[cfg_attr(feature = "schemars", schemars(schema_with = $schemars))] [u8; $bits / 8],
);
impl Hash { impl Hash {
fn internal_new(arr: [u8; $bits / 8]) -> Self { Hash(arr) } fn internal_new(arr: [u8; $bits / 8]) -> Self { Hash(arr) }
@ -211,6 +208,22 @@ macro_rules! hash_type {
fn internal_engine() -> HashEngine { Default::default() } 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 = <String>::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); crate::internal_macros::hash_trait_impls!($bits, $reverse);
}; };
} }

View File

@ -13,8 +13,7 @@ use crate::{FromSliceError, HashEngine as _};
crate::internal_macros::hash_type! { crate::internal_macros::hash_type! {
160, 160,
false, false,
"Output of the RIPEMD160 hash function.", "Output of the RIPEMD160 hash function."
"crate::util::json_hex_string::len_20"
} }
#[cfg(not(hashes_fuzz))] #[cfg(not(hashes_fuzz))]

View File

@ -13,8 +13,7 @@ use crate::{FromSliceError, HashEngine as _};
crate::internal_macros::hash_type! { crate::internal_macros::hash_type! {
160, 160,
false, false,
"Output of the SHA1 hash function.", "Output of the SHA1 hash function."
"crate::util::json_hex_string::len_20"
} }
fn from_engine(mut e: HashEngine) -> Hash { fn from_engine(mut e: HashEngine) -> Hash {

View File

@ -17,8 +17,7 @@ use crate::{hex, sha256d, FromSliceError, HashEngine as _};
crate::internal_macros::hash_type! { crate::internal_macros::hash_type! {
256, 256,
false, false,
"Output of the SHA256 hash function.", "Output of the SHA256 hash function."
"crate::util::json_hex_string::len_32"
} }
#[cfg(not(hashes_fuzz))] #[cfg(not(hashes_fuzz))]

View File

@ -12,8 +12,7 @@ use crate::{sha256, FromSliceError};
crate::internal_macros::hash_type! { crate::internal_macros::hash_type! {
256, 256,
true, true,
"Output of the SHA256d hash function.", "Output of the SHA256d hash function."
"crate::util::json_hex_string::len_32"
} }
type HashEngine = sha256::HashEngine; type HashEngine = sha256::HashEngine;

View File

@ -27,7 +27,13 @@ impl<T: Tag> schemars::JsonSchema for Hash<T> {
fn schema_name() -> String { "Hash".to_owned() } fn schema_name() -> String { "Hash".to_owned() }
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { 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 = <String>::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)] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct TestHashTag; pub struct TestHashTag;
impl sha256t::Tag for 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 = <String>::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`. /// A hash tagged with `$name`.
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
pub type TestHash = sha256t::Hash<TestHashTag>; pub type TestHash = sha256t::Hash<TestHashTag>;

View File

@ -13,8 +13,7 @@ use crate::{FromSliceError, HashEngine as _};
crate::internal_macros::hash_type! { crate::internal_macros::hash_type! {
512, 512,
false, false,
"Output of the SHA512 hash function.", "Output of the SHA512 hash function."
"crate::util::json_hex_string::len_64"
} }
#[cfg(not(hashes_fuzz))] #[cfg(not(hashes_fuzz))]

View File

@ -16,8 +16,7 @@ use crate::{sha512, FromSliceError};
crate::internal_macros::hash_type! { crate::internal_macros::hash_type! {
256, 256,
false, 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 <https://eprint.iacr.org/2010/548.pdf>. ", "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 <https://eprint.iacr.org/2010/548.pdf>."
"crate::util::json_hex_string::len_32"
} }
fn from_engine(e: HashEngine) -> Hash { fn from_engine(e: HashEngine) -> Hash {

View File

@ -12,8 +12,7 @@ use crate::{FromSliceError, Hash as _, HashEngine as _};
crate::internal_macros::hash_type! { crate::internal_macros::hash_type! {
64, 64,
false, false,
"Output of the SipHash24 hash function.", "Output of the SipHash24 hash function."
"crate::util::json_hex_string::len_8"
} }
#[cfg(not(hashes_fuzz))] #[cfg(not(hashes_fuzz))]

View File

@ -386,30 +386,6 @@ macro_rules! hash_newtype_known_attrs {
($($ignore:tt)*) => {}; ($($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 = <String>::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)] #[cfg(test)]
mod test { mod test {
use crate::{sha256, Hash}; use crate::{sha256, Hash};