From b5a1b965a6892d35b06ab315a531c718e02cd267 Mon Sep 17 00:00:00 2001 From: Adam Joseph Date: Fri, 25 Nov 2022 14:47:39 -0800 Subject: feat(tvix/eval): add --raw flag to eval, like cppnix Signed-off-by: Adam Joseph Change-Id: If07250a45fdf65a3f22ed8c37d7f37b45edccde9 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7416 Tested-by: BuildkiteCI Reviewed-by: tazjin --- tvix/eval/src/eval.rs | 3 +++ tvix/eval/src/main.rs | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/tvix/eval/src/eval.rs b/tvix/eval/src/eval.rs index 6a35630b3f..bfd827e2ca 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, + + #[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 22bea16d0e..bef48d07a0 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 { let mut path = dirs::data_dir(); if let Some(p) = path.as_mut() { -- cgit 1.4.1