git.delta.rocks / jrsonnet / refs/commits / c368769dca4a

difftreelog

source

crates/jrsonnet-cli/src/tla.rs2.0 KiBsourcehistory
1use clap::Parser;2use jrsonnet_evaluator::{3	error::{ErrorKind, Result},4	function::TlaArg,5	gc::WithCapacityExt as _,6	rustc_hash::FxHashMap,7	IStr,8};9use jrsonnet_parser::{ParserSettings, Source};1011use crate::{ExtFile, ExtStr};1213#[derive(Parser)]14#[clap(next_help_heading = "TOP LEVEL ARGUMENTS")]15pub struct TlaOpts {16	/// Add top level string argument.17	/// Top level arguments will be passed to function before manifestification stage.18	/// This is preferred to [`ExtVars`] method.19	/// If [=data] is not set then it will be read from `name` env variable.20	#[clap(long, short = 'A', name = "name[=tla data]", number_of_values = 1)]21	tla_str: Vec<ExtStr>,22	/// Read top level argument string from file.23	/// See also `--tla-str`24	#[clap(long, name = "name=tla path", number_of_values = 1)]25	tla_str_file: Vec<ExtFile>,26	/// Add top level argument from code.27	/// See also `--tla-str`28	#[clap(long, name = "name[=tla source]", number_of_values = 1)]29	tla_code: Vec<ExtStr>,30	/// Read top level argument code from file.31	/// See also `--tla-str`32	#[clap(long, name = "name=tla code path", number_of_values = 1)]33	tla_code_file: Vec<ExtFile>,34}35impl TlaOpts {36	pub fn tla_opts(&self) -> Result<FxHashMap<IStr, TlaArg>> {37		let mut out = FxHashMap::new();38		for (name, value) in self39			.tla_str40			.iter()41			.map(|c| (&c.name, &c.value))42			.chain(self.tla_str_file.iter().map(|c| (&c.name, &c.value)))43		{44			out.insert(name.into(), TlaArg::String(value.into()));45		}46		for (name, code) in self47			.tla_code48			.iter()49			.map(|c| (&c.name, &c.value))50			.chain(self.tla_code_file.iter().map(|c| (&c.name, &c.value)))51		{52			let source = Source::new_virtual(format!("<top-level-arg:{name}>").into(), code.into());53			out.insert(54				(name as &str).into(),55				TlaArg::Code(56					jrsonnet_parser::parse(57						code,58						&ParserSettings {59							source: source.clone(),60						},61					)62					.map_err(|e| ErrorKind::ImportSyntaxError {63						path: source,64						error: Box::new(e),65					})?,66				),67			);68		}69		Ok(out)70	}71}