1mod ext;2mod manifest;3mod tla;4mod trace;56pub use ext::*;7pub use manifest::*;8pub use tla::*;9pub use trace::*;1011use clap::Parser;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(Parser)]20#[clap(next_help_heading = "INPUT")]21pub struct InputOpts {22 23 #[clap(long, short = 'e')]24 pub exec: bool,2526 27 pub input: String,28}2930#[derive(Parser)]31#[clap(next_help_heading = "OPTIONS")]32pub struct MiscOpts {33 34 35 36 37 #[clap(long)]38 no_stdlib: bool,3940 41 42 #[clap(long, short = 's', default_value = "200")]43 max_stack: usize,4445 46 47 48 49 #[clap(long, short = 'J', multiple_occurrences = true)]50 jpath: Vec<PathBuf>,51}52impl ConfigureState for MiscOpts {53 fn configure(&self, state: &EvaluationState) -> Result<()> {54 if !self.no_stdlib {55 state.with_stdlib();56 }5758 let mut library_paths = self.jpath.clone();59 library_paths.reverse();60 if let Some(path) = env::var_os("JSONNET_PATH") {61 library_paths.extend(env::split_paths(path.as_os_str()));62 }6364 state.set_import_resolver(Box::new(FileImportResolver { library_paths }));6566 state.set_max_stack(self.max_stack);67 Ok(())68 }69}707172#[derive(Parser)]73#[clap(name = "jrsonnet", version, author)]74pub struct GeneralOpts {75 #[clap(flatten)]76 misc: MiscOpts,7778 #[clap(flatten)]79 tla: TLAOpts,80 #[clap(flatten)]81 ext: ExtVarOpts,8283 #[clap(flatten)]84 trace: TraceOpts,85}8687impl ConfigureState for GeneralOpts {88 fn configure(&self, state: &EvaluationState) -> Result<()> {89 90 self.trace.configure(state)?;91 self.misc.configure(state)?;92 self.tla.configure(state)?;93 self.ext.configure(state)?;94 Ok(())95 }96}9798#[derive(Parser)]99#[clap(next_help_heading = "GARBAGE COLLECTION")]100pub struct GcOpts {101 102 #[clap(long)]103 gc_collect_on_exit: bool,104 105 #[clap(long)]106 gc_print_stats: bool,107 108 109 110 #[clap(long)]111 gc_collect_before_printing_stats: bool,112}113impl GcOpts {114 pub fn configure_global(&self) {115 if !self.gc_collect_on_exit {116 gcmodule::set_thread_collect_on_drop(false)117 }118 }119 pub fn stats_printer(&self) -> Option<GcStatsPrinter> {120 self.gc_print_stats.then(|| GcStatsPrinter {121 collect_before_printing_stats: self.gc_collect_before_printing_stats,122 })123 }124}125126pub struct GcStatsPrinter {127 collect_before_printing_stats: bool,128}129impl Drop for GcStatsPrinter {130 fn drop(&mut self) {131 eprintln!("=== GC STATS ===");132 if self.collect_before_printing_stats {133 let collected = gcmodule::collect_thread_cycles();134 eprintln!("Collected: {}", collected);135 }136 eprintln!("Tracked: {}", gcmodule::count_thread_tracked())137 }138}