difftreelog
feat(cli) --no-trailing-newline
in: master
4 files changed
bindings/jsonnet/src/lib.rsdiffbeforeafterboth44/// 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}505051unsafe 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}185186/// 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}183191184/// 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 will185/// 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}314325315fn 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}398412399fn 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;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.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/manifest.rs
@@ -11,13 +11,6 @@
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<T> ManifestFormat for Box<T>
where
@@ -26,10 +19,6 @@
fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {
let inner = &**self;
inner.manifest_buf(val, buf)
- }
- fn file_trailing_newline(&self) -> bool {
- let inner = &**self;
- inner.file_trailing_newline()
}
}
impl<T> ManifestFormat for &'_ T
@@ -40,10 +29,6 @@
let inner = &**self;
inner.manifest_buf(val, buf)
}
- fn file_trailing_newline(&self) -> bool {
- let inner = &**self;
- inner.file_trailing_newline()
- }
}
pub struct BlackBoxFormat;
@@ -398,9 +383,6 @@
return Ok(());
}
JSON_TO_STRING.manifest_buf(val, out)
- }
- fn file_trailing_newline(&self) -> bool {
- false
}
}
pub struct StringFormat;
@@ -414,9 +396,6 @@
};
write!(out, "{s}").unwrap();
Ok(())
- }
- fn file_trailing_newline(&self) -> bool {
- false
}
}