git.delta.rocks / jrsonnet / refs/commits / 6e6d01b3c127

difftreelog

feat optional long string truncation in JsonFormat

Yaroslav Bolyukin2023-08-25parent: #0d81f3b.patch.diff
in: master

1 file changed

modifiedcrates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth
66 preserve_order: bool,66 preserve_order: bool,
67 #[cfg(feature = "exp-bigint")]67 #[cfg(feature = "exp-bigint")]
68 preserve_bigints: bool,68 preserve_bigints: bool,
69 debug_truncate_strings: Option<usize>,
69}70}
7071
71impl<'s> JsonFormat<'s> {72impl<'s> JsonFormat<'s> {
80 preserve_order,81 preserve_order,
81 #[cfg(feature = "exp-bigint")]82 #[cfg(feature = "exp-bigint")]
82 preserve_bigints: false,83 preserve_bigints: false,
84 debug_truncate_strings: None,
83 }85 }
84 }86 }
85 // Same format as std.toString87 // Same format as std.toString
93 preserve_order: false,95 preserve_order: false,
94 #[cfg(feature = "exp-bigint")]96 #[cfg(feature = "exp-bigint")]
95 preserve_bigints: false,97 preserve_bigints: false,
98 debug_truncate_strings: None,
96 }99 }
97 }100 }
98 pub fn std_to_json(101 pub fn std_to_json(
110 preserve_order,113 preserve_order,
111 #[cfg(feature = "exp-bigint")]114 #[cfg(feature = "exp-bigint")]
112 preserve_bigints: false,115 preserve_bigints: false,
116 debug_truncate_strings: None,
113 }117 }
114 }118 }
115 // Same format as CLI manifestification119 // Same format as CLI manifestification
132 preserve_order,136 preserve_order,
133 #[cfg(feature = "exp-bigint")]137 #[cfg(feature = "exp-bigint")]
134 preserve_bigints: false,138 preserve_bigints: false,
139 debug_truncate_strings: None,
135 }140 }
136 }141 }
142 // Same format as CLI manifestification
143 pub fn debug() -> Self {
144 Self {
145 padding: Cow::Borrowed(" "),
146 mtype: JsonFormatting::Manifest,
147 newline: "\n",
148 key_val_sep: ": ",
149 #[cfg(feature = "exp-preserve-order")]
150 preserve_order: true,
151 #[cfg(feature = "exp-bigint")]
152 preserve_bigints: true,
153 debug_truncate_strings: Some(256),
154 }
155 }
137}156}
138impl Default for JsonFormat<'static> {157impl Default for JsonFormat<'static> {
139 fn default() -> Self {158 fn default() -> Self {
146 preserve_order: false,165 preserve_order: false,
147 #[cfg(feature = "exp-bigint")]166 #[cfg(feature = "exp-bigint")]
148 preserve_bigints: false,167 preserve_bigints: false,
168 debug_truncate_strings: None,
149 }169 }
150 }170 }
151}171}
171 }191 }
172 }192 }
173 Val::Null => buf.push_str("null"),193 Val::Null => buf.push_str("null"),
174 Val::Str(s) => escape_string_json_buf(&s.clone().into_flat(), buf),194 Val::Str(s) => {
195 let flat = s.clone().into_flat();
196 if let Some(truncate) = options.debug_truncate_strings {
197 if flat.len() > truncate {
198 let (start, end) = flat.split_at(truncate / 2);
199 let (_, end) = end.split_at(end.len() - truncate / 2);
200 escape_string_json_buf(&format!("{start}..{end}"), buf);
201 } else {
202 escape_string_json_buf(&flat, buf);
203 }
204 } else {
205 escape_string_json_buf(&flat, buf);
206 }
207 }
175 Val::Num(n) => write!(buf, "{n}").unwrap(),208 Val::Num(n) => write!(buf, "{n}").unwrap(),
176 #[cfg(feature = "exp-bigint")]209 #[cfg(feature = "exp-bigint")]
177 Val::BigInt(n) => {210 Val::BigInt(n) => {