git.delta.rocks / jrsonnet / refs/commits / 68c8ac05879f

difftreelog

source

crates/jrsonnet-cli/src/lib.rs3.5 KiBsourcehistory
1mod manifest;2mod stdlib;3mod tla;4mod trace;56use std::{env, path::PathBuf};78use clap::Parser;9use jrsonnet_evaluator::{error::Result, FileImportResolver, State};10use jrsonnet_gcmodule::with_thread_object_space;11pub use manifest::*;12pub use stdlib::*;13pub use tla::*;14pub use trace::*;1516pub trait ConfigureState {17	fn configure(&self, s: &State) -> Result<()>;18}1920#[derive(Parser)]21#[clap(next_help_heading = "INPUT")]22pub struct InputOpts {23	/// Treat input as code, evaluate them instead of reading file24	#[clap(long, short = 'e')]25	pub exec: bool,2627	/// Path to the file to be compiled if `--evaluate` is unset, otherwise code itself28	pub input: String,29}3031#[derive(Parser)]32#[clap(next_help_heading = "OPTIONS")]33pub struct MiscOpts {34	/// Maximal allowed number of stack frames,35	/// stack overflow error will be raised if this number gets exceeded.36	#[clap(long, short = 's', default_value = "200")]37	max_stack: usize,3839	/// Library search dirs. (right-most wins)40	/// Any not found `imported` file will be searched in these.41	/// This can also be specified via `JSONNET_PATH` variable,42	/// which should contain a colon-separated (semicolon-separated on Windows) list of directories.43	#[clap(long, short = 'J', multiple_occurrences = true)]44	jpath: Vec<PathBuf>,45}46impl ConfigureState for MiscOpts {47	fn configure(&self, s: &State) -> Result<()> {48		let mut library_paths = self.jpath.clone();49		library_paths.reverse();50		if let Some(path) = env::var_os("JSONNET_PATH") {51			library_paths.extend(env::split_paths(path.as_os_str()));52		}5354		s.set_import_resolver(Box::new(FileImportResolver { library_paths }));5556		s.set_max_stack(self.max_stack);57		Ok(())58	}59}6061/// General configuration of jsonnet62#[derive(Parser)]63#[clap(name = "jrsonnet", version, author)]64pub struct GeneralOpts {65	#[clap(flatten)]66	misc: MiscOpts,6768	#[clap(flatten)]69	tla: TLAOpts,70	#[clap(flatten)]71	std: StdOpts,7273	#[clap(flatten)]74	trace: TraceOpts,75}7677impl ConfigureState for GeneralOpts {78	fn configure(&self, s: &State) -> Result<()> {79		// Configure trace first, because tla-code/ext-code can throw80		self.trace.configure(s)?;81		self.misc.configure(s)?;82		self.tla.configure(s)?;83		self.std.configure(s)?;84		Ok(())85	}86}8788#[derive(Parser)]89#[clap(next_help_heading = "GARBAGE COLLECTION")]90pub struct GcOpts {91	/// Do not skip gc on exit92	#[clap(long)]93	gc_collect_on_exit: bool,94	/// Print gc stats before exit95	#[clap(long)]96	gc_print_stats: bool,97	/// Force garbage collection before printing stats98	/// Useful for checking for memory leaks99	/// Does nothing useless --gc-print-stats is specified100	#[clap(long)]101	gc_collect_before_printing_stats: bool,102}103impl GcOpts {104	pub fn stats_printer(&self) -> (Option<GcStatsPrinter>, Option<LeakSpace>) {105		// Constructed structs have side-effects in Drop impl106		#[allow(clippy::unnecessary_lazy_evaluations)]107		(108			self.gc_print_stats.then(|| GcStatsPrinter {109				collect_before_printing_stats: self.gc_collect_before_printing_stats,110			}),111			(!self.gc_collect_on_exit).then(|| LeakSpace {}),112		)113	}114}115116pub struct LeakSpace {}117118impl Drop for LeakSpace {119	fn drop(&mut self) {120		with_thread_object_space(|s| s.leak())121	}122}123124pub struct GcStatsPrinter {125	collect_before_printing_stats: bool,126}127impl Drop for GcStatsPrinter {128	fn drop(&mut self) {129		eprintln!("=== GC STATS ===");130		if self.collect_before_printing_stats {131			let collected = jrsonnet_gcmodule::collect_thread_cycles();132			eprintln!("Collected: {}", collected);133		}134		eprintln!("Tracked: {}", jrsonnet_gcmodule::count_thread_tracked())135	}136}