git.delta.rocks / jrsonnet / refs/commits / 34a152fcae11

difftreelog

source

crates/jrsonnet-cli/src/lib.rs3.6 KiBsourcehistory
1mod ext;2mod manifest;3mod tla;4mod trace;56use std::{env, path::PathBuf};78use clap::Parser;9pub use ext::*;10use jrsonnet_evaluator::{error::Result, FileImportResolver, State};11use jrsonnet_gcmodule::with_thread_object_space;12pub use manifest::*;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	/// Disable standard library.35	/// By default standard library will be available via global `std` variable.36	/// Note that standard library will still be loaded37	/// if chosen manifestification method is not `none`.38	#[clap(long)]39	no_stdlib: bool,4041	/// Maximal allowed number of stack frames,42	/// stack overflow error will be raised if this number gets exceeded.43	#[clap(long, short = 's', default_value = "200")]44	max_stack: usize,4546	/// Library search dirs. (right-most wins)47	/// Any not found `imported` file will be searched in these.48	/// This can also be specified via `JSONNET_PATH` variable,49	/// which should contain a colon-separated (semicolon-separated on Windows) list of directories.50	#[clap(long, short = 'J', multiple_occurrences = true)]51	jpath: Vec<PathBuf>,52}53impl ConfigureState for MiscOpts {54	fn configure(&self, s: &State) -> Result<()> {55		if !self.no_stdlib {56			s.with_stdlib();57		}5859		let mut library_paths = self.jpath.clone();60		library_paths.reverse();61		if let Some(path) = env::var_os("JSONNET_PATH") {62			library_paths.extend(env::split_paths(path.as_os_str()));63		}6465		s.set_import_resolver(Box::new(FileImportResolver { library_paths }));6667		s.set_max_stack(self.max_stack);68		Ok(())69	}70}7172/// General configuration of jsonnet73#[derive(Parser)]74#[clap(name = "jrsonnet", version, author)]75pub struct GeneralOpts {76	#[clap(flatten)]77	misc: MiscOpts,7879	#[clap(flatten)]80	tla: TLAOpts,81	#[clap(flatten)]82	ext: ExtVarOpts,8384	#[clap(flatten)]85	trace: TraceOpts,86}8788impl ConfigureState for GeneralOpts {89	fn configure(&self, s: &State) -> Result<()> {90		// Configure trace first, because tla-code/ext-code can throw91		self.trace.configure(s)?;92		self.misc.configure(s)?;93		self.tla.configure(s)?;94		self.ext.configure(s)?;95		Ok(())96	}97}9899#[derive(Parser)]100#[clap(next_help_heading = "GARBAGE COLLECTION")]101pub struct GcOpts {102	/// Do not skip gc on exit103	#[clap(long)]104	gc_collect_on_exit: bool,105	/// Print gc stats before exit106	#[clap(long)]107	gc_print_stats: bool,108	/// Force garbage collection before printing stats109	/// Useful for checking for memory leaks110	/// Does nothing useless --gc-print-stats is specified111	#[clap(long)]112	gc_collect_before_printing_stats: bool,113}114impl GcOpts {115	pub fn stats_printer(&self) -> (Option<GcStatsPrinter>, Option<LeakSpace>) {116		(117			self.gc_print_stats.then(|| GcStatsPrinter {118				collect_before_printing_stats: self.gc_collect_before_printing_stats,119			}),120			(!self.gc_collect_on_exit).then(|| LeakSpace {}),121		)122	}123}124125pub struct LeakSpace {}126127impl Drop for LeakSpace {128	fn drop(&mut self) {129		with_thread_object_space(|s| s.leak())130	}131}132133pub struct GcStatsPrinter {134	collect_before_printing_stats: bool,135}136impl Drop for GcStatsPrinter {137	fn drop(&mut self) {138		eprintln!("=== GC STATS ===");139		if self.collect_before_printing_stats {140			let collected = jrsonnet_gcmodule::collect_thread_cycles();141			eprintln!("Collected: {}", collected);142		}143		eprintln!("Tracked: {}", jrsonnet_gcmodule::count_thread_tracked())144	}145}