Skip to main content

nix_compat/derivation/
hdm_lookup.rs

1use crate::{nixhash::Sha256, store_path::StorePathRef};
2
3/// Lookup hash derivation modulo for a derivation.
4///
5/// The [hash derivation modulo] is recursive. And so to calculate the HDM of a
6/// derivation it requires the HDM for all its input derviations.
7///
8/// This trait exists to provide the HDM values for those input derivations to
9/// the functions that calculate the HDM.
10///
11/// To help with implemeting this trait [`lookup_fn`] is provided.
12///
13/// [hash derivation module]: nix_compat::derivation#hash-derivation-module
14pub trait HashDerivationModuloLookup {
15    /// Lookup the [hash derivation modulo] of the provided derivation store path.
16    ///
17    /// [hash derivation module]: nix_compat::derivation#hash-derivation-module
18    fn lookup_hdm(&self, drv_path: &StorePathRef<'_>) -> Option<Sha256>;
19}
20
21/// Implement [`HashDerivationModuloLookup`] using the provided closure.
22pub fn lookup_fn<F>(func: F) -> impl HashDerivationModuloLookup
23where
24    F: Fn(&StorePathRef) -> Option<Sha256>,
25{
26    LookupFn { func }
27}
28
29struct LookupFn<F> {
30    func: F,
31}
32
33impl<F> HashDerivationModuloLookup for LookupFn<F>
34where
35    F: Fn(&StorePathRef) -> Option<Sha256>,
36{
37    fn lookup_hdm(&self, drv_path: &StorePathRef) -> Option<Sha256> {
38        (self.func)(drv_path)
39    }
40}
41
42#[cfg(feature = "hashbrown")]
43impl HashDerivationModuloLookup for hashbrown::HashMap<crate::store_path::StorePath, Sha256> {
44    fn lookup_hdm(&self, drv_path: &StorePathRef<'_>) -> Option<Sha256> {
45        self.get(drv_path).copied()
46    }
47}