git.delta.rocks / jrsonnet / refs/commits / 78be61658f34

difftreelog

refactor remove ManifestFormat from state

Yaroslav Bolyukin2022-11-09parent: #5f620e2.patch.diff
in: master

7 files changed

modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -93,7 +93,7 @@
 enum Error {
 	// Handled differently
 	#[error("evaluation error")]
-	Evaluation(jrsonnet_evaluator::error::LocError),
+	Evaluation(LocError),
 	#[error("io error")]
 	Io(#[from] std::io::Error),
 	#[error("input is not utf8 encoded")]
@@ -106,6 +106,11 @@
 		Self::Evaluation(e)
 	}
 }
+impl From<jrsonnet_evaluator::error::Error> for Error {
+	fn from(e: jrsonnet_evaluator::error::Error) -> Self {
+		Self::from(LocError::from(e))
+	}
+}
 
 fn main_catch(opts: Opts) -> bool {
 	let s = State::default();
@@ -144,9 +149,17 @@
 			dir.pop();
 			create_dir_all(dir)?;
 		}
-		for (file, data) in s.manifest_multi(val)?.iter() {
+		let Val::Obj(obj) = val else {
+			throw!("value should be object for --multi manifest, got {}", val.value_type())
+		};
+		for (field, data) in obj.iter(
+			#[cfg(feature = "exp-preserve-order")]
+			opts.manifest.preserve_order,
+		) {
+			let data = data.with_description(|| format!("getting field {field} for manifest"))?;
+
 			let mut path = multi.clone();
-			path.push(file as &str);
+			path.push(&field as &str);
 			if opts.output.create_output_dirs {
 				let mut dir = path.clone();
 				dir.pop();
@@ -154,7 +167,12 @@
 			}
 			println!("{}", path.to_str().expect("path"));
 			let mut file = File::create(path)?;
-			writeln!(file, "{}", data)?;
+			writeln!(
+				file,
+				"{}",
+				data.manifest(&manifest_format)
+					.with_description(|| format!("manifesting {field}"))?
+			)?;
 		}
 	} else if let Some(path) = opts.output.output_file {
 		if opts.output.create_output_dirs {
@@ -163,9 +181,9 @@
 			create_dir_all(dir)?;
 		}
 		let mut file = File::create(path)?;
-		writeln!(file, "{}", s.manifest(val)?)?;
+		writeln!(file, "{}", val.manifest(manifest_format)?)?;
 	} else {
-		let output = s.manifest(val)?;
+		let output = val.manifest(manifest_format)?;
 		if !output.is_empty() {
 			println!("{}", output);
 		}
modifiedcrates/jrsonnet-cli/src/manifest.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/manifest.rs
+++ b/crates/jrsonnet-cli/src/manifest.rs
@@ -1,7 +1,11 @@
 use std::{path::PathBuf, str::FromStr};
 
 use clap::{Parser, ValueEnum};
-use jrsonnet_evaluator::{error::Result, ManifestFormat, State};
+use jrsonnet_evaluator::{
+	error::Result,
+	stdlib::manifest::{JsonFormat, StringFormat, ToStringFormat, YamlFormat, YamlStreamFormat},
+	ManifestFormat, State,
+};
 
 use crate::ConfigureState;
 
@@ -36,7 +40,7 @@
 	#[clap(long, short = 'S', conflicts_with = "format")]
 	string: bool,
 	/// Write output as YAML stream, can be used with --format json/yaml
-	#[clap(long, short = 'y')]
+	#[clap(long, short = 'y', conflicts_with = "string")]
 	yaml_stream: bool,
 	/// Number of spaces to pad output manifest with.
 	/// `0` for hard tabs, `-1` for single line output [default: 3 for json, 2 for yaml]
@@ -45,34 +49,35 @@
 	/// Preserve order in object manifestification
 	#[cfg(feature = "exp-preserve-order")]
 	#[clap(long)]
-	exp_preserve_order: bool,
+	preserve_order: bool,
 }
 impl ConfigureState for ManifestOpts {
-	type Guards = ();
-	fn configure(&self, s: &State) -> Result<()> {
-		if self.string {
-			s.set_manifest_format(ManifestFormat::String);
+	type Guards = Box<dyn ManifestFormat>;
+	fn configure(&self, _s: &State) -> Result<Self::Guards> {
+		let format: Box<dyn ManifestFormat> = if self.string {
+			Box::new(StringFormat)
 		} else {
 			#[cfg(feature = "exp-preserve-order")]
-			let preserve_order = self.exp_preserve_order;
+			let preserve_order = self.preserve_order;
 			match self.format {
-				ManifestFormatName::String => s.set_manifest_format(ManifestFormat::String),
-				ManifestFormatName::Json => s.set_manifest_format(ManifestFormat::Json {
-					padding: self.line_padding.unwrap_or(3),
+				ManifestFormatName::String => Box::new(ToStringFormat),
+				ManifestFormatName::Json => Box::new(JsonFormat::cli(
+					self.line_padding.unwrap_or(3),
 					#[cfg(feature = "exp-preserve-order")]
 					preserve_order,
-				}),
-				ManifestFormatName::Yaml => s.set_manifest_format(ManifestFormat::Yaml {
-					padding: self.line_padding.unwrap_or(2),
+				)),
+				ManifestFormatName::Yaml => Box::new(YamlFormat::cli(
+					self.line_padding.unwrap_or(2),
 					#[cfg(feature = "exp-preserve-order")]
 					preserve_order,
-				}),
+				)),
 			}
-		}
-		if self.yaml_stream {
-			s.set_manifest_format(ManifestFormat::YamlStream(Box::new(s.manifest_format())))
-		}
-		Ok(())
+		};
+		Ok(if self.yaml_stream {
+			Box::new(YamlStreamFormat(format))
+		} else {
+			format
+		})
 	}
 }
 
modifiedcrates/jrsonnet-cli/src/stdlib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/stdlib.rs
+++ b/crates/jrsonnet-cli/src/stdlib.rs
@@ -60,8 +60,6 @@
 pub struct StdOpts {
 	/// Disable standard library.
 	/// By default standard library will be available via global `std` variable.
-	/// Note that standard library will still be loaded
-	/// if chosen manifestification method is not `none`.
 	#[clap(long)]
 	no_stdlib: bool,
 	/// Add string external variable.
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/lib.rs
1//! jsonnet interpreter implementation2#![cfg_attr(feature = "nightly", feature(thread_local))]3#![deny(unsafe_op_in_unsafe_fn)]4#![warn(5	clippy::all,6	clippy::nursery,7	clippy::pedantic,8	// missing_docs,9	elided_lifetimes_in_paths,10	explicit_outlives_requirements,11	noop_method_call,12	single_use_lifetimes,13	variant_size_differences,14	rustdoc::all15)]16#![allow(17	macro_expanded_macro_exports_accessed_by_absolute_paths,18	clippy::ptr_arg,19	// Too verbose20	clippy::must_use_candidate,21	// A lot of functions pass around errors thrown by code22	clippy::missing_errors_doc,23	// A lot of pointers have interior Rc24	clippy::needless_pass_by_value,25	// Its fine26	clippy::wildcard_imports,27	clippy::enum_glob_use,28	clippy::module_name_repetitions,29	// TODO: fix individual issues, however this works as intended almost everywhere30	clippy::cast_precision_loss,31	clippy::cast_possible_wrap,32	clippy::cast_possible_truncation,33	clippy::cast_sign_loss,34	// False positives35	// https://github.com/rust-lang/rust-clippy/issues/690236	clippy::use_self,37	// https://github.com/rust-lang/rust-clippy/issues/853938	clippy::iter_with_drain,39	// ci is being run with nightly, but library should work on stable40	clippy::missing_const_for_fn,41)]4243// For jrsonnet-macros44extern crate self as jrsonnet_evaluator;4546mod ctx;47mod dynamic;48pub mod error;49mod evaluate;50pub mod function;51pub mod gc;52mod import;53mod integrations;54mod map;55mod obj;56pub mod stack;57pub mod stdlib;58mod tla;59pub mod trace;60pub mod typed;61pub mod val;6263use std::{64	any::Any,65	cell::{Ref, RefCell, RefMut},66	collections::HashMap,67	fmt::{self, Debug},68	path::Path,69};7071pub use ctx::*;72pub use dynamic::*;73use error::{Error::*, LocError, Result, ResultExt};74pub use evaluate::*;75use function::{CallLocation, TlaArg};76use gc::{GcHashMap, TraceBox};77use hashbrown::hash_map::RawEntryMut;78pub use import::*;79use jrsonnet_gcmodule::{Cc, Trace};80pub use jrsonnet_interner::{IBytes, IStr};81pub use jrsonnet_parser as parser;82use jrsonnet_parser::*;83pub use obj::*;84use stack::check_depth;85pub use tla::apply_tla;86pub use val::{ManifestFormat, Thunk, Val};8788/// Thunk without bound `super`/`this`89/// object inheritance may be overriden multiple times, and will be fixed only on field read90pub trait Unbound: Trace {91	/// Type of value after object context is bound92	type Bound;93	/// Create value bound to specified object context94	fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;95}9697/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code98/// Standard jsonnet fields are always unbound99#[derive(Clone, Trace)]100pub enum MaybeUnbound {101	/// Value needs to be bound to `this`/`super`102	Unbound(Cc<TraceBox<dyn Unbound<Bound = Val>>>),103	/// Value is object-independent104	Bound(Thunk<Val>),105}106107impl Debug for MaybeUnbound {108	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {109		write!(f, "MaybeUnbound")110	}111}112impl MaybeUnbound {113	/// Attach object context to value, if required114	pub fn evaluate(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {115		match self {116			Self::Unbound(v) => v.bind(sup, this),117			Self::Bound(v) => Ok(v.evaluate()?),118		}119	}120}121122/// During import, this trait will be called to create initial context for file.123/// It may initialize global variables, stdlib for example.124pub trait ContextInitializer: Trace {125	/// Initialize default file context.126	fn initialize(&self, state: State, for_file: Source) -> Context;127	/// Allows upcasting from abstract to concrete context initializer.128	/// jrsonnet by itself doesn't use this method, it is allowed for it to panic.129	fn as_any(&self) -> &dyn Any;130}131132/// Context initializer which adds nothing.133#[derive(Trace)]134pub struct DummyContextInitializer;135impl ContextInitializer for DummyContextInitializer {136	fn initialize(&self, state: State, _for_file: Source) -> Context {137		ContextBuilder::new(state).build()138	}139	fn as_any(&self) -> &dyn Any {140		self141	}142}143144/// Dynamically reconfigurable evaluation settings145#[derive(Trace)]146pub struct EvaluationSettings {147	/// Context initializer, which will be used for imports and everything148	/// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`149	pub context_initializer: TraceBox<dyn ContextInitializer>,150	/// Used to resolve file locations/contents151	pub import_resolver: TraceBox<dyn ImportResolver>,152	/// Used in manifestification functions153	pub manifest_format: ManifestFormat,154	/// Used for bindings155	pub trace_format: TraceBox<dyn TraceFormat>,156}157impl Default for EvaluationSettings {158	fn default() -> Self {159		Self {160			context_initializer: tb!(DummyContextInitializer),161			import_resolver: tb!(DummyImportResolver),162		}163	}164}165166#[derive(Trace)]167struct FileData {168	string: Option<IStr>,169	bytes: Option<IBytes>,170	parsed: Option<LocExpr>,171	evaluated: Option<Val>,172173	evaluating: bool,174}175impl FileData {176	fn new_string(data: IStr) -> Self {177		Self {178			string: Some(data),179			bytes: None,180			parsed: None,181			evaluated: None,182			evaluating: false,183		}184	}185	fn new_bytes(data: IBytes) -> Self {186		Self {187			string: None,188			bytes: Some(data),189			parsed: None,190			evaluated: None,191			evaluating: false,192		}193	}194}195196#[derive(Default, Trace)]197pub struct EvaluationStateInternals {198	/// Internal state199	file_cache: RefCell<GcHashMap<SourcePath, FileData>>,200	/// Settings, safe to change at runtime201	settings: RefCell<EvaluationSettings>,202}203204/// Maintains stack trace and import resolution205#[derive(Default, Clone, Trace)]206pub struct State(Cc<EvaluationStateInternals>);207208impl State {209	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise210	pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {211		let mut file_cache = self.file_cache();212		let mut file = file_cache.raw_entry_mut().from_key(&path);213214		let file = match file {215			RawEntryMut::Occupied(ref mut d) => d.get_mut(),216			RawEntryMut::Vacant(v) => {217				let data = self.settings().import_resolver.load_file_contents(&path)?;218				v.insert(219					path.clone(),220					FileData::new_string(221						std::str::from_utf8(&data)222							.map_err(|_| ImportBadFileUtf8(path.clone()))?223							.into(),224					),225				)226				.1227			}228		};229		if let Some(str) = &file.string {230			return Ok(str.clone());231		}232		if file.string.is_none() {233			file.string = Some(234				file.bytes235					.as_ref()236					.expect("either string or bytes should be set")237					.clone()238					.cast_str()239					.ok_or_else(|| ImportBadFileUtf8(path.clone()))?,240			);241		}242		Ok(file.string.as_ref().expect("just set").clone())243	}244	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise245	pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {246		let mut file_cache = self.file_cache();247		let mut file = file_cache.raw_entry_mut().from_key(&path);248249		let file = match file {250			RawEntryMut::Occupied(ref mut d) => d.get_mut(),251			RawEntryMut::Vacant(v) => {252				let data = self.settings().import_resolver.load_file_contents(&path)?;253				v.insert(path.clone(), FileData::new_bytes(data.as_slice().into()))254					.1255			}256		};257		if let Some(str) = &file.bytes {258			return Ok(str.clone());259		}260		if file.bytes.is_none() {261			file.bytes = Some(262				file.string263					.as_ref()264					.expect("either string or bytes should be set")265					.clone()266					.cast_bytes(),267			);268		}269		Ok(file.bytes.as_ref().expect("just set").clone())270	}271	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise272	pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {273		let mut file_cache = self.file_cache();274		let mut file = file_cache.raw_entry_mut().from_key(&path);275276		let file = match file {277			RawEntryMut::Occupied(ref mut d) => d.get_mut(),278			RawEntryMut::Vacant(v) => {279				let data = self.settings().import_resolver.load_file_contents(&path)?;280				v.insert(281					path.clone(),282					FileData::new_string(283						std::str::from_utf8(&data)284							.map_err(|_| ImportBadFileUtf8(path.clone()))?285							.into(),286					),287				)288				.1289			}290		};291		if let Some(val) = &file.evaluated {292			return Ok(val.clone());293		}294		if file.string.is_none() {295			file.string = Some(296				std::str::from_utf8(297					file.bytes298						.as_ref()299						.expect("either string or bytes should be set"),300				)301				.map_err(|_| ImportBadFileUtf8(path.clone()))?302				.into(),303			);304		}305		let code = file.string.as_ref().expect("just set");306		let file_name = Source::new(path.clone(), code.clone());307		if file.parsed.is_none() {308			file.parsed = Some(309				jrsonnet_parser::parse(310					code,311					&ParserSettings {312						file_name: file_name.clone(),313					},314				)315				.map_err(|e| ImportSyntaxError {316					path: file_name.clone(),317					error: Box::new(e),318				})?,319			);320		}321		let parsed = file.parsed.as_ref().expect("just set").clone();322		if file.evaluating {323			throw!(InfiniteRecursionDetected)324		}325		file.evaluating = true;326		// Dropping file cache guard here, as evaluation may use this map too327		drop(file_cache);328		let res = evaluate(self.create_default_context(file_name), &parsed);329330		let mut file_cache = self.file_cache();331		let mut file = file_cache.raw_entry_mut().from_key(&path);332333		let RawEntryMut::Occupied(file) = &mut file else {334			unreachable!("this file was just here!")335		};336		let file = file.get_mut();337		file.evaluating = false;338		match res {339			Ok(v) => {340				file.evaluated = Some(v.clone());341				Ok(v)342			}343			Err(e) => Err(e),344		}345	}346347	/// Has same semantics as `import 'path'` called from `from` file348	pub fn import_from(&self, from: &SourcePath, path: &str) -> Result<Val> {349		let resolved = self.resolve_from(from, path)?;350		self.import_resolved(resolved)351	}352	pub fn import(&self, path: impl AsRef<Path>) -> Result<Val> {353		let resolved = self.resolve(path)?;354		self.import_resolved(resolved)355	}356357	/// Creates context with all passed global variables358	pub fn create_default_context(&self, source: Source) -> Context {359		let context_initializer = &self.settings().context_initializer;360		context_initializer.initialize(self.clone(), source)361	}362363	/// Executes code creating a new stack frame364	pub fn push<T>(365		e: CallLocation<'_>,366		frame_desc: impl FnOnce() -> String,367		f: impl FnOnce() -> Result<T>,368	) -> Result<T> {369		let _guard = check_depth()?;370371		f().with_description_src(e, frame_desc)372	}373374	/// Executes code creating a new stack frame375	pub fn push_val(376		&self,377		e: &ExprLocation,378		frame_desc: impl FnOnce() -> String,379		f: impl FnOnce() -> Result<Val>,380	) -> Result<Val> {381		let _guard = check_depth()?;382383		f().with_description_src(e, frame_desc)384	}385	/// Executes code creating a new stack frame386	pub fn push_description<T>(387		frame_desc: impl FnOnce() -> String,388		f: impl FnOnce() -> Result<T>,389	) -> Result<T> {390		let _guard = check_depth()?;391392		f().with_description(frame_desc)393	}394}395396/// Internals397impl State {398	fn file_cache(&self) -> RefMut<'_, GcHashMap<SourcePath, FileData>> {399		self.0.file_cache.borrow_mut()400	}401	pub fn settings(&self) -> Ref<'_, EvaluationSettings> {402		self.0.settings.borrow()403	}404	pub fn settings_mut(&self) -> RefMut<'_, EvaluationSettings> {405		self.0.settings.borrow_mut()406	}407}408409/// Raw methods evaluate passed values but don't perform TLA execution410impl State {411	/// Parses and evaluates the given snippet412	pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {413		let code = code.into();414		let source = Source::new_virtual(name.into(), code.clone());415		let parsed = jrsonnet_parser::parse(416			&code,417			&ParserSettings {418				file_name: source.clone(),419			},420		)421		.map_err(|e| ImportSyntaxError {422			path: source.clone(),423			error: Box::new(e),424		})?;425		evaluate(self.create_default_context(source), &parsed)426	}427}428429/// Settings utilities430impl State {431	// Only panics in case of [`ImportResolver`] contract violation432	#[allow(clippy::missing_panics_doc)]433	pub fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {434		self.import_resolver().resolve_from(from, path.as_ref())435	}436437	// Only panics in case of [`ImportResolver`] contract violation438	#[allow(clippy::missing_panics_doc)]439	pub fn resolve(&self, path: impl AsRef<Path>) -> Result<SourcePath> {440		self.import_resolver().resolve(path.as_ref())441	}442	pub fn import_resolver(&self) -> Ref<'_, dyn ImportResolver> {443		Ref::map(self.settings(), |s| &*s.import_resolver)444	}445	pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {446		self.settings_mut().import_resolver = TraceBox(resolver);447	}448	pub fn context_initializer(&self) -> Ref<'_, dyn ContextInitializer> {449		Ref::map(self.settings(), |s| &*s.context_initializer)450	}451452	pub fn manifest_format(&self) -> ManifestFormat {453		self.settings().manifest_format.clone()454	}455	pub fn set_manifest_format(&self, format: ManifestFormat) {456		self.settings_mut().manifest_format = format;457	}458459	pub fn trace_format(&self) -> Ref<'_, dyn TraceFormat> {460		Ref::map(self.settings(), |s| &*s.trace_format)461	}462	pub fn set_trace_format(&self, format: impl TraceFormat) {463		self.settings_mut().trace_format = tb!(format);464	}465466	pub fn max_trace(&self) -> usize {467		self.settings().max_trace468	}469	pub fn set_max_trace(&self, trace: usize) {470		self.settings_mut().max_trace = trace;471	}472}
modifiedcrates/jrsonnet-evaluator/src/stdlib/manifest.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/stdlib/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/manifest.rs
@@ -1,6 +1,8 @@
+use std::{borrow::Cow, fmt::Write};
+
 use crate::{
 	error::{Error::*, Result},
-	throw, State, Val,
+	throw, ManifestFormat, State, Val,
 };
 
 #[derive(PartialEq, Eq, Clone, Copy)]
@@ -16,16 +18,88 @@
 	Minify,
 }
 
-pub struct ManifestJsonOptions<'s> {
-	pub padding: &'s str,
-	pub mtype: ManifestType,
-	pub newline: &'s str,
-	pub key_val_sep: &'s str,
+pub struct JsonFormat<'s> {
+	padding: Cow<'s, str>,
+	mtype: ManifestType,
+	newline: &'s str,
+	key_val_sep: &'s str,
 	#[cfg(feature = "exp-preserve-order")]
-	pub preserve_order: bool,
+	preserve_order: bool,
 }
 
