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.rsdiffbeforeafterboth1use std::{2 fs::{File, create_dir_all},3 io::{Read, Write},4};56use clap::{CommandFactory, Parser};7use clap_complete::Shell;8use jrsonnet_cli::{GcOpts, ManifestOpts, MiscOpts, OutputOpts, StdOpts, TlaOpts, TraceOpts};9use jrsonnet_evaluator::{10 ResultExt, State, Val, apply_tla, bail,11 error::{Error as JrError, ErrorKind},12};13use jrsonnet_ir::{SourceDefaultIgnoreJpath, SourcePath};1415#[cfg(feature = "mimalloc")]16#[global_allocator]17static GLOBAL: mimallocator::Mimalloc = mimallocator::Mimalloc;1819#[derive(Parser)]20enum SubOpts {21 /// Generate completions for specified shell22 Generate {23 /// Target shell name24 shell: Shell,25 },26}2728#[derive(Parser)]29#[clap(next_help_heading = "DEBUG")]30struct DebugOpts {31 /// Required OS stack size.32 /// This shouldn't be changed unless jrsonnet is failing with stack overflow error.33 #[clap(long, name = "size")]34 pub os_stack: Option<usize>,35}3637#[derive(Parser)]38#[clap(next_help_heading = "INPUT")]39struct InputOpts {40 /// Treat input as code, evaluate it instead of reading file.41 #[clap(long, short = 'e')]42 pub exec: bool,4344 /// Path to the file to be compiled if `--exec` is unset, otherwise code itself.45 pub input: Option<String>,4647 /// After executing input, apply specified code.48 /// Output of the initial input will be accessible using `_`.49 #[cfg(feature = "exp-apply")]50 #[clap(long)]51 pub exp_apply: Vec<String>,52}5354/// Jsonnet commandline interpreter (Rust implementation)55#[derive(Parser)]56#[clap(57 args_conflicts_with_subcommands = true,58 disable_version_flag = true,59 version,60 author61)]62struct Opts {63 #[clap(subcommand)]64 sub: Option<SubOpts>,65 /// Print version66 #[clap(long)]67 version: bool,6869 #[clap(flatten)]70 input: InputOpts,71 #[clap(flatten)]72 misc: MiscOpts,73 #[clap(flatten)]74 tla: TlaOpts,75 #[clap(flatten)]76 std: StdOpts,77 #[clap(flatten)]78 gc: GcOpts,7980 #[clap(flatten)]81 trace: TraceOpts,82 #[clap(flatten)]83 manifest: ManifestOpts,84 #[clap(flatten)]85 output: OutputOpts,86 #[clap(flatten)]87 debug: DebugOpts,88}8990// TODO: Add unix_sigpipe = "sig_dfl"91fn main() {92 let opts: Opts = Opts::parse();9394 if opts.version {95 print!("{}", Opts::command().render_version());96 std::process::exit(0)97 }9899 if let Some(sub) = opts.sub {100 match sub {101 SubOpts::Generate { shell } => {102 use clap_complete::generate;103 let app = &mut Opts::command();104 let buf = &mut std::io::stdout();105 generate(shell, app, "jrsonnet", buf);106 std::process::exit(0)107 }108 }109 }110111 let success = if let Some(size) = opts.debug.os_stack {112 std::thread::Builder::new()113 .stack_size(size * 1024 * 1024)114 .spawn(|| main_catch(opts))115 .expect("new thread spawned")116 .join()117 .expect("thread finished successfully")118 } else {119 main_catch(opts)120 };121 if !success {122 std::process::exit(1);123 }124}125126#[derive(thiserror::Error, Debug)]127enum Error {128 // Handled differently129 #[error("evaluation error")]130 Evaluation(JrError),131 #[error("io error")]132 Io(#[from] std::io::Error),133 #[error("input is not utf8 encoded")]134 Utf8(#[from] std::str::Utf8Error),135 #[error("missing input argument")]136 MissingInputArgument,137}138impl From<JrError> for Error {139 fn from(e: JrError) -> Self {140 Self::Evaluation(e)141 }142}143impl From<ErrorKind> for Error {144 fn from(e: ErrorKind) -> Self {145 Self::from(JrError::from(e))146 }147}148149fn main_catch(opts: Opts) -> bool {150 let trace = opts.trace.trace_format();151 if let Err(e) = main_real(opts) {152 if let Error::Evaluation(e) = e {153 let mut out = String::new();154 trace.write_trace(&mut out, &e).expect("format error");155 eprintln!("{out}");156 } else {157 eprintln!("{e}");158 }159 return false;160 }161 true162}163164fn main_real(opts: Opts) -> Result<(), Error> {165 let _gc_leak_guard = opts.gc.leak_on_exit();166 let _gc_print_stats = opts.gc.stats_printer();167 let _stack_depth_override = opts.misc.stack_size_override();168169 let import_resolver = opts.misc.import_resolver();170 let std = opts.std.context_initializer()?;171172 let mut s = State::builder();173 s.import_resolver(import_resolver).context_initializer(std);174 let s = s.build();175 let _s = s.enter();176177 let input = opts.input.input.ok_or(Error::MissingInputArgument)?;178 let val = if opts.input.exec {179 s.evaluate_snippet("<cmdline>".to_owned(), &input as &str)?180 } else if input == "-" {181 let mut input = Vec::new();182 std::io::stdin().read_to_end(&mut input)?;183 let input_str = std::str::from_utf8(&input)?;184 s.evaluate_snippet("<stdin>".to_owned(), input_str)?185 } else {186 s.import_from(&SourcePath::new(SourceDefaultIgnoreJpath), input.as_str())?187 };188189 let tla = opts.tla.tla_opts()?;190 #[allow(191 // It is not redundant/unused in exp-apply192 unused_mut,193 clippy::redundant_clone,194 )]195 let mut val = apply_tla(&tla, val)?;196197 #[cfg(feature = "exp-apply")]198 for apply in opts.input.exp_apply {199 use jrsonnet_evaluator::{InitialUnderscore, Thunk};200 val = s.evaluate_snippet_with(201 "<exp_apply>".to_owned(),202 &apply,203 &InitialUnderscore(Thunk::evaluated(val)),204 )?;205 }206207 let manifest_format = opts.manifest.manifest_format();208 if let Some(multi) = opts.output.multi {209 if opts.output.create_output_dirs {210 let mut dir = multi.clone();211 dir.pop();212 create_dir_all(dir)?;213 }214 let Val::Obj(obj) = val else {215 bail!(216 "value should be object for --multi manifest, got {}",217 val.value_type()218 )219 };220 for (field, data) in obj.iter(221 #[cfg(feature = "exp-preserve-order")]222 opts.manifest.preserve_order,223 ) {224 let data = data.with_description(|| format!("getting field {field} for manifest"))?;225226 let mut path = multi.clone();227 path.push(&field as &str);228 if opts.output.create_output_dirs {229 let mut dir = path.clone();230 dir.pop();231 create_dir_all(dir)?;232 }233 println!("{}", path.to_str().expect("path"));234 let mut file = File::create(path)?;235 write!(236 file,237 "{}",238 data.manifest(&manifest_format)239 .with_description(|| format!("manifesting {field}"))?,240 )?;241 if manifest_format.file_trailing_newline() {242 writeln!(file)?;243 }244 file.flush()?;245 }246 } else if let Some(path) = opts.output.output_file {247 if opts.output.create_output_dirs {248 let mut dir = path.clone();249 dir.pop();250 create_dir_all(dir)?;251 }252 let mut file = File::create(path)?;253 writeln!(file, "{}", val.manifest(manifest_format)?)?;254 } else {255 let output = val.manifest(manifest_format)?;256 if !output.is_empty() {257 println!("{output}");258 }259 }260261 Ok(())262}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
}
}