From 3e915af8bb2ef3f9714a3697ad9ac07ac192eaad Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Fri, 16 Jun 2023 14:49:58 +0300 Subject: 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 Tested-by: BuildkiteCI Reviewed-by: flokli --- tvix/serde/src/de.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tvix/serde/src/de.rs b/tvix/serde/src/de.rs index 43efc71c6f..49a069a515 100644 --- a/tvix/serde/src/de.rs +++ b/tvix/serde/src/de.rs @@ -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 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 +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(); -- cgit 1.4.1