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

difftreelog

Merge pull request #64 from CertainLach/feat/manifest-yaml-doc-builtin

Yaroslav Bolyukin2021-10-29parents: #27b30fb #c0cb444.patch.diff
in: master
Make manifestYamlDoc builtin

6 files changed

modifiedcrates/jrsonnet-cli/src/manifest.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/manifest.rs
+++ b/crates/jrsonnet-cli/src/manifest.rs
@@ -38,9 +38,9 @@
 	#[clap(long, short = 'y')]
 	yaml_stream: bool,
 	/// Number of spaces to pad output manifest with.
-	/// `0` for hard tabs, `-1` for single line output
-	#[clap(long, default_value = "3")]
-	line_padding: usize,
+	/// `0` for hard tabs, `-1` for single line output [default: 3 for json, 2 for yaml]
+	#[clap(long)]
+	line_padding: Option<usize>,
 }
 impl ConfigureState for ManifestOpts {
 	fn configure(&self, state: &EvaluationState) -> Result<()> {
@@ -50,10 +50,10 @@
 			match self.format {
 				ManifestFormatName::String => state.set_manifest_format(ManifestFormat::String),
 				ManifestFormatName::Json => {
-					state.set_manifest_format(ManifestFormat::Json(self.line_padding))
+					state.set_manifest_format(ManifestFormat::Json(self.line_padding.unwrap_or(3)))
 				}
 				ManifestFormatName::Yaml => {
-					state.set_manifest_format(ManifestFormat::Yaml(self.line_padding))
+					state.set_manifest_format(ManifestFormat::Yaml(self.line_padding.unwrap_or(2)))
 				}
 			}
 		}
modifiedcrates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/manifest.rs
@@ -156,3 +156,127 @@
 	}
 	buf.push('"');
 }
