about summary refs log tree commit diff
path: root/tvix
diff options
context:
space:
mode:
Diffstat (limited to 'tvix')
-rw-r--r--tvix/nix-compat/src/derivation/mod.rs26
-rw-r--r--tvix/nix-compat/src/derivation/tests/mod.rs4
2 files changed, 19 insertions, 11 deletions
diff --git a/tvix/nix-compat/src/derivation/mod.rs b/tvix/nix-compat/src/derivation/mod.rs
index cecaa3f42d80..f500161f9181 100644
--- a/tvix/nix-compat/src/derivation/mod.rs
+++ b/tvix/nix-compat/src/derivation/mod.rs
@@ -40,6 +40,9 @@ pub struct Derivation {
 }
 
 impl Derivation {
+    /// write the Derivation to the given [std::fmt::Write], in ATerm format.
+    ///
+    /// The only errors returns are these when writing to the passed writer.
     pub fn serialize(&self, writer: &mut impl std::fmt::Write) -> Result<(), std::fmt::Error> {
         writer.write_str(write::DERIVATION_PREFIX)?;
         writer.write_char(write::PAREN_OPEN)?;
@@ -57,6 +60,18 @@ impl Derivation {
         Ok(())
     }
 
+    /// return the ATerm serialization as a string.
+    pub fn to_aterm_string(&self) -> String {
+        let mut buffer = String::new();
+
+        // invoke serialize and write to the buffer.
+        // Note we only propagate errors writing to the writer in serialize,
+        // which won't panic for the string we write to.
+        self.serialize(&mut buffer).unwrap();
+
+        buffer
+    }
+
     /// Returns the fixed output path and its hash
     // (if the Derivation is fixed output),
     /// or None if there is no fixed output.
@@ -116,7 +131,7 @@ impl Derivation {
         // it as a hex-encoded string (prefixed with sha256:).
         let aterm_digest = {
             let mut derivation_hasher = Sha256::new();
-            derivation_hasher.update(self.to_string());
+            derivation_hasher.update(self.to_aterm_string());
             derivation_hasher.finalize()
         };
 
@@ -171,7 +186,7 @@ impl Derivation {
                 };
 
                 // write the ATerm of that to the hash function
-                hasher.update(replaced_derivation.to_string());
+                hasher.update(replaced_derivation.to_aterm_string());
 
                 hasher.finalize()
             }
@@ -281,10 +296,3 @@ impl Derivation {
         Ok(())
     }
 }
-
-impl std::fmt::Display for Derivation {
-    /// Formats the Derivation in ATerm representation.
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        self.serialize(f)
-    }
-}
diff --git a/tvix/nix-compat/src/derivation/tests/mod.rs b/tvix/nix-compat/src/derivation/tests/mod.rs
index 57c08f5a75f3..7b0ef6c3d961 100644
--- a/tvix/nix-compat/src/derivation/tests/mod.rs
+++ b/tvix/nix-compat/src/derivation/tests/mod.rs
@@ -44,13 +44,13 @@ fn validate(path_to_drv_file: &str) {
 }
 
 #[test_resources("src/derivation/tests/derivation_tests/*.drv")]
-fn check_to_string(path_to_drv_file: &str) {
+fn check_to_aterm_string(path_to_drv_file: &str) {
     let data = read_file(&format!("{}.json", path_to_drv_file));
     let derivation: Derivation = serde_json::from_str(&data).expect("JSON was not well-formatted");
 
     let expected = read_file(path_to_drv_file);
 
-    assert_eq!(expected, derivation.to_string());
+    assert_eq!(expected, derivation.to_aterm_string());
 }
 
 #[test_case("bar","0hm2f1psjpcwg8fijsmr4wwxrx59s092-bar.drv"; "fixed_sha256")]