Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit ad3a777

Browse files
committed
Revert "remove schemars"
This reverts commit 4665e8d.
1 parent d780cb0 commit ad3a777

File tree

12 files changed

+232
-7
lines changed

12 files changed

+232
-7
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ unstable = [] # for benchmarking
2222

2323
[dependencies]
2424
serde = { version = "1.0", default-features = false, optional = true }
25+
schemars = { version = "0.8.0", optional = true }
2526

2627
[dev-dependencies]
2728
serde_test = "1.0"
2829
serde_json = "1.0"
30+
jsonschema-valid = "0.4.0"
2931

3032
[target.wasm32-unknown-unknown.dev-dependencies]
3133
wasm-bindgen-test = "0.3"

src/hash160.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ use Error;
2828

2929
/// Output of the Bitcoin HASH160 hash function
3030
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
31+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
3132
#[repr(transparent)]
32-
pub struct Hash([u8; 20]);
33+
pub struct Hash(
34+
#[cfg_attr(feature = "schemars", schemars(schema_with="crate::util::json_hex_string::len_20"))]
35+
[u8; 20]
36+
);
37+
38+
3339

3440
hex_fmt_impl!(Debug, Hash);
3541
hex_fmt_impl!(Display, Hash);
@@ -161,6 +167,24 @@ mod tests {
161167
assert_tokens(&hash.compact(), &[Token::BorrowedBytes(&HASH_BYTES[..])]);
162168
assert_tokens(&hash.readable(), &[Token::Str("132072df690933835eb8b6ad0b77e7b6f14acad7")]);
163169
}
170+
171+
#[cfg(all(feature = "schemars",feature = "serde"))]
172+
#[test]
173+
fn jsonschema_accurate() {
174+
static HASH_BYTES: [u8; 20] = [
175+
0x13, 0x20, 0x72, 0xdf,
176+
0x69, 0x09, 0x33, 0x83,
177+
0x5e, 0xb8, 0xb6, 0xad,
178+
0x0b, 0x77, 0xe7, 0xb6,
179+
0xf1, 0x4a, 0xca, 0xd7,
180+
];
181+
182+
let hash = hash160::Hash::from_slice(&HASH_BYTES).expect("right number of bytes");
183+
let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap();
184+
let s = schemars::schema_for! (hash160::Hash);
185+
let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap();
186+
assert!(jsonschema_valid::Config::from_schema(&schema, None).unwrap().validate(&js).is_ok());
187+
}
164188
}
165189

166190
#[cfg(all(test, feature="unstable"))]

src/hmac.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ use Error;
2929

3030
/// A hash computed from a RFC 2104 HMAC. Parameterized by the underlying hash function.
3131
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
32+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
33+
#[cfg_attr(feature = "schemars", schemars(transparent))]
3234
#[repr(transparent)]
3335
pub struct Hmac<T: HashTrait>(T);
3436

@@ -385,6 +387,27 @@ mod tests {
385387
)],
386388
);
387389
}
390+
391+
#[cfg(all(feature = "schemars",feature = "serde"))]
392+
#[test]
393+
fn jsonschema_accurate() {
394+
static HASH_BYTES: [u8; 64] = [
395+
0x8b, 0x41, 0xe1, 0xb7, 0x8a, 0xd1, 0x15, 0x21,
396+
0x11, 0x3c, 0x52, 0xff, 0x18, 0x2a, 0x1b, 0x8e,
397+
0x0a, 0x19, 0x57, 0x54, 0xaa, 0x52, 0x7f, 0xcd,
398+
0x00, 0xa4, 0x11, 0x62, 0x0b, 0x46, 0xf2, 0x0f,
399+
0xff, 0xfb, 0x80, 0x88, 0xcc, 0xf8, 0x54, 0x97,
400+
0x12, 0x1a, 0xd4, 0x49, 0x9e, 0x08, 0x45, 0xb8,
401+
0x76, 0xf6, 0xdd, 0x66, 0x40, 0x08, 0x8a, 0x2f,
402+
0x0b, 0x2d, 0x8a, 0x60, 0x0b, 0xdf, 0x4c, 0x0c,
403+
];
404+
405+
let hash = Hmac::<sha512::Hash>::from_slice(&HASH_BYTES).expect("right number of bytes");
406+
let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap();
407+
let s = schemars::schema_for! (Hmac::<sha512::Hash>);
408+
let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap();
409+
assert!(jsonschema_valid::Config::from_schema(&schema, None).unwrap().validate(&js).is_ok());
410+
}
388411
}
389412

