about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2023-01-29T12·43+0300
committertazjin <tazjin@tvl.su>2023-01-29T17·39+0000
commit124af9e5d5909e9bca536067c276726a37aea629 (patch)
tree0d0ff040942b5464890b0bee9093ee43857b0c1e
parent91146b204ff7a47eb67d6fb18bbcad0e1d3bbecd (diff)
refactor(tvix/cli): add helper method for strong string coercion r/5776
This is repetitive and error prone (e.g. switching around
to_string/as_str has drastic consequences) due to the ToString
overloads.

Change-Id: I9b16a2e0e05e4c21e83f43e9f603746eb42e53f7
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7947
Reviewed-by: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
Autosubmit: tazjin <tazjin@tvl.su>
-rw-r--r--tvix/cli/src/derivation.rs58
1 files changed, 29 insertions, 29 deletions
diff --git a/tvix/cli/src/derivation.rs b/tvix/cli/src/derivation.rs
index 122330b963ef..012294375e11 100644
--- a/tvix/cli/src/derivation.rs
+++ b/tvix/cli/src/derivation.rs
@@ -173,26 +173,22 @@ fn populate_output_configuration(
         (Some(hash), Some(algo), hash_mode) => match drv.outputs.get_mut("out") {
             None => return Err(Error::ConflictingOutputTypes.into()),
             Some(out) => {
-                let algo = algo
-                    .force(vm)?
-                    .coerce_to_string(CoercionKind::Strong, vm)?
-                    .as_str()
-                    .to_string();
-
-                let digest_str = hash
-                    .force(vm)?
-                    .coerce_to_string(CoercionKind::Strong, vm)?
-                    .as_str()
-                    .to_string();
+                let algo = strong_coerce_to_string(
+                    vm,
+                    &algo,
+                    "evaluating outputHashAlgo of a derivation",
+                )?;
+
+                let digest_str =
+                    strong_coerce_to_string(vm, &hash, "evaluating outputHash of a derivation")?;
 
                 let hash_mode = match hash_mode {
                     None => None,
-                    Some(mode) => Some(
-                        mode.force(vm)?
-                            .coerce_to_string(CoercionKind::Strong, vm)?
-                            .as_str()
-                            .to_string(),
-                    ),
+                    Some(mode) => Some(strong_coerce_to_string(
+                        vm,
+                        &mode,
+                        "evaluating outputHashMode of a derivation",
+                    )?),
                 };
 
                 // construct out.hash
@@ -227,13 +223,11 @@ fn handle_derivation_parameters(
         "args" => {
             let args = value.to_list()?;
             for arg in args {
-                drv.arguments.push(
-                    arg.force(vm)?
-                        .coerce_to_string(CoercionKind::Strong, vm)
-                        .context("handling command-line builder arguments")?
-                        .as_str()
-                        .to_string(),
-                );
+                drv.arguments.push(strong_coerce_to_string(
+                    vm,
+                    &arg,
+                    "handling command-line builder arguments",
+                )?);
             }
 
             // The arguments do not appear in the environment.
@@ -264,6 +258,16 @@ fn handle_derivation_parameters(
     Ok(true)
 }
 
+fn strong_coerce_to_string(vm: &mut VM, val: &Value, ctx: &str) -> Result<String, ErrorKind> {
+    Ok(val
+        .force(vm)
+        .context(ctx)?
+        .coerce_to_string(CoercionKind::Strong, vm)
+        .context(ctx)?
+        .as_str()
+        .to_string())
+}
+
 #[builtins(state = "Rc<RefCell<KnownPaths>>")]
 mod derivation_builtins {
     use super::*;
@@ -316,11 +320,7 @@ mod derivation_builtins {
                 continue;
             }
 
-            let val_str = value
-                .force(vm)?
-                .coerce_to_string(CoercionKind::Strong, vm)?
-                .as_str()
-                .to_string();
+            let val_str = strong_coerce_to_string(vm, &value, "evaluating derivation attributes")?;
 
             // handle_derivation_parameters tells us whether the
             // argument should be added to the environment; continue