1use crate::{2 create_error_result, evaluate,3 function::{parse_function_call, place_args},4 Context, Error, ObjValue, Result,5};6use jsonnet_parser::{ArgsDesc, LocExpr, ParamsDesc};7use std::{8 cell::RefCell,9 fmt::{Debug, Display},10 rc::Rc,11};1213enum LazyValInternals {14 Computed(Val),15 Waiting(Box<dyn Fn() -> Result<Val>>),16}17#[derive(Clone)]18pub struct LazyVal(Rc<RefCell<LazyValInternals>>);19impl LazyVal {20 pub fn new(f: Box<dyn Fn() -> Result<Val>>) -> Self {21 LazyVal(Rc::new(RefCell::new(LazyValInternals::Waiting(f))))22 }23 pub fn new_resolved(val: Val) -> Self {24 LazyVal(Rc::new(RefCell::new(LazyValInternals::Computed(val))))25 }26 pub fn evaluate(&self) -> Result<Val> {27 let new_value = match &*self.0.borrow() {28 LazyValInternals::Computed(v) => return Ok(v.clone()),29 LazyValInternals::Waiting(f) => f()?,30 };31 *self.0.borrow_mut() = LazyValInternals::Computed(new_value.clone());32 Ok(new_value)33 }34}3536#[macro_export]37macro_rules! lazy_val {38 ($f: expr) => {39 $crate::LazyVal::new(Box::new($f))40 };41}42#[macro_export]43macro_rules! resolved_lazy_val {44 ($f: expr) => {45 $crate::LazyVal::new_resolved($f)46 };47}48impl Debug for LazyVal {49 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {50 write!(f, "Lazy")51 }52}53impl PartialEq for LazyVal {54 fn eq(&self, other: &Self) -> bool {55 Rc::ptr_eq(&self.0, &other.0)56 }57}5859#[derive(Debug, PartialEq, Clone)]60pub struct FuncDesc {61 pub ctx: Context,62 pub params: ParamsDesc,63 pub body: LocExpr,64}65impl FuncDesc {66 67 pub fn evaluate(&self, call_ctx: Context, args: &ArgsDesc, tailstrict: bool) -> Result<Val> {68 let ctx = parse_function_call(69 call_ctx,70 Some(self.ctx.clone()),71 &self.params,72 args,73 tailstrict,74 )?;75 evaluate(ctx, &self.body)76 }7778 pub fn evaluate_values(&self, call_ctx: Context, args: &[Val]) -> Result<Val> {79 let ctx = place_args(call_ctx, Some(self.ctx.clone()), &self.params, args)?;80 evaluate(ctx, &self.body)81 }82}8384#[derive(Debug, Clone, Copy, PartialEq)]85pub enum ValType {86 Bool,87 Null,88 Str,89 Num,90 Arr,91 Obj,92 Func,93}94impl ValType {95 pub fn name(&self) -> &'static str {96 use ValType::*;97 match self {98 Bool => "boolean",99 Null => "null",100 Str => "string",101 Num => "number",102 Arr => "array",103 Obj => "object",104 Func => "function",105 }106 }107}108impl Display for ValType {109 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {110 write!(f, "{}", self.name())111 }112}113114#[derive(Debug, PartialEq, Clone)]115pub enum Val {116 Bool(bool),117 Null,118 Str(Rc<str>),119 Num(f64),120 Lazy(LazyVal),121 Arr(Rc<Vec<Val>>),122 Obj(ObjValue),123 Func(FuncDesc),124125 126 Intristic(Rc<str>, Rc<str>),127}128macro_rules! matches_unwrap {129 ($e: expr, $p: pat, $r: expr) => {130 match $e {131 $p => $r,132 _ => panic!("no match"),133 }134 };135}136impl Val {137 pub fn assert_type(&self, context: &'static str, val_type: ValType) -> Result<()> {138 let this_type = self.value_type()?;139 if this_type != val_type {140 create_error_result(Error::TypeMismatch(context, vec![val_type], this_type))141 } else {142 Ok(())143 }144 }145 pub fn try_cast_bool(self, context: &'static str) -> Result<bool> {146 self.assert_type(context, ValType::Bool)?;147 Ok(matches_unwrap!(self.unwrap_if_lazy()?, Val::Bool(v), v))148 }149 pub fn try_cast_str(self, context: &'static str) -> Result<Rc<str>> {150 self.assert_type(context, ValType::Str)?;151 Ok(matches_unwrap!(self.unwrap_if_lazy()?, Val::Str(v), v))152 }153 pub fn try_cast_num(self, context: &'static str) -> Result<f64> {154 self.assert_type(context, ValType::Num)?;155 Ok(matches_unwrap!(self.unwrap_if_lazy()?, Val::Num(v), v))156 }157 pub fn unwrap_if_lazy(&self) -> Result<Self> {158 Ok(if let Val::Lazy(v) = self {159 v.evaluate()?.unwrap_if_lazy()?160 } else {161 self.clone()162 })163 }164 pub fn value_type(&self) -> Result<ValType> {165 Ok(match self {166 Val::Str(..) => ValType::Str,167 Val::Num(..) => ValType::Num,168 Val::Arr(..) => ValType::Arr,169 Val::Obj(..) => ValType::Obj,170 Val::Func(..) => ValType::Func,171 Val::Bool(_) => ValType::Bool,172 Val::Null => ValType::Null,173 Val::Intristic(_, _) => ValType::Func,174 Val::Lazy(_) => self.clone().unwrap_if_lazy()?.value_type()?,175 })176 }177 #[cfg(feature = "faster")]178 pub fn into_json(self, padding: usize) -> Result<Rc<str>> {179 manifest_json_ex(&self, &" ".repeat(padding)).map(|s| s.into())180 }181 #[cfg(not(feature = "faster"))]182 pub fn into_json(self, padding: usize) -> Result<Rc<str>> {183 with_state(|s| {184 let ctx = s185 .create_default_context()?186 .with_var("__tmp__to_json__".into(), self)?;187 Ok(evaluate(188 ctx,189 &el!(Expr::Apply(190 el!(Expr::Index(191 el!(Expr::Var("std".into())),192 el!(Expr::Str("manifestJsonEx".into()))193 )),194 ArgsDesc(vec![195 Arg(None, el!(Expr::Var("__tmp__to_json__".into()))),196 Arg(None, el!(Expr::Str(" ".repeat(padding).into())))197 ]),198 false199 )),200 )?201 .try_cast_str("to json")?)202 })203 }204}205206pub fn manifest_json_ex(val: &Val, padding: &str) -> Result<String> {207 let mut out = String::new();208 manifest_json_ex_buf(val, &mut out, padding, &mut String::new())?;209 Ok(out)210}211fn manifest_json_ex_buf(212 val: &Val,213 buf: &mut String,214 padding: &str,215 cur_padding: &mut String,216) -> Result<()> {217 use std::fmt::Write;218 match val.unwrap_if_lazy()? {219 Val::Bool(v) => {220 if v {221 buf.push_str("true");222 } else {223 buf.push_str("false");224 }225 }226 Val::Null => buf.push_str("null"),227 Val::Str(s) => buf.push_str(&escape_string_json(&s)),228 Val::Num(n) => write!(buf, "{}", n).unwrap(),229 Val::Arr(items) => {230 buf.push_str("[\n");231 if !items.is_empty() {232 let old_len = cur_padding.len();233 cur_padding.push_str(padding);234 for (i, item) in items.iter().enumerate() {235 if i != 0 {236 buf.push_str(",\n")237 }238 buf.push_str(cur_padding);239 manifest_json_ex_buf(item, buf, padding, cur_padding)?;240 }241 cur_padding.truncate(old_len);242 }243 buf.push('\n');244 buf.push_str(cur_padding);245 buf.push(']');246 }247 Val::Obj(obj) => {248 buf.push_str("{\n");249 let fields = obj.visible_fields();250 if !fields.is_empty() {251 let old_len = cur_padding.len();252 cur_padding.push_str(padding);253 for (i, field) in fields.into_iter().enumerate() {254 if i != 0 {255 buf.push_str(",\n")256 }257 buf.push_str(cur_padding);258 buf.push_str(&escape_string_json(&field));259 buf.push_str(": ");260 manifest_json_ex_buf(&obj.get(field)?.unwrap(), buf, padding, cur_padding)?;261 }262 cur_padding.truncate(old_len);263 }264 buf.push('\n');265 buf.push_str(cur_padding);266 buf.push('}');267 }268 Val::Func(_) | Val::Intristic(_, _) => panic!("tried to manifest function"),269 Val::Lazy(_) => unreachable!(),270 };271 Ok(())272}273pub fn escape_string_json(s: &str) -> String {274 use std::fmt::Write;275 let mut out = String::new();276 out.push('"');277 for c in s.chars() {278 match c {279 '"' => out.push_str("\\\""),280 '\\' => out.push_str("\\\\"),281 '\u{0008}' => out.push_str("\\b"),282 '\u{000c}' => out.push_str("\\f"),283 '\n' => out.push_str("\\n"),284 '\r' => out.push_str("\\r"),285 '\t' => out.push_str("\\t"),286 c if c < 32 as char || (c >= 127 as char && c <= 159 as char) => {287 write!(out, "\\u{:04x}", c as u32).unwrap()288 }289 c => out.push(c),290 }291 }292 out.push('"');293 out294}295296#[test]297fn json_test() {298 assert_eq!(escape_string_json("\u{001f}"), "\"\\u001f\"")299}