git.delta.rocks / jrsonnet / refs/commits / 59f19ba582ad

difftreelog

source

crates/jrsonnet-stdlib/src/parse.rs1.1 KiBsourcehistory
1use jrsonnet_evaluator::{IStr, Result, Val, error, function::builtin};2use serde_saphyr::options;34#[builtin]5pub fn builtin_parse_json(str: IStr) -> Result<Val> {6	let value: Val = serde_json::from_str(&str).map_err(|e| error!("failed to parse json: {e}"))?;7	Ok(value)8}910#[builtin]11pub fn builtin_parse_yaml(str: IStr) -> Result<Val> {12	let needs_synthetic_null = str.trim_end().ends_with("\n---");1314	let mut out = serde_saphyr::from_multiple_with_options::<Val>(15		&str,16		options! {17			// Golang/C++ compat18			legacy_octal_numbers: true,19			// Disable budget limits - we trust the YAML input20			budget: None,21		},22	)23	.map_err(|e| error!("failed to parse yaml: {e}"))?;2425	// saphyr and other yaml implementations disagree on how to handle an empty document in multi-document stream.26	// Saphyr only considers document started after anything is emitted after the document delimiter27	if needs_synthetic_null {28		out.push(Val::Null);29	}3031	Ok(if out.is_empty() {32		Val::Null33	} else if out.len() == 1 {34		out.into_iter().next().unwrap()35	} else {36		Val::Arr(out.into())37	})38}