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
--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -5,7 +5,7 @@
 use jsonnet_parser::{el, Arg, ArgsDesc, Expr, LocExpr, ParserSettings};
 use location::{offset_to_location, CodeLocation};
 use std::env::current_dir;
-use std::{collections::HashMap, path::PathBuf, str::FromStr, rc::Rc};
+use std::{collections::HashMap, path::PathBuf, rc::Rc, str::FromStr};
 
 enum Format {
 	None,
@@ -173,40 +173,20 @@
 				}
 				v => v,
 			};
-			let v = match opts.format {
-				Format::Json => {
-					if opts.no_stdlib {
-						evaluator.with_stdlib();
-					}
-					evaluator.add_global("__tmp__to_json__".into(), v);
-					let v = evaluator.parse_evaluate_raw(&format!(
-						"std.manifestJsonEx(__tmp__to_json__, \"{}\")",
-						" ".repeat(opts.line_padding),
-					));
-					match v {
-						Ok(v) => v,
-						Err(err) => {
-							print_error(&err, evaluator, &opts);
-							std::process::exit(1);
-						}
-					}
-				}
+			let v = evaluator.run_in_state(|| match opts.format {
+				Format::Json => Ok(Val::Str(v.into_json(opts.line_padding)?)),
 				Format::Yaml => {
-					if opts.no_stdlib {
-						evaluator.with_stdlib();
-					}
 					evaluator.add_global("__tmp__to_yaml__".into(), v);
-					let v = evaluator
-						.parse_evaluate_raw("std.manifestYamlDoc(__tmp__to_yaml__, \"  \")");
-					match v {
-						Ok(v) => v,
-						Err(err) => {
-							print_error(&err, evaluator, &opts);
-							std::process::exit(1);
-						}
-					}
+					evaluator.parse_evaluate_raw("std.manifestYamlDoc(__tmp__to_yaml__, \"  \")")
+				}
+				_ => Ok(v),
+			});
+			let v = match v {
+				Ok(v) => v,
+				Err(err) => {
+					print_error(&err, evaluator, &opts);
+					std::process::exit(1);
 				}
-				_ => v,
 			};
 			match v {
 				Val::Str(s) => println!("{}", s),
modifiedcrates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/evaluate.rs
+++ b/crates/jsonnet-evaluator/src/evaluate.rs
@@ -668,6 +668,7 @@
 						) {
 							Val::Arr(Rc::new(
 								arr.iter()
+									.cloned()
 									.filter(|e| {
 										predicate
 											.evaluate_values(context.clone(), &[e.clone()])
@@ -675,7 +676,6 @@
 											.try_cast_bool("filter predicate")
 											.unwrap()
 									})
-									.cloned()
 									.collect(),
 							))
 						} else {
modifiedcrates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/lib.rs
+++ b/crates/jsonnet-evaluator/src/lib.rs
@@ -296,7 +296,7 @@
 		Err(LocError(err, self.stack_trace()))
 	}
 
-	fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {
+	pub fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {
 		EVAL_STATE.with(|v| {
 			let has_state = v.borrow().is_some();
 			if !has_state {
@@ -364,14 +364,11 @@
 		($str: expr) => {{
 			let evaluator = EvaluationState::default();
 			evaluator.with_stdlib();
-			let val = evaluator.parse_evaluate_raw($str).unwrap();
-			evaluator.add_global("__tmp__to_yaml__".into(), val);
 			evaluator
-				.parse_evaluate_raw("std.manifestJsonEx(__tmp__to_yaml__, \"\")")
+				.parse_evaluate_raw($str)
 				.unwrap()
-				.try_cast_str("there should be json string")
+				.into_json(0)
 				.unwrap()
-				.clone()
 				.replace("\n", "")
 			}};
 	}
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