git.delta.rocks / jrsonnet / refs/commits / 38c2f665c6fe

difftreelog

source

crates/jrsonnet-stdlib/src/parse.rs1.1 KiBsourcehistory
1use jrsonnet_evaluator::{IStr, Result, Val, function::builtin, runtime_error};2use serde_saphyr::options;34#[builtin]5pub fn builtin_parse_json(str: IStr) -> Result<Val> {6	let value: Val =7		serde_json::from_str(&str).map_err(|e| runtime_error!("failed to parse json: {e}"))?;8	Ok(value)9}1011#[builtin]12pub fn builtin_parse_yaml(str: IStr) -> Result<Val> {13	let needs_synthetic_null = str.trim_end().ends_with("\n---");1415	let mut out = serde_saphyr::from_multiple_with_options::<Val>(16		&str,17		options! {18			// Golang/C++ compat19			legacy_octal_numbers: true,20			// Disable budget limits - we trust the YAML input21			budget: None,22		},23	)24	.map_err(|e| runtime_error!("failed to parse yaml: {e}"))?;2526	// saphyr and other yaml implementations disagree on how to handle an empty document in multi-document stream.27	// Saphyr only considers document started after anything is emitted after the document delimiter28	if needs_synthetic_null {29		out.push(Val::Null);30	}3132	Ok(if out.is_empty() {33		Val::Null34	} else if out.len() == 1 {35		out.into_iter().next().unwrap()36	} else {37		Val::Arr(out.into())38	})39}