1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use clap::Parser;
use jrsonnet_evaluator::{
	error::{ErrorKind, Result},
	function::TlaArg,
	gc::GcHashMap,
	IStr, State,
};
use jrsonnet_parser::{ParserSettings, Source};

use crate::{ConfigureState, ExtFile, ExtStr};

#[derive(Parser)]
#[clap(next_help_heading = "TOP LEVEL ARGUMENTS")]
pub struct TlaOpts {
	/// Add top level string argument.
	/// Top level arguments will be passed to function before manifestification stage.
	/// This is preferred to ExtVars method.
	/// If [=data] is not set then it will be read from `name` env variable.
	#[clap(long, short = 'A', name = "name[=tla data]", number_of_values = 1)]
	tla_str: Vec<ExtStr>,
	/// Read top level argument string from file.
	/// See also `--tla-str`
	#[clap(long, name = "name=tla path", number_of_values = 1)]
	tla_str_file: Vec<ExtFile>,
	/// Add top level argument from code.
	/// See also `--tla-str`
	#[clap(long, name = "name[=tla source]", number_of_values = 1)]
	tla_code: Vec<ExtStr>,
	/// Read top level argument code from file.
	/// See also `--tla-str`
	#[clap(long, name = "name=tla code path", number_of_values = 1)]
	tla_code_file: Vec<ExtFile>,
}
impl ConfigureState for TlaOpts {
	type Guards = GcHashMap<IStr, TlaArg>;
	fn configure(&self, _s: &State) -> Result<Self::Guards> {
		let mut out = GcHashMap::new();
		for (name, value) in self
			.tla_str
			.iter()
			.map(|c| (&c.name, &c.value))
			.chain(self.tla_str_file.iter().map(|c| (&c.name, &c.value)))
		{
			out.insert(name.into(), TlaArg::String(value.into()));
		}
		for (name, code) in self
			.tla_code
			.iter()
			.map(|c| (&c.name, &c.value))
			.chain(self.tla_code_file.iter().map(|c| (&c.name, &c.value)))
		{
			let source = Source::new_virtual(format!("<top-level-arg:{name}>").into(), code.into());
			out.insert(
				(name as &str).into(),
				TlaArg::Code(
					jrsonnet_parser::parse(
						code,
						&ParserSettings {
							source: source.clone(),
						},
					)
					.map_err(|e| ErrorKind::ImportSyntaxError {
						path: source,
						error: Box::new(e),
					})?,
				),
			);
		}
		Ok(out)
	}
}