difftreelog
Merge pull request #74 from eagletmt/std-json-minified
in: master
Add std.manifestJsonMinified()
5 files changed
crates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth18pub struct ManifestJsonOptions<'s> {18pub struct ManifestJsonOptions<'s> {19 pub padding: &'s str,19 pub padding: &'s str,20 pub mtype: ManifestType,20 pub mtype: ManifestType,21 pub newline: &'s str,22 pub key_val_sep: &'s str,21}23}222423pub fn manifest_json_ex(val: &Val, options: &ManifestJsonOptions<'_>) -> Result<String> {25pub fn manifest_json_ex(val: &Val, options: &ManifestJsonOptions<'_>) -> Result<String> {48 buf.push('[');50 buf.push('[');49 if !items.is_empty() {51 if !items.is_empty() {50 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {52 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {51 buf.push('\n');53 buf.push_str(options.newline);52 }54 }535554 let old_len = cur_padding.len();56 let old_len = cur_padding.len();59 if mtype == ManifestType::ToString {61 if mtype == ManifestType::ToString {60 buf.push(' ');62 buf.push(' ');61 } else if mtype != ManifestType::Minify {63 } else if mtype != ManifestType::Minify {62 buf.push('\n');64 buf.push_str(options.newline);63 }65 }64 }66 }65 buf.push_str(cur_padding);67 buf.push_str(cur_padding);68 cur_padding.truncate(old_len);70 cur_padding.truncate(old_len);697170 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {72 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {71 buf.push('\n');73 buf.push_str(options.newline);72 buf.push_str(cur_padding);74 buf.push_str(cur_padding);73 }75 }74 } else if mtype == ManifestType::Std {76 } else if mtype == ManifestType::Std {85 let fields = obj.fields();87 let fields = obj.fields();86 if !fields.is_empty() {88 if !fields.is_empty() {87 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {89 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {88 buf.push('\n');90 buf.push_str(options.newline);89 }91 }909291 let old_len = cur_padding.len();93 let old_len = cur_padding.len();96 if mtype == ManifestType::ToString {98 if mtype == ManifestType::ToString {97 buf.push(' ');99 buf.push(' ');98 } else if mtype != ManifestType::Minify {100 } else if mtype != ManifestType::Minify {99 buf.push('\n');101 buf.push_str(options.newline);100 }102 }101 }103 }102 buf.push_str(cur_padding);104 buf.push_str(cur_padding);103 escape_string_json_buf(&field, buf);105 escape_string_json_buf(&field, buf);104 buf.push_str(": ");106 buf.push_str(options.key_val_sep);105 crate::push(107 crate::push(106 None,108 None,107 || format!("field <{}> manifestification", field.clone()),109 || format!("field <{}> manifestification", field.clone()),114 cur_padding.truncate(old_len);116 cur_padding.truncate(old_len);115117116 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {118 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {117 buf.push('\n');119 buf.push_str(options.newline);118 buf.push_str(cur_padding);120 buf.push_str(cur_padding);119 }121 }120 } else if mtype == ManifestType::Std {122 } else if mtype == ManifestType::Std {crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth121 ("trace".into(), builtin_trace),121 ("trace".into(), builtin_trace),122 ("join".into(), builtin_join),122 ("join".into(), builtin_join),123 ("escapeStringJson".into(), builtin_escape_string_json),123 ("escapeStringJson".into(), builtin_escape_string_json),124 ("manifestJsonEx".into(), builtin_manifest_json_ex),124 ("manifestJsonExImpl".into(), builtin_manifest_json_ex_impl),125 ("manifestYamlDocImpl".into(), builtin_manifest_yaml_doc),125 ("manifestYamlDocImpl".into(), builtin_manifest_yaml_doc),126 ("reverse".into(), builtin_reverse),126 ("reverse".into(), builtin_reverse),127 ("id".into(), builtin_id),127 ("id".into(), builtin_id),754 })754 })755}755}756756757fn builtin_manifest_json_ex(757fn builtin_manifest_json_ex_impl(758 context: Context,758 context: Context,759 _loc: Option<&ExprLocation>,759 _loc: Option<&ExprLocation>,760 args: &ArgsDesc,760 args: &ArgsDesc,761) -> Result<Val> {761) -> Result<Val> {762 parse_args!(context, "manifestJsonEx", args, 2, [762 parse_args!(context, "manifestJsonEx", args, 4, [763 0, value: ty!(any);763 0, value: ty!(any);764 1, indent: ty!(string) => Val::Str;764 1, indent: ty!(string) => Val::Str;765 2, newline: ty!(string) => Val::Str;766 3, key_val_sep: ty!(string) => Val::Str;765 ], {767 ], {766 Ok(Val::Str(manifest_json_ex(&value, &ManifestJsonOptions {768 Ok(Val::Str(manifest_json_ex(&value, &ManifestJsonOptions {767 padding: &indent,769 padding: &indent,768 mtype: ManifestType::Std,770 mtype: ManifestType::Std,771 newline: &newline,772 key_val_sep: &key_val_sep,769 })?.into()))773 })?.into()))770 })774 })771}775}crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth809 );809 );810 }810 }811812 #[test]813 fn json_minified() {814 assert_json!(815 r#"std.manifestJsonMinified({a:3, b:4, c:6})"#,816 r#""{\"a\":3,\"b\":4,\"c\":6}""#817 );818 }811819812 #[test]820 #[test]813 fn parse_json() {821 fn parse_json() {crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth451 &ManifestJsonOptions {451 &ManifestJsonOptions {452 padding: "",452 padding: "",453 mtype: ManifestType::ToString,453 mtype: ManifestType::ToString,454 newline: "\n",455 key_val_sep: ": ",454 },456 },455 )?457 )?456 .into(),458 .into(),535 } else {537 } else {536 ManifestType::Manifest538 ManifestType::Manifest537 },539 },540 newline: "\n",541 key_val_sep: ": ",538 },542 },539 )543 )540 .map(|s| s.into())544 .map(|s| s.into())547 &ManifestJsonOptions {551 &ManifestJsonOptions {548 padding: &" ".repeat(padding),552 padding: &" ".repeat(padding),549 mtype: ManifestType::Std,553 mtype: ManifestType::Std,554 newline: "\n",555 key_val_sep: ": ",550 },556 },551 )557 )552 .map(|s| s.into())558 .map(|s| s.into())crates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth372372373 manifestJson(value):: std.manifestJsonEx(value, ' '),373 manifestJson(value):: std.manifestJsonEx(value, ' '),374374375 manifestJsonEx:: $intrinsic(manifestJsonEx),375 manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),376376377 manifestJsonExImpl:: $intrinsic(manifestJsonExImpl),378379 manifestJsonEx(value, indent, newline='\n', key_val_sep=': '):: std.manifestJsonExImpl(value, indent, newline, key_val_sep),380377 manifestYamlDocImpl:: $intrinsic(manifestYamlDocImpl),381 manifestYamlDocImpl:: $intrinsic(manifestYamlDocImpl),378382379 manifestYamlDoc(value, indent_array_in_object=false):: std.manifestYamlDocImpl(value, indent_array_in_object),383 manifestYamlDoc(value, indent_array_in_object=false):: std.manifestYamlDocImpl(value, indent_array_in_object),