+
+pub struct ManifestYamlOptions<'s> {
+	/// Padding before fields, i.e
+	/// ```yaml
+	/// a:
+	///   b:
+	/// ## <- this
+	/// ```
+	pub padding: &'s str,
+	/// Padding before array elements in objects
+	/// ```yaml
+	/// a:
+	///   - 1
+	/// ## <- this
+	/// ```
+	pub arr_element_padding: &'s str,
+}
+
+pub fn manifest_yaml_ex(val: &Val, options: &ManifestYamlOptions<'_>) -> Result<String> {
+	let mut out = String::new();
+	manifest_yaml_ex_buf(val, &mut out, &mut String::new(), options)?;
+	Ok(out)
+}
+fn manifest_yaml_ex_buf(
+	val: &Val,
+	buf: &mut String,
+	cur_padding: &mut String,
+	options: &ManifestYamlOptions<'_>,
+) -> Result<()> {
+	use std::fmt::Write;
+	match val {
+		Val::Bool(v) => {
+			if *v {
+				buf.push_str("true")
+			} else {
+				buf.push_str("false")
+			}
+		}
+		Val::Null => buf.push_str("null"),
+		Val::Str(s) => {
+			if s.is_empty() {
+				buf.push_str("\"\"");
+			} else if let Some(s) = s.strip_suffix('\n') {
+				buf.push('|');
+				for line in s.split('\n') {
+					buf.push('\n');
+					buf.push_str(options.padding);
+					buf.push_str(line);
+				}
+			} else {
+				escape_string_json_buf(s, buf)
+			}
+		}
+		Val::Num(n) => write!(buf, "{}", *n).unwrap(),
+		Val::Arr(a) => {
+			if a.is_empty() {
+				buf.push_str("[]");
+			} else {
+				for (i, item) in a.iter().enumerate() {
+					if i != 0 {
+						buf.push('\n');
+						buf.push_str(cur_padding);
+					}
+					let item = item?;
+					buf.push('-');
+					match &item {
+						Val::Arr(a) if !a.is_empty() => {
+							buf.push('\n');
+							buf.push_str(cur_padding);
+							buf.push_str(options.padding);
+						}
+						_ => buf.push(' '),
+					}
+					let extra_padding = match &item {
+						Val::Arr(a) => !a.is_empty(),
+						Val::Obj(o) => !o.is_empty(),
+						_ => false,
+					};
+					let prev_len = cur_padding.len();
+					if extra_padding {
+						cur_padding.push_str(options.padding);
+					}
+					manifest_yaml_ex_buf(&item, buf, cur_padding, options)?;
+					cur_padding.truncate(prev_len);
+				}
+			}
+		}
+		Val::Obj(o) => {
+			if o.is_empty() {
+				buf.push_str("{}");
+			} else {
+				for (i, key) in o.fields().iter().enumerate() {
+					if i != 0 {
+						buf.push('\n');
+						buf.push_str(cur_padding);
+					}
+					escape_string_json_buf(key, buf);
+					buf.push(':');
+					let prev_len = cur_padding.len();
+					let item = o.get(key.clone())?.expect("field exists");
+					match &item {
+						Val::Arr(a) if !a.is_empty() => {
+							buf.push('\n');
+							buf.push_str(cur_padding);
+							buf.push_str(options.arr_element_padding);
+							cur_padding.push_str(options.arr_element_padding);
+						}
+						Val::Obj(o) if !o.is_empty() => {
+							buf.push('\n');
+							buf.push_str(cur_padding);
+							buf.push_str(options.padding);
+							cur_padding.push_str(options.padding);
+						}
+						_ => buf.push(' '),
+					}
+					manifest_yaml_ex_buf(&item, buf, cur_padding, options)?;
+					cur_padding.truncate(prev_len);
+				}
+			}
+		}
+		Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),
+	}
+	Ok(())
+}
modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/builtin/mod.rs
1use crate::{2	equals,3	error::{Error::*, Result},4	operator::evaluate_mod_op,5	parse_args, primitive_equals, push, throw, with_state, ArrValue, Context, EvaluationState,6	FuncVal, IndexableVal, LazyVal, Val,7};8use format::{format_arr, format_obj};9use jrsonnet_gc::Gc;10use jrsonnet_interner::IStr;11use jrsonnet_parser::{ArgsDesc, ExprLocation};12use jrsonnet_types::ty;13use std::{collections::HashMap, path::PathBuf, rc::Rc};1415pub mod stdlib;16pub use stdlib::*;1718use self::manifest::{escape_string_json, manifest_json_ex, ManifestJsonOptions, ManifestType};1920pub mod format;21pub mod manifest;22pub mod sort;2324pub fn std_format(str: IStr, vals: Val) -> Result<Val> {25	push(26		Some(&ExprLocation(Rc::from(PathBuf::from("std.jsonnet")), 0, 0)),27		|| format!("std.format of {}", str),28		|| {29			Ok(match vals {30				Val::Arr(vals) => Val::Str(format_arr(&str, &vals.evaluated()?)?.into()),31				Val::Obj(obj) => Val::Str(format_obj(&str, &obj)?.into()),32				o => Val::Str(format_arr(&str, &[o])?.into()),33			})34		},35	)36}3738pub fn std_slice(39	indexable: IndexableVal,40	index: Option<usize>,41	end: Option<usize>,42	step: Option<usize>,43) -> Result<Val> {44	let index = index.unwrap_or(0);45	let end = end.unwrap_or_else(|| match &indexable {46		IndexableVal::Str(_) => usize::MAX,47		IndexableVal::Arr(v) => v.len(),48	});49	let step = step.unwrap_or(1);50	match &indexable {51		IndexableVal::Str(s) => Ok(Val::Str(52			(s.chars()53				.skip(index)54				.take(end - index)55				.step_by(step)56				.collect::<String>())57			.into(),58		)),59		IndexableVal::Arr(arr) => Ok(Val::Arr(60			(arr.iter()61				.skip(index)62				.take(end - index)63				.step_by(step)64				.collect::<Result<Vec<Val>>>()?)65			.into(),66		)),67	}68}6970type Builtin = fn(context: Context, loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val>;7172type BuiltinsType = HashMap<Box<str>, Builtin>;7374thread_local! {75	static BUILTINS: BuiltinsType = {76		[77			("length".into(), builtin_length as Builtin),78			("type".into(), builtin_type),79			("makeArray".into(), builtin_make_array),80			("codepoint".into(), builtin_codepoint),81			("objectFieldsEx".into(), builtin_object_fields_ex),82			("objectHasEx".into(), builtin_object_has_ex),83			("slice".into(), builtin_slice),84			("substr".into(), builtin_substr),85			("primitiveEquals".into(), builtin_primitive_equals),86			("equals".into(), builtin_equals),87			("modulo".into(), builtin_modulo),88			("mod".into(), builtin_mod),89			("floor".into(), builtin_floor),90			("ceil".into(), builtin_ceil),91			("log".into(), builtin_log),92			("pow".into(), builtin_pow),93			("sqrt".into(), builtin_sqrt),94			("sin".into(), builtin_sin),95			("cos".into(), builtin_cos),96			("tan".into(), builtin_tan),97			("asin".into(), builtin_asin),98			("acos".into(), builtin_acos),99			("atan".into(), builtin_atan),100			("exp".into(), builtin_exp),101			("mantissa".into(), builtin_mantissa),102			("exponent".into(), builtin_exponent),103			("extVar".into(), builtin_ext_var),104			("native".into(), builtin_native),105			("filter".into(), builtin_filter),106			("map".into(), builtin_map),107			("flatMap".into(), builtin_flatmap),108			("foldl".into(), builtin_foldl),109			("foldr".into(), builtin_foldr),110			("sortImpl".into(), builtin_sort_impl),111			("format".into(), builtin_format),112			("range".into(), builtin_range),113			("char".into(), builtin_char),114			("encodeUTF8".into(), builtin_encode_utf8),115			("decodeUTF8".into(), builtin_decode_utf8),116			("md5".into(), builtin_md5),117			("base64".into(), builtin_base64),118			("base64DecodeBytes".into(), builtin_base64_decode_bytes),119			("base64Decode".into(), builtin_base64_decode),120			("trace".into(), builtin_trace),121			("join".into(), builtin_join),122			("escapeStringJson".into(), builtin_escape_string_json),123			("manifestJsonEx".into(), builtin_manifest_json_ex),124			("reverse".into(), builtin_reverse),125			("id".into(), builtin_id),126			("strReplace".into(), builtin_str_replace),127			("splitLimit".into(), builtin_splitlimit),128			("parseJson".into(), builtin_parse_json),129			("asciiUpper".into(), builtin_ascii_upper),130			("asciiLower".into(), builtin_ascii_lower),131			("member".into(), builtin_member),132			("count".into(), builtin_count),133		].iter().cloned().collect()134	};135}136137fn builtin_length(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {138	parse_args!(context, "length", args, 1, [139		0, x: ty!((string | object | array));140	], {141		Ok(match x {142			Val::Str(n) => Val::Num(n.chars().count() as f64),143			Val::Arr(a) => Val::Num(a.len() as f64),144			Val::Obj(o) => Val::Num(145				o.fields_visibility()146					.into_iter()147					.filter(|(_k, v)| *v)148					.count() as f64,149			),150			_ => unreachable!(),151		})152	})153}154155fn builtin_type(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {156	parse_args!(context, "type", args, 1, [157		0, x: ty!(any);158	], {159		Ok(Val::Str(x.value_type().name().into()))160	})161}162163fn builtin_make_array(164	context: Context,165	_loc: Option<&ExprLocation>,166	args: &ArgsDesc,167) -> Result<Val> {168	parse_args!(context, "makeArray", args, 2, [169		0, sz: ty!(BoundedNumber<(Some(0.0)), (None)>) => Val::Num;170		1, func: ty!(function) => Val::Func;171	], {172		let mut out = Vec::with_capacity(sz as usize);173		for i in 0..sz as usize {174			out.push(LazyVal::new_resolved(func.evaluate_values(175				context.clone(),176				&[Val::Num(i as f64)]177			)?))178		}179		Ok(Val::Arr(out.into()))180	})181}182183fn builtin_codepoint(184	context: Context,185	_loc: Option<&ExprLocation>,186	args: &ArgsDesc,187) -> Result<Val> {188	parse_args!(context, "codepoint", args, 1, [189		0, str: ty!(char) => Val::Str;190	], {191		Ok(Val::Num(str.chars().next().unwrap() as u32 as f64))192	})193}194195fn builtin_object_fields_ex(196	context: Context,197	_loc: Option<&ExprLocation>,198	args: &ArgsDesc,199) -> Result<Val> {200	parse_args!(context, "objectFieldsEx", args, 2, [201		0, obj: ty!(object) => Val::Obj;202		1, inc_hidden: ty!(boolean) => Val::Bool;203	], {204		let out = obj.fields_ex(inc_hidden);205		Ok(Val::Arr(out.into_iter().map(Val::Str).collect::<Vec<_>>().into()))206	})207}208209fn builtin_object_has_ex(210	context: Context,211	_loc: Option<&ExprLocation>,212	args: &ArgsDesc,213) -> Result<Val> {214	parse_args!(context, "objectHasEx", args, 3, [215		0, obj: ty!(object) => Val::Obj;216		1, f: ty!(string) => Val::Str;217		2, inc_hidden: ty!(boolean) => Val::Bool;218	], {219		Ok(Val::Bool(obj.has_field_ex(f, inc_hidden)))220	})221}222223fn builtin_parse_json(224	context: Context,225	_loc: Option<&ExprLocation>,226	args: &ArgsDesc,227) -> Result<Val> {228	parse_args!(context, "parseJson", args, 1, [229		0, s: ty!(string) => Val::Str;230	], {231		let state = EvaluationState::default();232		let path = PathBuf::from("std.parseJson").into();233		state.evaluate_snippet_raw(path ,s)234	})235}236237fn builtin_slice(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {238	parse_args!(context, "slice", args, 4, [239		0, indexable: ty!((string | array));240		1, index: ty!((number | null));241		2, end: ty!((number | null));242		3, step: ty!((number | null));243	], {244		std_slice(245			indexable.into_indexable()?,246			index.try_cast_nullable_num("index")?.map(|v| v as usize),247			end.try_cast_nullable_num("end")?.map(|v| v as usize),248			step.try_cast_nullable_num("step")?.map(|v| v as usize),249		)250	})251}252253fn builtin_substr(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {254	parse_args!(context, "substr", args, 3, [255		0, str: ty!(string) => Val::Str;256		1, from: ty!(BoundedNumber<(Some(0.0)), (None)>) => Val::Num;257		2, len: ty!(BoundedNumber<(Some(0.0)), (None)>) => Val::Num;258	], {259		let out: String = str.chars().skip(from as usize).take(len as usize).collect();260		Ok(Val::Str(out.into()))261	})262}263264fn builtin_primitive_equals(265	context: Context,266	_loc: Option<&ExprLocation>,267	args: &ArgsDesc,268) -> Result<Val> {269	parse_args!(context, "primitiveEquals", args, 2, [270		0, a: ty!(any);271		1, b: ty!(any);272	], {273		Ok(Val::Bool(primitive_equals(&a, &b)?))274	})275}276277fn builtin_equals(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {278	parse_args!(context, "equals", args, 2, [279		0, a: ty!(any);280		1, b: ty!(any);281	], {282		Ok(Val::Bool(equals(&a, &b)?))283	})284}285286fn builtin_modulo(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {287	parse_args!(context, "modulo", args, 2, [288		0, a: ty!(number) => Val::Num;289		1, b: ty!(number) => Val::Num;290	], {291		Ok(Val::Num(a % b))292	})293}294295fn builtin_mod(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {296	parse_args!(context, "mod", args, 2, [297		0, a: ty!((number | string));298		1, b: ty!(any);299	], {300		evaluate_mod_op(&a, &b)301	})302}303304fn builtin_floor(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {305	parse_args!(context, "floor", args, 1, [306		0, x: ty!(number) => Val::Num;307	], {308		Ok(Val::Num(x.floor()))309	})310}311312fn builtin_ceil(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {313	parse_args!(context, "ceil", args, 1, [314		0, x: ty!(number) => Val::Num;315	], {316		Ok(Val::Num(x.ceil()))317	})318}319320fn builtin_log(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {321	parse_args!(context, "log", args, 1, [322		0, n: ty!(number) => Val::Num;323	], {324		Ok(Val::Num(n.ln()))325	})326}327328fn builtin_pow(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {329	parse_args!(context, "pow", args, 2, [330		0, x: ty!(number) => Val::Num;331		1, n: ty!(number) => Val::Num;332	], {333		Ok(Val::Num(x.powf(n)))334	})335}336337fn builtin_sqrt(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {338	parse_args!(context, "sqrt", args, 1, [339		0, x: ty!(BoundedNumber<(Some(0.0)), (None)>) => Val::Num;340	], {341		Ok(Val::Num(x.sqrt()))342	})343}344345fn builtin_sin(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {346	parse_args!(context, "sin", args, 1, [347		0, x: ty!(number) => Val::Num;348	], {349		Ok(Val::Num(x.sin()))350	})351}352353fn builtin_cos(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {354	parse_args!(context, "cos", args, 1, [355		0, x: ty!(number) => Val::Num;356	], {357		Ok(Val::Num(x.cos()))358	})359}360361fn builtin_tan(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {362	parse_args!(context, "tan", args, 1, [363		0, x: ty!(number) => Val::Num;364	], {365		Ok(Val::Num(x.tan()))366	})367}368369fn builtin_asin(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {370	parse_args!(context, "asin", args, 1, [371		0, x: ty!(number) => Val::Num;372	], {373		Ok(Val::Num(x.asin()))374	})375}376377fn builtin_acos(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {378	parse_args!(context, "acos", args, 1, [379		0, x: ty!(number) => Val::Num;380	], {381		Ok(Val::Num(x.acos()))382	})383}384385fn builtin_atan(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {386	parse_args!(context, "atan", args, 1, [387		0, x: ty!(number) => Val::Num;388	], {389		Ok(Val::Num(x.atan()))390	})391}392393fn builtin_exp(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {394	parse_args!(context, "exp", args, 1, [395		0, x: ty!(number) => Val::Num;396	], {397		Ok(Val::Num(x.exp()))398	})399}400401fn frexp(s: f64) -> (f64, i16) {402	if 0.0 == s {403		(s, 0)404	} else {405		let lg = s.abs().log2();406		let x = (lg - lg.floor() - 1.0).exp2();407		let exp = lg.floor() + 1.0;408		(s.signum() * x, exp as i16)409	}410}411412fn builtin_mantissa(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {413	parse_args!(context, "mantissa", args, 1, [414		0, x: ty!(number) => Val::Num;415	], {416		Ok(Val::Num(frexp(x).0))417	})418}419420fn builtin_exponent(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {421	parse_args!(context, "exponent", args, 1, [422		0, x: ty!(number) => Val::Num;423	], {424		Ok(Val::Num(frexp(x).1.into()))425	})426}427428fn builtin_ext_var(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {429	parse_args!(context, "extVar", args, 1, [430		0, x: ty!(string) => Val::Str;431	], {432		Ok(with_state(|s| s.settings().ext_vars.get(&x).cloned()).ok_or(UndefinedExternalVariable(x))?)433	})434}435436fn builtin_native(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {437	parse_args!(context, "native", args, 1, [438		0, x: ty!(string) => Val::Str;439	], {440		Ok(with_state(|s| s.settings().ext_natives.get(&x).cloned()).map(|v| Val::Func(Gc::new(FuncVal::NativeExt(x.clone(), v)))).ok_or(UndefinedExternalFunction(x))?)441	})442}443444fn builtin_filter(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {445	parse_args!(context, "filter", args, 2, [446		0, func: ty!(function) => Val::Func;447		1, arr: ty!(array) => Val::Arr;448	], {449		Ok(Val::Arr(arr.filter(|val| func450			.evaluate_values(context.clone(), &[val.clone()])?451			.try_cast_bool("filter predicate"))?))452	})453}454455fn builtin_map(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {456	parse_args!(context, "map", args, 2, [457		0, func: ty!(function) => Val::Func;458		1, arr: ty!(array) => Val::Arr;459	], {460		Ok(Val::Arr(arr.map(|val| func461			.evaluate_values(context.clone(), &[val]))?))462	})463}464465fn builtin_flatmap(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {466	parse_args!(context, "flatMap", args, 2, [467		0, func: ty!(function) => Val::Func;468		1, arr: ty!((array | string));469	], {470		match arr {471			Val::Str(s) => {472				let mut out = String::new();473				for c in s.chars() {474					match func.evaluate_values(context.clone(), &[Val::Str(c.to_string().into())])? {475						Val::Str(o) => out.push_str(&o),476						_ => throw!(RuntimeError("in std.join all items should be strings".into())),477					};478				}479				Ok(Val::Str(out.into()))480			},481			Val::Arr(a) => {482				let mut out = Vec::new();483				for el in a.iter() {484					let el = el?;485					match func.evaluate_values(context.clone(), &[el])? {486						Val::Arr(o) => for oe in o.iter() {487							out.push(oe?)488						},489						_ => throw!(RuntimeError("in std.join all items should be arrays".into())),490					};491				}492				Ok(Val::Arr(out.into()))493			},494			_ => unreachable!(),495		}496	})497}498499fn builtin_foldl(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {500	parse_args!(context, "foldl", args, 3, [501		0, func: ty!(function) => Val::Func;502		1, arr: ty!(array) => Val::Arr;503		2, init: ty!(any);504	], {505		let mut acc = init;506		for i in arr.iter() {507			acc = func.evaluate_values(context.clone(), &[acc, i?])?;508		}509		Ok(acc)510	})511}512513fn builtin_foldr(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {514	parse_args!(context, "foldr", args, 3, [515		0, func: ty!(function) => Val::Func;516		1, arr: ty!(array) => Val::Arr;517		2, init: ty!(any);518	], {519		let mut acc = init;520		for i in arr.iter().rev() {521			acc = func.evaluate_values(context.clone(), &[i?, acc])?;522		}523		Ok(acc)524	})525}526527#[allow(non_snake_case)]528fn builtin_sort_impl(529	context: Context,530	_loc: Option<&ExprLocation>,531	args: &ArgsDesc,532) -> Result<Val> {533	parse_args!(context, "sort", args, 2, [534		0, arr: ty!(array) => Val::Arr;535		1, keyF: ty!(function) => Val::Func;536	], {537		if arr.len() <= 1 {538			return Ok(Val::Arr(arr))539		}540		Ok(Val::Arr(ArrValue::Eager(sort::sort(context, arr.evaluated()?, &keyF)?)))541	})542}543544fn builtin_format(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {545	parse_args!(context, "format", args, 2, [546		0, str: ty!(string) => Val::Str;547		1, vals: ty!(any)548	], {549		std_format(str, vals)550	})551}552553fn builtin_range(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {554	parse_args!(context, "range", args, 2, [555		0, from: ty!(number) => Val::Num;556		1, to: ty!(number) => Val::Num;557	], {558		if to < from {559			return Ok(Val::Arr(ArrValue::new_eager()))560		}561		let mut out = Vec::with_capacity((1+to as usize-from as usize).max(0));562		for i in from as usize..=to as usize {563			out.push(Val::Num(i as f64));564		}565		Ok(Val::Arr(out.into()))566	})567}568569fn builtin_char(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {570	parse_args!(context, "char", args, 1, [571		0, n: ty!(number) => Val::Num;572	], {573		let mut out = String::new();574		out.push(std::char::from_u32(n as u32).ok_or_else(||575			InvalidUnicodeCodepointGot(n as u32)576		)?);577		Ok(Val::Str(out.into()))578	})579}580581fn builtin_encode_utf8(582	context: Context,583	_loc: Option<&ExprLocation>,584	args: &ArgsDesc,585) -> Result<Val> {586	parse_args!(context, "encodeUTF8", args, 1, [587		0, str: ty!(string) => Val::Str;588	], {589		Ok(Val::Arr((str.bytes().map(|b| Val::Num(b as f64)).collect::<Vec<Val>>()).into()))590	})591}592593fn builtin_decode_utf8(594	context: Context,595	_loc: Option<&ExprLocation>,596	args: &ArgsDesc,597) -> Result<Val> {598	parse_args!(context, "decodeUTF8", args, 1, [599		0, arr: ty!((Array<ubyte>)) => Val::Arr;600	], {601		let data: Result<Vec<u8>> = arr.iter().map(|v| v.map(|v| match v{602			Val::Num(n) => n as u8,603			_ => unreachable!(),604		})).collect();605		let data = data?;606		Ok(Val::Str(String::from_utf8(data).map_err(|_| RuntimeError("bad utf8".into()))?.into()))607	})608}609610fn builtin_md5(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {611	parse_args!(context, "md5", args, 1, [612		0, str: ty!(string) => Val::Str;613	], {614		Ok(Val::Str(format!("{:x}", md5::compute(&str.as_bytes())).into()))615	})616}617618fn builtin_trace(context: Context, loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {619	parse_args!(context, "trace", args, 2, [620		0, str: ty!(string) => Val::Str;621		1, rest: ty!(any);622	], {623		eprint!("TRACE:");624		if let Some(loc) = loc {625			with_state(|s|{626				let locs = s.map_source_locations(&loc.0, &[loc.1]);627				eprint!(" {}:{}", loc.0.file_name().unwrap().to_str().unwrap(), locs[0].line);628			});629		}630		eprintln!(" {}", str);631		Ok(rest)632	})633}634635fn builtin_base64(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {636	parse_args!(context, "base64", args, 1, [637		0, input: ty!((string | (Array<number>)));638	], {639		Ok(Val::Str(match input {640			Val::Str(s) => {641				base64::encode(s.bytes().collect::<Vec<_>>()).into()642			},643			Val::Arr(a) => {644				base64::encode(a.iter().map(|v| {645					Ok(v?.unwrap_num()? as u8)646				}).collect::<Result<Vec<_>>>()?).into()647			},648			_ => unreachable!()649		}))650	})651}652653fn builtin_base64_decode_bytes(654	context: Context,655	_loc: Option<&ExprLocation>,656	args: &ArgsDesc,657) -> Result<Val> {658	parse_args!(context, "base64DecodeBytes", args, 1, [659		0, input: ty!(string) => Val::Str;660	], {661		Ok(Val::Arr(662			base64::decode(&input.as_bytes())663				.map_err(|_| RuntimeError("bad base64".into()))?664				.iter()665				.map(|v| Val::Num(*v as f64)).collect::<Vec<_>>().into()666		))667	})668}669670fn builtin_base64_decode(671	context: Context,672	_loc: Option<&ExprLocation>,673	args: &ArgsDesc,674) -> Result<Val> {675	parse_args!(context, "base64Decode", args, 1, [676		0, input: ty!(string) => Val::Str;677	], {678		Ok(Val::Str(679			String::from_utf8(base64::decode(&input.as_bytes())680				.map_err(|_| RuntimeError("bad base64".into()))?)681				.map_err(|_| RuntimeError("bad utf8".into()))?.into()682		))683	})684}685686fn builtin_join(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {687	parse_args!(context, "join", args, 2, [688		0, sep: ty!((string | array));689		1, arr: ty!(array) => Val::Arr;690	], {691		Ok(match sep {692			Val::Arr(joiner_items) => {693				let mut out = Vec::new();694695				let mut first = true;696				for item in arr.iter() {697					let item = item?.clone();698					if let Val::Arr(items) = item {699						if !first {700							out.reserve(joiner_items.len());701							// TODO: extend702							for item in joiner_items.iter() {703								out.push(item?);704							}705						}706						first = false;707						out.reserve(items.len());708						// TODO: extend709						for item in items.iter() {710							out.push(item?);711						}712					} else {713						throw!(RuntimeError("in std.join all items should be arrays".into()));714					}715				}716717				Val::Arr(out.into())718			},719			Val::Str(sep) => {720				let mut out = String::new();721722				let mut first = true;723				for item in arr.iter() {724					let item = item?.clone();725					if let Val::Str(item) = item {726						if !first {727							out += &sep;728						}729						first = false;730						out += &item;731					} else {732						throw!(RuntimeError("in std.join all items should be strings".into()));733					}734				}735736				Val::Str(out.into())737			},738			_ => unreachable!()739		})740	})741}742743fn builtin_escape_string_json(744	context: Context,745	_loc: Option<&ExprLocation>,746	args: &ArgsDesc,747) -> Result<Val> {748	parse_args!(context, "escapeStringJson", args, 1, [749		0, str_: ty!(string) => Val::Str;750	], {751		Ok(Val::Str(escape_string_json(&str_).into()))752	})753}754755fn builtin_manifest_json_ex(756	context: Context,757	_loc: Option<&ExprLocation>,758	args: &ArgsDesc,759) -> Result<Val> {760	parse_args!(context, "manifestJsonEx", args, 2, [761		0, value: ty!(any);762		1, indent: ty!(string) => Val::Str;763	], {764		Ok(Val::Str(manifest_json_ex(&value, &ManifestJsonOptions {765			padding: &indent,766			mtype: ManifestType::Std,767		})?.into()))768	})769}770771fn builtin_reverse(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {772	parse_args!(context, "reverse", args, 1, [773		0, value: ty!(array) => Val::Arr;774	], {775		Ok(Val::Arr(value.reversed()))776	})777}778779fn builtin_id(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {780	parse_args!(context, "id", args, 1, [781		0, v: ty!(any);782	], {783		Ok(v)784	})785}786787fn builtin_str_replace(788	context: Context,789	_loc: Option<&ExprLocation>,790	args: &ArgsDesc,791) -> Result<Val> {792	parse_args!(context, "strReplace", args, 3, [793		0, str: ty!(string) => Val::Str;794		1, from: ty!(string) => Val::Str;795		2, to: ty!(string) => Val::Str;796	], {797		let mut out = String::new();798		let mut last_idx = 0;799		while let Some(idx) = (&str[last_idx..]).find(&from as &str) {800			out.push_str(&str[last_idx..last_idx+idx]);801			out.push_str(&to);802			last_idx += idx + from.len();803		}804		if last_idx == 0 {805			return Ok(Val::Str(str))806		}807		out.push_str(&str[last_idx..]);808		Ok(Val::Str(out.into()))809	})810}811812fn builtin_splitlimit(813	context: Context,814	_loc: Option<&ExprLocation>,815	args: &ArgsDesc,816) -> Result<Val> {817	parse_args!(context, "splitLimit", args, 3, [818		0, str: ty!(string) => Val::Str;819		1, c: ty!(char) => Val::Str;820		2, maxsplits: ty!(number) => Val::Num;821	], {822		let maxsplits = maxsplits as isize;823		let c = c.chars().next().unwrap();824825		let out: Vec<Val> = if maxsplits == -1 {826			str.split(c).map(|s| Val::Str(s.into())).collect()827		} else {828			str.splitn(maxsplits as usize + 1, c).map(|s| Val::Str(s.into())).collect()829		};830831		Ok(Val::Arr(out.into()))832	})833}834835fn builtin_ascii_upper(836	context: Context,837	_loc: Option<&ExprLocation>,838	args: &ArgsDesc,839) -> Result<Val> {840	parse_args!(context, "asciiUpper", args, 1, [841		0, str: ty!(string) => Val::Str;842	], {843		Ok(Val::Str(str.to_ascii_uppercase().into()))844	})845}846847fn builtin_ascii_lower(848	context: Context,849	_loc: Option<&ExprLocation>,850	args: &ArgsDesc,851) -> Result<Val> {852	parse_args!(context, "asciiLower", args, 1, [853		0, str: ty!(string) => Val::Str;854	], {855		Ok(Val::Str(str.to_ascii_lowercase().into()))856	})857}858859fn builtin_member(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {860	parse_args!(context, "member", args, 2, [861		0, arr: ty!((array | string));862		1, x: ty!(any);863	], {864		match arr {865			Val::Str(s) => {866				let x = x.try_cast_str("x should be string")?;867				Ok(Val::Bool(!x.is_empty() && s.contains(&*x)))868			}869			Val::Arr(a) => {870				for item in a.iter() {871					let item = item?;872					if equals(&item, &x)? {873						return Ok(Val::Bool(true));874					}875				}876				Ok(Val::Bool(false))877			}878			_ => unreachable!(),879		}880	})881}882883fn builtin_count(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {884	parse_args!(context, "count", args, 2, [885		0, arr: ty!(array) => Val::Arr;886		1, x: ty!(any);887	], {888		let mut count = 0;889		for item in arr.iter() {890			let item = item?;891			if equals(&item, &x)? {892				count += 1;893			}894		}895		Ok(Val::Num(count as f64))896	})897}898899pub fn call_builtin(900	context: Context,901	loc: Option<&ExprLocation>,902	name: &str,903	args: &ArgsDesc,904) -> Result<Val> {905	BUILTINS906		.with(|builtins| builtins.get(name).copied())907		.ok_or_else(|| IntrinsicNotFound(name.into()))?(context, loc, args)908}
modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -105,6 +105,17 @@
 		}))
 	}
 
+	pub fn is_empty(&self) -> bool {
+		if !self.0.this_entries.is_empty() {
+			return false;
+		}
+		self.0
+			.super_obj
+			.as_ref()
+			.map(|s| s.is_empty())
+			.unwrap_or(true)
+	}
+
 	/// Run callback for every field found in object
 	pub(crate) fn enum_fields(&self, handler: &mut impl FnMut(&IStr, &Visibility) -> bool) -> bool {
 		if let Some(s) = &self.0.super_obj {
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -1,17 +1,20 @@
 use crate::{
 	builtin::{
 		call_builtin,
-		manifest::{manifest_json_ex, ManifestJsonOptions, ManifestType},
+		manifest::{
+			manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType,
+			ManifestYamlOptions,
+		},
 	},
 	error::{Error::*, LocError},
 	evaluate,
 	function::{parse_function_call, parse_function_call_map, place_args},
 	native::NativeCallback,
-	throw, with_state, Context, ObjValue, Result,
+	throw, Context, ObjValue, Result,
 };
 use jrsonnet_gc::{Gc, GcCell, Trace};
 use jrsonnet_interner::IStr;
-use jrsonnet_parser::{el, ArgsDesc, Expr, ExprLocation, LiteralType, LocExpr, ParamsDesc};
+use jrsonnet_parser::{ArgsDesc, ExprLocation, LocExpr, ParamsDesc};
 use jrsonnet_types::ValType;
 use std::{collections::HashMap, fmt::Debug, rc::Rc};
 
@@ -393,6 +396,12 @@
 	pub fn unwrap_num(self) -> Result<f64> {
 		Ok(matches_unwrap!(self, Self::Num(v), v))
 	}
+	pub fn unwrap_str(self) -> Result<IStr> {
+		Ok(matches_unwrap!(self, Self::Str(v), v))
+	}
+	pub fn unwrap_arr(self) -> Result<ArrValue> {
+		Ok(matches_unwrap!(self, Self::Arr(v), v))
+	}
 	pub fn unwrap_func(self) -> Result<Gc<FuncVal>> {
 		Ok(matches_unwrap!(self, Self::Func(v), v))
 	}
@@ -544,33 +553,15 @@
 	}
 
 	pub fn to_yaml(&self, padding: usize) -> Result<IStr> {
-		with_state(|s| {
-			let ctx = s
-				.create_default_context()
-				.with_var("__tmp__to_json__".into(), self.clone());
-			evaluate(
-				ctx,
-				&el!(Expr::Apply(
-					el!(Expr::Index(
-						el!(Expr::Var("std".into())),
-						el!(Expr::Str("manifestYamlDoc".into()))
-					)),
-					ArgsDesc::new(
-						vec![
-							el!(Expr::Var("__tmp__to_json__".into())),
-							el!(Expr::Literal(if padding != 0 {
-								LiteralType::True
-							} else {
-								LiteralType::False
-							})),
-						],
-						vec![]
-					),
-					false
-				)),
-			)?
-			.try_cast_str("to json")
-		})
+		let padding = &" ".repeat(padding);
+		manifest_yaml_ex(
+			self,
+			&ManifestYamlOptions {
+				padding,
+				arr_element_padding: padding,
+			},
+		)
+		.map(|s| s.into())
 	}
 	pub fn into_indexable(self) -> Result<IndexableVal> {
 		Ok(match self {
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -374,88 +374,9 @@
 
   manifestJsonEx:: $intrinsic(manifestJsonEx),
 
-  manifestYamlDoc(value, indent_array_in_object=false)::
-    local aux(v, path, cindent) =
-      if v == true then
-        'true'
-      else if v == false then
-        'false'
-      else if v == null then
-        'null'
-      else if std.isNumber(v) then
-        '' + v
-      else if std.isString(v) then
-        local len = std.length(v);
-        if len == 0 then
-          '""'
-        else if v[len - 1] == '\n' then
-          local split = std.split(v, '\n');
-          std.join('\n' + cindent + '  ', ['|'] + split[0:std.length(split) - 1])
-        else
-          std.escapeStringJson(v)
-      else if std.isFunction(v) then
-        error 'Tried to manifest function at ' + path
-      else if std.isArray(v) then
-        if std.length(v) == 0 then
-          '[]'
-        else
-          local params(value) =
-            if std.isArray(value) && std.length(value) > 0 then {
-              // While we could avoid the new line, it yields YAML that is
-              // hard to read, e.g.:
-              // - - - 1
-              //     - 2
-              //   - - 3
-              //     - 4
-              new_indent: cindent + '  ',
-              space: '\n' + self.new_indent,
-            } else if std.isObject(value) && std.length(value) > 0 then {
-              new_indent: cindent + '  ',
-              // In this case we can start on the same line as the - because the indentation
-              // matches up then.  The converse is not true, because fields are not always
-              // 1 character long.
-              space: ' ',
-            } else {
-              // In this case, new_indent is only used in the case of multi-line strings.
-              new_indent: cindent,
-              space: ' ',
-            };
-          local range = std.range(0, std.length(v) - 1);
-          local parts = [
-            '-' + param.space + aux(v[i], path + [i], param.new_indent)
-            for i in range
-            for param in [params(v[i])]
-          ];
-          std.join('\n' + cindent, parts)
-      else if std.isObject(v) then
-        if std.length(v) == 0 then
-          '{}'
-        else
-          local params(value) =
-            if std.isArray(value) && std.length(value) > 0 then {
-              // Not indenting allows e.g.
-              // ports:
-              // - 80
-              // instead of
-              // ports:
-              //   - 80
-              new_indent: if indent_array_in_object then cindent + '  ' else cindent,
-              space: '\n' + self.new_indent,
-            } else if std.isObject(value) && std.length(value) > 0 then {
-              new_indent: cindent + '  ',
-              space: '\n' + self.new_indent,
-            } else {
-              // In this case, new_indent is only used in the case of multi-line strings.
-              new_indent: cindent,
-              space: ' ',
-            };
-          local lines = [
-            std.escapeStringJson(k) + ':' + param.space + aux(v[k], path + [k], param.new_indent)
-            for k in std.objectFields(v)
-            for param in [params(v[k])]
-          ];
-          std.join('\n' + cindent, lines);
-    aux(value, [], ''),
+  manifestYamlDocImpl:: $intrinsic(manifestYamlDocImpl),
+
+  manifestYamlDoc(value, indent_array_in_object=false):: std.manifestYamlDocImpl(value, indent_array_in_object),
 
   manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::
     if !std.isArray(value) then