difftreelog
refactor saner imports from TLA/std.extVars
in: master
13 files changed
bindings/jsonnet/src/import.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/import.rs
+++ b/bindings/jsonnet/src/import.rs
@@ -14,7 +14,7 @@
use jrsonnet_evaluator::{
bail,
error::{ErrorKind::*, Result},
- ImportResolver,
+ AsPathLike, ImportResolver, ResolvePath,
};
use jrsonnet_gcmodule::Acyclic;
use jrsonnet_parser::{SourceDirectory, SourceFile, SourcePath};
@@ -38,7 +38,7 @@
out: RefCell<HashMap<SourcePath, Vec<u8>>>,
}
impl ImportResolver for CallbackImportResolver {
- fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
+ fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {
let base = if let Some(p) = from.downcast_ref::<SourceFile>() {
let mut o = p.path().to_owned();
o.pop();
@@ -51,7 +51,11 @@
unreachable!("can't resolve this path");
};
let base = unsafe { crate::unparse_path(&base) };
- let rel = CString::new(path).unwrap();
+ let rel = path.as_path();
+ let rel = match rel {
+ ResolvePath::Str(s) => CString::new(s.as_bytes()).unwrap(),
+ ResolvePath::Path(p) => unsafe { crate::unparse_path(p) },
+ };
let found_here: *mut c_char = null_mut();
let mut buf = null_mut();
bindings/jsonnet/src/lib.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -28,7 +28,7 @@
rustc_hash::FxHashMap,
stack::set_stack_depth_limit,
trace::{CompactFormat, PathResolver, TraceFormat},
- FileImportResolver, IStr, ImportResolver, Result, State, Val,
+ AsPathLike, FileImportResolver, IStr, ImportResolver, Result, State, Val,
};
use jrsonnet_gcmodule::Acyclic;
use jrsonnet_parser::SourcePath;
@@ -62,18 +62,18 @@
}
}
-unsafe fn unparse_path(input: &Path) -> Cow<'_, CStr> {
+unsafe fn unparse_path(input: &Path) -> CString {
#[cfg(target_family = "unix")]
{
use std::os::unix::ffi::OsStrExt;
let str = CString::new(input.as_os_str().as_bytes()).expect("input has zero byte in it");
- Cow::Owned(str)
+ str
}
#[cfg(not(target_family = "unix"))]
{
let str = input.as_os_str().to_str().expect("bad utf-8");
let cstr = CString::new(str).expect("input has NUL inside");
- Cow::Owned(cstr)
+ cstr
}
}
@@ -93,16 +93,12 @@
self.inner.borrow().load_file_contents(resolved)
}
- fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
+ fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {
self.inner.borrow().resolve_from(from, path)
}
- fn resolve_from_default(&self, path: &str) -> Result<SourcePath> {
+ fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {
self.inner.borrow().resolve_from_default(path)
- }
-
- fn resolve(&self, path: &Path) -> Result<SourcePath> {
- self.inner.borrow().resolve(path)
}
}
bindings/jsonnet/src/vars_tlas.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/vars_tlas.rs
+++ b/bindings/jsonnet/src/vars_tlas.rs
@@ -3,7 +3,6 @@
use std::{ffi::CStr, os::raw::c_char};
use jrsonnet_evaluator::{function::TlaArg, IStr};
-use jrsonnet_parser::{ParserSettings, Source};
use crate::VM;
@@ -84,14 +83,7 @@
let code = unsafe { CStr::from_ptr(code) };
let name: IStr = name.to_str().expect("name is not utf-8").into();
- let code: IStr = code.to_str().expect("code is not utf-8").into();
- let code = jrsonnet_parser::parse(
- &code,
- &ParserSettings {
- source: Source::new_virtual(format!("<top-level-arg:{name}>").into(), code.clone()),
- },
- )
- .expect("can't parse TLA code");
+ let code: String = code.to_str().expect("code is not utf-8").to_owned();
- vm.tla_args.insert(name, TlaArg::Code(code));
+ vm.tla_args.insert(name, TlaArg::InlineCode(code));
}
cmds/jrsonnet/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -182,7 +182,7 @@
let input_str = std::str::from_utf8(&input)?;
s.evaluate_snippet("<stdin>".to_owned(), input_str)?
} else {
- s.import(&input)?
+ s.import(input.as_str())?
};
let tla = opts.tla.tla_opts()?;
crates/jrsonnet-cli/src/stdlib.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/stdlib.rs
+++ b/crates/jrsonnet-cli/src/stdlib.rs
@@ -1,7 +1,7 @@
-use std::{fs::read_to_string, str::FromStr};
+use std::str::FromStr;
use clap::Parser;
-use jrsonnet_evaluator::{trace::PathResolver, Result};
+use jrsonnet_evaluator::{function::TlaArg, trace::PathResolver, Result};
use jrsonnet_stdlib::ContextInitializer;
#[derive(Clone)]
@@ -54,25 +54,20 @@
#[derive(Clone)]
pub struct ExtFile {
pub name: String,
- pub value: String,
+ pub path: String,
}
impl FromStr for ExtFile {
type Err = String;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
- let out: Vec<&str> = s.split('=').collect();
- if out.len() != 2 {
+ let Some((name, path)) = s.split_once('=') else {
return Err("bad ext-file syntax".to_owned());
- }
- let file = read_to_string(out[1]);
- match file {
- Ok(content) => Ok(Self {
- name: out[0].into(),
- value: content,
- }),
- Err(e) => Err(format!("{e}")),
- }
+ };
+ Ok(Self {
+ name: name.into(),
+ path: path.into(),
+ })
}
}
@@ -110,16 +105,27 @@
}
let ctx = ContextInitializer::new(PathResolver::new_cwd_fallback());
for ext in &self.ext_str {
- ctx.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());
+ ctx.settings_mut().ext_vars.insert(
+ ext.name.as_str().into(),
+ TlaArg::String(ext.value.as_str().into()),
+ );
}
for ext in &self.ext_str_file {
- ctx.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());
+ ctx.settings_mut().ext_vars.insert(
+ ext.name.as_str().into(),
+ TlaArg::ImportStr(ext.path.clone()),
+ );
}
for ext in &self.ext_code {
- ctx.add_ext_code(&ext.name as &str, &ext.value as &str)?;
+ ctx.settings_mut().ext_vars.insert(
+ ext.name.as_str().into(),
+ TlaArg::InlineCode(ext.value.clone()),
+ );
}
for ext in &self.ext_code_file {
- ctx.add_ext_code(&ext.name as &str, &ext.value as &str)?;
+ ctx.settings_mut()
+ .ext_vars
+ .insert(ext.name.as_str().into(), TlaArg::Import(ext.path.clone()));
}
Ok(Some(ctx))
}
crates/jrsonnet-cli/src/tla.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/tla.rs
+++ b/crates/jrsonnet-cli/src/tla.rs
@@ -1,12 +1,5 @@
use clap::Parser;
-use jrsonnet_evaluator::{
- error::{ErrorKind, Result},
- function::TlaArg,
- gc::WithCapacityExt as _,
- rustc_hash::FxHashMap,
- IStr,
-};
-use jrsonnet_parser::{ParserSettings, Source};
+use jrsonnet_evaluator::{IStr, error::Result, function::TlaArg, gc::WithCapacityExt as _, rustc_hash::FxHashMap};
use crate::{ExtFile, ExtStr};
@@ -35,37 +28,27 @@
impl TlaOpts {
pub fn tla_opts(&self) -> Result<FxHashMap<IStr, TlaArg>> {
let mut out = FxHashMap::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 ext in &self.tla_str {
+ out.insert(
+ ext.name.as_str().into(),
+ TlaArg::String(ext.value.as_str().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());
+ for ext in &self.tla_str_file {
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),
- })?,
- ),
+ ext.name.as_str().into(),
+ TlaArg::ImportStr(ext.name.as_str().into()),
+ );
+ }
+ for ext in &self.tla_code {
+ out.insert(
+ ext.name.as_str().into(),
+ TlaArg::InlineCode(ext.value.clone()),
);
}
+ for ext in &self.tla_code_file {
+ out.insert(ext.name.as_str().into(), TlaArg::Import(ext.path.clone()));
+ }
Ok(out)
}
}
crates/jrsonnet-evaluator/src/async_import.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/async_import.rs
+++ b/crates/jrsonnet-evaluator/src/async_import.rs
@@ -1,7 +1,6 @@
-use std::{any::Any, cell::RefCell, future::Future, path::Path};
+use std::{any::Any, cell::RefCell, future::Future};
use jrsonnet_gcmodule::Acyclic;
-use jrsonnet_interner::IStr;
use jrsonnet_parser::{
ArgsDesc, AssertStmt, BindSpec, CompSpec, Destruct, Expr, FieldMember, FieldName, ForSpecData,
IfSpecData, LocExpr, Member, ObjBody, Param, ParamsDesc, ParserSettings, SliceDesc, Source,
@@ -9,10 +8,10 @@
};
use rustc_hash::FxHashMap;
-use crate::{bail, FileData, ImportResolver, State};
+use crate::{AsPathLike, FileData, ImportResolver, ResolvePathOwned, State};
pub struct Import {
- path: IStr,
+ path: ResolvePathOwned,
expression: bool,
}
@@ -137,7 +136,7 @@
Expr::Import(v) | Expr::ImportStr(v) | Expr::ImportBin(v) => {
if let Expr::Str(s) = &*v.expr() {
out.0.push(Import {
- path: s.clone(),
+ path: ResolvePathOwned::Str(s.to_string()),
expression: matches!(&*expr.expr(), Expr::Import(_)),
});
}
@@ -229,16 +228,14 @@
fn resolve_from(
&self,
from: &SourcePath,
- path: &str,
+ path: &dyn AsPathLike,
) -> impl Future<Output = Result<SourcePath, Self::Error>>;
fn resolve_from_default(
&self,
- path: &str,
+ path: &dyn AsPathLike,
) -> impl Future<Output = Result<SourcePath, Self::Error>> {
async { self.resolve_from(&SourcePath::default(), path).await }
}
- /// Resolves absolute path, doesn't supports jpath and other fancy things
- fn resolve(&self, path: &Path) -> impl Future<Output = Result<SourcePath, Self::Error>>;
/// Load resolved file
/// This should only be called with value returned
@@ -253,31 +250,25 @@
#[derive(Acyclic)]
struct ResolvedImportResolver {
- resolved: RefCell<FxHashMap<(SourcePath, IStr), (SourcePath, bool)>>,
+ resolved: RefCell<FxHashMap<(SourcePath, ResolvePathOwned), (SourcePath, bool)>>,
}
impl ImportResolver for ResolvedImportResolver {
fn load_file_contents(&self, _resolved: &SourcePath) -> crate::Result<Vec<u8>> {
unreachable!("all files should be loaded at this point");
}
- fn resolve_from(&self, from: &SourcePath, path: &str) -> crate::Result<SourcePath> {
+ fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> crate::Result<SourcePath> {
Ok(self
.resolved
.borrow()
- .get(&(from.clone(), path.into()))
+ .get(&(from.clone(), path.as_path().to_owned()))
.expect("all imports should be resolved at this point")
.0
.clone())
}
- fn resolve_from_default(&self, path: &str) -> crate::Result<SourcePath> {
+ fn resolve_from_default(&self, path: &dyn AsPathLike) -> crate::Result<SourcePath> {
self.resolve_from(&SourcePath::default(), path)
- }
-
- fn resolve(&self, path: &Path) -> crate::Result<SourcePath> {
- bail!(crate::error::ErrorKind::AbsoluteImportNotSupported(
- path.to_owned()
- ))
}
}
@@ -288,7 +279,7 @@
}
#[allow(clippy::future_not_send)]
-pub async fn async_import<H>(s: State, handler: H, path: impl AsRef<Path>) -> Result<(), H::Error>
+pub async fn async_import<H>(s: State, handler: H, path: &dyn AsPathLike) -> Result<(), H::Error>
where
H: AsyncImportResolver,
{
@@ -299,7 +290,7 @@
let mut resolved_map = resolved.resolved.borrow_mut();
let mut queue = vec![Job::LoadFile {
- path: handler.resolve(path.as_ref()).await?,
+ path: handler.resolve_from_default(path).await?,
parse: true,
}];
while let Some(job) = queue.pop() {
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -2,7 +2,6 @@
cmp::Ordering,
convert::Infallible,
fmt::{Debug, Display},
- path::PathBuf,
};
use jrsonnet_gcmodule::Trace;
@@ -16,7 +15,7 @@
stdlib::format::FormatError,
typed::TypeLocError,
val::ConvertNumValueError,
- ObjValue,
+ ObjValue, ResolvePathOwned,
};
pub(crate) fn format_found(list: &[IStr], what: &str) -> String {
@@ -180,9 +179,7 @@
StandaloneSuper,
#[error("can't resolve {1} from {0}")]
- ImportFileNotFound(SourcePath, String),
- #[error("can't resolve absolute {0}")]
- AbsoluteImportFileNotFound(PathBuf),
+ ImportFileNotFound(SourcePath, ResolvePathOwned),
#[error("resolved file not found: {:?}", .0)]
ResolvedFileNotFound(SourcePath),
#[error("can't import {0}: is a directory")]
@@ -192,9 +189,7 @@
#[error("import io error: {0}")]
ImportIo(String),
#[error("tried to import {1} from {0}, but imports are not supported")]
- ImportNotSupported(SourcePath, String),
- #[error("tried to import {0}, but absolute imports are not supported")]
- AbsoluteImportNotSupported(PathBuf),
+ ImportNotSupported(SourcePath, ResolvePathOwned),
#[error("can't import from virtual file")]
CantImportFromVirtualFile,
#[error(
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -682,7 +682,7 @@
};
let tmp = loc.clone().0;
let s = ctx.state();
- let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;
+ let resolved_path = s.resolve_from(tmp.source_path(), path)?;
match i {
Import(_) => in_frame(
CallLocation::new(&loc),
crates/jrsonnet-evaluator/src/function/arglike.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/arglike.rs
+++ b/crates/jrsonnet-evaluator/src/function/arglike.rs
@@ -2,7 +2,7 @@
use jrsonnet_gcmodule::Trace;
use jrsonnet_interner::IStr;
-use jrsonnet_parser::{ArgsDesc, LocExpr};
+use jrsonnet_parser::{ArgsDesc, LocExpr, SourceFifo, SourcePath};
use crate::{evaluate, typed::Typed, Context, Result, Thunk, Val};
@@ -41,22 +41,34 @@
#[derive(Clone, Trace)]
pub enum TlaArg {
String(IStr),
- Code(LocExpr),
Val(Val),
Lazy(Thunk<Val>),
+ Import(String),
+ ImportStr(String),
+ InlineCode(String),
}
impl ArgLike for TlaArg {
- fn evaluate_arg(&self, ctx: Context, tailstrict: bool) -> Result<Thunk<Val>> {
+ fn evaluate_arg(&self, ctx: Context, _tailstrict: bool) -> Result<Thunk<Val>> {
match self {
Self::String(s) => Ok(Thunk::evaluated(Val::string(s.clone()))),
- Self::Code(code) => Ok(if tailstrict {
- Thunk::evaluated(evaluate(ctx, code)?)
- } else {
- let code = code.clone();
- Thunk!(move || evaluate(ctx, &code))
- }),
Self::Val(val) => Ok(Thunk::evaluated(val.clone())),
Self::Lazy(lazy) => Ok(lazy.clone()),
+ Self::Import(p) => {
+ let resolved = ctx.state().resolve_from_default(&p.as_str())?;
+ Ok(Thunk!(move || ctx.state().import_resolved(resolved)))
+ }
+ Self::ImportStr(p) => {
+ let resolved = ctx.state().resolve_from_default(&p.as_str())?;
+ Ok(Thunk!(move || ctx
+ .state()
+ .import_resolved_str(resolved)
+ .map(Val::string)))
+ }
+ Self::InlineCode(p) => {
+ let resolved =
+ SourcePath::new(SourceFifo("<inline code>".to_owned(), p.as_bytes().into()));
+ Ok(Thunk!(move || ctx.state().import_resolved(resolved)))
+ }
}
}
}
crates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -1,7 +1,8 @@
use std::{
any::Any,
+ borrow::Cow,
env::current_dir,
- fs,
+ fmt, fs,
io::{ErrorKind, Read},
path::{Path, PathBuf},
};
@@ -9,12 +10,85 @@
use fs::File;
use jrsonnet_gcmodule::Acyclic;
use jrsonnet_interner::IBytes;
-use jrsonnet_parser::{SourceDirectory, SourceFifo, SourceFile, SourcePath};
+use jrsonnet_parser::{IStr, SourceDirectory, SourceFifo, SourceFile, SourcePath};
use crate::{
bail,
error::{ErrorKind::*, Result},
};
+#[derive(Clone, Debug, Acyclic, Eq, Hash, PartialEq)]
+pub enum ResolvePathOwned {
+ Str(String),
+ Path(PathBuf),
+}
+impl fmt::Display for ResolvePathOwned {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ ResolvePathOwned::Str(s) => write!(f, "{s}"),
+ ResolvePathOwned::Path(p) => write!(f, "{}", p.display()),
+ }
+ }
+}
+#[derive(Clone, Copy)]
+pub enum ResolvePath<'s> {
+ Str(&'s str),
+ Path(&'s Path),
+}
+impl ResolvePath<'_> {
+ pub fn to_owned(self) -> ResolvePathOwned {
+ match self {
+ ResolvePath::Str(s) => ResolvePathOwned::Str(s.to_owned()),
+ ResolvePath::Path(p) => ResolvePathOwned::Path(p.to_owned()),
+ }
+ }
+}
+impl AsRef<Path> for ResolvePath<'_> {
+ fn as_ref(&self) -> &Path {
+ match self {
+ ResolvePath::Str(s) => s.as_ref(),
+ ResolvePath::Path(p) => p,
+ }
+ }
+}
+pub trait AsPathLike {
+ fn as_path(&self) -> ResolvePath<'_>;
+}
+impl<T> AsPathLike for &T
+where
+ T: AsPathLike + ?Sized,
+{
+ fn as_path(&self) -> ResolvePath<'_> {
+ (*self).as_path()
+ }
+}
+impl AsPathLike for str {
+ fn as_path(&self) -> ResolvePath<'_> {
+ ResolvePath::Str(self)
+ }
+}
+impl AsPathLike for IStr {
+ fn as_path(&self) -> ResolvePath<'_> {
+ ResolvePath::Str(self)
+ }
+}
+impl AsPathLike for Cow<'_, Path> {
+ fn as_path(&self) -> ResolvePath<'_> {
+ ResolvePath::Path(self.as_ref())
+ }
+}
+impl AsPathLike for Path {
+ fn as_path(&self) -> ResolvePath<'_> {
+ ResolvePath::Path(self)
+ }
+}
+impl AsPathLike for ResolvePathOwned {
+ fn as_path(&self) -> ResolvePath<'_> {
+ match self {
+ ResolvePathOwned::Str(s) => ResolvePath::Str(s),
+ ResolvePathOwned::Path(path_buf) => ResolvePath::Path(path_buf),
+ }
+ }
+}
/// Implements file resolution logic for `import` and `importStr`
pub trait ImportResolver: Acyclic + Any {
@@ -24,15 +98,11 @@
///
/// `from` should only be returned from [`ImportResolver::resolve`], or from other defined file, any other value
/// may result in panic
- fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
- bail!(ImportNotSupported(from.clone(), path.into()))
+ fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {
+ bail!(ImportNotSupported(from.clone(), path.as_path().to_owned()))
}
- fn resolve_from_default(&self, path: &str) -> Result<SourcePath> {
+ fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {
self.resolve_from(&SourcePath::default(), path)
- }
- /// Resolves absolute path, doesn't supports jpath and other fancy things
- fn resolve(&self, path: &Path) -> Result<SourcePath> {
- bail!(AbsoluteImportNotSupported(path.to_owned()))
}
/// Load resolved file
@@ -105,7 +175,8 @@
}
impl ImportResolver for FileImportResolver {
- fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
+ fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {
+ let path = path.as_path();
let mut direct = if let Some(f) = from.downcast_ref::<SourceFile>() {
let mut o = f.path().to_owned();
o.pop();
@@ -130,12 +201,6 @@
}
}
bail!(ImportFileNotFound(from.clone(), path.to_owned()))
- }
- fn resolve(&self, path: &Path) -> Result<SourcePath> {
- let Some(source) = check_path(path)? else {
- bail!(AbsoluteImportFileNotFound(path.to_owned()))
- };
- Ok(source)
}
fn load_file_contents(&self, id: &SourcePath) -> Result<Vec<u8>> {
@@ -155,7 +220,7 @@
Ok(out)
}
- fn resolve_from_default(&self, path: &str) -> Result<SourcePath> {
+ fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {
self.resolve_from(&SourcePath::default(), path)
}
}
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -29,7 +29,6 @@
cell::{RefCell, RefMut},
collections::hash_map::Entry,
fmt::{self, Debug},
- path::Path,
rc::Rc,
};
@@ -349,12 +348,12 @@
}
/// Has same semantics as `import 'path'` called from `from` file
- pub fn import_from(&self, from: &SourcePath, path: &str) -> Result<Val> {
- let resolved = self.resolve_from(from, path)?;
+ pub fn import_from(&self, from: &SourcePath, path: impl AsPathLike) -> Result<Val> {
+ let resolved = self.resolve_from(from, &path)?;
self.import_resolved(resolved)
}
- pub fn import(&self, path: impl AsRef<Path>) -> Result<Val> {
- let resolved = self.resolve(path)?;
+ pub fn import(&self, path: impl AsPathLike) -> Result<Val> {
+ let resolved = self.resolve_from_default(&path)?;
self.import_resolved(resolved)
}
@@ -468,14 +467,12 @@
impl State {
// Only panics in case of [`ImportResolver`] contract violation
#[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())
+ pub fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {
+ self.import_resolver().resolve_from(from, path)
}
-
- // Only panics in case of [`ImportResolver`] contract violation
#[allow(clippy::missing_panics_doc)]
- pub fn resolve(&self, path: impl AsRef<Path>) -> Result<SourcePath> {
- self.import_resolver().resolve(path.as_ref())
+ pub fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {
+ self.import_resolver().resolve_from_default(path)
}
pub fn import_resolver(&self) -> &dyn ImportResolver {
&*self.0.import_resolver
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth1#![allow(clippy::similar_names)]23use std::{4 cell::{Ref, RefCell, RefMut},5 collections::HashMap,6 f64,7 rc::Rc,8};910pub use arrays::*;11pub use compat::*;12pub use encoding::*;13pub use hash::*;14use jrsonnet_evaluator::{15 error::{ErrorKind::*, Result},16 function::{CallLocation, FuncVal, TlaArg},17 trace::PathResolver,18 val::NumValue,19 ContextBuilder, IStr, ObjValue, ObjValueBuilder, Thunk, Val,20};21use jrsonnet_gcmodule::{Acyclic, Cc, Trace};22use jrsonnet_parser::Source;23pub use manifest::*;24pub use math::*;25pub use misc::*;26pub use objects::*;27pub use operator::*;28pub use parse::*;29pub use sets::*;30pub use sort::*;31pub use strings::*;32pub use types::*;3334#[cfg(feature = "exp-regex")]35pub use crate::regex::*;3637mod arrays;38mod compat;39mod encoding;40mod hash;41mod manifest;42mod math;43mod misc;44mod objects;45mod operator;46mod parse;47#[cfg(feature = "exp-regex")]48mod regex;49mod sets;50mod sort;51mod strings;52mod types;5354#[allow(clippy::too_many_lines)]55pub fn stdlib_uncached(settings: Cc<RefCell<Settings>>) -> ObjValue {56 let mut builder = ObjValueBuilder::new();5758 // FIXME: Use PHF59 for (name, builtin) in [60 // Types61 ("type", builtin_type::INST),62 ("isString", builtin_is_string::INST),63 ("isNumber", builtin_is_number::INST),64 ("isBoolean", builtin_is_boolean::INST),65 ("isObject", builtin_is_object::INST),66 ("isArray", builtin_is_array::INST),67 ("isFunction", builtin_is_function::INST),68 ("isNull", builtin_is_null::INST),69 // Arrays70 ("makeArray", builtin_make_array::INST),71 ("repeat", builtin_repeat::INST),72 ("slice", builtin_slice::INST),73 ("map", builtin_map::INST),74 ("mapWithIndex", builtin_map_with_index::INST),75 ("mapWithKey", builtin_map_with_key::INST),76 ("flatMap", builtin_flatmap::INST),77 ("filter", builtin_filter::INST),78 ("foldl", builtin_foldl::INST),79 ("foldr", builtin_foldr::INST),80 ("range", builtin_range::INST),81 ("join", builtin_join::INST),82 ("lines", builtin_lines::INST),83 ("resolvePath", builtin_resolve_path::INST),84 ("deepJoin", builtin_deep_join::INST),85 ("reverse", builtin_reverse::INST),86 ("any", builtin_any::INST),87 ("all", builtin_all::INST),88 ("member", builtin_member::INST),89 ("find", builtin_find::INST),90 ("contains", builtin_contains::INST),91 ("count", builtin_count::INST),92 ("avg", builtin_avg::INST),93 ("removeAt", builtin_remove_at::INST),94 ("remove", builtin_remove::INST),95 ("flattenArrays", builtin_flatten_arrays::INST),96 ("flattenDeepArray", builtin_flatten_deep_array::INST),97 ("prune", builtin_prune::INST),98 ("filterMap", builtin_filter_map::INST),99 // Math100 ("abs", builtin_abs::INST),101 ("sign", builtin_sign::INST),102 ("max", builtin_max::INST),103 ("min", builtin_min::INST),104 ("clamp", builtin_clamp::INST),105 ("sum", builtin_sum::INST),106 ("modulo", builtin_modulo::INST),107 ("floor", builtin_floor::INST),108 ("ceil", builtin_ceil::INST),109 ("log", builtin_log::INST),110 ("log2", builtin_log2::INST),111 ("log10", builtin_log10::INST),112 ("pow", builtin_pow::INST),113 ("sqrt", builtin_sqrt::INST),114 ("sin", builtin_sin::INST),115 ("cos", builtin_cos::INST),116 ("tan", builtin_tan::INST),117 ("asin", builtin_asin::INST),118 ("acos", builtin_acos::INST),119 ("atan", builtin_atan::INST),120 ("atan2", builtin_atan2::INST),121 ("exp", builtin_exp::INST),122 ("mantissa", builtin_mantissa::INST),123 ("exponent", builtin_exponent::INST),124 ("round", builtin_round::INST),125 ("isEven", builtin_is_even::INST),126 ("isOdd", builtin_is_odd::INST),127 ("isInteger", builtin_is_integer::INST),128 ("isDecimal", builtin_is_decimal::INST),129 ("deg2rad", builtin_deg2rad::INST),130 ("rad2deg", builtin_rad2deg::INST),131 ("hypot", builtin_hypot::INST),132 // Operator133 ("mod", builtin_mod::INST),134 ("primitiveEquals", builtin_primitive_equals::INST),135 ("equals", builtin_equals::INST),136 ("xor", builtin_xor::INST),137 ("xnor", builtin_xnor::INST),138 ("format", builtin_format::INST),139 // Sort140 ("sort", builtin_sort::INST),141 ("uniq", builtin_uniq::INST),142 ("set", builtin_set::INST),143 ("minArray", builtin_min_array::INST),144 ("maxArray", builtin_max_array::INST),145 // Hash146 ("md5", builtin_md5::INST),147 ("sha1", builtin_sha1::INST),148 ("sha256", builtin_sha256::INST),149 ("sha512", builtin_sha512::INST),150 ("sha3", builtin_sha3::INST),151 // Encoding152 ("encodeUTF8", builtin_encode_utf8::INST),153 ("decodeUTF8", builtin_decode_utf8::INST),154 ("base64", builtin_base64::INST),155 ("base64Decode", builtin_base64_decode::INST),156 ("base64DecodeBytes", builtin_base64_decode_bytes::INST),157 // Objects158 ("objectFieldsEx", builtin_object_fields_ex::INST),159 ("objectFields", builtin_object_fields::INST),160 ("objectFieldsAll", builtin_object_fields_all::INST),161 ("objectValues", builtin_object_values::INST),162 ("objectValuesAll", builtin_object_values_all::INST),163 ("objectKeysValues", builtin_object_keys_values::INST),164 ("objectKeysValuesAll", builtin_object_keys_values_all::INST),165 ("objectHasEx", builtin_object_has_ex::INST),166 ("objectHas", builtin_object_has::INST),167 ("objectHasAll", builtin_object_has_all::INST),168 ("objectRemoveKey", builtin_object_remove_key::INST),169 // Manifest170 ("escapeStringJson", builtin_escape_string_json::INST),171 ("escapeStringPython", builtin_escape_string_python::INST),172 ("escapeStringXML", builtin_escape_string_xml::INST),173 ("manifestJsonEx", builtin_manifest_json_ex::INST),174 ("manifestJson", builtin_manifest_json::INST),175 ("manifestJsonMinified", builtin_manifest_json_minified::INST),176 ("manifestYamlDoc", builtin_manifest_yaml_doc::INST),177 ("manifestYamlStream", builtin_manifest_yaml_stream::INST),178 ("manifestTomlEx", builtin_manifest_toml_ex::INST),179 ("manifestToml", builtin_manifest_toml::INST),180 ("toString", builtin_to_string::INST),181 ("manifestPython", builtin_manifest_python::INST),182 ("manifestPythonVars", builtin_manifest_python_vars::INST),183 ("manifestXmlJsonml", builtin_manifest_xml_jsonml::INST),184 ("manifestIni", builtin_manifest_ini::INST),185 // Parse186 ("parseJson", builtin_parse_json::INST),187 ("parseYaml", builtin_parse_yaml::INST),188 // Strings189 ("codepoint", builtin_codepoint::INST),190 ("substr", builtin_substr::INST),191 ("char", builtin_char::INST),192 ("strReplace", builtin_str_replace::INST),193 ("escapeStringBash", builtin_escape_string_bash::INST),194 ("escapeStringDollars", builtin_escape_string_dollars::INST),195 ("isEmpty", builtin_is_empty::INST),196 ("equalsIgnoreCase", builtin_equals_ignore_case::INST),197 ("splitLimit", builtin_splitlimit::INST),198 ("splitLimitR", builtin_splitlimitr::INST),199 ("split", builtin_split::INST),200 ("asciiUpper", builtin_ascii_upper::INST),201 ("asciiLower", builtin_ascii_lower::INST),202 ("findSubstr", builtin_find_substr::INST),203 ("parseInt", builtin_parse_int::INST),204 #[cfg(feature = "exp-bigint")]205 ("bigint", builtin_bigint::INST),206 ("parseOctal", builtin_parse_octal::INST),207 ("parseHex", builtin_parse_hex::INST),208 ("stringChars", builtin_string_chars::INST),209 ("lstripChars", builtin_lstrip_chars::INST),210 ("rstripChars", builtin_rstrip_chars::INST),211 ("stripChars", builtin_strip_chars::INST),212 ("trim", builtin_trim::INST),213 // Misc214 ("length", builtin_length::INST),215 ("get", builtin_get::INST),216 ("startsWith", builtin_starts_with::INST),217 ("endsWith", builtin_ends_with::INST),218 ("assertEqual", builtin_assert_equal::INST),219 ("mergePatch", builtin_merge_patch::INST),220 // Sets221 ("setMember", builtin_set_member::INST),222 ("setInter", builtin_set_inter::INST),223 ("setDiff", builtin_set_diff::INST),224 ("setUnion", builtin_set_union::INST),225 // Regex226 #[cfg(feature = "exp-regex")]227 ("regexQuoteMeta", builtin_regex_quote_meta::INST),228 // Compat229 ("__compare", builtin___compare::INST),230 ("__compare_array", builtin___compare_array::INST),231 ("__array_less", builtin___array_less::INST),232 ("__array_greater", builtin___array_greater::INST),233 ("__array_less_or_equal", builtin___array_less_or_equal::INST),234 (235 "__array_greater_or_equal",236 builtin___array_greater_or_equal::INST,237 ),238 ]239 .iter()240 .copied()241 {242 builder.method(name, builtin);243 }244245 builder.method(246 "extVar",247 builtin_ext_var {248 settings: settings.clone(),249 },250 );251 builder.method(252 "native",253 builtin_native {254 settings: settings.clone(),255 },256 );257 builder.method("trace", builtin_trace { settings });258 builder.method("id", FuncVal::Id);259260 builder.field("pi").hide().value(Val::Num(261 NumValue::new(f64::consts::PI).expect("pi is finite"),262 ));263264 #[cfg(feature = "exp-regex")]265 {266 // Regex267 let regex_cache = RegexCache::default();268 builder.method(269 "regexFullMatch",270 builtin_regex_full_match {271 cache: regex_cache.clone(),272 },273 );274 builder.method(275 "regexPartialMatch",276 builtin_regex_partial_match {277 cache: regex_cache.clone(),278 },279 );280 builder.method(281 "regexReplace",282 builtin_regex_replace {283 cache: regex_cache.clone(),284 },285 );286 builder.method(287 "regexGlobalReplace",288 builtin_regex_global_replace { cache: regex_cache },289 );290 };291292 builder.build()293}294295pub trait TracePrinter: Acyclic {296 fn print_trace(&self, loc: CallLocation, value: IStr);297}298299#[derive(Acyclic)]300pub struct StdTracePrinter {301 resolver: PathResolver,302}303impl StdTracePrinter {304 pub fn new(resolver: PathResolver) -> Self {305 Self { resolver }306 }307}308impl TracePrinter for StdTracePrinter {309 fn print_trace(&self, loc: CallLocation, value: IStr) {310 eprint!("TRACE:");311 if let Some(loc) = loc.0 {312 let locs = loc.0.map_source_locations(&[loc.1]);313 eprint!(314 " {}:{}",315 loc.0.source_path().path().map_or_else(316 || loc.0.source_path().to_string(),317 |p| self.resolver.resolve(p)318 ),319 locs[0].line320 );321 }322 eprintln!(" {value}");323 }324}325326#[derive(Clone, Trace)]327pub struct Settings {328 /// Used for `std.extVar`329 pub ext_vars: HashMap<IStr, TlaArg>,330 /// Used for `std.native`331 pub ext_natives: HashMap<IStr, FuncVal>,332 /// Used for `std.trace`333 pub trace_printer: Rc<dyn TracePrinter>,334 /// Used for `std.thisFile`335 pub path_resolver: PathResolver,336}337338fn extvar_source(name: &str, code: impl Into<IStr>) -> Source {339 let source_name = format!("<extvar:{name}>");340 Source::new_virtual(source_name.into(), code.into())341}342343#[derive(Trace, Clone)]344pub struct ContextInitializer {345 /// std without applied thisFile overlay346 stdlib_obj: ObjValue,347 settings: Cc<RefCell<Settings>>,348}349impl ContextInitializer {350 pub fn new(resolver: PathResolver) -> Self {351 let settings = Settings {352 ext_vars: HashMap::new(),353 ext_natives: HashMap::new(),354 trace_printer: Rc::new(StdTracePrinter::new(resolver.clone())),355 path_resolver: resolver,356 };357 let settings = Cc::new(RefCell::new(settings));358 let stdlib_obj = stdlib_uncached(settings.clone());359 Self {360 stdlib_obj,361 settings,362 }363 }364 pub fn settings(&self) -> Ref<'_, Settings> {365 self.settings.borrow()366 }367 pub fn settings_mut(&self) -> RefMut<'_, Settings> {368 self.settings.borrow_mut()369 }370 pub fn add_ext_var(&self, name: IStr, value: Val) {371 self.settings_mut()372 .ext_vars373 .insert(name, TlaArg::Val(value));374 }375 pub fn add_ext_str(&self, name: IStr, value: IStr) {376 self.settings_mut()377 .ext_vars378 .insert(name, TlaArg::String(value));379 }380 pub fn add_ext_code(&self, name: &str, code: impl Into<IStr>) -> Result<()> {381 let code = code.into();382 let source = extvar_source(name, code.clone());383 let parsed = jrsonnet_parser::parse(384 &code,385 &jrsonnet_parser::ParserSettings {386 source: source.clone(),387 },388 )389 .map_err(|e| ImportSyntaxError {390 path: source,391 error: Box::new(e),392 })?;393 // self.data_mut().volatile_files.insert(source_name, code);394 self.settings_mut()395 .ext_vars396 .insert(name.into(), TlaArg::Code(parsed));397 Ok(())398 }399 pub fn add_native(&self, name: impl Into<IStr>, cb: impl Into<FuncVal>) {400 self.settings_mut()401 .ext_natives402 .insert(name.into(), cb.into());403 }404}405impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {406 fn reserve_vars(&self) -> usize {407 1408 }409 fn populate(&self, source: Source, builder: &mut ContextBuilder) {410 let mut std = ObjValueBuilder::new();411 std.with_super(self.stdlib_obj.clone());412 std.field("thisFile").hide().value({413 let source_path = source.source_path();414 source_path.path().map_or_else(415 || source_path.to_string(),416 |p| self.settings().path_resolver.resolve(p),417 )418 });419 let stdlib_with_this_file = std.build();420421 builder.bind("std", Thunk::evaluated(Val::Obj(stdlib_with_this_file)));422 }423 fn as_any(&self) -> &dyn std::any::Any {424 self425 }426}