From b6fda6517c154a49e3a901f8409a4196ac66ed22 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Sat, 10 Aug 2024 06:57:04 +1000 Subject: [PATCH] Implement JsonSchema for siphash::Hash We lost this impl at some stage in the last few weeks and did not notice because of a bug in `run_task` [0]. Implement `JsonSchema` for `siphash` as we do for all the other hash types. [0] https://github.com/rust-bitcoin/rust-bitcoin-maintainer-tools/issues/10 --- hashes/src/siphash24.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/hashes/src/siphash24.rs b/hashes/src/siphash24.rs index 2e57adcdd..9895ee0f5 100644 --- a/hashes/src/siphash24.rs +++ b/hashes/src/siphash24.rs @@ -14,6 +14,21 @@ use crate::HashEngine as _; #[repr(transparent)] pub struct Hash([u8; 8]); +#[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 mut schema: schemars::schema::SchemaObject = ::json_schema(gen).into(); + schema.string = Some(Box::new(schemars::schema::StringValidation { + max_length: Some(8 * 2), + min_length: Some(8 * 2), + pattern: Some("[0-9a-fA-F]+".to_owned()), + })); + schema.into() + } +} + #[cfg(not(hashes_fuzz))] fn from_engine(e: HashEngine) -> Hash { Hash::from_u64(Hash::from_engine_to_u64(e)) }