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

difftreelog

source

crates/jrsonnet-stdlib/src/manifest/mod.rs3.8 KiBsourcehistory
1mod python;2mod toml;3mod xml;4mod yaml;56use jrsonnet_evaluator::{7	function::builtin,8	manifest::{escape_string_json, JsonFormat, YamlStreamFormat},9	IStr, ObjValue, Result, Val,10};11pub use python::{PythonFormat, PythonVarsFormat};12pub use toml::TomlFormat;13pub use xml::XmlJsonmlFormat;14pub use yaml::YamlFormat;1516#[builtin]17pub fn builtin_escape_string_json(str_: IStr) -> Result<String> {18	Ok(escape_string_json(&str_))19}2021#[builtin]22pub fn builtin_escape_string_python(str: IStr) -> Result<String> {23	Ok(escape_string_json(&str))24}2526#[builtin]27pub fn builtin_manifest_json_ex(28	value: Val,29	indent: String,30	newline: Option<IStr>,31	key_val_sep: Option<IStr>,3233	#[default(false)]34	#[cfg(feature = "exp-preserve-order")]35	preserve_order: bool,36) -> Result<String> {37	let newline = newline.as_deref().unwrap_or("\n");38	let key_val_sep = key_val_sep.as_deref().unwrap_or(": ");39	value.manifest(JsonFormat::std_to_json(40		indent,41		newline,42		key_val_sep,43		#[cfg(feature = "exp-preserve-order")]44		preserve_order,45	))46}4748#[builtin]49pub fn builtin_manifest_json(50	value: Val,5152	#[default(false)]53	#[cfg(feature = "exp-preserve-order")]54	preserve_order: bool,55) -> Result<String> {56	builtin_manifest_json_ex(57		value,58		"    ".to_owned(),59		None,60		None,61		#[cfg(feature = "exp-preserve-order")]62		preserve_order,63	)64}6566#[builtin]67pub fn builtin_manifest_json_minified(68	value: Val,6970	#[default(false)]71	#[cfg(feature = "exp-preserve-order")]72	preserve_order: bool,73) -> Result<String> {74	value.manifest(JsonFormat::minify(75		#[cfg(feature = "exp-preserve-order")]76		preserve_order,77	))78}7980#[builtin]81pub fn builtin_manifest_yaml_doc(82	value: Val,83	#[default(false)] indent_array_in_object: bool,84	#[default(true)] quote_keys: bool,8586	#[default(false)]87	#[cfg(feature = "exp-preserve-order")]88	preserve_order: bool,89) -> Result<String> {90	value.manifest(YamlFormat::std_to_yaml(91		indent_array_in_object,92		quote_keys,93		#[cfg(feature = "exp-preserve-order")]94		preserve_order,95	))96}9798#[builtin]99pub fn builtin_manifest_yaml_stream(100	value: Val,101	#[default(false)] indent_array_in_object: bool,102	#[default(true)] c_document_end: bool,103	#[default(true)] quote_keys: bool,104105	#[default(false)]106	#[cfg(feature = "exp-preserve-order")]107	preserve_order: bool,108) -> Result<String> {109	value.manifest(YamlStreamFormat::std_yaml_stream(110		YamlFormat::std_to_yaml(111			indent_array_in_object,112			quote_keys,113			#[cfg(feature = "exp-preserve-order")]114			preserve_order,115		),116		c_document_end,117	))118}119120#[builtin]121pub fn builtin_manifest_toml_ex(122	value: ObjValue,123	indent: String,124125	#[default(false)]126	#[cfg(feature = "exp-preserve-order")]127	preserve_order: bool,128) -> Result<String> {129	Val::Obj(value).manifest(TomlFormat::std_to_toml(130		indent,131		#[cfg(feature = "exp-preserve-order")]132		preserve_order,133	))134}135136#[builtin]137pub fn builtin_manifest_toml(138	value: ObjValue,139140	#[default(false)]141	#[cfg(feature = "exp-preserve-order")]142	preserve_order: bool,143) -> Result<String> {144	builtin_manifest_toml_ex(145		value,146		"  ".to_owned(),147		#[cfg(feature = "exp-preserve-order")]148		preserve_order,149	)150}151152#[builtin]153pub fn builtin_to_string(a: Val) -> Result<IStr> {154	a.to_string()155}156157#[builtin]158pub fn builtin_manifest_python(159	v: Val,160161	#[default(false)]162	#[cfg(feature = "exp-preserve-order")]163	preserve_order: bool,164) -> Result<String> {165	v.manifest(PythonFormat::std(166		#[cfg(feature = "exp-preserve-order")]167		preserve_order,168	))169}170#[builtin]171pub fn builtin_manifest_python_vars(172	conf: Val,173174	#[default(false)]175	#[cfg(feature = "exp-preserve-order")]176	preserve_order: bool,177) -> Result<String> {178	conf.manifest(PythonVarsFormat::std(179		#[cfg(feature = "exp-preserve-order")]180		preserve_order,181	))182}183184#[builtin]185pub fn builtin_escape_string_xml(str_: String) -> String {186	xml::escape_string_xml(str_.as_str())187}188189#[builtin]190pub fn builtin_manifest_xml_jsonml(value: Val) -> Result<String> {191	value.manifest(XmlJsonmlFormat::std_to_xml())192}