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

difftreelog

source

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