about summary refs log blame commit diff
path: root/tvix/nix-compat/src/derivation/output.rs
blob: 37161cbe98b588800d476e58ea541b1575b13af6 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
                                   

                                                
                                    
 
                                                                       

                     

                     
                                                



                                    
                                     
     
 
                                                                                    




                                                                                     

                     
             
         
                                  
                                                                                 

                                                                                     
         

              
 
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.as_bytes()) {
                return Err(OutputError::InvalidOutputPath(self.path.to_string(), e));
            }
        }
        Ok(())
    }
}