about summary refs log tree commit diff
path: root/tvix
diff options
context:
space:
mode:
authorAdam Joseph <adam@westernsemico.com>2022-11-25T22·47-0800
committerclbot <clbot@tvl.fyi>2022-11-26T21·20+0000
commitb5a1b965a6892d35b06ab315a531c718e02cd267 (patch)
tree9043d0e73b74c81d7160ff5dec5b1aa69ee104d9 /tvix
parent56c00df71036a3560a5f710f527fe7ece54eb665 (diff)
feat(tvix/eval): add --raw flag to eval, like cppnix r/5341
Signed-off-by: Adam Joseph <adam@westernsemico.com>
Change-Id: If07250a45fdf65a3f22ed8c37d7f37b45edccde9
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7416
Tested-by: BuildkiteCI
Reviewed-by: tazjin <tazjin@tvl.su>
Diffstat (limited to 'tvix')
-rw-r--r--tvix/eval/src/eval.rs3
-rw-r--r--tvix/eval/src/main.rs15
2 files changed, 16 insertions, 2 deletions
diff --git a/tvix/eval/src/eval.rs b/tvix/eval/src/eval.rs
index 6a35630b3ff7..bfd827e2ca5b 100644
--- a/tvix/eval/src/eval.rs
+++ b/tvix/eval/src/eval.rs
@@ -36,6 +36,9 @@ pub struct Options {
     /// A colon-separated list of directories to use to resolve `<...>`-style paths
     #[cfg_attr(feature = "repl", clap(long, short = 'I', env = "NIX_PATH"))]
     nix_search_path: Option<NixSearchPath>,
+
+    #[cfg_attr(feature = "repl", clap(long))]
+    pub raw: bool,
 }
 
 impl Options {
diff --git a/tvix/eval/src/main.rs b/tvix/eval/src/main.rs
index 22bea16d0e20..bef48d07a0c4 100644
--- a/tvix/eval/src/main.rs
+++ b/tvix/eval/src/main.rs
@@ -2,6 +2,7 @@ use std::{fs, path::PathBuf};
 
 use clap::Parser;
 use rustyline::{error::ReadlineError, Editor};
+use tvix_eval::Value;
 
 #[derive(Parser)]
 struct Args {
@@ -21,8 +22,9 @@ fn main() {
     if let Some(file) = args.script {
         run_file(file, args.eval_options)
     } else if let Some(expr) = args.expr {
+        let raw = args.eval_options.raw;
         if let Ok(result) = tvix_eval::interpret(&expr, None, args.eval_options) {
-            println!("=> {} :: {}", result, result.type_of())
+            println_result(&result, raw);
         }
     } else {
         run_prompt(args.eval_options)
@@ -34,12 +36,21 @@ fn run_file(mut path: PathBuf, eval_options: tvix_eval::Options) {
         path.push("default.nix");
     }
     let contents = fs::read_to_string(&path).expect("failed to read the input file");
+    let raw = eval_options.raw;
     match tvix_eval::interpret(&contents, Some(path), eval_options) {
-        Ok(result) => println!("=> {} :: {}", result, result.type_of()),
+        Ok(result) => println_result(&result, raw),
         Err(err) => eprintln!("{}", err),
     }
 }
 
+fn println_result(result: &Value, raw: bool) {
+    if raw {
+        println!("{}", result.to_str().unwrap().as_str())
+    } else {
+        println!("=> {} :: {}", result, result.type_of())
+    }
+}
+
 fn state_dir() -> Option<PathBuf> {
     let mut path = dirs::data_dir();
     if let Some(p) = path.as_mut() {