difftreelog
perf std.manifestTomlEx builtin
in: master
5 files changed
crates/jrsonnet-cli/src/manifest.rsdiffbeforeafterboth1use std::{path::PathBuf, str::FromStr};1use std::path::PathBuf;223use clap::{Parser, ValueEnum};3use clap::{Parser, ValueEnum};4use jrsonnet_evaluator::{4use jrsonnet_evaluator::{5 error::Result,5 error::Result,6 manifest::{JsonFormat, ManifestFormat, StringFormat, ToStringFormat, YamlStreamFormat},6 manifest::{JsonFormat, ManifestFormat, StringFormat, ToStringFormat, YamlStreamFormat},7 State,7 State,8};8};9use jrsonnet_stdlib::YamlFormat;9use jrsonnet_stdlib::{TomlFormat, YamlFormat};101011use crate::ConfigureState;11use crate::ConfigureState;121216 String,16 String,17 Json,17 Json,18 Yaml,18 Yaml,19 Toml,19}20}2021impl FromStr for ManifestFormatName {22 type Err = &'static str;23 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {24 Ok(match s {25 "string" => ManifestFormatName::String,26 "json" => ManifestFormatName::Json,27 "yaml" => ManifestFormatName::Yaml,28 _ => return Err("no such format"),29 })30 }31}322133#[derive(Parser)]22#[derive(Parser)]34#[clap(next_help_heading = "MANIFESTIFICATION OUTPUT")]23#[clap(next_help_heading = "MANIFESTIFICATION OUTPUT")]44 #[clap(long, short = 'y', conflicts_with = "string")]33 #[clap(long, short = 'y', conflicts_with = "string")]45 yaml_stream: bool,34 yaml_stream: bool,46 /// Number of spaces to pad output manifest with.35 /// Number of spaces to pad output manifest with.47 /// `0` for hard tabs, `-1` for single line output [default: 3 for json, 2 for yaml]36 /// `0` for hard tabs, `-1` for single line output [default: 3 for json, 2 for yaml/toml]48 #[clap(long)]37 #[clap(long)]49 line_padding: Option<usize>,38 line_padding: Option<usize>,50 /// Preserve order in object manifestification39 /// Preserve order in object manifestification72 #[cfg(feature = "exp-preserve-order")]61 #[cfg(feature = "exp-preserve-order")]73 preserve_order,62 preserve_order,74 )),63 )),64 ManifestFormatName::Toml => Box::new(TomlFormat::cli(65 self.line_padding.unwrap_or(2),66 #[cfg(feature = "exp-preserve-order")]67 preserve_order,68 )),75 }69 }76 };70 };77 Ok(if self.yaml_stream {71 Ok(if self.yaml_stream {crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth121 ("escapeStringJson", builtin_escape_string_json::INST),121 ("escapeStringJson", builtin_escape_string_json::INST),122 ("manifestJsonEx", builtin_manifest_json_ex::INST),122 ("manifestJsonEx", builtin_manifest_json_ex::INST),123 ("manifestYamlDoc", builtin_manifest_yaml_doc::INST),123 ("manifestYamlDoc", builtin_manifest_yaml_doc::INST),124 ("manifestTomlEx", builtin_manifest_toml_ex::INST),124 // Parsing125 // Parsing125 ("parseJson", builtin_parse_json::INST),126 ("parseJson", builtin_parse_json::INST),126 ("parseYaml", builtin_parse_yaml::INST),127 ("parseYaml", builtin_parse_yaml::INST),crates/jrsonnet-stdlib/src/manifest/mod.rsdiffbeforeafterboth1mod toml;1mod yaml;2mod yaml;233use jrsonnet_evaluator::{4use jrsonnet_evaluator::{4 error::Result,5 error::Result,5 function::builtin,6 function::builtin,6 manifest::{escape_string_json, JsonFormat},7 manifest::{escape_string_json, JsonFormat},7 typed::Any,8 typed::Any,8 IStr,9 IStr, ObjValue, Val,9};10};11pub use toml::TomlFormat;10pub use yaml::YamlFormat;12pub use yaml::YamlFormat;111312#[builtin]14#[builtin]48 ))50 ))49}51}5253#[builtin]54pub fn builtin_manifest_toml_ex(55 value: ObjValue,56 indent: IStr,57 #[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,58) -> Result<String> {59 Val::Obj(value).manifest(TomlFormat::std_to_toml(60 indent.to_string(),61 #[cfg(feature = "exp-preserve-order")]62 preserve_order.unwrap_or(false),63 ))64}5065crates/jrsonnet-stdlib/src/manifest/toml.rsdiffbeforeafterbothno changes
crates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth100100101 manifestToml(value):: std.manifestTomlEx(value, ' '),101 manifestToml(value):: std.manifestTomlEx(value, ' '),102102103 manifestTomlEx(value, indent)::104 local105 escapeStringToml = std.escapeStringJson,106 escapeKeyToml(key) =107 local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));108 if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),109 isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),110 isSection(v) = std.isObject(v) || isTableArray(v),111 renderValue(v, indexedPath, inline, cindent) =112 if v == true then113 'true'114 else if v == false then115 'false'116 else if v == null then117 error 'Tried to manifest "null" at ' + indexedPath118 else if std.isNumber(v) then119 '' + v120 else if std.isString(v) then121 escapeStringToml(v)122 else if std.isFunction(v) then123 error 'Tried to manifest function at ' + indexedPath124 else if std.isArray(v) then125 if std.length(v) == 0 then126 '[]'127 else128 local range = std.range(0, std.length(v) - 1);129 local new_indent = if inline then '' else cindent + indent;130 local separator = if inline then ' ' else '\n';131 local lines = ['[' + separator]132 + std.join([',' + separator],133 [134 [new_indent + renderValue(v[i], indexedPath + [i], true, '')]135 for i in range136 ])137 + [separator + (if inline then '' else cindent) + ']'];138 std.join('', lines)139 else if std.isObject(v) then140 local lines = ['{ ']141 + std.join([', '],142 [143 [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]144 for k in std.objectFields(v)145 ])146 + [' }'];147 std.join('', lines),148 renderTableInternal(v, path, indexedPath, cindent) =149 local kvp = std.flattenArrays([150 [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]151 for k in std.objectFields(v)152 if !isSection(v[k])153 ]);154 local sections = [std.join('\n', kvp)] + [155 (156 if std.isObject(v[k]) then157 renderTable(v[k], path + [k], indexedPath + [k], cindent)158 else159 renderTableArray(v[k], path + [k], indexedPath + [k], cindent)160 )161 for k in std.objectFields(v)162 if isSection(v[k])163 ];164 std.join('\n\n', sections),165 renderTable(v, path, indexedPath, cindent) =166 cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'167 + (if v == {} then '' else '\n')168 + renderTableInternal(v, path, indexedPath, cindent + indent),169 renderTableArray(v, path, indexedPath, cindent) =170 local range = std.range(0, std.length(v) - 1);171 local sections = [172 (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'173 + (if v[i] == {} then '' else '\n')174 + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))175 for i in range176 ];177 std.join('\n\n', sections);178 if std.isObject(value) then179 renderTableInternal(value, [], [], '')180 else181 error 'TOML body must be an object. Got ' + std.type(value),182183 escapeStringPython(str)::103 escapeStringPython(str)::184 std.escapeStringJson(str),104 std.escapeStringJson(str),