difftreelog
feat minify json
in: master
2 files changed
crates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth1use crate::error::Error::*;2use crate::error::Result;3use crate::{throw, Val};45#[derive(PartialEq)]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}1516pub struct ManifestJsonOptions<'s> {17 pub padding: &'s str,18 pub mtype: ManifestType,19}2021pub(crate) fn manifest_json_ex(val: &Val, options: &ManifestJsonOptions<'_>) -> Result<String> {22 let mut out = String::new();23 manifest_json_ex_buf(val, &mut out, &mut String::new(), options)?;24 Ok(out)25}26fn manifest_json_ex_buf(27 val: &Val,28 buf: &mut String,29 cur_padding: &mut String,30 options: &ManifestJsonOptions<'_>,31) -> Result<()> {32 use std::fmt::Write;33 match val.unwrap_if_lazy()? {34 Val::Bool(v) => {35 if v {36 buf.push_str("true");37 } else {38 buf.push_str("false");39 }40 }41 Val::Null => buf.push_str("null"),42 Val::Str(s) => buf.push_str(&escape_string_json(&s)),43 Val::Num(n) => write!(buf, "{}", n).unwrap(),44 Val::Arr(items) => {45 buf.push('[');46 if !items.is_empty() {47 if options.mtype != ManifestType::ToString {48 buf.push('\n');49 }5051 let old_len = cur_padding.len();52 cur_padding.push_str(options.padding);53 for (i, item) in items.iter().enumerate() {54 if i != 0 {55 buf.push(',');56 if options.mtype == ManifestType::ToString {57 buf.push(' ');58 } else {59 buf.push('\n');60 }61 }62 buf.push_str(cur_padding);63 manifest_json_ex_buf(item, buf, cur_padding, options)?;64 }65 cur_padding.truncate(old_len);6667 if options.mtype != ManifestType::ToString {68 buf.push('\n');69 buf.push_str(cur_padding);70 }71 } else if options.mtype == ManifestType::Std {72 buf.push_str("\n\n");73 buf.push_str(cur_padding);74 } else if options.mtype == ManifestType::ToString {75 buf.push(' ');76 }77 buf.push(']');78 }79 Val::Obj(obj) => {80 buf.push('{');81 let fields = obj.visible_fields();82 if !fields.is_empty() {83 if options.mtype != ManifestType::ToString {84 buf.push('\n');85 }8687 let old_len = cur_padding.len();88 cur_padding.push_str(options.padding);89 for (i, field) in fields.into_iter().enumerate() {90 if i != 0 {91 buf.push(',');92 if options.mtype == ManifestType::ToString {93 buf.push(' ');94 } else {95 buf.push('\n');96 }97 }98 buf.push_str(cur_padding);99 buf.push_str(&escape_string_json(&field));100 buf.push_str(": ");101 manifest_json_ex_buf(&obj.get(field)?.unwrap(), buf, cur_padding, options)?;102 }103 cur_padding.truncate(old_len);104105 if options.mtype != ManifestType::ToString {106 buf.push('\n');107 buf.push_str(cur_padding);108 }109 } else if options.mtype == ManifestType::Std {110 buf.push_str("\n\n");111 buf.push_str(cur_padding);112 } else if options.mtype == ManifestType::ToString {113 buf.push(' ');114 }115 buf.push('}');116 }117 Val::Func(_) | Val::Intristic(_, _) => {118 throw!(RuntimeError("tried to manifest function".into()))119 }120 Val::Lazy(_) => unreachable!(),121 };122 Ok(())123}124pub fn escape_string_json(s: &str) -> String {125 use std::fmt::Write;126 let mut out = String::new();127 out.push('"');128 for c in s.chars() {129 match c {130 '"' => out.push_str("\\\""),131 '\\' => out.push_str("\\\\"),132 '\u{0008}' => out.push_str("\\b"),133 '\u{000c}' => out.push_str("\\f"),134 '\n' => out.push_str("\\n"),135 '\r' => out.push_str("\\r"),136 '\t' => out.push_str("\\t"),137 c if c < 32 as char || (c >= 127 as char && c <= 159 as char) => {138 write!(out, "\\u{:04x}", c as u32).unwrap()139 }140 c => out.push(c),141 }142 }143 out.push('"');144 out145}146147#[test]148fn json_test() {149 assert_eq!(escape_string_json("\u{001f}"), "\"\\u001f\"")150}1use 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(crate) 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.unwrap_if_lazy()? {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 {78 buf.push(' ');79 }80 buf.push(']');81 }82 Val::Obj(obj) => {83 buf.push('{');84 let fields = obj.visible_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 manifest_json_ex_buf(&obj.get(field)?.unwrap(), buf, cur_padding, options)?;105 }106 cur_padding.truncate(old_len);107108 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {109 buf.push('\n');110 buf.push_str(cur_padding);111 }112 } else if mtype == ManifestType::Std {113 buf.push_str("\n\n");114 buf.push_str(cur_padding);115 } else if mtype == ManifestType::ToString {116 buf.push(' ');117 }118 buf.push('}');119 }120 Val::Func(_) | Val::Intristic(_, _) => {121 throw!(RuntimeError("tried to manifest function".into()))122 }123 Val::Lazy(_) => unreachable!(),124 };125 Ok(())126}127pub fn escape_string_json(s: &str) -> String {128 use std::fmt::Write;129 let mut out = String::new();130 out.push('"');131 for c in s.chars() {132 match c {133 '"' => out.push_str("\\\""),134 '\\' => out.push_str("\\\\"),135 '\u{0008}' => out.push_str("\\b"),136 '\u{000c}' => out.push_str("\\f"),137 '\n' => out.push_str("\\n"),138 '\r' => out.push_str("\\r"),139 '\t' => out.push_str("\\t"),140 c if c < 32 as char || (c >= 127 as char && c <= 159 as char) => {141 write!(out, "\\u{:04x}", c as u32).unwrap()142 }143 c => out.push(c),144 }145 }146 out.push('"');147 out148}149150#[test]151fn json_test() {152 assert_eq!(escape_string_json("\u{001f}"), "\"\\u001f\"")153}crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -227,7 +227,11 @@
&self,
&ManifestJsonOptions {
padding: &" ".repeat(padding),
- mtype: ManifestType::Manifest,
+ mtype: if padding == 0 {
+ ManifestType::Minify
+ } else {
+ ManifestType::Manifest
+ },
},
)
.map(|s| s.into())