summaryrefslogtreecommitdiffstats
path: root/crates/rebel-common/src/string_hash.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/rebel-common/src/string_hash.rs')
-rw-r--r--crates/rebel-common/src/string_hash.rs53
1 files changed, 53 insertions, 0 deletions
diff --git a/crates/rebel-common/src/string_hash.rs b/crates/rebel-common/src/string_hash.rs
new file mode 100644
index 0000000..a2b00db
--- /dev/null
+++ b/crates/rebel-common/src/string_hash.rs
@@ -0,0 +1,53 @@
+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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: serde::Serializer,
+ {
+ hex::serialize(self.0, serializer)
+ }
+}
+impl<'de> Deserialize<'de> for StringHash {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ 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);