From f6270c13babfd787bd2b05447b8f9f0a099280b1 Mon Sep 17 00:00:00 2001 From: Лач Date: Wed, 10 Jun 2020 18:42:23 +0000 Subject: [PATCH] feat: TLA support --- --- a/cmds/jrsonnet/src/main.rs +++ b/cmds/jrsonnet/src/main.rs @@ -2,9 +2,10 @@ use clap::Clap; use jsonnet_evaluator::{EvaluationSettings, EvaluationState, LocError, StackTrace, Val}; +use jsonnet_parser::{el, Arg, ArgsDesc, Expr, LocExpr, ParserSettings}; use location::{offset_to_location, CodeLocation}; use std::env::current_dir; -use std::{path::PathBuf, str::FromStr}; +use std::{collections::HashMap, path::PathBuf, str::FromStr}; enum Format { None, @@ -134,6 +135,36 @@ let result = evaluator.evaluate_file(&input); match result { Ok(v) => { + let v = match v { + Val::Func(f) => { + let mut desc_map = HashMap::new(); + for ExtStr { name, value } in opts.tla_str.iter().cloned() { + desc_map.insert(name, el!(Expr::Str(value))); + } + for ExtStr { name, value } in opts.tla_code.iter().cloned() { + desc_map.insert( + name, + jsonnet_parser::parse( + &value, + &ParserSettings { + file_name: PathBuf::new(), + loc_data: false, + }, + ) + .unwrap(), + ); + } + evaluator.add_global("__tmp__tlf__".to_owned(), Val::Func(f)); + evaluator + .evaluate_raw(el!(Expr::Apply( + el!(Expr::Var("__tmp__tlf__".to_owned())), + ArgsDesc(desc_map.into_iter().map(|(k, v)| Arg(Some(k), v)).collect()), + false, + ))) + .unwrap() + } + v => v, + }; let v = match opts.format { Format::Json => { if opts.no_stdlib { --- a/crates/jsonnet-evaluator/src/lib.rs +++ b/crates/jsonnet-evaluator/src/lib.rs @@ -191,9 +191,14 @@ file_name: PathBuf::from("raw.jsonnet"), loc_data: true, }, - ); + ) + .unwrap(); + self.evaluate_raw(parsed) + } + + pub fn evaluate_raw(&self, code: LocExpr) -> Result { self.begin_state(); - let value = evaluate(self.create_default_context()?, &parsed.unwrap()); + let value = evaluate(self.create_default_context()?, &code); self.end_state(); value } -- gitstuff