Implement garbage collect on the server
This commit is contained in:
@ -33,6 +33,20 @@ impl Sha256 {
|
||||
|
||||
Sha256(hash_arr)
|
||||
}
|
||||
|
||||
pub fn from_hex(hex_str: &str) -> Option<Self> {
|
||||
if hex_str.len() != 32 * 2 {
|
||||
return None; // String has wrong length
|
||||
}
|
||||
|
||||
let mut out = [0u8; 32];
|
||||
for (i, byte) in out.iter_mut().enumerate() {
|
||||
let string_index = i * 2;
|
||||
*byte = u8::from_str_radix(&hex_str[string_index..=string_index + 1], 16).ok()?;
|
||||
}
|
||||
|
||||
Some(Sha256(out))
|
||||
}
|
||||
}
|
||||
|
||||
fn as_hex<S>(hash: &[u8; 32], serializer: S) -> Result<S::Ok, S::Error>
|
||||
@ -48,22 +62,10 @@ where
|
||||
{
|
||||
use serde::de::Error;
|
||||
|
||||
fn hex2bytes(s: &str) -> Option<[u8; 32]> {
|
||||
if s.len() != 32 * 2 {
|
||||
return None; // String has wrong length
|
||||
}
|
||||
|
||||
let mut out = [0u8; 32];
|
||||
for (i, byte) in out.iter_mut().enumerate() {
|
||||
let string_index = i * 2;
|
||||
*byte = u8::from_str_radix(&s[string_index..=string_index + 1], 16).ok()?;
|
||||
}
|
||||
|
||||
return Some(out);
|
||||
}
|
||||
|
||||
String::deserialize(deserializer).and_then(|string| {
|
||||
hex2bytes(&string).ok_or_else(|| Error::custom("Invalid HEX String!".to_string()))
|
||||
Ok(Sha256::from_hex(&string)
|
||||
.ok_or_else(|| Error::custom("Invalid HEX String!".to_string()))?
|
||||
.0)
|
||||
})
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user