390413
#[cfg(all(test, feature="unstable"))]

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
#[cfg(feature="serde")] pub extern crate serde;
4242
#[cfg(all(test,feature="serde"))] extern crate serde_test;
4343

44+
#[cfg(feature = "schemars")] extern crate schemars;
45+
4446
#[macro_use] mod util;
4547
#[macro_use] pub mod serde_macros;
4648
#[cfg(any(test, feature = "std"))] mod std_impls;

src/ripemd160.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ impl EngineTrait for HashEngine {
7676

7777
/// Output of the RIPEMD160 hash function
7878
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
79+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
7980
#[repr(transparent)]
80-
pub struct Hash([u8; 20]);
81+
pub struct Hash(
82+
#[cfg_attr(feature = "schemars", schemars(schema_with="util::json_hex_string::len_20"))]
83+
[u8; 20]
84+
);
8185

8286
hex_fmt_impl!(Debug, Hash);
8387
hex_fmt_impl!(Display, Hash);
@@ -543,6 +547,24 @@ mod tests {
543547
assert_tokens(&hash.compact(), &[Token::BorrowedBytes(&HASH_BYTES[..])]);
544548
assert_tokens(&hash.readable(), &[Token::Str("132072df690933835eb8b6ad0b77e7b6f14acad7")]);
545549
}
550+
551+
#[cfg(all(feature = "schemars",feature = "serde"))]
552+
#[test]
553+
fn jsonschema_accurate() {
554+
static HASH_BYTES: [u8; 20] = [
555+
0x13, 0x20, 0x72, 0xdf,
556+
0x69, 0x09, 0x33, 0x83,
557+
0x5e, 0xb8, 0xb6, 0xad,
558+
0x0b, 0x77, 0xe7, 0xb6,
559+
0xf1, 0x4a, 0xca, 0xd7,
560+
];
561+
562+
let hash = ripemd160::Hash::from_slice(&HASH_BYTES).expect("right number of bytes");
563+
let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap();
564+
let s = schemars::schema_for! (ripemd160::Hash);
565+
let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap();
566+
assert!(jsonschema_valid::Config::from_schema(&schema, None).unwrap().validate(&js).is_ok());
567+
}
546568
}
547569

548570
#[cfg(all(test, feature="unstable"))]

src/sha1.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,12 @@ impl EngineTrait for HashEngine {
7171

7272
/// Output of the SHA1 hash function
7373
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
74+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
7475
#[repr(transparent)]
75-
pub struct Hash([u8; 20]);
76+
pub struct Hash(
77+
#[cfg_attr(feature = "schemars", schemars(schema_with="util::json_hex_string::len_20"))]
78+
[u8; 20]
79+
);
7680

7781
hex_fmt_impl!(Debug, Hash);
7882
hex_fmt_impl!(Display, Hash);
@@ -268,6 +272,24 @@ mod tests {
268272
assert_tokens(&hash.compact(), &[Token::BorrowedBytes(&HASH_BYTES[..])]);
269273
assert_tokens(&hash.readable(), &[Token::Str("132072df690933835eb8b6ad0b77e7b6f14acad7")]);
270274
}
275+
276+
#[cfg(all(feature = "schemars",feature = "serde"))]
277+
#[test]
278+
fn jsonschema_accurate() {
279+
static HASH_BYTES: [u8; 20] = [
280+
0x13, 0x20, 0x72, 0xdf,
281+
0x69, 0x09, 0x33, 0x83,
282+
0x5e, 0xb8, 0xb6, 0xad,
283+
0x0b, 0x77, 0xe7, 0xb6,
284+
0xf1, 0x4a, 0xca, 0xd7,
285+
];
286+
287+
let hash = sha1::Hash::from_slice(&HASH_BYTES).expect("right number of bytes");
288+
let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap();
289+
let s = schemars::schema_for! (sha1::Hash);
290+
let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap();
291+
assert!(jsonschema_valid::Config::from_schema(&schema, None).unwrap().validate(&js).is_ok());
292+
}
271293
}
272294

