git.delta.rocks / jrsonnet / refs/commits / 9a5055138e5b

difftreelog

refactor move yaml implementation to stdlib

Yaroslav Bolyukin2022-11-12parent: #92bd203.patch.diff
in: master

5 files changed

addedcrates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-evaluator/src/manifest.rs
@@ -0,0 +1,373 @@
+use std::{borrow::Cow, fmt::Write};
+
+use crate::{
+	error::{ErrorKind::*, Result},
+	throw, State, Val,
+};
+
+pub trait ManifestFormat {
+	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()>;
+	fn manifest(&self, val: Val) -> Result<String> {
+		let mut out = String::new();
+		self.manifest_buf(val, &mut out)?;
+		Ok(out)
+	}
+}
+impl<T> ManifestFormat for Box<T>
+where
+	T: ManifestFormat + ?Sized,
+{
+	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {
+		let inner = &**self;
+		inner.manifest_buf(val, buf)
+	}
+}
+impl<T> ManifestFormat for &'_ T
+where
+	T: ManifestFormat + ?Sized,
+{
+	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {
+		let inner = &**self;
+		inner.manifest_buf(val, buf)
+	}
+}
+
+#[derive(PartialEq, Eq, Clone, Copy)]
+enum JsonFormatting {
+	// Applied in manifestification
+	Manifest,
+	/// Used for std.manifestJson
+	/// Empty array/objects extends to "[\n\n]" instead of "[ ]" as in manifest
+	Std,
+	/// No line breaks, used in `obj+''`
+	ToString,
+	/// Minified json
+	Minify,
+}
+
+pub struct JsonFormat<'s> {
+	padding: Cow<'s, str>,
+	mtype: JsonFormatting,
+	newline: &'s str,
+	key_val_sep: &'s str,
+	#[cfg(feature = "exp-preserve-order")]
+	preserve_order: bool,
+}
+
+impl<'s> JsonFormat<'s> {
+	// Minifying format
+	pub fn minify(#[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> Self {
+		Self {
+			padding: Cow::Borrowed(""),
+			mtype: JsonFormatting::Minify,
+			newline: "\n",
+			key_val_sep: ":",
+			#[cfg(feature = "exp-preserve-order")]
+			preserve_order,
+		}
+	}
+	// Same format as std.toString
+	pub fn std_to_string() -> Self {
+		Self {
+			padding: Cow::Borrowed(""),
+			mtype: JsonFormatting::ToString,
+			newline: "\n",
+			key_val_sep: ": ",
+			#[cfg(feature = "exp-preserve-order")]
+			preserve_order: false,
+		}
+	}
+	pub fn std_to_json(
+		padding: String,
+		newline: &'s str,
+		key_val_sep: &'s str,
+		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,
+	) -> Self {
+		Self {
+			padding: Cow::Owned(padding),
+			mtype: JsonFormatting::Std,
+			newline,
+			key_val_sep,
+			#[cfg(feature = "exp-preserve-order")]
+			preserve_order,
+		}
+	}
+	// Same format as CLI manifestification
+	pub fn cli(
+		padding: usize,
+		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,
+	) -> Self {
+		if padding == 0 {
+			return Self::minify(
+				#[cfg(feature = "exp-preserve-order")]
+				preserve_order,
+			);
+		}
+		Self {
+			padding: Cow::Owned(" ".repeat(padding)),
+			mtype: JsonFormatting::Manifest,
+			newline: "\n",
+			key_val_sep: ": ",
+			#[cfg(feature = "exp-preserve-order")]
+			preserve_order,
+		}
+	}
+}
+impl Default for JsonFormat<'static> {
+	fn default() -> Self {
+		Self {
+			padding: Cow::Borrowed("    "),
+			mtype: JsonFormatting::Manifest,
+			newline: "\n",
+			key_val_sep: ": ",
+			#[cfg(feature = "exp-preserve-order")]
+			preserve_order: false,
+		}
+	}
+}
+
+pub fn manifest_json_ex(val: &Val, options: &JsonFormat<'_>) -> Result<String> {
+	let mut out = String::new();
+	manifest_json_ex_buf(val, &mut out, &mut String::new(), options)?;
+	Ok(out)
+}
+fn manifest_json_ex_buf(
+	val: &Val,
+	buf: &mut String,
+	cur_padding: &mut String,
+	options: &JsonFormat<'_>,
+) -> Result<()> {
+	let mtype = options.mtype;
+	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) => escape_string_json_buf(s, buf),
+		Val::Num(n) => write!(buf, "{n}").unwrap(),
+		Val::Arr(items) => {
+			buf.push('[');
+			if !items.is_empty() {
+				if mtype != JsonFormatting::ToString && mtype != JsonFormatting::Minify {
+					buf.push_str(options.newline);
+				}
+
+				let old_len = cur_padding.len();
+				cur_padding.push_str(&options.padding);
+				for (i, item) in items.iter().enumerate() {
+					if i != 0 {
+						buf.push(',');
+						if mtype == JsonFormatting::ToString {
+							buf.push(' ');
+						} else if mtype != JsonFormatting::Minify {
+							buf.push_str(options.newline);
+						}
+					}
+					buf.push_str(cur_padding);
+					manifest_json_ex_buf(&item?, buf, cur_padding, options)?;
+				}
+				cur_padding.truncate(old_len);
+
+				if mtype != JsonFormatting::ToString && mtype != JsonFormatting::Minify {
+					buf.push_str(options.newline);
+					buf.push_str(cur_padding);
+				}
+			} else if mtype == JsonFormatting::Std {
+				buf.push_str("\n\n");
+				buf.push_str(cur_padding);
+			} else if mtype == JsonFormatting::ToString || mtype == JsonFormatting::Manifest {
+				buf.push(' ');
+			}
+			buf.push(']');
+		}
+		Val::Obj(obj) => {
+			obj.run_assertions()?;
+			buf.push('{');
+			let fields = obj.fields(
+				#[cfg(feature = "exp-preserve-order")]
+				options.preserve_order,
+			);
+			if !fields.is_empty() {
+				if mtype != JsonFormatting::ToString && mtype != JsonFormatting::Minify {
+					buf.push_str(options.newline);
+				}
+
+				let old_len = cur_padding.len();
+				cur_padding.push_str(&options.padding);
+				for (i, field) in fields.into_iter().enumerate() {
+					if i != 0 {
+						buf.push(',');
+						if mtype == JsonFormatting::ToString {
+							buf.push(' ');
+						} else if mtype != JsonFormatting::Minify {
+							buf.push_str(options.newline);
+						}
+					}
+					buf.push_str(cur_padding);
+					escape_string_json_buf(&field, buf);
+					buf.push_str(options.key_val_sep);
+					State::push_description(
+						|| format!("field <{}> manifestification", field.clone()),
+						|| {
+							let value = obj.get(field.clone())?.unwrap();
+							manifest_json_ex_buf(&value, buf, cur_padding, options)?;
+							Ok(())
+						},
+					)?;
+				}
+				cur_padding.truncate(old_len);
+
+				if mtype != JsonFormatting::ToString && mtype != JsonFormatting::Minify {
+					buf.push_str(options.newline);
+					buf.push_str(cur_padding);
+				}
+			} else if mtype == JsonFormatting::Std {
+				buf.push_str("\n\n");
+				buf.push_str(cur_padding);
+			} else if mtype == JsonFormatting::ToString || mtype == JsonFormatting::Manifest {
+				buf.push(' ');
+			}
+			buf.push('}');
+		}
+		Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),
+	};
+	Ok(())
+}
+
+impl ManifestFormat for JsonFormat<'_> {
+	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {
+		manifest_json_ex_buf(&val, buf, &mut String::new(), self)
+	}
+}
+
+pub struct ToStringFormat;
+impl ManifestFormat for ToStringFormat {
+	fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> {
+		JsonFormat::std_to_string().manifest_buf(val, out)
+	}
+}
+pub struct StringFormat;
+impl ManifestFormat for StringFormat {
+	fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> {
+		let Val::Str(s) = val else {
+			throw!("output should be string for string manifest format, got {}", val.value_type())
+		};
+		out.write_str(&s).unwrap();
+		Ok(())
+	}
+}
+
+pub struct YamlStreamFormat<I>(pub I);
+impl<I: ManifestFormat> ManifestFormat for YamlStreamFormat<I> {
+	fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> {
+		let Val::Arr(arr) = val else {
+			throw!("output should be array for yaml stream format, got {}", val.value_type())
+		};
+		if !arr.is_empty() {
+			for v in arr.iter() {
+				let v = v?;
+				out.push_str("---\n");
+				self.0.manifest_buf(v, out)?;
+				out.push('\n');
+			}
+			out.push_str("...");
+		}
+		Ok(())
+	}
+}
+
+pub fn escape_string_json(s: &str) -> String {
+	let mut buf = String::new();
+	escape_string_json_buf(s, &mut buf);
+	buf
+}
+
+// Json string encoding was borrowed from https://github.com/serde-rs/json
+
+const BB: u8 = b'b'; // \x08
+const TT: u8 = b't'; // \x09
+const NN: u8 = b'n'; // \x0A
+const FF: u8 = b'f'; // \x0C
+const RR: u8 = b'r'; // \x0D
+const QU: u8 = b'"'; // \x22
+const BS: u8 = b'\\'; // \x5C
+const UU: u8 = b'u'; // \x00...\x1F except the ones above
+const __: u8 = 0;
+
+// Lookup table of escape sequences. A value of b'x' at index i means that byte
+// i is escaped as "\x" in JSON. A value of 0 means that byte i is not escaped.
+static ESCAPE: [u8; 256] = [
+	//   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
+	UU, UU, UU, UU, UU, UU, UU, UU, BB, TT, NN, UU, FF, RR, UU, UU, // 0
+	UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, // 1
+	__, __, QU, __, __, __, __, __, __, __, __, __, __, __, __, __, // 2
+	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 3
+	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 4
+	__, __, __, __, __, __, __, __, __, __, __, __, BS, __, __, __, // 5
+	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 6
+	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 7
+	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8
+	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9
+	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A
+	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B
+	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C
+	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D
+	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E
+	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F
+];
+
+pub fn escape_string_json_buf(value: &str, buf: &mut String) {
+	// Safety: we only write correct utf-8 in this function
+	let buf: &mut Vec<u8> = unsafe { &mut *(buf as *mut String).cast::<Vec<u8>>() };
+	let bytes = value.as_bytes();
+
+	// Perfect for ascii strings, removes any reallocations
+	buf.reserve(value.len() + 2);
+
+	buf.push(b'"');
+
+	let mut start = 0;
+
+	for (i, &byte) in bytes.iter().enumerate() {
+		let escape = ESCAPE[byte as usize];
+		if escape == __ {
+			continue;
+		}
+
+		if start < i {
+			buf.extend_from_slice(&bytes[start..i]);
+		}
+		start = i + 1;
+
+		match escape {
+			self::BB | self::TT | self::NN | self::FF | self::RR | self::QU | self::BS => {
+				buf.extend_from_slice(&[b'\\', escape]);
+			}
+			self::UU => {
+				static HEX_DIGITS: [u8; 16] = *b"0123456789abcdef";
+				let bytes = &[
+					b'\\',
+					b'u',
+					b'0',
+					b'0',
+					HEX_DIGITS[(byte >> 4) as usize],
+					HEX_DIGITS[(byte & 0xF) as usize],
+				];
+				buf.extend_from_slice(bytes);
+			}
+			_ => unreachable!(),
+		}
+	}
+
+	if start == bytes.len() {
+		buf.push(b'"');
+		return;
+	}
+
+	buf.extend_from_slice(&bytes[start..]);
+	buf.push(b'"');
+}
deletedcrates/jrsonnet-evaluator/src/stdlib/manifest.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/stdlib/manifest.rs
1use std::{borrow::Cow, fmt::Write};23use crate::{4	error::{Error::*, Result},5	throw, ManifestFormat, State, Val,6};78#[derive(PartialEq, Eq, Clone, Copy)]9pub enum ManifestType {10	// Applied in manifestification11	Manifest,12	/// Used for std.manifestJson13	/// Empty array/objects extends to "[\n\n]" instead of "[ ]" as in manifest14	Std,15	/// No line breaks, used in `obj+''`16	ToString,17	/// Minified json18	Minify,19}2021pub struct JsonFormat<'s> {22	padding: Cow<'s, str>,23	mtype: ManifestType,24	newline: &'s str,25	key_val_sep: &'s str,26	#[cfg(feature = "exp-preserve-order")]27	preserve_order: bool,28}2930impl<'s> JsonFormat<'s> {31	// Minifying format32	pub fn minify(#[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> Self {33		Self {34			padding: Cow::Borrowed(""),35			mtype: ManifestType::Minify,36			newline: "\n",37			key_val_sep: ":",38			#[cfg(feature = "exp-preserve-order")]39			preserve_order,40		}41	}42	// Same format as std.toString43	pub fn std_to_string() -> Self {44		Self {45			padding: Cow::Borrowed(""),46			mtype: ManifestType::ToString,47			newline: "\n",48			key_val_sep: ": ",49			#[cfg(feature = "exp-preserve-order")]50			preserve_order: false,51		}52	}53	pub fn std_to_json(54		padding: String,55		newline: &'s str,56		key_val_sep: &'s str,57		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,58	) -> Self {59		Self {60			padding: Cow::Owned(padding),61			mtype: ManifestType::Std,62			newline,63			key_val_sep,64			#[cfg(feature = "exp-preserve-order")]65			preserve_order,66		}67	}68	// Same format as CLI manifestification69	pub fn cli(70		padding: usize,71		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,72	) -> Self {73		if padding == 0 {74			return Self::minify(75				#[cfg(feature = "exp-preserve-order")]76				preserve_order,77			);78		}79		Self {80			padding: Cow::Owned(" ".repeat(padding)),81			mtype: ManifestType::Manifest,82			newline: "\n",83			key_val_sep: ": ",84			#[cfg(feature = "exp-preserve-order")]85			preserve_order,86		}87	}88}89impl Default for JsonFormat<'static> {90	fn default() -> Self {91		Self {92			padding: Cow::Borrowed("    "),93			mtype: ManifestType::Manifest,94			newline: "\n",95			key_val_sep: ": ",96			#[cfg(feature = "exp-preserve-order")]97			preserve_order: false,98		}99	}100}101102pub fn manifest_json_ex(val: &Val, options: &JsonFormat<'_>) -> Result<String> {103	let mut out = String::new();104	manifest_json_ex_buf(val, &mut out, &mut String::new(), options)?;105	Ok(out)106}107fn manifest_json_ex_buf(108	val: &Val,109	buf: &mut String,110	cur_padding: &mut String,111	options: &JsonFormat<'_>,112) -> Result<()> {113	let mtype = options.mtype;114	match val {115		Val::Bool(v) => {116			if *v {117				buf.push_str("true");118			} else {119				buf.push_str("false");120			}121		}122		Val::Null => buf.push_str("null"),123		Val::Str(s) => escape_string_json_buf(s, buf),124		Val::Num(n) => write!(buf, "{n}").unwrap(),125		Val::Arr(items) => {126			buf.push('[');127			if !items.is_empty() {128				if mtype != ManifestType::ToString && mtype != ManifestType::Minify {129					buf.push_str(options.newline);130				}131132				let old_len = cur_padding.len();133				cur_padding.push_str(&options.padding);134				for (i, item) in items.iter().enumerate() {135					if i != 0 {136						buf.push(',');137						if mtype == ManifestType::ToString {138							buf.push(' ');139						} else if mtype != ManifestType::Minify {140							buf.push_str(options.newline);141						}142					}143					buf.push_str(cur_padding);144					manifest_json_ex_buf(&item?, buf, cur_padding, options)?;145				}146				cur_padding.truncate(old_len);147148				if mtype != ManifestType::ToString && mtype != ManifestType::Minify {149					buf.push_str(options.newline);150					buf.push_str(cur_padding);151				}152			} else if mtype == ManifestType::Std {153				buf.push_str("\n\n");154				buf.push_str(cur_padding);155			} else if mtype == ManifestType::ToString || mtype == ManifestType::Manifest {156				buf.push(' ');157			}158			buf.push(']');159		}160		Val::Obj(obj) => {161			obj.run_assertions()?;162			buf.push('{');163			let fields = obj.fields(164				#[cfg(feature = "exp-preserve-order")]165				options.preserve_order,166			);167			if !fields.is_empty() {168				if mtype != ManifestType::ToString && mtype != ManifestType::Minify {169					buf.push_str(options.newline);170				}171172				let old_len = cur_padding.len();173				cur_padding.push_str(&options.padding);174				for (i, field) in fields.into_iter().enumerate() {175					if i != 0 {176						buf.push(',');177						if mtype == ManifestType::ToString {178							buf.push(' ');179						} else if mtype != ManifestType::Minify {180							buf.push_str(options.newline);181						}182					}183					buf.push_str(cur_padding);184					escape_string_json_buf(&field, buf);185					buf.push_str(options.key_val_sep);186					State::push_description(187						|| format!("field <{}> manifestification", field.clone()),188						|| {189							let value = obj.get(field.clone())?.unwrap();190							manifest_json_ex_buf(&value, buf, cur_padding, options)?;191							Ok(())192						},193					)?;194				}195				cur_padding.truncate(old_len);196197				if mtype != ManifestType::ToString && mtype != ManifestType::Minify {198					buf.push_str(options.newline);199					buf.push_str(cur_padding);200				}201			} else if mtype == ManifestType::Std {202				buf.push_str("\n\n");203				buf.push_str(cur_padding);204			} else if mtype == ManifestType::ToString || mtype == ManifestType::Manifest {205				buf.push(' ');206			}207			buf.push('}');208		}209		Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),210	};211	Ok(())212}213214impl ManifestFormat for JsonFormat<'_> {215	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {216		manifest_json_ex_buf(&val, buf, &mut String::new(), &self)217	}218}219220pub struct ToStringFormat;221impl ManifestFormat for ToStringFormat {222	fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> {223		JsonFormat::std_to_string().manifest_buf(val, out)224	}225}226pub struct StringFormat;227impl ManifestFormat for StringFormat {228	fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> {229		let Val::Str(s) = val else {230			throw!("output should be string for string manifest format, got {}", val.value_type())231		};232		out.write_str(&s).unwrap();233		Ok(())234	}235}236237pub struct YamlStreamFormat<I>(pub I);238impl<I: ManifestFormat> ManifestFormat for YamlStreamFormat<I> {239	fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> {240		let Val::Arr(arr) = val else {241			throw!("output should be array for yaml stream format, got {}", val.value_type())242		};243		if !arr.is_empty() {244			for v in arr.iter() {245				let v = v?;246				out.push_str("---\n");247				self.0.manifest_buf(v, out)?;248				out.push('\n');249			}250			out.push_str("...");251		}252		Ok(())253	}254}255256pub fn escape_string_json(s: &str) -> String {257	let mut buf = String::new();258	escape_string_json_buf(s, &mut buf);259	buf260}261262// Json string encoding was borrowed from https://github.com/serde-rs/json263264const BB: u8 = b'b'; // \x08265const TT: u8 = b't'; // \x09266const NN: u8 = b'n'; // \x0A267const FF: u8 = b'f'; // \x0C268const RR: u8 = b'r'; // \x0D269const QU: u8 = b'"'; // \x22270const BS: u8 = b'\\'; // \x5C271const UU: u8 = b'u'; // \x00...\x1F except the ones above272const __: u8 = 0;273274// Lookup table of escape sequences. A value of b'x' at index i means that byte275// i is escaped as "\x" in JSON. A value of 0 means that byte i is not escaped.276static ESCAPE: [u8; 256] = [277	//   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F278	UU, UU, UU, UU, UU, UU, UU, UU, BB, TT, NN, UU, FF, RR, UU, UU, // 0279	UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, // 1280	__, __, QU, __, __, __, __, __, __, __, __, __, __, __, __, __, // 2281	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 3282	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 4283	__, __, __, __, __, __, __, __, __, __, __, __, BS, __, __, __, // 5284	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 6285	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 7286	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8287	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9288	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A289	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B290	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C291	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D292	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E293	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F294];295296fn escape_string_json_buf(value: &str, buf: &mut String) {297	// Safety: we only write correct utf-8 in this function298	let mut buf: &mut Vec<u8> = unsafe { core::mem::transmute(buf) };299	let bytes = value.as_bytes();300301	// Perfect for ascii strings, removes any reallocations302	buf.reserve(value.len() + 2);303304	buf.push(b'"');305306	let mut start = 0;307308	for (i, &byte) in bytes.iter().enumerate() {309		let escape = ESCAPE[byte as usize];310		if escape == __ {311			continue;312		}313314		if start < i {315			buf.extend_from_slice(&bytes[start..i]);316		}317		start = i + 1;318319		match escape {320			self::BB | self::TT | self::NN | self::FF | self::RR | self::QU | self::BS => {321				buf.extend_from_slice(&[b'\\', escape])322			}323			self::UU => {324				static HEX_DIGITS: [u8; 16] = *b"0123456789abcdef";325				let bytes = &[326					b'\\',327					b'u',328					b'0',329					b'0',330					HEX_DIGITS[(byte >> 4) as usize],331					HEX_DIGITS[(byte & 0xF) as usize],332				];333				buf.extend_from_slice(bytes)334			}335			_ => unreachable!(),336		}337	}338339	if start == bytes.len() {340		buf.push(b'"');341		return;342	}343344	buf.extend_from_slice(&bytes[start..]);345	buf.push(b'"');346}347348pub struct YamlFormat<'s> {349	/// Padding before fields, i.e350	/// ```yaml351	/// a:352	///   b:353	/// ## <- this354	/// ```355	padding: Cow<'s, str>,356	/// Padding before array elements in objects357	/// ```yaml358	/// a:359	///   - 1360	/// ## <- this361	/// ```362	arr_element_padding: Cow<'s, str>,363	/// Should yaml keys appear unescaped, when possible364	/// ```yaml365	/// "safe_key": 1366	/// # vs367	/// safe_key: 1368	/// ```369	quote_keys: bool,370	/// If true - then order of fields is preserved as written,371	/// instead of sorting alphabetically372	#[cfg(feature = "exp-preserve-order")]373	preserve_order: bool,374}375impl YamlFormat<'_> {376	pub fn cli(377		padding: usize,378		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,379	) -> Self {380		let padding = " ".repeat(padding);381		Self {382			padding: Cow::Owned(padding.clone()),383			arr_element_padding: Cow::Owned(padding),384			quote_keys: false,385			#[cfg(feature = "exp-preserve-order")]386			preserve_order,387		}388	}389	pub fn std_to_yaml(390		indent_array_in_object: bool,391		quote_keys: bool,392		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,393	) -> Self {394		Self {395			padding: Cow::Borrowed("  "),396			arr_element_padding: Cow::Borrowed(if indent_array_in_object { "  " } else { "" }),397			quote_keys,398			#[cfg(feature = "exp-preserve-order")]399			preserve_order,400		}401	}402}403impl ManifestFormat for YamlFormat<'_> {404	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {405		manifest_yaml_ex_buf(&val, buf, &mut String::new(), self)406	}407}408409/// From <https://github.com/chyh1990/yaml-rust/blob/da52a68615f2ecdd6b7e4567019f280c433c1521/src/emitter.rs#L289>410/// With added date check411fn yaml_needs_quotes(string: &str) -> bool {412	fn need_quotes_spaces(string: &str) -> bool {413		string.starts_with(' ') || string.ends_with(' ')414	}415416	string.is_empty()417		|| need_quotes_spaces(string)418		|| string.starts_with(|c| matches!(c, '&' | '*' | '?' | '|' | '-' | '<' | '>' | '=' | '!' | '%' | '@'))419		|| string.contains(|c| matches!(c, ':' | '{' | '}' | '[' | ']' | ',' | '#' | '`' | '\"' | '\'' | '\\' | '\0'..='\x06' | '\t' | '\n' | '\r' | '\x0e'..='\x1a' | '\x1c'..='\x1f'))420		|| [421			// http://yaml.org/type/bool.html422			// Note: 'y', 'Y', 'n', 'N', is not quoted deliberately, as in libyaml. PyYAML also parse423			// them as string, not booleans, although it is violating the YAML 1.1 specification.424			// See https://github.com/dtolnay/serde-yaml/pull/83#discussion_r152628088.425			"yes", "Yes", "YES", "no", "No", "NO", "True", "TRUE", "true", "False", "FALSE", "false",426			"on", "On", "ON", "off", "Off", "OFF", // http://yaml.org/type/null.html427			"null", "Null", "NULL", "~",428		].contains(&string)429		|| (string.chars().all(|c| matches!(c, '0'..='9' | '-'))430			&& string.chars().filter(|c| *c == '-').count() == 2)431		|| string.starts_with('.')432		|| string.starts_with("0x")433		|| string.parse::<i64>().is_ok()434		|| string.parse::<f64>().is_ok()435}436437pub fn manifest_yaml_ex(val: &Val, options: &YamlFormat<'_>) -> Result<String> {438	let mut out = String::new();439	manifest_yaml_ex_buf(val, &mut out, &mut String::new(), options)?;440	Ok(out)441}442443#[allow(clippy::too_many_lines)]444fn manifest_yaml_ex_buf(445	val: &Val,446	buf: &mut String,447	cur_padding: &mut String,448	options: &YamlFormat<'_>,449) -> Result<()> {450	match val {451		Val::Bool(v) => {452			if *v {453				buf.push_str("true");454			} else {455				buf.push_str("false");456			}457		}458		Val::Null => buf.push_str("null"),459		Val::Str(s) => {460			if s.is_empty() {461				buf.push_str("\"\"");462			} else if let Some(s) = s.strip_suffix('\n') {463				buf.push('|');464				for line in s.split('\n') {465					buf.push('\n');466					buf.push_str(cur_padding);467					buf.push_str(&options.padding);468					buf.push_str(line);469				}470			} else if !options.quote_keys && !yaml_needs_quotes(s) {471				buf.push_str(s);472			} else {473				escape_string_json_buf(s, buf);474			}475		}476		Val::Num(n) => write!(buf, "{}", *n).unwrap(),477		Val::Arr(a) => {478			if a.is_empty() {479				buf.push_str("[]");480			} else {481				for (i, item) in a.iter().enumerate() {482					if i != 0 {483						buf.push('\n');484						buf.push_str(cur_padding);485					}486					let item = item?;487					buf.push('-');488					match &item {489						Val::Arr(a) if !a.is_empty() => {490							buf.push('\n');491							buf.push_str(cur_padding);492							buf.push_str(&options.padding);493						}494						_ => buf.push(' '),495					}496					let extra_padding = match &item {497						Val::Arr(a) => !a.is_empty(),498						Val::Obj(o) => !o.is_empty(),499						_ => false,500					};501					let prev_len = cur_padding.len();502					if extra_padding {503						cur_padding.push_str(&options.padding);504					}505					manifest_yaml_ex_buf(&item, buf, cur_padding, options)?;506					cur_padding.truncate(prev_len);507				}508			}509		}510		Val::Obj(o) => {511			if o.is_empty() {512				buf.push_str("{}");513			} else {514				for (i, key) in o515					.fields(516						#[cfg(feature = "exp-preserve-order")]517						options.preserve_order,518					)519					.iter()520					.enumerate()521				{522					if i != 0 {523						buf.push('\n');524						buf.push_str(cur_padding);525					}526					if !options.quote_keys && !yaml_needs_quotes(key) {527						buf.push_str(key);528					} else {529						escape_string_json_buf(key, buf);530					}531					buf.push(':');532					let prev_len = cur_padding.len();533					let item = o.get(key.clone())?.expect("field exists");534					match &item {535						Val::Arr(a) if !a.is_empty() => {536							buf.push('\n');537							buf.push_str(cur_padding);538							buf.push_str(&options.arr_element_padding);539							cur_padding.push_str(&options.arr_element_padding);540						}541						Val::Obj(o) if !o.is_empty() => {542							buf.push('\n');543							buf.push_str(cur_padding);544							buf.push_str(&options.padding);545							cur_padding.push_str(&options.padding);546						}547						_ => buf.push(' '),548					}549					manifest_yaml_ex_buf(&item, buf, cur_padding, options)?;550					cur_padding.truncate(prev_len);551				}552			}553		}554		Val::Func(_) => throw!("tried to manifest function"),555	}556	Ok(())557}
deletedcrates/jrsonnet-stdlib/src/manifest.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/manifest.rs
+++ /dev/null
@@ -1,46 +0,0 @@
-use jrsonnet_evaluator::{
-	error::Result,
-	function::builtin,
-	stdlib::manifest::{escape_string_json, JsonFormat, YamlFormat},
-	typed::Any,
-	IStr,
-};
-
-#[builtin]
-pub fn builtin_escape_string_json(str_: IStr) -> Result<String> {
-	Ok(escape_string_json(&str_))
-}
-
-#[builtin]
-pub fn builtin_manifest_json_ex(
-	value: Any,
-	indent: IStr,
-	newline: Option<IStr>,
-	key_val_sep: Option<IStr>,
-	#[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,
-) -> Result<String> {
-	let newline = newline.as_deref().unwrap_or("\n");
-	let key_val_sep = key_val_sep.as_deref().unwrap_or(": ");
-	value.0.manifest(JsonFormat::std_to_json(
-		indent.to_string(),
-		newline,
-		key_val_sep,
-		#[cfg(feature = "exp-preserve-order")]
-		preserve_order.unwrap_or(false),
-	))
-}
-
-#[builtin]
-pub fn builtin_manifest_yaml_doc(
-	value: Any,
-	indent_array_in_object: Option<bool>,
-	quote_keys: Option<bool>,
-	#[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,
-) -> Result<String> {
-	value.0.manifest(YamlFormat::std_to_yaml(
-		indent_array_in_object.unwrap_or(false),
-		quote_keys.unwrap_or(true),
-		#[cfg(feature = "exp-preserve-order")]
-		preserve_order.unwrap_or(false),
-	))
-}
addedcrates/jrsonnet-stdlib/src/manifest/mod.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-stdlib/src/manifest/mod.rs
@@ -0,0 +1,49 @@
+mod yaml;
+
+use jrsonnet_evaluator::{
+	error::Result,
+	function::builtin,
+	manifest::{escape_string_json, JsonFormat},
+	typed::Any,
+	IStr,
+};
+pub use yaml::YamlFormat;
+
+#[builtin]
+pub fn builtin_escape_string_json(str_: IStr) -> Result<String> {
+	Ok(escape_string_json(&str_))
+}
+
+#[builtin]
+pub fn builtin_manifest_json_ex(
+	value: Any,
+	indent: IStr,
+	newline: Option<IStr>,
+	key_val_sep: Option<IStr>,
+	#[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,
+) -> Result<String> {
+	let newline = newline.as_deref().unwrap_or("\n");
+	let key_val_sep = key_val_sep.as_deref().unwrap_or(": ");
+	value.0.manifest(JsonFormat::std_to_json(
+		indent.to_string(),
+		newline,
+		key_val_sep,
+		#[cfg(feature = "exp-preserve-order")]
+		preserve_order.unwrap_or(false),
+	))
+}
+
+#[builtin]
+pub fn builtin_manifest_yaml_doc(
+	value: Any,
+	indent_array_in_object: Option<bool>,
+	quote_keys: Option<bool>,
+	#[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,
+) -> Result<String> {
+	value.0.manifest(YamlFormat::std_to_yaml(
+		indent_array_in_object.unwrap_or(false),
+		quote_keys.unwrap_or(true),
+		#[cfg(feature = "exp-preserve-order")]
+		preserve_order.unwrap_or(false),
+	))
+}
addedcrates/jrsonnet-stdlib/src/manifest/yaml.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-stdlib/src/manifest/yaml.rs
@@ -0,0 +1,218 @@
+use std::{borrow::Cow, fmt::Write};
+
+use jrsonnet_evaluator::{
+	manifest::{escape_string_json_buf, ManifestFormat},
+	throw, Result, Val,
+};
+
+pub struct YamlFormat<'s> {
+	/// Padding before fields, i.e
+	/// ```yaml
+	/// a:
+	///   b:
+	/// ## <- this
+	/// ```
+	padding: Cow<'s, str>,
+	/// Padding before array elements in objects
+	/// ```yaml
+	/// a:
+	///   - 1
+	/// ## <- this
+	/// ```
+	arr_element_padding: Cow<'s, str>,
+	/// Should yaml keys appear unescaped, when possible
+	/// ```yaml
+	/// "safe_key": 1
+	/// # vs
+	/// safe_key: 1
+	/// ```
+	quote_keys: bool,
+	/// If true - then order of fields is preserved as written,
+	/// instead of sorting alphabetically
+	#[cfg(feature = "exp-preserve-order")]
+	preserve_order: bool,
+}
+impl YamlFormat<'_> {
+	pub fn cli(
+		padding: usize,
+		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,
+	) -> Self {
+		let padding = " ".repeat(padding);
+		Self {
+			padding: Cow::Owned(padding.clone()),
+			arr_element_padding: Cow::Owned(padding),
+			quote_keys: false,
+			#[cfg(feature = "exp-preserve-order")]
+			preserve_order,
+		}
+	}
+	pub fn std_to_yaml(
+		indent_array_in_object: bool,
+		quote_keys: bool,
+		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,
+	) -> Self {
+		Self {
+			padding: Cow::Borrowed("  "),
+			arr_element_padding: Cow::Borrowed(if indent_array_in_object { "  " } else { "" }),
+			quote_keys,
+			#[cfg(feature = "exp-preserve-order")]
+			preserve_order,
+		}
+	}
+}
+impl ManifestFormat for YamlFormat<'_> {
+	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {
+		manifest_yaml_ex_buf(&val, buf, &mut String::new(), self)
+	}
+}
+
+/// From <https://github.com/chyh1990/yaml-rust/blob/da52a68615f2ecdd6b7e4567019f280c433c1521/src/emitter.rs#L289>
+/// With added date check
+fn yaml_needs_quotes(string: &str) -> bool {
+	fn need_quotes_spaces(string: &str) -> bool {
+		string.starts_with(' ') || string.ends_with(' ')
+	}
+
+	string.is_empty()
+		|| need_quotes_spaces(string)
+		|| string.starts_with(|c| matches!(c, '&' | '*' | '?' | '|' | '-' | '<' | '>' | '=' | '!' | '%' | '@'))
+		|| string.contains(|c| matches!(c, ':' | '{' | '}' | '[' | ']' | ',' | '#' | '`' | '\"' | '\'' | '\\' | '\0'..='\x06' | '\t' | '\n' | '\r' | '\x0e'..='\x1a' | '\x1c'..='\x1f'))
+		|| [
+			// http://yaml.org/type/bool.html
+			// Note: 'y', 'Y', 'n', 'N', is not quoted deliberately, as in libyaml. PyYAML also parse
+			// them as string, not booleans, although it is violating the YAML 1.1 specification.
+			// See https://github.com/dtolnay/serde-yaml/pull/83#discussion_r152628088.
+			"yes", "Yes", "YES", "no", "No", "NO", "True", "TRUE", "true", "False", "FALSE", "false",
+			"on", "On", "ON", "off", "Off", "OFF", // http://yaml.org/type/null.html
+			"null", "Null", "NULL", "~",
+		].contains(&string)
+		|| (string.chars().all(|c| matches!(c, '0'..='9' | '-'))
+			&& string.chars().filter(|c| *c == '-').count() == 2)
+		|| string.starts_with('.')
+		|| string.starts_with("0x")
+		|| string.parse::<i64>().is_ok()
+		|| string.parse::<f64>().is_ok()
+}
+
+#[allow(dead_code)]
+fn manifest_yaml_ex(val: &Val, options: &YamlFormat<'_>) -> Result<String> {
+	let mut out = String::new();
+	manifest_yaml_ex_buf(val, &mut out, &mut String::new(), options)?;
+	Ok(out)
+}
+
+#[allow(clippy::too_many_lines)]
+fn manifest_yaml_ex_buf(
+	val: &Val,
+	buf: &mut String,
+	cur_padding: &mut String,
+	options: &YamlFormat<'_>,
+) -> Result<()> {
+	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(cur_padding);
+					buf.push_str(&options.padding);
+					buf.push_str(line);
+				}
+			} else if !options.quote_keys && !yaml_needs_quotes(s) {
+				buf.push_str(s);
+			} 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(
+						#[cfg(feature = "exp-preserve-order")]
+						options.preserve_order,
+					)
+					.iter()
+					.enumerate()
+				{
+					if i != 0 {
+						buf.push('\n');
+						buf.push_str(cur_padding);
+					}
+					if !options.quote_keys && !yaml_needs_quotes(key) {
+						buf.push_str(key);
+					} else {
+						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!("tried to manifest function"),
+	}
+	Ok(())
+}