| 181 | } |
| 182 | |
| 183 | pub fn deserialize<'de, D>(deserializer: D) -> Result<[u8; 32], D::Error> |
| 184 | where |
| 185 | D: Deserializer<'de>, |
| 186 | { |
| 187 | let s = String::deserialize(deserializer)?; |
| 188 | if s.len() != 64 { |
| 189 | return Err(serde::de::Error::custom(format!( |
| 190 | "expected 64 hex chars, got {}", |
| 191 | s.len() |
| 192 | ))); |
| 193 | } |
| 194 | |
| 195 | let mut hash = [0u8; 32]; |
| 196 | for (i, chunk) in s.as_bytes().chunks(2).enumerate() { |
| 197 | let hex_str = |
| 198 | std::str::from_utf8(chunk).map_err(|_| serde::de::Error::custom("invalid utf8"))?; |
| 199 | hash[i] = u8::from_str_radix(hex_str, 16) |
| 200 | .map_err(|_| serde::de::Error::custom(format!("invalid hex: {}", hex_str)))?; |
| 201 | } |
| 202 | |
| 203 | Ok(hash) |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // ═══════════════════════════════════════════════════════════════════════ |