difftreelog
fix run assertions while manifesting empty object
in: master
1 file changed
crates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth1use crate::error::Error::*;2use crate::error::Result;3use crate::{throw, Val};45#[derive(PartialEq, Clone, Copy)]6pub enum ManifestType {7 // Applied in manifestification8 Manifest,9 /// Used for std.manifestJson10 /// Empty array/objects extends to "[\n\n]" instead of "[ ]" as in manifest11 Std,12 /// No line breaks, used in `obj+''`13 ToString,14 /// Minified json15 Minify,16}1718pub struct ManifestJsonOptions<'s> {19 pub padding: &'s str,20 pub mtype: ManifestType,21}2223pub fn manifest_json_ex(val: &Val, options: &ManifestJsonOptions<'_>) -> Result<String> {24 let mut out = String::new();25 manifest_json_ex_buf(val, &mut out, &mut String::new(), options)?;26 Ok(out)27}28fn manifest_json_ex_buf(29 val: &Val,30 buf: &mut String,31 cur_padding: &mut String,32 options: &ManifestJsonOptions<'_>,33) -> Result<()> {34 use std::fmt::Write;35 let mtype = options.mtype;36 match val {37 Val::Bool(v) => {38 if *v {39 buf.push_str("true");40 } else {41 buf.push_str("false");42 }43 }44 Val::Null => buf.push_str("null"),45 Val::Str(s) => buf.push_str(&escape_string_json(s)),46 Val::Num(n) => write!(buf, "{}", n).unwrap(),47 Val::Arr(items) => {48 buf.push('[');49 if !items.is_empty() {50 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {51 buf.push('\n');52 }5354 let old_len = cur_padding.len();55 cur_padding.push_str(options.padding);56 for (i, item) in items.iter().enumerate() {57 if i != 0 {58 buf.push(',');59 if mtype == ManifestType::ToString {60 buf.push(' ');61 } else if mtype != ManifestType::Minify {62 buf.push('\n');63 }64 }65 buf.push_str(cur_padding);66 manifest_json_ex_buf(&item?, buf, cur_padding, options)?;67 }68 cur_padding.truncate(old_len);6970 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {71 buf.push('\n');72 buf.push_str(cur_padding);73 }74 } else if mtype == ManifestType::Std {75 buf.push_str("\n\n");76 buf.push_str(cur_padding);77 } else if mtype == ManifestType::ToString || mtype == ManifestType::Manifest {78 buf.push(' ');79 }80 buf.push(']');81 }82 Val::Obj(obj) => {83 buf.push('{');84 let fields = obj.fields();85 if !fields.is_empty() {86 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {87 buf.push('\n');88 }8990 let old_len = cur_padding.len();91 cur_padding.push_str(options.padding);92 for (i, field) in fields.into_iter().enumerate() {93 if i != 0 {94 buf.push(',');95 if mtype == ManifestType::ToString {96 buf.push(' ');97 } else if mtype != ManifestType::Minify {98 buf.push('\n');99 }100 }101 buf.push_str(cur_padding);102 buf.push_str(&escape_string_json(&field));103 buf.push_str(": ");104 crate::push(105 None,106 || format!("field <{}> manifestification", field.clone()),107 || {108 let value = obj.get(field.clone())?.unwrap();109 manifest_json_ex_buf(&value, buf, cur_padding, options)110 },111 )?;112 }113 cur_padding.truncate(old_len);114115 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {116 buf.push('\n');117 buf.push_str(cur_padding);118 }119 } else if mtype == ManifestType::Std {120 buf.push_str("\n\n");121 buf.push_str(cur_padding);122 } else if mtype == ManifestType::ToString || mtype == ManifestType::Manifest {123 buf.push(' ');124 }125 buf.push('}');126 }127 Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),128 };129 Ok(())130}131pub fn escape_string_json(s: &str) -> String {132 use std::fmt::Write;133 let mut out = String::new();134 out.push('"');135 for c in s.chars() {136 match c {137 '"' => out.push_str("\\\""),138 '\\' => out.push_str("\\\\"),139 '\u{0008}' => out.push_str("\\b"),140 '\u{000c}' => out.push_str("\\f"),141 '\n' => out.push_str("\\n"),142 '\r' => out.push_str("\\r"),143 '\t' => out.push_str("\\t"),144 c if c < 32 as char || (c >= 127 as char && c <= 159 as char) => {145 write!(out, "\\u{:04x}", c as u32).unwrap()146 }147 c => out.push(c),148 }149 }150 out.push('"');151 out152}153154#[test]155fn json_test() {156 assert_eq!(escape_string_json("\u{001f}"), "\"\\u001f\"")157}