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

difftreelog

source

crates/jrsonnet-stdlib/src/parse.rs886 Bsourcehistory
1use jrsonnet_evaluator::{2	error::{ErrorKind::RuntimeError, Result},3	function::builtin,4	IStr, Val,5};6use serde::Deserialize;78#[builtin]9pub fn builtin_parse_json(str: IStr) -> Result<Val> {10	let value: Val = serde_json::from_str(&str)11		.map_err(|e| RuntimeError(format!("failed to parse json: {e}").into()))?;12	Ok(value)13}1415#[builtin]16pub fn builtin_parse_yaml(str: IStr) -> Result<Val> {17	use serde_yaml_with_quirks::DeserializingQuirks;18	let value = serde_yaml_with_quirks::Deserializer::from_str_with_quirks(19		&str,20		DeserializingQuirks { old_octals: true },21	);22	let mut out = vec![];23	for item in value {24		let val = Val::deserialize(item)25			.map_err(|e| RuntimeError(format!("failed to parse yaml: {e}").into()))?;26		out.push(val);27	}28	Ok(if out.is_empty() {29		Val::Null30	} else if out.len() == 1 {31		out.into_iter().next().unwrap()32	} else {33		Val::Arr(out.into())34	})35}