Skip to content

Commit

Permalink
refactor: use State::import(..) for file-sourced TLAs
Browse files Browse the repository at this point in the history
  • Loading branch information
JarvisCraft committed Nov 1, 2023
1 parent e7e620b commit 3baf492
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
8 changes: 7 additions & 1 deletion cmds/jrsonnet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;

Expand Down
25 changes: 8 additions & 17 deletions crates/jrsonnet-cli/src/tla.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -21,33 +22,23 @@ 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<ExtFile>,
tla_str_file: Vec<PathBuf>,
/// 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>,
tla_code_file: Vec<PathBuf>,
}
impl TlaOpts {
pub fn tla_opts(&self) -> Result<GcHashMap<IStr, TlaArg>> {
pub fn tla_opts(&self) -> Result<(GcHashMap<IStr, TlaArg>, &Vec<PathBuf>, &Vec<PathBuf>)> {

Check warning on line 36 in crates/jrsonnet-cli/src/tla.rs

View workflow job for this annotation

GitHub Actions / clippy

very complex type used. Consider factoring parts into `type` definitions

warning: very complex type used. Consider factoring parts into `type` definitions --> crates/jrsonnet-cli/src/tla.rs:36:28 | 36 | pub fn tla_opts(&self) -> Result<(GcHashMap<IStr, TlaArg>, &Vec<PathBuf>, &Vec<PathBuf>)> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#type_complexity = note: `#[warn(clippy::type_complexity)]` on by default
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!("<top-level-arg:{name}>").into(), code.into());
out.insert(
(name as &str).into(),
Expand All @@ -65,6 +56,6 @@ impl TlaOpts {
),
);
}
Ok(out)
Ok((out, &self.tla_str_file, &self.tla_code_file))
}
}
12 changes: 10 additions & 2 deletions crates/jrsonnet-evaluator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@ impl State {
let resolved = self.resolve(path)?;
self.import_resolved(resolved)
}
pub fn import_str(&self, path: impl AsRef<Path>) -> Result<IStr> {
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 {
Expand Down Expand Up @@ -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<SourcePath> {
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<Path>) -> Result<SourcePath> {
self.import_resolver().resolve(path.as_ref())
Expand Down
2 changes: 0 additions & 2 deletions crates/jrsonnet-stdlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 3baf492

Please sign in to comment.