git.delta.rocks / jrsonnet / refs/commits / 4ee68f9fc91b

difftreelog

feat(cli) --no-trailing-newline

qmsmqktvYaroslav Bolyukin2026-05-05parent: #d867152.patch.diff
in: master

4 files changed

modifiedbindings/jsonnet/src/lib.rsdiffbeforeafterboth
44/// If this does not match `LIB_JSONNET_VERSION`44/// If this does not match `LIB_JSONNET_VERSION`
45/// then there is a mismatch between header and compiled library.45/// then there is a mismatch between header and compiled library.
46#[unsafe(no_mangle)]46#[unsafe(no_mangle)]
47pub extern "C" fn jsonnet_version() -> &'static [u8; 12] {47pub extern "C" fn jsonnet_version() -> &'static [u8; 8] {
48 b"v0.22.0-rc1\0"48 b"v0.22.0\0"
49}49}
5050
51unsafe fn parse_path(input: &CStr) -> Cow<'_, Path> {51unsafe fn parse_path(input: &CStr) -> Cow<'_, Path> {
105pub struct VM {105pub struct VM {
106 state: State,106 state: State,
107 manifest_format: Box<dyn ManifestFormat>,107 manifest_format: Box<dyn ManifestFormat>,
108 trailing_newline: bool,
108 trace_format: Box<dyn TraceFormat>,109 trace_format: Box<dyn TraceFormat>,
109 tla_args: FxHashMap<IStr, TlaArg>,110 tla_args: FxHashMap<IStr, TlaArg>,
110}111}
142 state,143 state,
143 manifest_format: Box::new(JsonFormat::default()),144 manifest_format: Box::new(JsonFormat::default()),
144 trace_format: Box::new(CompactFormat::default()),145 trace_format: Box::new(CompactFormat::default()),
146 trailing_newline: true,
145 tla_args: FxHashMap::new(),147 tla_args: FxHashMap::new(),
146 }))148 }))
147}149}
181 };183 };
182}184}
185
186/// Enable/disable trailing newline in manifested/string output.
187#[unsafe(no_mangle)]
188pub extern "C" fn jsonnet_set_trailing_newline(vm: &mut VM, enable: c_int) {
189 vm.trailing_newline = enable != 0;
190}
183191
184/// Allocate, resize, or free a buffer. This will abort if the memory cannot be allocated. It will192/// Allocate, resize, or free a buffer. This will abort if the memory cannot be allocated. It will
185/// only return NULL if sz was zero.193/// only return NULL if sz was zero.
285 .and_then(|val| apply_tla(&vm.tla_args, val))293 .and_then(|val| apply_tla(&vm.tla_args, val))
286 .and_then(|val| val.manifest(&vm.manifest_format))294 .and_then(|val| val.manifest(&vm.manifest_format))
287 {295 {
288 Ok(v) => {296 Ok(mut v) => {
297 if vm.trailing_newline {
298 v.push('\n');
299 }
289 *error = 0;300 *error = 0;
290 CString::new(&*v as &str).unwrap().into_raw()301 CString::new(&*v as &str).unwrap().into_raw()
291 }302 }
312 Ok(out)323 Ok(out)
313}324}
314325
315fn multi_to_raw(multi: Vec<(IStr, IStr)>) -> *const c_char {326fn multi_to_raw(multi: Vec<(IStr, IStr)>, trailing_newline: bool) -> *const c_char {
316 let mut out = Vec::new();327 let mut out = Vec::new();
317 for (i, (k, v)) in multi.iter().enumerate() {328 for (i, (k, v)) in multi.iter().enumerate() {
318 if i != 0 {329 if i != 0 {
321 out.extend_from_slice(k.as_bytes());332 out.extend_from_slice(k.as_bytes());
322 out.push(0);333 out.push(0);
323 out.extend_from_slice(v.as_bytes());334 out.extend_from_slice(v.as_bytes());
335 if trailing_newline {
336 out.push(b'\n');
337 }
324 }338 }
325 out.push(0);339 out.push(0);
326 out.push(0);340 out.push(0);
345 {359 {
346 Ok(v) => {360 Ok(v) => {
347 *error = 0;361 *error = 0;
348 multi_to_raw(v)362 multi_to_raw(v, vm.trailing_newline)
349 }363 }
350 Err(e) => {364 Err(e) => {
351 *error = 1;365 *error = 1;
374 {388 {
375 Ok(v) => {389 Ok(v) => {
376 *error = 0;390 *error = 0;
377 multi_to_raw(v)391 multi_to_raw(v, vm.trailing_newline)
378 }392 }
379 Err(e) => {393 Err(e) => {
380 *error = 1;394 *error = 1;
396 Ok(out)410 Ok(out)
397}411}
398412
399fn stream_to_raw(multi: Vec<IStr>) -> *const c_char {413fn stream_to_raw(multi: Vec<IStr>, trailing_newline: bool) -> *const c_char {
400 let mut out = Vec::new();414 let mut out = Vec::new();
401 for (i, v) in multi.iter().enumerate() {415 for (i, v) in multi.iter().enumerate() {
402 if i != 0 {416 if i != 0 {
403 out.push(0);417 out.push(0);
404 }418 }
405 out.extend_from_slice(v.as_bytes());419 out.extend_from_slice(v.as_bytes());
420 if trailing_newline {
421 out.push(b'\n');
422 }
406 }423 }
407 out.push(0);424 out.push(0);
408 out.push(0);425 out.push(0);
427 {444 {
428 Ok(v) => {445 Ok(v) => {
429 *error = 0;446 *error = 0;
430 stream_to_raw(v)447 stream_to_raw(v, vm.trailing_newline)
431 }448 }
432 Err(e) => {449 Err(e) => {
433 *error = 1;450 *error = 1;
456 {473 {
457 Ok(v) => {474 Ok(v) => {
458 *error = 0;475 *error = 0;
459 stream_to_raw(v)476 stream_to_raw(v, vm.trailing_newline)
460 }477 }
461 Err(e) => {478 Err(e) => {
462 *error = 1;479 *error = 1;
modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
238 data.manifest(&manifest_format)238 data.manifest(&manifest_format)
239 .with_description(|| format!("manifesting {field}"))?,239 .with_description(|| format!("manifesting {field}"))?,
240 )?;240 )?;
241 if manifest_format.file_trailing_newline() {241 if !opts.manifest.no_trailing_newline {
242 writeln!(file)?;242 writeln!(file)?;
243 }243 }
244 file.flush()?;244 file.flush()?;
modifiedcrates/jrsonnet-cli/src/manifest.rsdiffbeforeafterboth
38 /// [default: 3 for json, 2 for yaml/toml]38 /// [default: 3 for json, 2 for yaml/toml]
39 #[clap(long)]39 #[clap(long)]
40 line_padding: Option<usize>,40 line_padding: Option<usize>,
41 /// No not add a trailing newline to the output
42 #[clap(long)]
43 pub no_trailing_newline: bool,
41 /// Preserve order in object manifestification44 /// Preserve order in object manifestification
42 #[cfg(feature = "exp-preserve-order")]45 #[cfg(feature = "exp-preserve-order")]
43 #[clap(long)]46 #[clap(long)]
modifiedcrates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth
11 self.manifest_buf(val, &mut out)?;11 self.manifest_buf(val, &mut out)?;
12 Ok(out)12 Ok(out)
13 }13 }
14 /// When outputing to file, is it safe to append a trailing newline (I.e newline won't change
15 /// the meaning).
16 ///
17 /// Default implementation returns `true`
18 fn file_trailing_newline(&self) -> bool {
19 true
20 }
21}14}
22impl<T> ManifestFormat for Box<T>15impl<T> ManifestFormat for Box<T>
23where16where
27 let inner = &**self;20 let inner = &**self;
28 inner.manifest_buf(val, buf)21 inner.manifest_buf(val, buf)
29 }22 }
30 fn file_trailing_newline(&self) -> bool {
31 let inner = &**self;
32 inner.file_trailing_newline()
33 }
34}23}
35impl<T> ManifestFormat for &'_ T24impl<T> ManifestFormat for &'_ T
36where25where
40 let inner = &**self;29 let inner = &**self;
41 inner.manifest_buf(val, buf)30 inner.manifest_buf(val, buf)
42 }31 }
43 fn file_trailing_newline(&self) -> bool {
44 let inner = &**self;
45 inner.file_trailing_newline()
46 }
47}32}
4833
49pub struct BlackBoxFormat;34pub struct BlackBoxFormat;
399 }384 }
400 JSON_TO_STRING.manifest_buf(val, out)385 JSON_TO_STRING.manifest_buf(val, out)
401 }386 }
402 fn file_trailing_newline(&self) -> bool {
403 false
404 }
405}387}
406pub struct StringFormat;388pub struct StringFormat;
407impl ManifestFormat for StringFormat {389impl ManifestFormat for StringFormat {
415 write!(out, "{s}").unwrap();397 write!(out, "{s}").unwrap();
416 Ok(())398 Ok(())
417 }399 }
418 fn file_trailing_newline(&self) -> bool {
419 false
420 }
421}400}
422401
423pub struct YamlStreamFormat<I> {402pub struct YamlStreamFormat<I> {