about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2023-06-16T11·49+0300
committerclbot <clbot@tvl.fyi>2023-06-16T12·05+0000
commit3e915af8bb2ef3f9714a3697ad9ac07ac192eaad (patch)
tree78f1f7b600f99c02310b062244139995e22eed12
parent80403d56b5a1c94343b8c6b1acb7e8d23e75b9c1 (diff)
feat(tvix/serde): add a function to with custom configuration r/6324
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>
-rw-r--r--tvix/serde/src/de.rs17
1 files changed, 16 insertions, 1 deletions
diff --git a/tvix/serde/src/de.rs b/tvix/serde/src/de.rs
index 43efc71c6f67..49a069a51588 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<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();