git.delta.rocks / jrsonnet / refs/commits / 2d3e9127fca2

difftreelog

Merge remote-tracking branch 'origin/master' into gcmodule

Yaroslav Bolyukin2022-01-04parents: #fa16ccf #e1fb5e1.patch.diff
in: master

7 files changed

modifiedcrates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/builtin/manifest.rs
1use crate::error::Error::*;2use crate::error::Result;3use crate::push_description_frame;4use crate::{throw, Val};56#[derive(PartialEq, Clone, Copy)]7pub enum ManifestType {8	// Applied in manifestification9	Manifest,10	/// Used for std.manifestJson11	/// Empty array/objects extends to "[\n\n]" instead of "[ ]" as in manifest12	Std,13	/// No line breaks, used in `obj+''`14	ToString,15	/// Minified json16	Minify,17}1819pub struct ManifestJsonOptions<'s> {20	pub padding: &'s str,21	pub mtype: ManifestType,22}2324pub fn manifest_json_ex(val: &Val, options: &ManifestJsonOptions<'_>) -> Result<String> {25	let mut out = String::new();26	manifest_json_ex_buf(val, &mut out, &mut String::new(), options)?;27	Ok(out)28}29fn manifest_json_ex_buf(30	val: &Val,31	buf: &mut String,32	cur_padding: &mut String,33	options: &ManifestJsonOptions<'_>,34) -> Result<()> {35	use std::fmt::Write;36	let mtype = options.mtype;37	match val {38		Val::Bool(v) => {39			if *v {40				buf.push_str("true");41			} else {42				buf.push_str("false");43			}44		}45		Val::Null => buf.push_str("null"),46		Val::Str(s) => escape_string_json_buf(s, buf),47		Val::Num(n) => write!(buf, "{}", n).unwrap(),48		Val::Arr(items) => {49			buf.push('[');50			if !items.is_empty() {51				if mtype != ManifestType::ToString && mtype != ManifestType::Minify {52					buf.push('\n');53				}5455				let old_len = cur_padding.len();56				cur_padding.push_str(options.padding);57				for (i, item) in items.iter().enumerate() {58					if i != 0 {59						buf.push(',');60						if mtype == ManifestType::ToString {61							buf.push(' ');62						} else if mtype != ManifestType::Minify {63							buf.push('\n');64						}65					}66					buf.push_str(cur_padding);67					manifest_json_ex_buf(&item?, buf, cur_padding, options)?;68				}69				cur_padding.truncate(old_len);7071				if mtype != ManifestType::ToString && mtype != ManifestType::Minify {72					buf.push('\n');73					buf.push_str(cur_padding);74				}75			} else if mtype == ManifestType::Std {76				buf.push_str("\n\n");77				buf.push_str(cur_padding);78			} else if mtype == ManifestType::ToString || mtype == ManifestType::Manifest {79				buf.push(' ');80			}81			buf.push(']');82		}83		Val::Obj(obj) => {84			obj.run_assertions()?;85			buf.push('{');86			let fields = obj.fields();87			if !fields.is_empty() {88				if mtype != ManifestType::ToString && mtype != ManifestType::Minify {89					buf.push('\n');90				}9192				let old_len = cur_padding.len();93				cur_padding.push_str(options.padding);94				for (i, field) in fields.into_iter().enumerate() {95					if i != 0 {96						buf.push(',');97						if mtype == ManifestType::ToString {98							buf.push(' ');99						} else if mtype != ManifestType::Minify {100							buf.push('\n');101						}102					}103					buf.push_str(cur_padding);104					escape_string_json_buf(&field, buf);105					buf.push_str(": ");106					push_description_frame(107						|| format!("field <{}> manifestification", field.clone()),108						|| {109							let value = obj.get(field.clone())?.unwrap();110							manifest_json_ex_buf(&value, buf, cur_padding, options)?;111							Ok(Val::Null)112						},113					)?;114				}115				cur_padding.truncate(old_len);116117				if mtype != ManifestType::ToString && mtype != ManifestType::Minify {118					buf.push('\n');119					buf.push_str(cur_padding);120				}121			} else if mtype == ManifestType::Std {122				buf.push_str("\n\n");123				buf.push_str(cur_padding);124			} else if mtype == ManifestType::ToString || mtype == ManifestType::Manifest {125				buf.push(' ');126			}127			buf.push('}');128		}129		Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),130	};131	Ok(())132}133134pub fn escape_string_json(s: &str) -> String {135	let mut buf = String::new();136	escape_string_json_buf(s, &mut buf);137	buf138}139140fn escape_string_json_buf(s: &str, buf: &mut String) {141	use std::fmt::Write;142	buf.push('"');143	for c in s.chars() {144		match c {145			'"' => buf.push_str("\\\""),146			'\\' => buf.push_str("\\\\"),147			'\u{0008}' => buf.push_str("\\b"),148			'\u{000c}' => buf.push_str("\\f"),149			'\n' => buf.push_str("\\n"),150			'\r' => buf.push_str("\\r"),151			'\t' => buf.push_str("\\t"),152			c if c < 32 as char || (c >= 127 as char && c <= 159 as char) => {153				write!(buf, "\\u{:04x}", c as u32).unwrap()154			}155			c => buf.push(c),156		}157	}158	buf.push('"');159}160161pub struct ManifestYamlOptions<'s> {162	/// Padding before fields, i.e163	/// ```yaml164	/// a:165	///   b:166	/// ## <- this167	/// ```168	pub padding: &'s str,169	/// Padding before array elements in objects170	/// ```yaml171	/// a:172	///   - 1173	/// ## <- this174	/// ```175	pub arr_element_padding: &'s str,176	/// Should yaml keys appear unescaped, when possible177	/// ```yaml178	/// "safe_key": 1179	/// # vs180	/// safe_key: 1181	/// ```182	pub quote_keys: bool,183}184185/// From https://github.com/chyh1990/yaml-rust/blob/da52a68615f2ecdd6b7e4567019f280c433c1521/src/emitter.rs#L289186/// With added date check187fn yaml_needs_quotes(string: &str) -> bool {188	fn need_quotes_spaces(string: &str) -> bool {189		string.starts_with(' ') || string.ends_with(' ')190	}191192	string.is_empty()193		|| need_quotes_spaces(string)194		|| string.starts_with(|c| matches!(c, '&' | '*' | '?' | '|' | '-' | '<' | '>' | '=' | '!' | '%' | '@'))195		|| string.contains(|c| matches!(c, ':' | '{' | '}' | '[' | ']' | ',' | '#' | '`' | '\"' | '\'' | '\\' | '\0'..='\x06' | '\t' | '\n' | '\r' | '\x0e'..='\x1a' | '\x1c'..='\x1f'))196		|| [197			// http://yaml.org/type/bool.html198			// Note: 'y', 'Y', 'n', 'N', is not quoted deliberately, as in libyaml. PyYAML also parse199			// them as string, not booleans, although it is violating the YAML 1.1 specification.200			// See https://github.com/dtolnay/serde-yaml/pull/83#discussion_r152628088.201			"yes", "Yes", "YES", "no", "No", "NO", "True", "TRUE", "true", "False", "FALSE", "false",202			"on", "On", "ON", "off", "Off", "OFF", // http://yaml.org/type/null.html203			"null", "Null", "NULL", "~",204		].contains(&string)205		|| (string.chars().all(|c| matches!(c, '0'..='9' | '-'))206			&& string.chars().filter(|c| *c == '-').count() == 2)207		|| string.starts_with('.')208		|| string.starts_with("0x")209		|| string.parse::<i64>().is_ok()210		|| string.parse::<f64>().is_ok()211}212213pub fn manifest_yaml_ex(val: &Val, options: &ManifestYamlOptions<'_>) -> Result<String> {214	let mut out = String::new();215	manifest_yaml_ex_buf(val, &mut out, &mut String::new(), options)?;216	Ok(out)217}218fn manifest_yaml_ex_buf(219	val: &Val,220	buf: &mut String,221	cur_padding: &mut String,222	options: &ManifestYamlOptions<'_>,223) -> Result<()> {224	use std::fmt::Write;225	match val {226		Val::Bool(v) => {227			if *v {228				buf.push_str("true")229			} else {230				buf.push_str("false")231			}232		}233		Val::Null => buf.push_str("null"),234		Val::Str(s) => {235			if s.is_empty() {236				buf.push_str("\"\"");237			} else if let Some(s) = s.strip_suffix('\n') {238				buf.push('|');239				for line in s.split('\n') {240					buf.push('\n');241					buf.push_str(options.padding);242					buf.push_str(line);243				}244			} else if !options.quote_keys && !yaml_needs_quotes(s) {245				buf.push_str(s);246			} else {247				escape_string_json_buf(s, buf);248			}249		}250		Val::Num(n) => write!(buf, "{}", *n).unwrap(),251		Val::Arr(a) => {252			if a.is_empty() {253				buf.push_str("[]");254			} else {255				for (i, item) in a.iter().enumerate() {256					if i != 0 {257						buf.push('\n');258						buf.push_str(cur_padding);259					}260					let item = item?;261					buf.push('-');262					match &item {263						Val::Arr(a) if !a.is_empty() => {264							buf.push('\n');265							buf.push_str(cur_padding);266							buf.push_str(options.padding);267						}268						_ => buf.push(' '),269					}270					let extra_padding = match &item {271						Val::Arr(a) => !a.is_empty(),272						Val::Obj(o) => !o.is_empty(),273						_ => false,274					};275					let prev_len = cur_padding.len();276					if extra_padding {277						cur_padding.push_str(options.padding);278					}279					manifest_yaml_ex_buf(&item, buf, cur_padding, options)?;280					cur_padding.truncate(prev_len);281				}282			}283		}284		Val::Obj(o) => {285			if o.is_empty() {286				buf.push_str("{}");287			} else {288				for (i, key) in o.fields().iter().enumerate() {289					if i != 0 {290						buf.push('\n');291						buf.push_str(cur_padding);292					}293					if !options.quote_keys && !yaml_needs_quotes(key) {294						buf.push_str(key);295					} else {296						escape_string_json_buf(key, buf);297					}298					buf.push(':');299					let prev_len = cur_padding.len();300					let item = o.get(key.clone())?.expect("field exists");301					match &item {302						Val::Arr(a) if !a.is_empty() => {303							buf.push('\n');304							buf.push_str(cur_padding);305							buf.push_str(options.arr_element_padding);306							cur_padding.push_str(options.arr_element_padding);307						}308						Val::Obj(o) if !o.is_empty() => {309							buf.push('\n');310							buf.push_str(cur_padding);311							buf.push_str(options.padding);312							cur_padding.push_str(options.padding);313						}314						_ => buf.push(' '),315					}316					manifest_yaml_ex_buf(&item, buf, cur_padding, options)?;317					cur_padding.truncate(prev_len);318				}319			}320		}321		Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),322	}323	Ok(())324}
modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -1,6 +1,5 @@
 use crate::function::StaticBuiltin;
