Skip to content

Commit 3baf492

Browse files
committed
refactor: use State::import(..) for file-sourced TLAs
1 parent e7e620b commit 3baf492

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

cmds/jrsonnet/src/main.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,13 @@ fn main_real(s: &State, opts: Opts) -> Result<(), Error> {
186186
s.import(&input)?
187187
};
188188

189-
let tla = opts.tla.tla_opts()?;
189+
let (tla, tla_str_paths, tla_code_paths) = opts.tla.tla_opts()?;
190+
for path in tla_str_paths {
191+
s.import_str(path)?;
192+
}
193+
for path in tla_code_paths {
194+
s.import(path)?;
195+
}
190196
#[allow(unused_mut)]
191197
let mut val = apply_tla(s.clone(), &tla, val)?;
192198

crates/jrsonnet-cli/src/tla.rs

+8-17
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ use jrsonnet_evaluator::{
66
IStr,
77
};
88
use jrsonnet_parser::{ParserSettings, Source};
9+
use std::path::PathBuf;
910

10-
use crate::{ExtFile, ExtStr};
11+
use crate::ExtStr;
1112

1213
#[derive(Parser)]
1314
#[clap(next_help_heading = "TOP LEVEL ARGUMENTS")]
@@ -21,33 +22,23 @@ pub struct TlaOpts {
2122
/// Read top level argument string from file.
2223
/// See also `--tla-str`
2324
#[clap(long, name = "name=tla path", number_of_values = 1)]
24-
tla_str_file: Vec<ExtFile>,
25+
tla_str_file: Vec<PathBuf>,
2526
/// Add top level argument from code.
2627
/// See also `--tla-str`
2728
#[clap(long, name = "name[=tla source]", number_of_values = 1)]
2829
tla_code: Vec<ExtStr>,
2930
/// Read top level argument code from file.
3031
/// See also `--tla-str`
3132
#[clap(long, name = "name=tla code path", number_of_values = 1)]
32-
tla_code_file: Vec<ExtFile>,
33+
tla_code_file: Vec<PathBuf>,
3334
}
3435
impl TlaOpts {
35-
pub fn tla_opts(&self) -> Result<GcHashMap<IStr, TlaArg>> {
36+
pub fn tla_opts(&self) -> Result<(GcHashMap<IStr, TlaArg>, &Vec<PathBuf>, &Vec<PathBuf>)> {
3637
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-
{
38+
for (name, value) in self.tla_str.iter().map(|c| (&c.name, &c.value)) {
4339
out.insert(name.into(), TlaArg::String(value.into()));
4440
}
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-
{
41+
for (name, code) in self.tla_code.iter().map(|c| (&c.name, &c.value)) {
5142
let source = Source::new_virtual(format!("<top-level-arg:{name}>").into(), code.into());
5243
out.insert(
5344
(name as &str).into(),
@@ -65,6 +56,6 @@ impl TlaOpts {
6556
),
6657
);
6758
}
68-
Ok(out)
59+
Ok((out, &self.tla_str_file, &self.tla_code_file))
6960
}
7061
}

crates/jrsonnet-evaluator/src/lib.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,10 @@ impl State {
393393
let resolved = self.resolve(path)?;
394394
self.import_resolved(resolved)
395395
}
396+
pub fn import_str(&self, path: impl AsRef<Path>) -> Result<IStr> {
397+
let resolved = self.resolve(path)?;
398+
self.import_resolved_str(resolved)
399+
}
396400

397401
/// Creates context with all passed global variables
398402
pub fn create_default_context(&self, source: Source) -> Context {
@@ -558,13 +562,17 @@ impl State {
558562

559563
/// Settings utilities
560564
impl State {
561-
// Only panics in case of [`ImportResolver`] contract violation
565+
// # Panics
566+
//
567+
// If [`ImportResolver`] contract is violated.
562568
#[allow(clippy::missing_panics_doc)]
563569
pub fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
564570
self.import_resolver().resolve_from(from, path.as_ref())
565571
}
566572

567-
// Only panics in case of [`ImportResolver`] contract violation
573+
// # Panics
574+
//
575+
// If [`ImportResolver`] contract is violated.
568576
#[allow(clippy::missing_panics_doc)]
569577
pub fn resolve(&self, path: impl AsRef<Path>) -> Result<SourcePath> {
570578
self.import_resolver().resolve(path.as_ref())

crates/jrsonnet-stdlib/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,6 @@ impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {
352352
}
353353
#[cfg(feature = "legacy-this-file")]
354354
fn populate(&self, source: Source, builder: &mut ContextBuilder) {
355-
use jrsonnet_evaluator::val::StrValue;
356-
357355
let mut std = ObjValueBuilder::new();
358356
std.with_super(self.stdlib_obj.clone());
359357
std.field("thisFile")

0 commit comments

Comments
 (0)