about summary refs log tree commit diff
path: root/tvix/nix-compat/src/derivation/output.rs
blob: 4bfc7bf8014d6c0cfd818bc4a55885d53e2f4063 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use crate::derivation::OutputError;
use crate::nixhash::{HashAlgo, NixHashWithMode};
use crate::store_path::StorePath;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct Output {
    pub path: String,

    #[serde(flatten)]
    pub hash_with_mode: Option<NixHashWithMode>,
}

impl Output {
    pub fn is_fixed(&self) -> bool {
        self.hash_with_mode.is_some()
    }

    pub fn validate(&self, validate_output_paths: bool) -> Result<(), OutputError> {
        if let Some(hash) = &self.hash_with_mode {
            match hash {
                NixHashWithMode::Flat(h) | NixHashWithMode::Recursive(h) => {
                    if h.algo != HashAlgo::Sha1 || h.algo != HashAlgo::Sha256 {
                        return Err(OutputError::InvalidHashAlgo(h.algo.to_string()));
                    }
                }
            }
        }
        if validate_output_paths {
            if let Err(e) = StorePath::from_absolute_path(&self.path) {
                return Err(OutputError::InvalidOutputPath(self.path.to_string(), e));
            }
        }
        Ok(())
    }
}