use std::fmt::Display; use serde::{Deserialize, Serialize}; #[derive(Clone, Copy, PartialEq, Eq, Hash)] pub struct StringHash(pub [u8; 32]); impl Display for StringHash { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(&hex::encode(self.0)) } } impl std::fmt::Debug for StringHash { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "\"{}\"", self) } } impl Serialize for StringHash { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, { hex::serialize(self.0, serializer) } } impl<'de> Deserialize<'de> for StringHash { fn deserialize(deserializer: D) -> Result where D: serde::Deserializer<'de>, { Ok(StringHash(hex::deserialize(deserializer)?)) } } macro_rules! stringhash_newtype { ($id:ident) => { #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)] pub struct $id(pub StringHash); impl Display for $id { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.0.fmt(f) } } }; } stringhash_newtype!(InputHash); stringhash_newtype!(DependencyHash); stringhash_newtype!(LayerHash); stringhash_newtype!(ArchiveHash);