1use clap::Parser;
2use jrsonnet_evaluator::{
3 error::{ErrorKind, Result},
4 function::TlaArg,
5 gc::GcHashMap,
6 IStr,
7};
8use jrsonnet_parser::{ParserSettings, Source};
9
10use crate::{ExtFile, ExtStr};
11
12#[derive(Parser)]
13#[clap(next_help_heading = "TOP LEVEL ARGUMENTS")]
14pub struct TlaOpts {
15 #[clap(long, short = 'A', name = "name[=tla data]", number_of_values = 1)]
20 tla_str: Vec<ExtStr>,
21 #[clap(long, name = "name=tla path", number_of_values = 1)]
24 tla_str_file: Vec<ExtFile>,
25 #[clap(long, name = "name[=tla source]", number_of_values = 1)]
28 tla_code: Vec<ExtStr>,
29 #[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 self
38 .tla_str
39 .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 self
46 .tla_code
47 .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}