-use crate::typed::{Any, Null, PositiveF64, VecVal, M1};
-use crate::{self as jrsonnet_evaluator, Either, ObjValue};
+use crate::typed::{Any, PositiveF64, VecVal, M1};
 use crate::{
 	builtin::manifest::{manifest_yaml_ex, ManifestYamlOptions},
 	equals,
@@ -10,6 +9,7 @@
 	typed::{Either2, Either4},
 	with_state, ArrValue, Context, FuncVal, IndexableVal, Val,
 };
+use crate::{Either, ObjValue};
 use format::{format_arr, format_obj};
 use gcmodule::Cc;
 use jrsonnet_interner::IStr;
@@ -145,7 +145,7 @@
 fn builtin_length(x: Either![IStr, VecVal, ObjValue, Cc<FuncVal>]) -> Result<usize> {
 	use Either4::*;
 	Ok(match x {
-		A(x) => x.len(),
+		A(x) => x.chars().count(),
 		B(x) => x.0.len(),
 		C(x) => x
 			.fields_visibility()
@@ -566,12 +566,21 @@
 }
 
 #[jrsonnet_macros::builtin]
-fn builtin_manifest_json_ex(value: Any, indent: IStr) -> Result<String> {
+fn builtin_manifest_json_ex(
+	value: Any,
+	indent: IStr,
+	newline: Option<IStr>,
+	key_val_sep: Option<IStr>,
+) -> Result<String> {
+	let newline = newline.as_deref().unwrap_or("\n");
+	let key_val_sep = key_val_sep.as_deref().unwrap_or(": ");
 	manifest_json_ex(
 		&value.0,
 		&ManifestJsonOptions {
 			padding: &indent,
 			mtype: ManifestType::Std,
+			newline,
+			key_val_sep,
 		},
 	)
 }
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -5,6 +5,9 @@
 	clippy::ptr_arg
 )]
 
+// For jrsonnet-macros
+extern crate self as jrsonnet_evaluator;
+
 mod builtin;
 mod ctx;
 mod dynamic;
@@ -976,6 +979,14 @@
 	}
 
 	#[test]
