diff --git a/cmds/jrsonnet/src/main.rs b/cmds/jrsonnet/src/main.rs index 2ecc5215..eb1bd1fa 100644 --- a/cmds/jrsonnet/src/main.rs +++ b/cmds/jrsonnet/src/main.rs @@ -186,7 +186,13 @@ fn main_real(s: &State, opts: Opts) -> Result<(), Error> { s.import(&input)? }; - let tla = opts.tla.tla_opts()?; + let (tla, tla_str_paths, tla_code_paths) = opts.tla.tla_opts()?; + for path in tla_str_paths { + s.import_str(path)?; + } + for path in tla_code_paths { + s.import(path)?; + } #[allow(unused_mut)] let mut val = apply_tla(s.clone(), &tla, val)?; diff --git a/crates/jrsonnet-cli/src/tla.rs b/crates/jrsonnet-cli/src/tla.rs index ddd906f3..2c2da487 100644 --- a/crates/jrsonnet-cli/src/tla.rs +++ b/crates/jrsonnet-cli/src/tla.rs @@ -6,8 +6,9 @@ use jrsonnet_evaluator::{ IStr, }; use jrsonnet_parser::{ParserSettings, Source}; +use std::path::PathBuf; -use crate::{ExtFile, ExtStr}; +use crate::ExtStr; #[derive(Parser)] #[clap(next_help_heading = "TOP LEVEL ARGUMENTS")] @@ -21,7 +22,7 @@ pub struct TlaOpts { /// 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, + tla_str_file: Vec, /// Add top level argument from code. /// See also `--tla-str` #[clap(long, name = "name[=tla source]", number_of_values = 1)] @@ -29,25 +30,15 @@ pub struct TlaOpts { /// 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, + tla_code_file: Vec, } impl TlaOpts { - pub fn tla_opts(&self) -> Result> { + pub fn tla_opts(&self) -> Result<(GcHashMap, &Vec, &Vec)> { 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))) - { + for (name, value) in self.tla_str.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))) - { + for (name, code) in self.tla_code.iter().map(|c| (&c.name, &c.value)) { let source = Source::new_virtual(format!("").into(), code.into()); out.insert( (name as &str).into(), @@ -65,6 +56,6 @@ impl TlaOpts { ), ); } - Ok(out) + Ok((out, &self.tla_str_file, &self.tla_code_file)) } } diff --git a/crates/jrsonnet-evaluator/src/lib.rs b/crates/jrsonnet-evaluator/src/lib.rs index ce0692fa..33387473 100644 --- a/crates/jrsonnet-evaluator/src/lib.rs +++ b/crates/jrsonnet-evaluator/src/lib.rs @@ -393,6 +393,10 @@ impl State { let resolved = self.resolve(path)?; self.import_resolved(resolved) } + pub fn import_str(&self, path: impl AsRef) -> Result { + let resolved = self.resolve(path)?; + self.import_resolved_str(resolved) + } /// Creates context with all passed global variables pub fn create_default_context(&self, source: Source) -> Context { @@ -558,13 +562,17 @@ impl State { /// Settings utilities impl State { - // Only panics in case of [`ImportResolver`] contract violation + // # Panics + // + // If [`ImportResolver`] contract is violated. #[allow(clippy::missing_panics_doc)] pub fn resolve_from(&self, from: &SourcePath, path: &str) -> Result { self.import_resolver().resolve_from(from, path.as_ref()) } - // Only panics in case of [`ImportResolver`] contract violation + // # Panics + // + // If [`ImportResolver`] contract is violated. #[allow(clippy::missing_panics_doc)] pub fn resolve(&self, path: impl AsRef) -> Result { self.import_resolver().resolve(path.as_ref()) diff --git a/crates/jrsonnet-stdlib/src/lib.rs b/crates/jrsonnet-stdlib/src/lib.rs index c5dcea12..7c53dee5 100644 --- a/crates/jrsonnet-stdlib/src/lib.rs +++ b/crates/jrsonnet-stdlib/src/lib.rs @@ -352,8 +352,6 @@ impl jrsonnet_evaluator::ContextInitializer for ContextInitializer { } #[cfg(feature = "legacy-this-file")] fn populate(&self, source: Source, builder: &mut ContextBuilder) { - use jrsonnet_evaluator::val::StrValue; - let mut std = ObjValueBuilder::new(); std.with_super(self.stdlib_obj.clone()); std.field("thisFile")