git.delta.rocks / jrsonnet / refs/commits / 68bea05caa11

difftreelog

source

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