about summary refs log tree commit diff
path: root/tvix/derivation/src/derivation.rs
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2023-01-04T21·12+0100
committerflokli <flokli@flokli.de>2023-01-06T12·25+0000
commit9df9a2f1ab412848908efbabcb21ed6246263550 (patch)
treee497b45a4c68997c5dd018149b94e95cc19a7831 /tvix/derivation/src/derivation.rs
parentd4603fc0af5f2be27716d4d3c3fc6c514c8ffbe9 (diff)
feat(tvix/derivation): add get_fixed_output() helper function r/5609
This will return the fixed output of a derivation (and its hash), or
None if the Derivation is not fixed-output.

It will simplify the logic in the output path calculation a bit.

Change-Id: I1066cc18ee4fc419421a8c5995c93ba91b35588f
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7760
Tested-by: BuildkiteCI
Reviewed-by: tazjin <tazjin@tvl.su>
Diffstat (limited to 'tvix/derivation/src/derivation.rs')
-rw-r--r--tvix/derivation/src/derivation.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/tvix/derivation/src/derivation.rs b/tvix/derivation/src/derivation.rs
index a5ea56fbbf89..a9e769543e00 100644
--- a/tvix/derivation/src/derivation.rs
+++ b/tvix/derivation/src/derivation.rs
@@ -69,6 +69,27 @@ impl Derivation {
         Ok(())
     }
 
+    /// Returns the fixed output path and its hash
+    // (if the Derivation is fixed output),
+    /// or None if there is no fixed output.
+    /// This takes some shortcuts in case more than one output exists, as this
+    /// can't be a valid fixed-output Derivation.
+    pub fn get_fixed_output(&self) -> Option<(&String, &Hash)> {
+        if self.outputs.len() != 1 {
+            return None;
+        }
+
+        match self.outputs.get("out") {
+            #[allow(clippy::manual_map)]
+            Some(out_output) => match &out_output.hash {
+                Some(out_output_hash) => Some((&out_output.path, out_output_hash)),
+                // There has to be a hash, otherwise it would not be FOD
+                None => None,
+            },
+            None => None,
+        }
+    }
+
     /// Returns the drv path of a Derivation struct.
     ///
     /// The drv path is calculated like this: