git.delta.rocks / jrsonnet / refs/commits / 99a6e6f99f1b

difftreelog

source

crates/jrsonnet-cli/src/lib.rs4.0 KiBsourcehistory
1mod ext;2mod manifest;3mod tla;4mod trace;56pub use ext::*;7pub use manifest::*;8pub use tla::*;9pub use trace::*;1011use clap::Clap;12use jrsonnet_evaluator::{error::Result, EvaluationState, FileImportResolver};13use std::{env, path::PathBuf};1415pub trait ConfigureState {16	fn configure(&self, state: &EvaluationState) -> Result<()>;17}1819#[derive(Clap)]20#[clap(help_heading = "INPUT")]21pub struct InputOpts {22	#[clap(23		long,24		short = 'e',25		about = "Treat input as code, evaluate them instead of reading file"26	)]27	pub exec: bool,2829	#[clap(30		about = "Path to the file to be compiled if `--evaluate` is unset, otherwise code itself"31	)]32	pub input: String,33}3435#[derive(Clap)]36#[clap(help_heading = "OPTIONS")]37pub struct MiscOpts {38	/// Disable standard library.39	/// By default standard library will be available via global `std` variable.40	/// Note that standard library will still be loaded41	/// if chosen manifestification method is not `none`.42	#[clap(long)]43	no_stdlib: bool,4445	/// Maximal allowed number of stack frames,46	/// stack overflow error will be raised if this number gets exceeded.47	#[clap(long, short = 's', default_value = "200")]48	max_stack: usize,4950	/// Library search dirs. (right-most wins)51	/// Any not found `imported` file will be searched in these.52	/// This can also be specified via `JSONNET_PATH` variable,53	/// which should contain a colon-separated (semicolon-separated on Windows) list of directories.54	#[clap(long, short = 'J', multiple_occurrences = true)]55	jpath: Vec<PathBuf>,56}57impl ConfigureState for MiscOpts {58	fn configure(&self, state: &EvaluationState) -> Result<()> {59		if !self.no_stdlib {60			state.with_stdlib();61		}6263		let mut library_paths = self.jpath.clone();64		library_paths.reverse();65		if let Some(path) = env::var_os("JSONNET_PATH") {66			library_paths.extend(env::split_paths(path.as_os_str()));67		}6869		state.set_import_resolver(Box::new(FileImportResolver { library_paths }));7071		state.set_max_stack(self.max_stack);72		Ok(())73	}74}7576/// General configuration of jsonnet77#[derive(Clap)]78#[clap(name = "jrsonnet", version, author)]79pub struct GeneralOpts {80	#[clap(flatten)]81	misc: MiscOpts,8283	#[clap(flatten)]84	tla: TLAOpts,85	#[clap(flatten)]86	ext: ExtVarOpts,8788	#[clap(flatten)]89	trace: TraceOpts,90}9192impl ConfigureState for GeneralOpts {93	fn configure(&self, state: &EvaluationState) -> Result<()> {94		// Configure trace first, because tla-code/ext-code can throw95		self.trace.configure(state)?;96		self.misc.configure(state)?;97		self.tla.configure(state)?;98		self.ext.configure(state)?;99		Ok(())100	}101}102103#[derive(Clap)]104#[clap(help_heading = "GARBAGE COLLECTION")]105pub struct GcOpts {106	/// Min bytes allocated to start garbage collection107	#[clap(long, default_value = "20000000")]108	gc_initial_threshold: usize,109	/// How much heap should grow after unsuccessful garbage collection110	#[clap(long)]111	gc_used_space_ratio: Option<f64>,112	/// Do not skip gc on exit113	#[clap(long)]114	gc_collect_on_exit: bool,115	/// Print gc stats before exit116	#[clap(long)]117	gc_print_stats: bool,118	/// Force garbage collection before printing stats119	/// Useful for checking for memory leaks120	/// Does nothing useless --gc-print-stats is specified121	#[clap(long)]122	gc_collect_before_printing_stats: bool,123}124impl GcOpts {125	pub fn stats_printer(&self) -> Option<GcStatsPrinter> {126		self.gc_print_stats127			.then(|| GcStatsPrinter(self.gc_collect_before_printing_stats))128	}129	pub fn configure_global(&self) {130		jrsonnet_gc::configure(|config| {131			config.leak_on_drop = !self.gc_collect_on_exit;132			config.threshold = self.gc_initial_threshold;133			if let Some(used_space_ratio) = self.gc_used_space_ratio {134				config.used_space_ratio = used_space_ratio;135			}136		});137	}138}139pub struct GcStatsPrinter(bool);140impl Drop for GcStatsPrinter {141	fn drop(&mut self) {142		if self.0 {143			jrsonnet_gc::force_collect()144		}145		eprintln!("=== GC STATS ===");146		jrsonnet_gc::configure(|c| {147			eprintln!("Final threshold: {:?}", c.threshold);148		});149		let stats = jrsonnet_gc::stats();150		eprintln!("Collections performed: {}", stats.collections_performed);151		eprintln!("Bytes still allocated: {}", stats.bytes_allocated);152	}153}