difftreelog
feat minify json
in: master
2 files changed
crates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth2use crate::error::Result;2use crate::error::Result;3use crate::{throw, Val};3use crate::{throw, Val};445#[derive(PartialEq)]5#[derive(PartialEq, Clone, Copy)]6pub enum ManifestType {6pub enum ManifestType {7 // Applied in manifestification7 // Applied in manifestification8 Manifest,8 Manifest,9 /// Used for std.manifestJson9 /// Used for std.manifestJson10 /// Empty array/objects extends to "[\n\n]" instead of "[ ]" as in manifest10 /// Empty array/objects extends to "[\n\n]" instead of "[ ]" as in manifest11 Std,11 Std,12 // No line breaks, used in `obj+''`12 /// No line breaks, used in `obj+''`13 ToString,13 ToString,14 /// Minified json15 Minify,14}16}151716pub 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 }505353 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);666967 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 }868989 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);104107105 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('}');crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth227 &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::Minify232 } else {233 ManifestType::Manifest234 },231 },235 },232 )236 )233 .map(|s| s.into())237 .map(|s| s.into())