feat(tvix/serde): add a function to with custom configuration
This adds a `from_str_with_config` function which takes a user-supplied closure that sets additional settings on the `tvix_eval::Evaluation`. Note that users can not set `strict = false`, but other settings are not restricted. This solves b/262. Change-Id: Ice184400b843cfbcaa5b6fe251ced12b6815e085 Reviewed-on: https://cl.tvl.fyi/c/depot/+/8808 Autosubmit: tazjin <tazjin@tvl.su> Tested-by: BuildkiteCI Reviewed-by: flokli <flokli@flokli.de>
This commit is contained in:
		
							parent
							
								
									80403d56b5
								
							
						
					
					
						commit
						3e915af8bb
					
				
					 1 changed files with 16 additions and 1 deletions
				
			
		| 
						 | 
				
			
			@ -2,6 +2,7 @@
 | 
			
		|||
 | 
			
		||||
use serde::de::value::{MapDeserializer, SeqDeserializer};
 | 
			
		||||
use serde::de::{self, EnumAccess, VariantAccess};
 | 
			
		||||
pub use tvix_eval::Evaluation;
 | 
			
		||||
use tvix_eval::Value;
 | 
			
		||||
 | 
			
		||||
use crate::error::Error;
 | 
			
		||||
| 
						 | 
				
			
			@ -28,12 +29,26 @@ impl de::IntoDeserializer<'_, Error> for NixDeserializer {
 | 
			
		|||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Evaluate the Nix code in `src` and attempt to deserialise the
 | 
			
		||||
/// value it returns to `T`.
 | 
			
		||||
pub fn from_str<'code, T>(src: &'code str) -> Result<T, Error>
 | 
			
		||||
where
 | 
			
		||||
    T: serde::Deserialize<'code>,
 | 
			
		||||
{
 | 
			
		||||
    from_str_with_config(src, |_| /* no extra config */ ())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Evaluate the Nix code in `src`, with extra configuration for the
 | 
			
		||||
/// `tvix_eval::Evaluation` provided by the given closure.
 | 
			
		||||
pub fn from_str_with_config<'code, T, F>(src: &'code str, config: F) -> Result<T, Error>
 | 
			
		||||
where
 | 
			
		||||
    T: serde::Deserialize<'code>,
 | 
			
		||||
    F: FnOnce(&mut Evaluation),
 | 
			
		||||
{
 | 
			
		||||
    // First step is to evaluate the Nix code ...
 | 
			
		||||
    let mut eval = tvix_eval::Evaluation::new(src, None);
 | 
			
		||||
    let mut eval = Evaluation::new(src, None);
 | 
			
		||||
    config(&mut eval);
 | 
			
		||||
 | 
			
		||||
    eval.strict = true;
 | 
			
		||||
    let source = eval.source_map();
 | 
			
		||||
    let result = eval.evaluate();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue