impl fmt::Display for Sha256 for Logging

This commit is contained in:
2019-06-03 19:05:55 +02:00
parent 189b1955d7
commit b2ba9a9ba4
2 changed files with 13 additions and 7 deletions

View File

@ -1,5 +1,7 @@
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;
#[derive(Clone, Debug, PartialOrd, Ord, Deserialize, Serialize, Eq, Hash, PartialEq)]
pub struct Sha256(#[serde(serialize_with = "as_hex", deserialize_with = "from_hex")] pub [u8; 32]);
@ -49,6 +51,15 @@ impl Sha256 {
}
}
impl fmt::Display for Sha256 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for digit in self.0.iter() {
write!(f, "{:02X}", digit)?
}
Ok(())
}
}
fn as_hex<S>(hash: &[u8; 32], serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,