1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
mod toml;
mod yaml;

use jrsonnet_evaluator::{
	error::Result,
	function::builtin,
	manifest::{escape_string_json, JsonFormat},
	IStr, ObjValue, Val,
};
pub use toml::TomlFormat;
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: Val,
	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.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: Val,
	indent_array_in_object: Option<bool>,
	quote_keys: Option<bool>,
	#[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,
) -> Result<String> {
	value.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),
	))
}

#[builtin]
pub fn builtin_manifest_toml_ex(
	value: ObjValue,
	indent: IStr,
	#[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,
) -> Result<String> {
	Val::Obj(value).manifest(TomlFormat::std_to_toml(
		indent.to_string(),
		#[cfg(feature = "exp-preserve-order")]
		preserve_order.unwrap_or(false),
	))
}