git.delta.rocks / jrsonnet / refs/commits / f6270c13babf

difftreelog

feat TLA support

Лач2020-06-10parent: #d581de0.patch.diff
in: master

2 files changed

modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
22
3use clap::Clap;3use clap::Clap;
4use jsonnet_evaluator::{EvaluationSettings, EvaluationState, LocError, StackTrace, Val};4use jsonnet_evaluator::{EvaluationSettings, EvaluationState, LocError, StackTrace, Val};
5use jsonnet_parser::{el, Arg, ArgsDesc, Expr, LocExpr, ParserSettings};
5use location::{offset_to_location, CodeLocation};6use location::{offset_to_location, CodeLocation};
6use std::env::current_dir;7use std::env::current_dir;
7use std::{path::PathBuf, str::FromStr};8use std::{collections::HashMap, path::PathBuf, str::FromStr};
89
9enum Format {10enum Format {
10 None,11 None,
134 let result = evaluator.evaluate_file(&input);135 let result = evaluator.evaluate_file(&input);
135 match result {136 match result {
136 Ok(v) => {137 Ok(v) => {
138 let v = match v {
139 Val::Func(f) => {
140 let mut desc_map = HashMap::new();
141 for ExtStr { name, value } in opts.tla_str.iter().cloned() {
142 desc_map.insert(name, el!(Expr::Str(value)));
143 }
144 for ExtStr { name, value } in opts.tla_code.iter().cloned() {
145 desc_map.insert(
146 name,
147 jsonnet_parser::parse(
148 &value,
149 &ParserSettings {
150 file_name: PathBuf::new(),
151 loc_data: false,
152 },
153 )
154 .unwrap(),
155 );
156 }
157 evaluator.add_global("__tmp__tlf__".to_owned(), Val::Func(f));
158 evaluator
159 .evaluate_raw(el!(Expr::Apply(
160 el!(Expr::Var("__tmp__tlf__".to_owned())),
161 ArgsDesc(desc_map.into_iter().map(|(k, v)| Arg(Some(k), v)).collect()),
162 false,
163 )))
164 .unwrap()
165 }
166 v => v,
167 };
137 let v = match opts.format {168 let v = match opts.format {
138 Format::Json => {169 Format::Json => {
139 if opts.no_stdlib {170 if opts.no_stdlib {
modifiedcrates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- 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<Val> {
 		self.begin_state();
-		let value = evaluate(self.create_default_context()?, &parsed.unwrap());
+		let value = evaluate(self.create_default_context()?, &code);
 		self.end_state();
 		value
 	}