From 9df9a2f1ab412848908efbabcb21ed6246263550 Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Wed, 4 Jan 2023 22:12:03 +0100 Subject: feat(tvix/derivation): add get_fixed_output() helper function 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 --- tvix/derivation/src/derivation.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'tvix') 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: -- cgit 1.4.1