273295
#[cfg(all(test, feature="unstable"))]

src/sha256.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,12 @@ impl EngineTrait for HashEngine {
7272

7373
/// Output of the SHA256 hash function
7474
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
75+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
7576
#[repr(transparent)]
76-
pub struct Hash([u8; 32]);
77+
pub struct Hash(
78+
#[cfg_attr(feature = "schemars", schemars(schema_with="util::json_hex_string::len_32"))]
79+
[u8; 32]
80+
);
7781

7882
impl str::FromStr for Hash {
7983
type Err = ::hex::Error;
@@ -506,6 +510,23 @@ mod tests {
506510
assert_tokens(&hash.readable(), &[Token::Str("ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c")]);
507511
}
508512

513+
#[cfg(all(feature = "schemars",feature = "serde"))]
514+
#[test]
515+
fn jsonschema_accurate() {
516+
static HASH_BYTES: [u8; 32] = [
517+
0xef, 0x53, 0x7f, 0x25, 0xc8, 0x95, 0xbf, 0xa7,
518+
0x82, 0x52, 0x65, 0x29, 0xa9, 0xb6, 0x3d, 0x97,
519+
0xaa, 0x63, 0x15, 0x64, 0xd5, 0xd7, 0x89, 0xc2,
520+
0xb7, 0x65, 0x44, 0x8c, 0x86, 0x35, 0xfb, 0x6c,
521+
];
522+
523+
let hash = sha256::Hash::from_slice(&HASH_BYTES).expect("right number of bytes");
524+
let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap();
525+
let s = schemars::schema_for! (sha256::Hash);
526+
let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap();
527+
assert!(jsonschema_valid::Config::from_schema(&schema, None).unwrap().validate(&js).is_ok());
528+
}
529+
509530
#[cfg(target_arch = "wasm32")]
510531
mod wasm_tests {
511532
extern crate wasm_bindgen_test;

src/sha256d.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ use Error;
2222

2323
/// Output of the SHA256d hash function
2424
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
25+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
2526
#[repr(transparent)]
26-
pub struct Hash([u8; 32]);
27+
pub struct Hash(
28+
#[cfg_attr(feature = "schemars", schemars(schema_with="crate::util::json_hex_string::len_32"))]
29+
[u8; 32]
30+
);
2731

2832
hex_fmt_impl!(Debug, Hash);
2933
hex_fmt_impl!(Display, Hash);
@@ -147,6 +151,24 @@ input: &'static str,
147151
assert_tokens(&hash.compact(), &[Token::BorrowedBytes(&HASH_BYTES[..])]);
148152
assert_tokens(&hash.readable(), &[Token::Str("6cfb35868c4465b7c289d7d5641563aa973db6a929655282a7bf95c8257f53ef")]);
149153
}
154+
155+
#[cfg(all(feature = "schemars",feature = "serde"))]
156+
#[test]
157+
fn jsonschema_accurate() {
158+
static HASH_BYTES: [u8; 32] = [
159+
0xef, 0x53, 0x7f, 0x25, 0xc8, 0x95, 0xbf, 0xa7,
160+
0x82, 0x52, 0x65, 0x29, 0xa9, 0xb6, 0x3d, 0x97,
161+
0xaa, 0x63, 0x15, 0x64, 0xd5, 0xd7, 0x89, 0xc2,
162+
0xb7, 0x65, 0x44, 0x8c, 0x86, 0x35, 0xfb, 0x6c,
163+
];
164+
165+
let hash = sha256d::Hash::from_slice(&HASH_BYTES).expect("right number of bytes");
166+
let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap();
167+
let s = schemars::schema_for! (sha256d::Hash);
168+
let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap();
169+
assert!(jsonschema_valid::Config::from_schema(&schema, None).unwrap().validate(&js).is_ok());
170+
}
171+
150172
}
151173

152174
#[cfg(all(test, feature="unstable"))]

src/sha256t.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ pub trait Tag {
3131
}
3232

3333
/// Output of the SHA256t hash function.
34+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
3435
#[repr(transparent)]
3536
pub struct Hash<T: Tag>(
37+
#[cfg_attr(feature = "schemars", schemars(schema_with="crate::util::json_hex_string::len_32"))]
3638
[u8; 32],
39+
#[cfg_attr(feature = "schemars", schemars(skip))]
3740
PhantomData<T>
3841
);
3942

@@ -241,6 +244,7 @@ mod tests {
241244
];
242245

243246
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]
247+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
244248
pub struct TestHashTag;
245249

