git.delta.rocks / jrsonnet / refs/commits / ce6eeacd9f76

difftreelog

feat minify json

Lach2020-08-22parent: #4dbbc6e.patch.diff
in: master

2 files changed

modifiedcrates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth
2use crate::error::Result;2use crate::error::Result;
3use crate::{throw, Val};3use crate::{throw, Val};
44
5#[derive(PartialEq)]5#[derive(PartialEq, Clone, Copy)]
6pub enum ManifestType {6pub enum ManifestType {
7 // Applied in manifestification7 // Applied in manifestification
8 Manifest,8 Manifest,
9 /// Used for std.manifestJson9 /// Used for std.manifestJson
10 /// Empty array/objects extends to "[\n\n]" instead of "[ ]" as in manifest10 /// Empty array/objects extends to "[\n\n]" instead of "[ ]" as in manifest
11 Std,11 Std,
12 // No line breaks, used in `obj+''`12 /// No line breaks, used in `obj+''`
13 ToString,13 ToString,
14 /// Minified json
15 Minify,
14}16}
1517
16pub struct ManifestJsonOptions<'s> {18pub struct ManifestJsonOptions<'s> {
30 options: &ManifestJsonOptions<'_>,32 options: &ManifestJsonOptions<'_>,
31) -> Result<()> {33) -> Result<()> {
32 use std::fmt::Write;34 use std::fmt::Write;
35 let mtype = options.mtype;
33 match val.unwrap_if_lazy()? {36 match val.unwrap_if_lazy()? {
34 Val::Bool(v) => {37 Val::Bool(v) => {
35 if v {38 if v {
44 Val::Arr(items) => {47 Val::Arr(items) => {
45 buf.push('[');48 buf.push('[');
46 if !items.is_empty() {49 if !items.is_empty() {
47 if options.mtype != ManifestType::ToString {50 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {
48 buf.push('\n');51 buf.push('\n');
49 }52 }
5053
53 for (i, item) in items.iter().enumerate() {56 for (i, item) in items.iter().enumerate() {
54 if i != 0 {57 if i != 0 {
55 buf.push(',');58 buf.push(',');
56 if options.mtype == ManifestType::ToString {59 if mtype == ManifestType::ToString {
57 buf.push(' ');60 buf.push(' ');
58 } else {61 } else if mtype != ManifestType::Minify {
59 buf.push('\n');62 buf.push('\n');
60 }63 }
61 }64 }
64 }67 }
65 cur_padding.truncate(old_len);68 cur_padding.truncate(old_len);
6669
67 if options.mtype != ManifestType::ToString {70 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {
68 buf.push('\n');71 buf.push('\n');
69 buf.push_str(cur_padding);72 buf.push_str(cur_padding);
70 }73 }
71 } else if options.mtype == ManifestType::Std {74 } else if mtype == ManifestType::Std {
72 buf.push_str("\n\n");75 buf.push_str("\n\n");
73 buf.push_str(cur_padding);76 buf.push_str(cur_padding);
74 } else if options.mtype == ManifestType::ToString {77 } else if mtype == ManifestType::ToString {
75 buf.push(' ');78 buf.push(' ');
76 }79 }
77 buf.push(']');80 buf.push(']');
80 buf.push('{');83 buf.push('{');
81 let fields = obj.visible_fields();84 let fields = obj.visible_fields();
82 if !fields.is_empty() {85 if !fields.is_empty() {
83 if options.mtype != ManifestType::ToString {86 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {
84 buf.push('\n');87 buf.push('\n');
85 }88 }
8689
89 for (i, field) in fields.into_iter().enumerate() {92 for (i, field) in fields.into_iter().enumerate() {
90 if i != 0 {93 if i != 0 {
91 buf.push(',');94 buf.push(',');
92 if options.mtype == ManifestType::ToString {95 if mtype == ManifestType::ToString {
93 buf.push(' ');96 buf.push(' ');
94 } else {97 } else if mtype != ManifestType::Minify {
95 buf.push('\n');98 buf.push('\n');
96 }99 }
97 }100 }
102 }105 }
103 cur_padding.truncate(old_len);106 cur_padding.truncate(old_len);
104107
105 if options.mtype != ManifestType::ToString {108 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {
106 buf.push('\n');109 buf.push('\n');
107 buf.push_str(cur_padding);110 buf.push_str(cur_padding);
108 }111 }
109 } else if options.mtype == ManifestType::Std {112 } else if mtype == ManifestType::Std {
110 buf.push_str("\n\n");113 buf.push_str("\n\n");
111 buf.push_str(cur_padding);114 buf.push_str(cur_padding);
112 } else if options.mtype == ManifestType::ToString {115 } else if mtype == ManifestType::ToString {
113 buf.push(' ');116 buf.push(' ');
114 }117 }
115 buf.push('}');118 buf.push('}');
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
227 &self,227 &self,
228 &ManifestJsonOptions {228 &ManifestJsonOptions {
229 padding: &" ".repeat(padding),229 padding: &" ".repeat(padding),
230 mtype: ManifestType::Manifest,230 mtype: if padding == 0 {
231 ManifestType::Minify
232 } else {
233 ManifestType::Manifest
234 },
231 },235 },
232 )236 )
233 .map(|s| s.into())237 .map(|s| s.into())