about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2023-11-22T17·25+0200
committerflokli <flokli@flokli.de>2023-11-22T20·11+0000
commit1e04f60800286e881ea861607c14154538edc410 (patch)
tree3c013297d7608af284ae8391a3b91dafbf72e24b
parent671206a63f89f0ca58f830eabae0d7c31f70a750 (diff)
refactor(tvix/store): impl From<nixhash::CAHash> for nar_info::Ca r/7048
Change-Id: Iaa68044d3b469f15a932aa3b59548505eaa6b8bb
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10106
Autosubmit: flokli <flokli@flokli.de>
Reviewed-by: edef <edef@edef.eu>
Tested-by: BuildkiteCI
-rw-r--r--tvix/store/src/proto/mod.rs41
1 files changed, 25 insertions, 16 deletions
diff --git a/tvix/store/src/proto/mod.rs b/tvix/store/src/proto/mod.rs
index 748976e23a40..98bd9180d9a4 100644
--- a/tvix/store/src/proto/mod.rs
+++ b/tvix/store/src/proto/mod.rs
@@ -176,6 +176,30 @@ impl PathInfo {
     }
 }
 
+impl From<&nix_compat::nixhash::CAHash> for nar_info::Ca {
+    fn from(value: &nix_compat::nixhash::CAHash) -> Self {
+        nar_info::Ca {
+            r#type: match value {
+                CAHash::Flat(NixHash::Md5(_)) => nar_info::ca::Hash::FlatMd5.into(),
+                CAHash::Flat(NixHash::Sha1(_)) => nar_info::ca::Hash::FlatSha1.into(),
+                CAHash::Flat(NixHash::Sha256(_)) => nar_info::ca::Hash::FlatSha256.into(),
+                CAHash::Flat(NixHash::Sha512(_)) => nar_info::ca::Hash::FlatSha512.into(),
+                CAHash::Nar(NixHash::Md5(_)) => nar_info::ca::Hash::NarMd5.into(),
+                CAHash::Nar(NixHash::Sha1(_)) => nar_info::ca::Hash::NarSha1.into(),
+                CAHash::Nar(NixHash::Sha256(_)) => nar_info::ca::Hash::NarSha256.into(),
+                CAHash::Nar(NixHash::Sha512(_)) => nar_info::ca::Hash::NarSha512.into(),
+                CAHash::Text(_) => nar_info::ca::Hash::TextSha256.into(),
+            },
+            digest: match value {
+                CAHash::Flat(ref nixhash) | CAHash::Nar(ref nixhash) => {
+                    nixhash.digest_as_bytes().to_vec().into()
+                }
+                CAHash::Text(digest) => digest.to_vec().into(),
+            },
+        }
+    }
+}
+
 impl From<&nix_compat::narinfo::NarInfo<'_>> for NarInfo {
     /// Converts from a NarInfo (returned from the NARInfo parser) to the proto-
     /// level NarInfo struct.
@@ -189,21 +213,6 @@ impl From<&nix_compat::narinfo::NarInfo<'_>> for NarInfo {
             })
             .collect();
 
-        let ca = value.ca.as_ref().map(|ca_hash| nar_info::Ca {
-            r#type: match ca_hash {
-                CAHash::Flat(NixHash::Md5(_)) => nar_info::ca::Hash::FlatMd5.into(),
-                CAHash::Flat(NixHash::Sha1(_)) => nar_info::ca::Hash::FlatSha1.into(),
-                CAHash::Flat(NixHash::Sha256(_)) => nar_info::ca::Hash::FlatSha256.into(),
-                CAHash::Flat(NixHash::Sha512(_)) => nar_info::ca::Hash::FlatSha512.into(),
-                CAHash::Nar(NixHash::Md5(_)) => nar_info::ca::Hash::NarMd5.into(),
-                CAHash::Nar(NixHash::Sha1(_)) => nar_info::ca::Hash::NarSha1.into(),
-                CAHash::Nar(NixHash::Sha256(_)) => nar_info::ca::Hash::NarSha256.into(),
-                CAHash::Nar(NixHash::Sha512(_)) => nar_info::ca::Hash::NarSha512.into(),
-                CAHash::Text(_) => nar_info::ca::Hash::TextSha256.into(),
-            },
-            digest: Bytes::copy_from_slice(ca_hash.digest().digest_as_bytes()),
-        });
-
         NarInfo {
             nar_size: value.nar_size,
             nar_sha256: Bytes::copy_from_slice(&value.nar_hash),
@@ -213,7 +222,7 @@ impl From<&nix_compat::narinfo::NarInfo<'_>> for NarInfo {
                 name: sp.name().to_owned(),
                 digest: Bytes::copy_from_slice(sp.digest()),
             }),
-            ca,
+            ca: value.ca.as_ref().map(|ca| ca.into()),
         }
     }
 }