+	fn json_minified() {
+		assert_json!(
+			r#"std.manifestJsonMinified({a:3, b:4, c:6})"#,
+			r#""{\"a\":3,\"b\":4,\"c\":6}""#
+		);
+	}
+
+	#[test]
 	fn parse_json() {
 		assert_json!(
 			r#"std.parseJson('{"a": -1,"b": 1,"c": 3.141,"d": []}')"#,
modifiedcrates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -19,7 +19,7 @@
 	($($ty:ty)*) => {$(
 		impl Typed for $ty {
 			const TYPE: &'static ComplexValType =
-				&ComplexValType::BoundedNumber(Some(<$ty>::MIN as f64), Some(<$ty>::MAX as f64));
+				&ComplexValType::BoundedNumber(Some(Self::MIN as f64), Some(Self::MAX as f64));
 		}
 		impl TryFrom<Val> for $ty {
 			type Error = LocError;
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -400,6 +400,8 @@
 				&ManifestJsonOptions {
 					padding: "",
 					mtype: ManifestType::ToString,
+					newline: "\n",
+					key_val_sep: ": ",
 				},
 			)?
 			.into(),
@@ -484,6 +486,8 @@
 				} else {
 					ManifestType::Manifest
 				},
+				newline: "\n",
+				key_val_sep: ": ",
 			},
 		)
 		.map(|s| s.into())
@@ -496,6 +500,8 @@
 			&ManifestJsonOptions {
 				padding: &" ".repeat(padding),
 				mtype: ManifestType::Std,
+				newline: "\n",
+				key_val_sep: ": ",
 			},
 		)
 		.map(|s| s.into())
