From 0dad2a64ea4be027f26c0e5f6460ca47a51fe7c8 Mon Sep 17 00:00:00 2001 From: Лач Date: Thu, 16 Jul 2020 21:52:29 +0000 Subject: [PATCH] feat: into_yaml conversion --- --- a/crates/jrsonnet-evaluator/src/val.rs +++ b/crates/jrsonnet-evaluator/src/val.rs @@ -1,9 +1,9 @@ use crate::{ create_error_result, evaluate, function::{parse_function_call, place_args}, - Context, Error, ObjValue, Result, + with_state, Context, Error, ObjValue, Result, }; -use jrsonnet_parser::{ArgsDesc, LocExpr, ParamsDesc}; +use jrsonnet_parser::{el, Arg, ArgsDesc, Expr, LocExpr, ParamsDesc}; use std::{ cell::RefCell, fmt::{Debug, Display}, @@ -210,6 +210,28 @@ .try_cast_str("to json")?) }) } + pub fn into_yaml(self, padding: usize) -> Result> { + with_state(|s| { + let ctx = s + .create_default_context()? + .with_var("__tmp__to_json__".into(), self)?; + Ok(evaluate( + ctx, + &el!(Expr::Apply( + el!(Expr::Index( + el!(Expr::Var("std".into())), + el!(Expr::Str("manifestYamlDoc".into())) + )), + ArgsDesc(vec![ + Arg(None, el!(Expr::Var("__tmp__to_json__".into()))), + Arg(None, el!(Expr::Str(" ".repeat(padding).into()))) + ]), + false + )), + )? + .try_cast_str("to json")?) + }) + } } fn is_function_like(val: &Val) -> bool { -- gitstuff