246250
impl sha256t::Tag for TestHashTag {
@@ -267,4 +271,21 @@ mod tests {
267271
"29589d5122ec666ab5b4695070b6debc63881a4f85d88d93ddc90078038213ed"
268272
);
269273
}
274+
275+
#[cfg(all(feature = "schemars",feature = "serde"))]
276+
#[test]
277+
fn jsonschema_accurate() {
278+
static HASH_BYTES: [u8; 32] = [
279+
0xef, 0x53, 0x7f, 0x25, 0xc8, 0x95, 0xbf, 0xa7,
280+
0x82, 0x52, 0x65, 0x29, 0xa9, 0xb6, 0x3d, 0x97,
281+
0xaa, 0x63, 0x15, 0x64, 0xd5, 0xd7, 0x89, 0xc2,
282+
0xb7, 0x65, 0x44, 0x8c, 0x86, 0x35, 0xfb, 0x6c,
283+
];
284+
285+
let hash = TestHash::from_slice(&HASH_BYTES).expect("right number of bytes");
286+
let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap();
287+
let s = schemars::schema_for! (TestHash);
288+
let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap();
289+
assert!(jsonschema_valid::Config::from_schema(&schema, None).unwrap().validate(&js).is_ok());
290+
}
270291
}

src/sha512.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,12 @@ impl EngineTrait for HashEngine {
7878
}
7979

8080
/// Output of the SHA256 hash function
81+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
8182
#[repr(transparent)]
82-
pub struct Hash([u8; 64]);
83+
pub struct Hash(
84+
#[cfg_attr(feature = "schemars", schemars(schema_with="util::json_hex_string::len_64"))]
85+
[u8; 64]
86+
);
8387

8488
impl Copy for Hash {}
8589

@@ -437,6 +441,27 @@ mod tests {
437441
)],
438442
);
439443
}
444+
445+
#[cfg(all(feature = "schemars",feature = "serde"))]
446+
#[test]
447+
fn jsonschema_accurate() {
448+
static HASH_BYTES: [u8; 64] = [
449+
0x8b, 0x41, 0xe1, 0xb7, 0x8a, 0xd1, 0x15, 0x21,
450+
0x11, 0x3c, 0x52, 0xff, 0x18, 0x2a, 0x1b, 0x8e,
451+
0x0a, 0x19, 0x57, 0x54, 0xaa, 0x52, 0x7f, 0xcd,
452+
0x00, 0xa4, 0x11, 0x62, 0x0b, 0x46, 0xf2, 0x0f,
453+
0xff, 0xfb, 0x80, 0x88, 0xcc, 0xf8, 0x54, 0x97,
454+
0x12, 0x1a, 0xd4, 0x49, 0x9e, 0x08, 0x45, 0xb8,
455+
0x76, 0xf6, 0xdd, 0x66, 0x40, 0x08, 0x8a, 0x2f,
456+
0x0b, 0x2d, 0x8a, 0x60, 0x0b, 0xdf, 0x4c, 0x0c,
457+
];
458+
459+
let hash = sha512::Hash::from_slice(&HASH_BYTES).expect("right number of bytes");
460+
let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap();
461+
let s = schemars::schema_for! (sha512::Hash);
462+
let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap();
463+
assert!(jsonschema_valid::Config::from_schema(&schema, None).unwrap().validate(&js).is_ok());
464+
}
440465
}
441466

442467
#[cfg(all(test, feature="unstable"))]

0 commit comments

Comments
 (0)