From 4be0ffe86b04571dd08562657f168c0cbf1f3d71 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Thu, 04 May 2023 10:49:01 +0000 Subject: [PATCH] fix: do not write trailing newline in --multi for -S --- --- a/cmds/jrsonnet/src/main.rs +++ b/cmds/jrsonnet/src/main.rs @@ -200,12 +200,16 @@ } println!("{}", path.to_str().expect("path")); let mut file = File::create(path)?; - writeln!( + write!( file, "{}", data.manifest(&manifest_format) - .with_description(|| format!("manifesting {field}"))? + .with_description(|| format!("manifesting {field}"))?, )?; + if manifest_format.file_trailing_newline() { + writeln!(file)?; + } + file.flush()?; } } else if let Some(path) = opts.output.output_file { if opts.output.create_output_dirs { --- a/crates/jrsonnet-evaluator/src/manifest.rs +++ b/crates/jrsonnet-evaluator/src/manifest.rs @@ -12,6 +12,13 @@ self.manifest_buf(val, &mut out)?; Ok(out) } + /// When outputing to file, is it safe to append a trailing newline (I.e newline won't change + /// the meaning). + /// + /// Default implementation returns `true` + fn file_trailing_newline(&self) -> bool { + true + } } impl ManifestFormat for Box where @@ -21,6 +28,10 @@ let inner = &**self; inner.manifest_buf(val, buf) } + fn file_trailing_newline(&self) -> bool { + let inner = &**self; + inner.file_trailing_newline() + } } impl ManifestFormat for &'_ T where @@ -30,6 +41,10 @@ let inner = &**self; inner.manifest_buf(val, buf) } + fn file_trailing_newline(&self) -> bool { + let inner = &**self; + inner.file_trailing_newline() + } } #[derive(PartialEq, Eq, Clone, Copy)] @@ -253,6 +268,9 @@ fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> { JsonFormat::std_to_string().manifest_buf(val, out) } + fn file_trailing_newline(&self) -> bool { + false + } } pub struct StringFormat; impl ManifestFormat for StringFormat { @@ -263,6 +281,9 @@ write!(out, "{s}").unwrap(); Ok(()) } + fn file_trailing_newline(&self) -> bool { + false + } } pub struct YamlStreamFormat(pub I); -- gitstuff