-pub fn manifest_json_ex(val: &Val, options: &ManifestJsonOptions<'_>) -> Result<String> {
+impl<'s> JsonFormat<'s> {
+	// Minifying format
+	pub fn minify(#[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> Self {
+		Self {
+			padding: Cow::Borrowed(""),
+			mtype: ManifestType::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: ManifestType::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: ManifestType::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: ManifestType::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: ManifestType::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)
@@ -34,9 +108,8 @@
 	val: &Val,
 	buf: &mut String,
 	cur_padding: &mut String,
-	options: &ManifestJsonOptions<'_>,
+	options: &JsonFormat<'_>,
 ) -> Result<()> {
-	use std::fmt::Write;
 	let mtype = options.mtype;
 	match val {
 		Val::Bool(v) => {
@@ -57,7 +130,7 @@
 				}
 
 				let old_len = cur_padding.len();
-				cur_padding.push_str(options.padding);
+				cur_padding.push_str(&options.padding);
 				for (i, item) in items.iter().enumerate() {
 					if i != 0 {
 						buf.push(',');
@@ -97,7 +170,7 @@
 				}
 
 				let old_len = cur_padding.len();
-				cur_padding.push_str(options.padding);
+				cur_padding.push_str(&options.padding);
 				for (i, field) in fields.into_iter().enumerate() {
 					if i != 0 {
 						buf.push(',');
@@ -138,6 +211,48 @@
 	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);
@@ -145,7 +260,6 @@
 }
 
 fn escape_string_json_buf(s: &str, buf: &mut String) {
-	use std::fmt::Write;
 	buf.push('"');
 	for c in s.chars() {
 		match c {
@@ -165,33 +279,66 @@
 	buf.push('"');
 }
 
-pub struct ManifestYamlOptions<'s> {
+pub struct YamlFormat<'s> {
 	/// Padding before fields, i.e
 	/// ```yaml
 	/// a:
 	///   b:
 	/// ## <- this
 	/// ```
-	pub padding: &'s str,
+	padding: Cow<'s, str>,
 	/// Padding before array elements in objects
 	/// ```yaml
 	/// a:
 	///   - 1
 	/// ## <- this
 	/// ```
-	pub arr_element_padding: &'s str,
+	arr_element_padding: Cow<'s, str>,
 	/// Should yaml keys appear unescaped, when possible
 	/// ```yaml
 	/// "safe_key": 1
 	/// # vs
 	/// safe_key: 1
 	/// ```
-	pub quote_keys: bool,
+	quote_keys: bool,
 	/// If true - then order of fields is preserved as written,
 	/// instead of sorting alphabetically
 	#[cfg(feature = "exp-preserve-order")]
-	pub preserve_order: bool,
+	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
@@ -221,7 +368,7 @@
 		|| string.parse::<f64>().is_ok()
 }
 
-pub fn manifest_yaml_ex(val: &Val, options: &ManifestYamlOptions<'_>) -> Result<String> {
+pub 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)
@@ -232,9 +379,8 @@
 	val: &Val,
 	buf: &mut String,
 	cur_padding: &mut String,
-	options: &ManifestYamlOptions<'_>,
+	options: &YamlFormat<'_>,
 ) -> Result<()> {
-	use std::fmt::Write;
 	match val {
 		Val::Bool(v) => {
 			if *v {
@@ -252,7 +398,7 @@
 				for line in s.split('\n') {
 					buf.push('\n');
 					buf.push_str(cur_padding);
-					buf.push_str(options.padding);
+					buf.push_str(&options.padding);
 					buf.push_str(line);
 				}
 			} else if !options.quote_keys && !yaml_needs_quotes(s) {
@@ -277,7 +423,7 @@
 						Val::Arr(a) if !a.is_empty() => {
 							buf.push('\n');
 							buf.push_str(cur_padding);
-							buf.push_str(options.padding);
+							buf.push_str(&options.padding);
 						}
 						_ => buf.push(' '),
 					}
@@ -288,7 +434,7 @@
 					};
 					let prev_len = cur_padding.len();
 					if extra_padding {
-						cur_padding.push_str(options.padding);
+						cur_padding.push_str(&options.padding);
 					}
 					manifest_yaml_ex_buf(&item, buf, cur_padding, options)?;
 					cur_padding.truncate(prev_len);
@@ -323,14 +469,14 @@
 						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);
+							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_str(&options.padding);
+							cur_padding.push_str(&options.padding);
 						}
 						_ => buf.push(' '),
 					}
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -1,4 +1,4 @@
-use std::{cell::RefCell, fmt::Debug, rc::Rc};
+use std::{cell::RefCell, fmt::Debug};
 
 use jrsonnet_gcmodule::{Cc, Trace};
 use jrsonnet_interner::{IBytes, IStr};
@@ -8,9 +8,6 @@
 	error::{Error::*, LocError},
 	function::FuncVal,
 	gc::{GcHashMap, TraceBox},
-	stdlib::manifest::{
-		manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType, ManifestYamlOptions,
-	},
 	throw,
 	typed::BoundedUsize,
 	ObjValue, Result, Unbound, WeakObjValue,
@@ -122,34 +119,32 @@
 	}
 }
 
-#[derive(Clone, Trace)]
-pub enum ManifestFormat {
-	YamlStream(Box<ManifestFormat>),
-	Yaml {
-		padding: usize,
-		#[cfg(feature = "exp-preserve-order")]
-		preserve_order: bool,
-	},
-	Json {
-		padding: usize,
-		#[cfg(feature = "exp-preserve-order")]
-		preserve_order: bool,
-	},
-	ToString,
-	String,
+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 ManifestFormat {
-	#[cfg(feature = "exp-preserve-order")]
-	fn preserve_order(&self) -> bool {
-		match self {
-			ManifestFormat::YamlStream(s) => s.preserve_order(),
-			ManifestFormat::Yaml { preserve_order, .. } => *preserve_order,
-			ManifestFormat::Json { preserve_order, .. } => *preserve_order,
-			ManifestFormat::ToString => false,
-			ManifestFormat::String => false,
-		}
+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(Debug, Clone, Trace)]
 pub struct Slice {
@@ -641,172 +636,25 @@
 		}
 	}
 
+	pub fn manifest(&self, format: impl ManifestFormat) -> Result<String> {
+		fn manifest_dyn(val: &Val, manifest: &dyn ManifestFormat) -> Result<String> {
+			manifest.manifest(val.clone())
+		}
+		manifest_dyn(self, &format)
+	}
+
 	pub fn to_string(&self) -> Result<IStr> {
 		Ok(match self {
 			Self::Bool(true) => "true".into(),
 			Self::Bool(false) => "false".into(),
 			Self::Null => "null".into(),
 			Self::Str(s) => s.clone(),
-			v => manifest_json_ex(
-				v,
-				&ManifestJsonOptions {
-					padding: "",
-					mtype: ManifestType::ToString,
-					newline: "\n",
-					key_val_sep: ": ",
-					#[cfg(feature = "exp-preserve-order")]
-					preserve_order: false,
-				},
-			)?
-			.into(),
-		})
-	}
-
-	/// Expects value to be object, outputs (key, manifested value) pairs
-	pub fn manifest_multi(&self, ty: &ManifestFormat) -> Result<Vec<(IStr, IStr)>> {
-		let Self::Obj(obj) = self else {
-			throw!(MultiManifestOutputIsNotAObject);
-		};
-		let keys = obj.fields(
-			#[cfg(feature = "exp-preserve-order")]
-			ty.preserve_order(),
-		);
-		let mut out = Vec::with_capacity(keys.len());
-		for key in keys {
-			let value = obj
-				.get(key.clone())?
-				.expect("item in object")
-				.manifest(ty)?;
-			out.push((key, value));
-		}
-		Ok(out)
-	}
-
-	/// Expects value to be array, outputs manifested values
-	pub fn manifest_stream(&self, ty: &ManifestFormat) -> Result<Vec<IStr>> {
-		let Self::Arr(arr) = self else {
-			throw!(StreamManifestOutputIsNotAArray);
-		};
-		let mut out = Vec::with_capacity(arr.len());
-		for i in arr.iter() {
-			out.push(i?.manifest(ty)?);
-		}
-		Ok(out)
-	}
-
-	pub fn manifest(&self, ty: &ManifestFormat) -> Result<IStr> {
-		Ok(match ty {
-			ManifestFormat::YamlStream(format) => {
-				let Self::Arr(arr) = self else {
-					throw!(StreamManifestOutputIsNotAArray)
-				};
-				let mut out = String::new();
-
-				match format as &ManifestFormat {
-					ManifestFormat::YamlStream(_) => throw!(StreamManifestOutputCannotBeRecursed),
-					ManifestFormat::String => throw!(StreamManifestCannotNestString),
-					_ => {}
-				};
-
-				if !arr.is_empty() {
-					for v in arr.iter() {
-						out.push_str("---\n");
-						out.push_str(&v?.manifest(format)?);
-						out.push('\n');
-					}
-					out.push_str("...");
-				}
-
-				out.into()
-			}
-			ManifestFormat::Yaml {
-				padding,
-				#[cfg(feature = "exp-preserve-order")]
-				preserve_order,
-			} => self.to_yaml(
-				*padding,
-				#[cfg(feature = "exp-preserve-order")]
-				*preserve_order,
-			)?,
-			ManifestFormat::Json {
-				padding,
-				#[cfg(feature = "exp-preserve-order")]
-				preserve_order,
-			} => self.to_json(
-				*padding,
-				#[cfg(feature = "exp-preserve-order")]
-				*preserve_order,
-			)?,
-			ManifestFormat::ToString => self.to_string()?,
-			ManifestFormat::String => match self {
-				Self::Str(s) => s.clone(),
-				_ => throw!(StringManifestOutputIsNotAString),
-			},
+			_ => self
+				.manifest(crate::stdlib::manifest::ToStringFormat)
+				.map(IStr::from)?,
 		})
-	}
-
-	/// For manifestification
-	pub fn to_json(
-		&self,
-		padding: usize,
-		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,
-	) -> Result<IStr> {
-		manifest_json_ex(
-			self,
-			&ManifestJsonOptions {
-				padding: &" ".repeat(padding),
-				mtype: if padding == 0 {
-					ManifestType::Minify
-				} else {
-					ManifestType::Manifest
-				},
-				newline: "\n",
-				key_val_sep: ": ",
-				#[cfg(feature = "exp-preserve-order")]
-				preserve_order,
-			},
-		)
-		.map(Into::into)
 	}
 
-	/// Calls `std.manifestJson`
-	pub fn to_std_json(
-		&self,
-		padding: usize,
-		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,
-	) -> Result<Rc<str>> {
-		manifest_json_ex(
-			self,
-			&ManifestJsonOptions {
-				padding: &" ".repeat(padding),
-				mtype: ManifestType::Std,
-				newline: "\n",
-				key_val_sep: ": ",
-				#[cfg(feature = "exp-preserve-order")]
-				preserve_order,
-			},
-		)
-		.map(Into::into)
-	}
-
-	pub fn to_yaml(
-		&self,
-		padding: usize,
-		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,
-	) -> Result<IStr> {
-		let padding = &" ".repeat(padding);
-		manifest_yaml_ex(
-			self,
-			&ManifestYamlOptions {
-				padding,
-				arr_element_padding: padding,
-				quote_keys: false,
-				#[cfg(feature = "exp-preserve-order")]
-				preserve_order,
-			},
-		)
-		.map(Into::into)
-	}
 	pub fn into_indexable(self) -> Result<IndexableVal> {
 		Ok(match self {
 			Val::Str(s) => IndexableVal::Str(s),
modifiedcrates/jrsonnet-stdlib/src/manifest.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/manifest.rs
+++ b/crates/jrsonnet-stdlib/src/manifest.rs
@@ -1,10 +1,7 @@
 use jrsonnet_evaluator::{
 	error::Result,
 	function::builtin,
-	stdlib::manifest::{
-		escape_string_json, manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType,
-		ManifestYamlOptions,
-	},
+	stdlib::manifest::{escape_string_json, JsonFormat, YamlFormat},
 	typed::Any,
 	IStr,
 };
@@ -24,17 +21,13 @@
 ) -> 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,
-			#[cfg(feature = "exp-preserve-order")]
-			preserve_order: preserve_order.unwrap_or(false),
-		},
-	)
+	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]
@@ -44,18 +37,10 @@
 	quote_keys: Option<bool>,
 	#[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,
 ) -> Result<String> {
-	manifest_yaml_ex(
-		&value.0,
-		&ManifestYamlOptions {
-			padding: "  ",
-			arr_element_padding: if indent_array_in_object.unwrap_or(false) {
-				"  "
-			} else {
-				""
-			},
-			quote_keys: quote_keys.unwrap_or(true),
-			#[cfg(feature = "exp-preserve-order")]
-			preserve_order: preserve_order.unwrap_or(false),
-		},
-	)
+	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),
+	))
 }