modifiedcrates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -105,7 +105,7 @@
 				if let Some(opt_ty) = extract_type_from_option(&t.ty) {
 					quote! {{
 						if let Some(value) = parsed.get(#ident) {
-							Some(jrsonnet_evaluator::push_description_frame(
+							Some(::jrsonnet_evaluator::push_description_frame(
 								|| format!("argument <{}> evaluation", #ident),
 								|| <#opt_ty>::try_from(value.evaluate()?),
 							)?)
@@ -117,7 +117,7 @@
 					quote! {{
 						let value = parsed.get(#ident).unwrap();
 
-						jrsonnet_evaluator::push_description_frame(
+						::jrsonnet_evaluator::push_description_frame(
 							|| format!("argument <{}> evaluation", #ident),
 							|| <#ty>::try_from(value.evaluate()?),
 						)?
@@ -136,7 +136,7 @@
 		#[derive(Clone, Copy, gcmodule::Trace)]
 		#vis struct #name {}
 		const _: () = {
-			use jrsonnet_evaluator::function::{Builtin, StaticBuiltin, BuiltinParam, ArgsLike};
+			use ::jrsonnet_evaluator::function::{Builtin, StaticBuiltin, BuiltinParam, ArgsLike};
 			const PARAMS: &'static [BuiltinParam] = &[
 				#(#params),*
 			];
@@ -156,7 +156,7 @@
 					PARAMS
 				}
 				fn call(&self, context: Context, loc: Option<&ExprLocation>, args: &dyn ArgsLike) -> Result<Val> {
-					let parsed = jrsonnet_evaluator::function::parse_builtin_call(context, &PARAMS, args, false)?;
+					let parsed = ::jrsonnet_evaluator::function::parse_builtin_call(context, &PARAMS, args, false)?;
 
 					let result: #result = #name(#(#args),*);
 					let result = result?;
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -373,6 +373,8 @@
 
   manifestJson(value):: std.manifestJsonEx(value, '    ') tailstrict,
 
+  manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),
+
   manifestJsonEx:: $intrinsic(manifestJsonEx),
 
   manifestYamlDoc:: $intrinsic(manifestYamlDoc),
@@ -530,6 +532,9 @@
     else
       patch,
 
+  get(o, f, default = null, inc_hidden = true)::
+    if std.objectHasEx(o, f, inc_hidden) then o[f] else default,
+
   objectFields(o)::
     std.objectFieldsEx(o, false),