difftreelog
Merge branch 'master' into gc-v2
in: master
17 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -242,7 +242,6 @@
dependencies = [
"gc",
"jrsonnet-evaluator",
- "jrsonnet-interner",
"jrsonnet-parser",
]
bindings/jsonnet/Cargo.tomldiffbeforeafterboth--- a/bindings/jsonnet/Cargo.toml
+++ b/bindings/jsonnet/Cargo.toml
@@ -8,7 +8,6 @@
publish = false
[dependencies]
-jrsonnet-interner = { path = "../../crates/jrsonnet-interner", version = "0.3.8" }
jrsonnet-evaluator = { path = "../../crates/jrsonnet-evaluator", version = "0.3.8" }
jrsonnet-parser = { path = "../../crates/jrsonnet-parser", version = "0.3.8" }
gc = { version = "0.4.1", features = ["derive"] }
bindings/jsonnet/src/import.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/import.rs
+++ b/bindings/jsonnet/src/import.rs
@@ -2,9 +2,8 @@
use jrsonnet_evaluator::{
error::{Error::*, Result},
- throw, EvaluationState, ImportResolver,
+ throw, EvaluationState, IStr, ImportResolver,
};
-use jrsonnet_interner::IStr;
use std::{
any::Any,
cell::RefCell,
@@ -13,7 +12,7 @@
fs::File,
io::Read,
os::raw::{c_char, c_int},
- path::PathBuf,
+ path::{Path, PathBuf},
ptr::null_mut,
rc::Rc,
};
@@ -34,7 +33,7 @@
out: RefCell<HashMap<PathBuf, IStr>>,
}
impl ImportResolver for CallbackImportResolver {
- fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {
+ fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
let base = CString::new(from.to_str().unwrap()).unwrap().into_raw();
let rel = CString::new(path.to_str().unwrap()).unwrap().into_raw();
let found_here: *mut c_char = null_mut();
@@ -74,9 +73,9 @@
unsafe { CString::from_raw(result_ptr) };
}
- Ok(Rc::new(found_here_buf))
+ Ok(found_here_buf.into())
}
- fn load_file_contents(&self, resolved: &PathBuf) -> Result<IStr> {
+ fn load_file_contents(&self, resolved: &Path) -> Result<IStr> {
Ok(self.out.borrow().get(resolved).unwrap().clone())
}
unsafe fn as_any(&self) -> &dyn Any {
@@ -109,27 +108,27 @@
}
}
impl ImportResolver for NativeImportResolver {
- fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {
- let mut new_path = from.clone();
+ fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
+ let mut new_path = from.to_owned();
new_path.push(path);
if new_path.exists() {
- Ok(Rc::new(new_path))
+ Ok(new_path.into())
} else {
for library_path in self.library_paths.borrow().iter() {
let mut cloned = library_path.clone();
cloned.push(path);
if cloned.exists() {
- return Ok(Rc::new(cloned));
+ return Ok(cloned.into());
}
}
- throw!(ImportFileNotFound(from.clone(), path.clone()))
+ throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))
}
}
- fn load_file_contents(&self, id: &PathBuf) -> Result<IStr> {
- let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.clone()))?;
+ fn load_file_contents(&self, id: &Path) -> Result<IStr> {
+ let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.to_owned()))?;
let mut out = String::new();
file.read_to_string(&mut out)
- .map_err(|_e| ImportBadFileUtf8(id.clone()))?;
+ .map_err(|_e| ImportBadFileUtf8(id.to_owned()))?;
Ok(out.into())
}
unsafe fn as_any(&self) -> &dyn Any {
bindings/jsonnet/src/lib.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -9,14 +9,12 @@
pub mod vars_tlas;
use import::NativeImportResolver;
-use jrsonnet_evaluator::{EvaluationState, ManifestFormat, Val};
-use jrsonnet_interner::IStr;
+use jrsonnet_evaluator::{EvaluationState, IStr, ManifestFormat, Val};
use std::{
alloc::Layout,
ffi::{CStr, CString},
os::raw::{c_char, c_double, c_int, c_uint},
path::PathBuf,
- rc::Rc,
};
/// WASM stub
@@ -145,7 +143,7 @@
let snippet = CStr::from_ptr(snippet);
match vm
.evaluate_snippet_raw(
- Rc::new(PathBuf::from(filename.to_str().unwrap())),
+ PathBuf::from(filename.to_str().unwrap()).into(),
snippet.to_str().unwrap().into(),
)
.and_then(|v| vm.with_tla(v))
@@ -221,7 +219,7 @@
let snippet = CStr::from_ptr(snippet);
match vm
.evaluate_snippet_raw(
- Rc::new(PathBuf::from(filename.to_str().unwrap())),
+ PathBuf::from(filename.to_str().unwrap()).into(),
snippet.to_str().unwrap().into(),
)
.and_then(|v| vm.with_tla(v))
@@ -295,7 +293,7 @@
let snippet = CStr::from_ptr(snippet);
match vm
.evaluate_snippet_raw(
- Rc::new(PathBuf::from(filename.to_str().unwrap())),
+ PathBuf::from(filename.to_str().unwrap()).into(),
snippet.to_str().unwrap().into(),
)
.and_then(|v| vm.with_tla(v))
bindings/jsonnet/src/native.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/native.rs
+++ b/bindings/jsonnet/src/native.rs
@@ -8,7 +8,7 @@
use std::{
ffi::{c_void, CStr},
os::raw::{c_char, c_int},
- path::PathBuf,
+ path::Path,
rc::Rc,
};
@@ -27,7 +27,7 @@
unsafe_empty_trace!();
}
impl NativeCallbackHandler for JsonnetNativeCallbackHandler {
- fn call(&self, _from: Option<Rc<PathBuf>>, args: &[Val]) -> Result<Val, LocError> {
+ fn call(&self, _from: Option<Rc<Path>>, args: &[Val]) -> Result<Val, LocError> {
let mut n_args = Vec::new();
for a in args {
n_args.push(Some(Box::new(a.clone())));
cmds/jrsonnet/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -6,7 +6,6 @@
io::Read,
io::Write,
path::PathBuf,
- rc::Rc,
str::FromStr,
};
@@ -134,14 +133,14 @@
let val = if opts.input.exec {
state.set_manifest_format(ManifestFormat::ToString);
state.evaluate_snippet_raw(
- Rc::new(PathBuf::from("args")),
+ PathBuf::from("args").into(),
(&opts.input.input as &str).into(),
)?
} else if opts.input.input == "-" {
let mut input = Vec::new();
std::io::stdin().read_to_end(&mut input)?;
let input_str = std::str::from_utf8(&input)?.into();
- state.evaluate_snippet_raw(Rc::new(PathBuf::from("<stdin>")), input_str)?
+ state.evaluate_snippet_raw(PathBuf::from("<stdin>").into(), input_str)?
} else {
state.evaluate_file_raw(&PathBuf::from(opts.input.input))?
};
crates/jrsonnet-evaluator/build.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/build.rs
+++ b/crates/jrsonnet-evaluator/build.rs
@@ -15,7 +15,7 @@
let parsed = parse(
STDLIB_STR,
&ParserSettings {
- file_name: Rc::new(PathBuf::from("std.jsonnet")),
+ file_name: PathBuf::from("std.jsonnet").into(),
loc_data: true,
},
)
crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -177,7 +177,7 @@
0, s: ty!(string) => Val::Str;
], {
let state = EvaluationState::default();
- let path = Rc::new(PathBuf::from("std.parseJson"));
+ let path = PathBuf::from("std.parseJson").into();
state.evaluate_snippet_raw(path ,s)
})
}
crates/jrsonnet-evaluator/src/builtin/stdlib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/stdlib.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/stdlib.rs
@@ -1,5 +1,5 @@
use jrsonnet_parser::{LocExpr, ParserSettings};
-use std::{path::PathBuf, rc::Rc};
+use std::path::PathBuf;
thread_local! {
/// To avoid parsing again when issued from the same thread
@@ -25,7 +25,7 @@
jrsonnet_stdlib::STDLIB_STR,
&ParserSettings {
loc_data: true,
- file_name: Rc::new(PathBuf::from("std.jsonnet")),
+ file_name: PathBuf::from("std.jsonnet").into(),
},
)
.unwrap()
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -6,7 +6,10 @@
use jrsonnet_interner::IStr;
use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};
use jrsonnet_types::ValType;
-use std::{path::PathBuf, rc::Rc};
+use std::{
+ path::{Path, PathBuf},
+ rc::Rc,
+};
use thiserror::Error;
#[derive(Error, Debug, Clone, Trace, Finalize)]
@@ -87,7 +90,7 @@
.source_code.chars().nth(error.location.offset).map(|c| c.to_string()).unwrap_or_else(|| "EOF".into())
)]
ImportSyntaxError {
- path: Rc<PathBuf>,
+ path: Rc<Path>,
source_code: IStr,
#[unsafe_ignore_trace]
error: Box<jrsonnet_parser::ParseError>,
crates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate.rs
@@ -12,7 +12,7 @@
};
use jrsonnet_types::ValType;
use rustc_hash::{FxHashMap, FxHasher};
-use std::{collections::HashMap, hash::BuildHasherDefault, rc::Rc};
+use std::{collections::HashMap, hash::BuildHasherDefault};
pub fn evaluate_binding_in_future(
b: &BindSpec,
@@ -843,26 +843,26 @@
}
}
Import(path) => {
- let mut tmp = loc
+ let tmp = loc
.clone()
.expect("imports cannot be used without loc_data")
.0;
- let import_location = Rc::make_mut(&mut tmp);
+ let mut import_location = tmp.to_path_buf();
import_location.pop();
push(
loc.as_ref(),
|| format!("import {:?}", path),
- || with_state(|s| s.import_file(import_location, path)),
+ || with_state(|s| s.import_file(&import_location, path)),
)?
}
ImportStr(path) => {
- let mut tmp = loc
+ let tmp = loc
.clone()
.expect("imports cannot be used without loc_data")
.0;
- let import_location = Rc::make_mut(&mut tmp);
+ let mut import_location = tmp.to_path_buf();
import_location.pop();
- Val::Str(with_state(|s| s.import_file_str(import_location, path))?)
+ Val::Str(with_state(|s| s.import_file_str(&import_location, path))?)
}
})
}
crates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -6,17 +6,23 @@
use jrsonnet_interner::IStr;
use std::fs;
use std::io::Read;
-use std::{any::Any, cell::RefCell, collections::HashMap, path::PathBuf, rc::Rc};
+use std::{
+ any::Any,
+ cell::RefCell,
+ collections::HashMap,
+ path::{Path, PathBuf},
+ rc::Rc,
+};
/// Implements file resolution logic for `import` and `importStr`
pub trait ImportResolver {
/// Resolves real file path, e.g. `(/home/user/manifests, b.libjsonnet)` can correspond
/// both to `/home/user/manifests/b.libjsonnet` and to `/home/user/${vendor}/b.libjsonnet`
/// where `${vendor}` is a library path.
- fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>>;
+ fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>>;
/// Reads file from filesystem, should be used only with path received from `resolve_file`
- fn load_file_contents(&self, resolved: &PathBuf) -> Result<IStr>;
+ fn load_file_contents(&self, resolved: &Path) -> Result<IStr>;
/// # Safety
///
@@ -29,11 +35,11 @@
/// Dummy resolver, can't resolve/load any file
pub struct DummyImportResolver;
impl ImportResolver for DummyImportResolver {
- fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {
- throw!(ImportNotSupported(from.clone(), path.clone()))
+ fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
+ throw!(ImportNotSupported(from.into(), path.into()))
}
- fn load_file_contents(&self, _resolved: &PathBuf) -> Result<IStr> {
+ fn load_file_contents(&self, _resolved: &Path) -> Result<IStr> {
// Can be only caused by library direct consumer, not by supplied jsonnet
panic!("dummy resolver can't load any file")
}
@@ -57,27 +63,27 @@
pub library_paths: Vec<PathBuf>,
}
impl ImportResolver for FileImportResolver {
- fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {
- let mut new_path = from.clone();
- new_path.push(path);
- if new_path.exists() {
- Ok(Rc::new(new_path))
+ fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
+ let mut direct = from.to_path_buf();
+ direct.push(path);
+ if direct.exists() {
+ Ok(direct.into())
} else {
for library_path in self.library_paths.iter() {
let mut cloned = library_path.clone();
cloned.push(path);
if cloned.exists() {
- return Ok(Rc::new(cloned));
+ return Ok(cloned.into());
}
}
- throw!(ImportFileNotFound(from.clone(), path.clone()))
+ throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))
}
}
- fn load_file_contents(&self, id: &PathBuf) -> Result<IStr> {
- let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.clone()))?;
+ fn load_file_contents(&self, id: &Path) -> Result<IStr> {
+ let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.to_owned()))?;
let mut out = String::new();
file.read_to_string(&mut out)
- .map_err(|_e| ImportBadFileUtf8(id.clone()))?;
+ .map_err(|_e| ImportBadFileUtf8(id.to_owned()))?;
Ok(out.into())
}
unsafe fn as_any(&self) -> &dyn Any {
@@ -89,23 +95,23 @@
/// Caches results of the underlying resolver
pub struct CachingImportResolver {
- resolution_cache: RefCell<HashMap<ResolutionData, Result<Rc<PathBuf>>>>,
+ resolution_cache: RefCell<HashMap<ResolutionData, Result<Rc<Path>>>>,
loading_cache: RefCell<HashMap<PathBuf, Result<IStr>>>,
inner: Box<dyn ImportResolver>,
}
impl ImportResolver for CachingImportResolver {
- fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {
+ fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
self.resolution_cache
.borrow_mut()
- .entry((from.clone(), path.clone()))
+ .entry((from.to_owned(), path.to_owned()))
.or_insert_with(|| self.inner.resolve_file(from, path))
.clone()
}
- fn load_file_contents(&self, resolved: &PathBuf) -> Result<IStr> {
+ fn load_file_contents(&self, resolved: &Path) -> Result<IStr> {
self.loading_cache
.borrow_mut()
- .entry(resolved.clone())
+ .entry(resolved.to_owned())
.or_insert_with(|| self.inner.load_file_contents(resolved))
.clone()
}
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth27pub use function::parse_function_call;27pub use function::parse_function_call;28use gc::{Finalize, Gc, Trace};28use gc::{Finalize, Gc, Trace};29pub use import::*;29pub use import::*;30use jrsonnet_interner::IStr;30pub use jrsonnet_interner::IStr;31use jrsonnet_parser::*;31use jrsonnet_parser::*;32use native::NativeCallback;32use native::NativeCallback;33pub use obj::*;33pub use obj::*;37 collections::HashMap,37 collections::HashMap,38 fmt::Debug,38 fmt::Debug,39 hash::BuildHasherDefault,39 hash::BuildHasherDefault,40 path::PathBuf,40 path::{Path, PathBuf},41 rc::Rc,41 rc::Rc,42};42};43use trace::{offset_to_location, CodeLocation, CompactFormat, TraceFormat};43use trace::{offset_to_location, CodeLocation, CompactFormat, TraceFormat};110 /// Used for stack overflow detection, stacktrace is populated on unwind110 /// Used for stack overflow detection, stacktrace is populated on unwind111 stack_depth: usize,111 stack_depth: usize,112 /// Contains file source codes and evaluation results for imports and pretty-printed stacktraces112 /// Contains file source codes and evaluation results for imports and pretty-printed stacktraces113 files: HashMap<Rc<PathBuf>, FileData>,113 files: HashMap<Rc<Path>, FileData>,114 str_files: HashMap<Rc<PathBuf>, IStr>,114 str_files: HashMap<Rc<Path>, IStr>,115}115}116116117pub struct FileData {117pub struct FileData {157157158impl EvaluationState {158impl EvaluationState {159 /// Parses and adds file as loaded159 /// Parses and adds file as loaded160 pub fn add_file(&self, path: Rc<PathBuf>, source_code: IStr) -> Result<()> {160 pub fn add_file(&self, path: Rc<Path>, source_code: IStr) -> Result<()> {161 self.add_parsed_file(161 self.add_parsed_file(162 path.clone(),162 path.clone(),163 source_code.clone(),163 source_code.clone(),170 )170 )171 .map_err(|error| ImportSyntaxError {171 .map_err(|error| ImportSyntaxError {172 error: Box::new(error),172 error: Box::new(error),173 path,173 path: path.to_owned(),174 source_code,174 source_code,175 })?,175 })?,176 )?;176 )?;181 /// Adds file by source code and parsed expr181 /// Adds file by source code and parsed expr182 pub fn add_parsed_file(182 pub fn add_parsed_file(183 &self,183 &self,184 name: Rc<PathBuf>,184 name: Rc<Path>,185 source_code: IStr,185 source_code: IStr,186 parsed: LocExpr,186 parsed: LocExpr,187 ) -> Result<()> {187 ) -> Result<()> {196196197 Ok(())197 Ok(())198 }198 }199 pub fn get_source(&self, name: &PathBuf) -> Option<IStr> {199 pub fn get_source(&self, name: &Path) -> Option<IStr> {200 let ro_map = &self.data().files;200 let ro_map = &self.data().files;201 ro_map.get(name).map(|value| value.source_code.clone())201 ro_map.get(name).map(|value| value.source_code.clone())202 }202 }203 pub fn map_source_locations(&self, file: &PathBuf, locs: &[usize]) -> Vec<CodeLocation> {203 pub fn map_source_locations(&self, file: &Path, locs: &[usize]) -> Vec<CodeLocation> {204 offset_to_location(&self.get_source(file).unwrap(), locs)204 offset_to_location(&self.get_source(file).unwrap(), locs)205 }205 }206206207 pub(crate) fn import_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Val> {207 pub(crate) fn import_file(&self, from: &Path, path: &Path) -> Result<Val> {208 let file_path = self.resolve_file(from, path)?;208 let file_path = self.resolve_file(from, path)?;209 {209 {210 let data = self.data();210 let data = self.data();211 let files = &data.files;211 let files = &data.files;212 if files.contains_key(&file_path) {212 if files.contains_key(&file_path as &Path) {213 drop(data);213 drop(data);214 return self.evaluate_loaded_file_raw(&file_path);214 return self.evaluate_loaded_file_raw(&file_path);215 }215 }218 self.add_file(file_path.clone(), contents)?;218 self.add_file(file_path.clone(), contents)?;219 self.evaluate_loaded_file_raw(&file_path)219 self.evaluate_loaded_file_raw(&file_path)220 }220 }221 pub(crate) fn import_file_str(&self, from: &PathBuf, path: &PathBuf) -> Result<IStr> {221 pub(crate) fn import_file_str(&self, from: &Path, path: &Path) -> Result<IStr> {222 let path = self.resolve_file(from, path)?;222 let path = self.resolve_file(from, path)?;223 if !self.data().str_files.contains_key(&path) {223 if !self.data().str_files.contains_key(&path) {224 let file_str = self.load_file_contents(&path)?;224 let file_str = self.load_file_contents(&path)?;227 Ok(self.data().str_files.get(&path).cloned().unwrap())227 Ok(self.data().str_files.get(&path).cloned().unwrap())228 }228 }229229230 fn evaluate_loaded_file_raw(&self, name: &PathBuf) -> Result<Val> {230 fn evaluate_loaded_file_raw(&self, name: &Path) -> Result<Val> {231 let expr: LocExpr = {231 let expr: LocExpr = {232 let ro_map = &self.data().files;232 let ro_map = &self.data().files;233 let value = ro_map233 let value = ro_map253 /// Adds standard library global variable (std) to this evaluator253 /// Adds standard library global variable (std) to this evaluator254 pub fn with_stdlib(&self) -> &Self {254 pub fn with_stdlib(&self) -> &Self {255 use jrsonnet_stdlib::STDLIB_STR;255 use jrsonnet_stdlib::STDLIB_STR;256 let std_path = Rc::new(PathBuf::from("std.jsonnet"));256 let std_path: Rc<Path> = PathBuf::from("std.jsonnet").into();257 self.run_in_state(|| {257 self.run_in_state(|| {258 self.add_parsed_file(258 self.add_parsed_file(259 std_path.clone(),259 std_path.clone(),381381382/// Raw methods evaluate passed values but don't perform TLA execution382/// Raw methods evaluate passed values but don't perform TLA execution383impl EvaluationState {383impl EvaluationState {384 pub fn evaluate_file_raw(&self, name: &PathBuf) -> Result<Val> {384 pub fn evaluate_file_raw(&self, name: &Path) -> Result<Val> {385 self.run_in_state(|| self.import_file(&std::env::current_dir().expect("cwd"), name))385 self.run_in_state(|| self.import_file(&std::env::current_dir().expect("cwd"), name))386 }386 }387 pub fn evaluate_file_raw_nocwd(&self, name: &PathBuf) -> Result<Val> {387 pub fn evaluate_file_raw_nocwd(&self, name: &Path) -> Result<Val> {388 self.run_in_state(|| self.import_file(&PathBuf::from("."), name))388 self.run_in_state(|| self.import_file(&PathBuf::from("."), name))389 }389 }390 /// Parses and evaluates the given snippet390 /// Parses and evaluates the given snippet391 pub fn evaluate_snippet_raw(&self, source: Rc<PathBuf>, code: IStr) -> Result<Val> {391 pub fn evaluate_snippet_raw(&self, source: Rc<Path>, code: IStr) -> Result<Val> {392 let parsed = parse(392 let parsed = parse(393 &code,393 &code,394 &ParserSettings {394 &ParserSettings {420 }420 }421 pub fn add_ext_code(&self, name: IStr, code: IStr) -> Result<()> {421 pub fn add_ext_code(&self, name: IStr, code: IStr) -> Result<()> {422 let value =422 let value =423 self.evaluate_snippet_raw(Rc::new(PathBuf::from(format!("ext_code {}", name))), code)?;423 self.evaluate_snippet_raw(PathBuf::from(format!("ext_code {}", name)).into(), code)?;424 self.add_ext_var(name, value);424 self.add_ext_var(name, value);425 Ok(())425 Ok(())426 }426 }433 }433 }434 pub fn add_tla_code(&self, name: IStr, code: IStr) -> Result<()> {434 pub fn add_tla_code(&self, name: IStr, code: IStr) -> Result<()> {435 let value =435 let value =436 self.evaluate_snippet_raw(Rc::new(PathBuf::from(format!("tla_code {}", name))), code)?;436 self.evaluate_snippet_raw(PathBuf::from(format!("tla_code {}", name)).into(), code)?;437 self.add_tla(name, value);437 self.add_tla(name, value);438 Ok(())438 Ok(())439 }439 }440440441 pub fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {441 pub fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {442 self.settings().import_resolver.resolve_file(from, path)442 self.settings().import_resolver.resolve_file(from, path)443 }443 }444 pub fn load_file_contents(&self, path: &PathBuf) -> Result<IStr> {444 pub fn load_file_contents(&self, path: &Path) -> Result<IStr> {445 self.settings().import_resolver.load_file_contents(path)445 self.settings().import_resolver.load_file_contents(path)446 }446 }447447495 use jrsonnet_interner::IStr;495 use jrsonnet_interner::IStr;496 use jrsonnet_parser::*;496 use jrsonnet_parser::*;497 use std::{path::PathBuf, rc::Rc};497 use std::{498 path::{Path, PathBuf},499 rc::Rc,500 };498501499 #[test]502 #[test]503 state.run_in_state(|| {506 state.run_in_state(|| {504 state507 state505 .push(508 .push(506 Some(&ExprLocation(509 Some(&ExprLocation(PathBuf::from("test1.jsonnet").into(), 10, 20)),507 Rc::new(PathBuf::from("test1.jsonnet")),508 10,509 20,510 )),511 || "outer".to_owned(),510 || "outer".to_owned(),512 || {511 || {513 state.push(512 state.push(514 Some(&ExprLocation(513 Some(&ExprLocation(PathBuf::from("test2.jsonnet").into(), 30, 40)),515 Rc::new(PathBuf::from("test2.jsonnet")),516 30,517 40,518 )),533 assert!(primitive_equals(528 assert!(primitive_equals(534 &state529 &state535 .evaluate_snippet_raw(530 .evaluate_snippet_raw(536 Rc::new(PathBuf::from("raw.jsonnet")),531 PathBuf::from("raw.jsonnet").into(),537 r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#.into()532 r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#.into()538 )533 )539 .unwrap(),534 .unwrap(),546 ($str: expr) => {541 ($str: expr) => {547 EvaluationState::default()542 EvaluationState::default()548 .with_stdlib()543 .with_stdlib()549 .evaluate_snippet_raw(Rc::new(PathBuf::from("raw.jsonnet")), $str.into())544 .evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())550 .unwrap()545 .unwrap()551 };546 };552 }547 }556 evaluator.with_stdlib();551 evaluator.with_stdlib();557 evaluator.run_in_state(|| {552 evaluator.run_in_state(|| {558 evaluator553 evaluator559 .evaluate_snippet_raw(Rc::new(PathBuf::from("raw.jsonnet")), $str.into())554 .evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())560 .unwrap()555 .unwrap()561 .to_json(0)556 .to_json(0)562 .unwrap()557 .unwrap()913 "{:?}",908 "{:?}",914 jrsonnet_parser::parse(909 jrsonnet_parser::parse(915 "{ x: 1, y: 2 } == { x: 1, y: 2 }",910 "{ x: 1, y: 2 } == { x: 1, y: 2 }",916 &ParserSettings::default()911 &ParserSettings {912 file_name: PathBuf::from("equality").into(),913 loc_data: true,914 }917 )915 )918 );916 );919 assert_eval!("{ x: 1, y: 2 } == { x: 1, y: 2 }")917 assert_eval!("{ x: 1, y: 2 } == { x: 1, y: 2 }")929 #[derive(gc::Trace, gc::Finalize)]927 #[derive(gc::Trace, gc::Finalize)]930 struct NativeAdd;928 struct NativeAdd;931 impl NativeCallbackHandler for NativeAdd {929 impl NativeCallbackHandler for NativeAdd {932 fn call(&self, from: Option<Rc<PathBuf>>, args: &[Val]) -> crate::error::Result<Val> {930 fn call(&self, from: Option<Rc<Path>>, args: &[Val]) -> crate::error::Result<Val> {933 assert_eq!(931 assert_eq!(934 from.unwrap(),932 &from.unwrap() as &Path,935 Rc::new(PathBuf::from("native_caller.jsonnet"))933 &PathBuf::from("native_caller.jsonnet")936 );934 );937 match (&args[0], &args[1]) {935 match (&args[0], &args[1]) {938 (Val::Num(a), Val::Num(b)) => Ok(Val::Num(a + b)),936 (Val::Num(a), Val::Num(b)) => Ok(Val::Num(a + b)),951 )),949 )),952 );950 );953 evaluator.evaluate_snippet_raw(951 evaluator.evaluate_snippet_raw(954 Rc::new(PathBuf::from("native_caller.jsonnet")),952 PathBuf::from("native_caller.jsonnet").into(),955 "std.assertEqual(std.native(\"native_add\")(1, 2), 3)".into(),953 "std.assertEqual(std.native(\"native_add\")(1, 2), 3)".into(),956 )?;954 )?;957 Ok(())955 Ok(())100310011004 struct TestImportResolver(IStr);1002 struct TestImportResolver(IStr);1005 impl crate::import::ImportResolver for TestImportResolver {1003 impl crate::import::ImportResolver for TestImportResolver {1006 fn resolve_file(&self, _: &PathBuf, _: &PathBuf) -> crate::error::Result<Rc<PathBuf>> {1004 fn resolve_file(&self, _: &Path, _: &Path) -> crate::error::Result<Rc<Path>> {1007 Ok(Rc::new(PathBuf::from("/test")))1005 Ok(PathBuf::from("/test").into())1008 }1006 }100910071010 fn load_file_contents(&self, _: &PathBuf) -> crate::error::Result<IStr> {1008 fn load_file_contents(&self, _: &Path) -> crate::error::Result<IStr> {1011 Ok(self.0.clone())1009 Ok(self.0.clone())1012 }1010 }10131011103010281031 let error = state1029 let error = state1032 .evaluate_snippet_raw(1030 .evaluate_snippet_raw(1033 Rc::new(PathBuf::from("issue40.jsonnet")),1031 PathBuf::from("issue40.jsonnet").into(),1034 r#"1032 r#"1035 local conf = {1033 local conf = {1036 n: ""1034 n: ""crates/jrsonnet-evaluator/src/native.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/native.rs
+++ b/crates/jrsonnet-evaluator/src/native.rs
@@ -4,11 +4,11 @@
use gc::{Finalize, Trace};
use jrsonnet_parser::ParamsDesc;
use std::fmt::Debug;
-use std::path::PathBuf;
+use std::path::Path;
use std::rc::Rc;
pub trait NativeCallbackHandler: Trace {
- fn call(&self, from: Option<Rc<PathBuf>>, args: &[Val]) -> Result<Val>;
+ fn call(&self, from: Option<Rc<Path>>, args: &[Val]) -> Result<Val>;
}
#[derive(Trace, Finalize)]
@@ -20,7 +20,7 @@
pub fn new(params: ParamsDesc, handler: Box<dyn NativeCallbackHandler>) -> Self {
Self { params, handler }
}
- pub fn call(&self, caller: Option<Rc<PathBuf>>, args: &[Val]) -> Result<Val> {
+ pub fn call(&self, caller: Option<Rc<Path>>, args: &[Val]) -> Result<Val> {
self.handler.call(caller, args)
}
}
crates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -2,7 +2,7 @@
use crate::{error::Error, EvaluationState, LocError};
pub use location::*;
-use std::path::PathBuf;
+use std::path::{Path, PathBuf};
/// The way paths should be displayed
pub enum PathResolver {
@@ -15,7 +15,7 @@
}
impl PathResolver {
- pub fn resolve(&self, from: &PathBuf) -> String {
+ pub fn resolve(&self, from: &Path) -> String {
match self {
Self::FileName => from.file_name().unwrap().to_string_lossy().into_owned(),
Self::Absolute => from.to_string_lossy().into_owned(),
@@ -255,7 +255,7 @@
&self,
out: &mut dyn std::fmt::Write,
source: &str,
- origin: &PathBuf,
+ origin: &Path,
start: &CodeLocation,
end: &CodeLocation,
desc: &str,
crates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -7,7 +7,7 @@
use std::{
fmt::{Debug, Display},
ops::Deref,
- path::PathBuf,
+ path::{Path, PathBuf},
rc::Rc,
};
@@ -405,7 +405,7 @@
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Clone, PartialEq)]
-pub struct ExprLocation(pub Rc<PathBuf>, pub usize, pub usize);
+pub struct ExprLocation(pub Rc<Path>, pub usize, pub usize);
impl Finalize for ExprLocation {}
unsafe impl Trace for ExprLocation {
unsafe_empty_trace!();
crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -1,15 +1,17 @@
#![allow(clippy::redundant_closure_call)]
use peg::parser;
-use std::{path::PathBuf, rc::Rc};
+use std::{
+ path::{Path, PathBuf},
+ rc::Rc,
+};
mod expr;
pub use expr::*;
pub use peg;
-#[derive(Default)]
pub struct ParserSettings {
pub loc_data: bool,
- pub file_name: Rc<PathBuf>,
+ pub file_name: Rc<Path>,
}
parser! {
@@ -304,7 +306,6 @@
use super::{expr::*, parse};
use crate::ParserSettings;
use std::path::PathBuf;
- use std::rc::Rc;
macro_rules! parse {
($s:expr) => {
@@ -312,7 +313,7 @@
$s,
&ParserSettings {
loc_data: false,
- file_name: Rc::new(PathBuf::from("/test.jsonnet")),
+ file_name: PathBuf::from("/test.jsonnet").into(),
},
)
.unwrap()