difftreelog
feat(cli) --no-trailing-newline
in: master
4 files changed
bindings/jsonnet/src/lib.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -44,8 +44,8 @@
/// If this does not match `LIB_JSONNET_VERSION`
/// then there is a mismatch between header and compiled library.
#[unsafe(no_mangle)]
-pub extern "C" fn jsonnet_version() -> &'static [u8; 12] {
- b"v0.22.0-rc1\0"
+pub extern "C" fn jsonnet_version() -> &'static [u8; 8] {
+ b"v0.22.0\0"
}
unsafe fn parse_path(input: &CStr) -> Cow<'_, Path> {
@@ -105,6 +105,7 @@
pub struct VM {
state: State,
manifest_format: Box<dyn ManifestFormat>,
+ trailing_newline: bool,
trace_format: Box<dyn TraceFormat>,
tla_args: FxHashMap<IStr, TlaArg>,
}
@@ -142,6 +143,7 @@
state,
manifest_format: Box::new(JsonFormat::default()),
trace_format: Box::new(CompactFormat::default()),
+ trailing_newline: true,
tla_args: FxHashMap::new(),
}))
}
@@ -181,6 +183,12 @@
};
}
+/// Enable/disable trailing newline in manifested/string output.
+#[unsafe(no_mangle)]
+pub extern "C" fn jsonnet_set_trailing_newline(vm: &mut VM, enable: c_int) {
+ vm.trailing_newline = enable != 0;
+}
+
/// Allocate, resize, or free a buffer. This will abort if the memory cannot be allocated. It will
/// only return NULL if sz was zero.
///
@@ -285,7 +293,10 @@
.and_then(|val| apply_tla(&vm.tla_args, val))
.and_then(|val| val.manifest(&vm.manifest_format))
{
- Ok(v) => {
+ Ok(mut v) => {
+ if vm.trailing_newline {
+ v.push('\n');
+ }
*error = 0;
CString::new(&*v as &str).unwrap().into_raw()
}
@@ -312,7 +323,7 @@
Ok(out)
}
-fn multi_to_raw(multi: Vec<(IStr, IStr)>) -> *const c_char {
+fn multi_to_raw(multi: Vec<(IStr, IStr)>, trailing_newline: bool) -> *const c_char {
let mut out = Vec::new();
for (i, (k, v)) in multi.iter().enumerate() {
if i != 0 {
@@ -321,6 +332,9 @@
out.extend_from_slice(k.as_bytes());
out.push(0);
out.extend_from_slice(v.as_bytes());
+ if trailing_newline {
+ out.push(b'\n');
+ }
}
out.push(0);
out.push(0);
@@ -345,7 +359,7 @@
{
Ok(v) => {
*error = 0;
- multi_to_raw(v)
+ multi_to_raw(v, vm.trailing_newline)
}
Err(e) => {
*error = 1;
@@ -374,7 +388,7 @@
{
Ok(v) => {
*error = 0;
- multi_to_raw(v)
+ multi_to_raw(v, vm.trailing_newline)
}
Err(e) => {
*error = 1;
@@ -396,13 +410,16 @@
Ok(out)
}
-fn stream_to_raw(multi: Vec<IStr>) -> *const c_char {
+fn stream_to_raw(multi: Vec<IStr>, trailing_newline: bool) -> *const c_char {
let mut out = Vec::new();
for (i, v) in multi.iter().enumerate() {
if i != 0 {
out.push(0);
}
out.extend_from_slice(v.as_bytes());
+ if trailing_newline {
+ out.push(b'\n');
+ }
}
out.push(0);
out.push(0);
@@ -427,7 +444,7 @@
{
Ok(v) => {
*error = 0;
- stream_to_raw(v)
+ stream_to_raw(v, vm.trailing_newline)
}
Err(e) => {
*error = 1;
@@ -456,7 +473,7 @@
{
Ok(v) => {
*error = 0;
- stream_to_raw(v)
+ stream_to_raw(v, vm.trailing_newline)
}
Err(e) => {
*error = 1;
cmds/jrsonnet/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -238,7 +238,7 @@
data.manifest(&manifest_format)
.with_description(|| format!("manifesting {field}"))?,
)?;
- if manifest_format.file_trailing_newline() {
+ if !opts.manifest.no_trailing_newline {
writeln!(file)?;
}
file.flush()?;
crates/jrsonnet-cli/src/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/manifest.rs
+++ b/crates/jrsonnet-cli/src/manifest.rs
@@ -38,6 +38,9 @@
/// [default: 3 for json, 2 for yaml/toml]
#[clap(long)]
line_padding: Option<usize>,
+ /// No not add a trailing newline to the output
+ #[clap(long)]
+ pub no_trailing_newline: bool,
/// Preserve order in object manifestification
#[cfg(feature = "exp-preserve-order")]
#[clap(long)]
crates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth11 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 change15 /// the meaning).16 ///17 /// Default implementation returns `true`18 fn file_trailing_newline(&self) -> bool {19 true20 }21}14}22impl<T> ManifestFormat for Box<T>15impl<T> ManifestFormat for Box<T>23where16where27 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 &'_ T36where25where40 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}483349pub 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 false404 }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 false420 }421}400}422401423pub struct YamlStreamFormat<I> {402pub struct YamlStreamFormat<I> {