git.delta.rocks / jrsonnet / refs/commits / 6a58b0ec3386

difftreelog

perf use json serialization directly

Лач2020-06-26parent: #bb84d33.patch.diff
in: master

4 files changed

modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
5use jsonnet_parser::{el, Arg, ArgsDesc, Expr, LocExpr, ParserSettings};5use jsonnet_parser::{el, Arg, ArgsDesc, Expr, LocExpr, ParserSettings};
6use location::{offset_to_location, CodeLocation};6use location::{offset_to_location, CodeLocation};
7use std::env::current_dir;7use std::env::current_dir;
8use std::{collections::HashMap, path::PathBuf, str::FromStr, rc::Rc};8use std::{collections::HashMap, path::PathBuf, rc::Rc, str::FromStr};
99
10enum Format {10enum Format {
11 None,11 None,
173 }173 }
174 v => v,174 v => v,
175 };175 };
176 let v = match opts.format {176 let v = evaluator.run_in_state(|| match opts.format {
177 Format::Json => {177 Format::Json => Ok(Val::Str(v.into_json(opts.line_padding)?)),
178 if opts.no_stdlib {
179 evaluator.with_stdlib();
180 }
181 evaluator.add_global("__tmp__to_json__".into(), v);
182 let v = evaluator.parse_evaluate_raw(&format!(
183 "std.manifestJsonEx(__tmp__to_json__, \"{}\")",
184 " ".repeat(opts.line_padding),
185 ));
186 match v {
187 Ok(v) => v,
188 Err(err) => {
189 print_error(&err, evaluator, &opts);
190 std::process::exit(1);
191 }
192 }
193 }
194 Format::Yaml => {178 Format::Yaml => {
195 if opts.no_stdlib {
196 evaluator.with_stdlib();
197 }
198 evaluator.add_global("__tmp__to_yaml__".into(), v);179 evaluator.add_global("__tmp__to_yaml__".into(), v);
199 let v = evaluator180 evaluator.parse_evaluate_raw("std.manifestYamlDoc(__tmp__to_yaml__, \" \")")
200 .parse_evaluate_raw("std.manifestYamlDoc(__tmp__to_yaml__, \" \")");
201 match v {
202 Ok(v) => v,
203 Err(err) => {
204 print_error(&err, evaluator, &opts);
205 std::process::exit(1);
206 }
207 }
208 }181 }
209 _ => v,182 _ => Ok(v),
210 };183 });
184 let v = match v {
185 Ok(v) => v,
186 Err(err) => {
187 print_error(&err, evaluator, &opts);
188 std::process::exit(1);
189 }
190 };
211 match v {191 match v {
212 Val::Str(s) => println!("{}", s),192 Val::Str(s) => println!("{}", s),
213 Val::Num(n) => println!("{}", n),193 Val::Num(n) => println!("{}", n),
modifiedcrates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
667 evaluate(context.clone(), &args[1].1)?,667 evaluate(context.clone(), &args[1].1)?,
668 ) {668 ) {
669 Val::Arr(Rc::new(669 Val::Arr(Rc::new(
670 arr.iter()670 arr.iter()
671 .cloned()
671 .filter(|e| {672 .filter(|e| {
672 predicate673 predicate
673 .evaluate_values(context.clone(), &[e.clone()])674 .evaluate_values(context.clone(), &[e.clone()])
674 .unwrap()675 .unwrap()
675 .try_cast_bool("filter predicate")676 .try_cast_bool("filter predicate")
676 .unwrap()677 .unwrap()
677 })678 })
678 .cloned()
679 .collect(),679 .collect(),
680 ))680 ))
681 } else {681 } else {
modifiedcrates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth
296 Err(LocError(err, self.stack_trace()))296 Err(LocError(err, self.stack_trace()))
297 }297 }
298298
299 fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {299 pub fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {
300 EVAL_STATE.with(|v| {300 EVAL_STATE.with(|v| {
301 let has_state = v.borrow().is_some();301 let has_state = v.borrow().is_some();
302 if !has_state {302 if !has_state {
364 ($str: expr) => {{364 ($str: expr) => {{
365 let evaluator = EvaluationState::default();365 let evaluator = EvaluationState::default();
366 evaluator.with_stdlib();366 evaluator.with_stdlib();
367 let val = evaluator.parse_evaluate_raw($str).unwrap();367 evaluator
368 .parse_evaluate_raw($str)
369 .unwrap()
368 evaluator.add_global("__tmp__to_yaml__".into(), val);370 .into_json(0)
369 evaluator
370 .parse_evaluate_raw("std.manifestJsonEx(__tmp__to_yaml__, \"\")")
371 .unwrap()371 .unwrap()
372 .try_cast_str("there should be json string")
373 .unwrap()
374 .clone()
375 .replace("\n", "")372 .replace("\n", "")
376 }};373 }};
377 }374 }
modifiedcrates/jsonnet-evaluator/src/val.rsdiffbeforeafterboth
1use crate::{1use crate::{
2 create_error, evaluate,2 create_error, evaluate,
3 function::{inline_parse_function_call, place_args},3 function::{inline_parse_function_call, place_args},
4 with_state, Context, Error, ObjValue, Result,4 Context, Error, ObjValue, Result,
5};5};
6use jsonnet_parser::{el, Arg, ArgsDesc, Expr, LocExpr, ParamsDesc};6use jsonnet_parser::{ArgsDesc, LocExpr, ParamsDesc};
7use std::{7use std::{
8 cell::RefCell,8 cell::RefCell,
9 fmt::{Debug, Display},9 fmt::{Debug, Display},
170 Val::Lazy(_) => self.clone().unwrap_if_lazy()?.value_type()?,170 Val::Lazy(_) => self.clone().unwrap_if_lazy()?.value_type()?,
171 })171 })
172 }172 }
173 #[cfg(feature = "faster")]
174 pub fn into_json(self, padding: usize) -> Result<Rc<str>> {
175 manifest_json_ex(&self, &" ".repeat(padding)).map(|s| s.into())
176 }
177 #[cfg(not(feature = "faster"))]
173 pub fn into_json(self, padding: usize) -> Result<Rc<str>> {178 pub fn into_json(self, padding: usize) -> Result<Rc<str>> {
174 with_state(|s| {179 with_state(|s| {
175 let ctx = s180 let ctx = s