git.delta.rocks / jrsonnet / refs/commits / ff2926d8d537

difftreelog

refactor receive Path in public api arguments

Yaroslav Bolyukin2021-06-14parent: #ad64d8a.patch.diff
in: master
Fixes #39
Fixes #47

14 files changed

modifiedbindings/jsonnet/src/import.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/import.rs
+++ b/bindings/jsonnet/src/import.rs
@@ -12,7 +12,7 @@
 	fs::File,
 	io::Read,
 	os::raw::{c_char, c_int},
-	path::PathBuf,
+	path::{Path, PathBuf},
 	ptr::null_mut,
 	rc::Rc,
 };
@@ -33,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();
@@ -73,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 {
@@ -108,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 {
modifiedbindings/jsonnet/src/lib.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -15,7 +15,6 @@
 	ffi::{CStr, CString},
 	os::raw::{c_char, c_double, c_int, c_uint},
 	path::PathBuf,
-	rc::Rc,
 };
 
 /// WASM stub
@@ -144,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))
@@ -220,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))
@@ -294,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))
modifiedcmds/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))?
 	};
modifiedcrates/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,
 		},
 	)
modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -174,7 +174,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)
 	})
 }
modifiedcrates/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()
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -5,7 +5,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)]
@@ -86,7 +89,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,
 		error: Box<jrsonnet_parser::ParseError>,
 	},
modifiedcrates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate.rs
@@ -609,26 +609,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))?)
 		}
 	})
 }
modifiedcrates/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()
 	}
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -35,7 +35,7 @@
 	collections::HashMap,
 	fmt::Debug,
 	hash::BuildHasherDefault,
-	path::PathBuf,
+	path::{Path, PathBuf},
 	rc::Rc,
 };
 use trace::{offset_to_location, CodeLocation, CompactFormat, TraceFormat};
@@ -109,8 +109,8 @@
 	/// Used for stack overflow detection, stacktrace is populated on unwind
 	stack_depth: usize,
 	/// Contains file source codes and evaluation results for imports and pretty-printed stacktraces
-	files: HashMap<Rc<PathBuf>, FileData>,
-	str_files: HashMap<Rc<PathBuf>, IStr>,
+	files: HashMap<Rc<Path>, FileData>,
+	str_files: HashMap<Rc<Path>, IStr>,
 }
 
 pub struct FileData {
@@ -156,7 +156,7 @@
 
 impl EvaluationState {
 	/// Parses and adds file as loaded
-	pub fn add_file(&self, path: Rc<PathBuf>, source_code: IStr) -> Result<()> {
+	pub fn add_file(&self, path: Rc<Path>, source_code: IStr) -> Result<()> {
 		self.add_parsed_file(
 			path.clone(),
 			source_code.clone(),
@@ -169,7 +169,7 @@
 			)
 			.map_err(|error| ImportSyntaxError {
 				error: Box::new(error),
-				path,
+				path: path.to_owned(),
 				source_code,
 			})?,
 		)?;
@@ -180,7 +180,7 @@
 	/// Adds file by source code and parsed expr
 	pub fn add_parsed_file(
 		&self,
-		name: Rc<PathBuf>,
+		name: Rc<Path>,
 		source_code: IStr,
 		parsed: LocExpr,
 	) -> Result<()> {
@@ -195,20 +195,20 @@
 
 		Ok(())
 	}
-	pub fn get_source(&self, name: &PathBuf) -> Option<IStr> {
+	pub fn get_source(&self, name: &Path) -> Option<IStr> {
 		let ro_map = &self.data().files;
 		ro_map.get(name).map(|value| value.source_code.clone())
 	}
-	pub fn map_source_locations(&self, file: &PathBuf, locs: &[usize]) -> Vec<CodeLocation> {
+	pub fn map_source_locations(&self, file: &Path, locs: &[usize]) -> Vec<CodeLocation> {
 		offset_to_location(&self.get_source(file).unwrap(), locs)
 	}
 
-	pub(crate) fn import_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Val> {
+	pub(crate) fn import_file(&self, from: &Path, path: &Path) -> Result<Val> {
 		let file_path = self.resolve_file(from, path)?;
 		{
 			let data = self.data();
 			let files = &data.files;
-			if files.contains_key(&file_path) {
+			if files.contains_key(&file_path as &Path) {
 				drop(data);
 				return self.evaluate_loaded_file_raw(&file_path);
 			}
@@ -217,7 +217,7 @@
 		self.add_file(file_path.clone(), contents)?;
 		self.evaluate_loaded_file_raw(&file_path)
 	}
-	pub(crate) fn import_file_str(&self, from: &PathBuf, path: &PathBuf) -> Result<IStr> {
+	pub(crate) fn import_file_str(&self, from: &Path, path: &Path) -> Result<IStr> {
 		let path = self.resolve_file(from, path)?;
 		if !self.data().str_files.contains_key(&path) {
 			let file_str = self.load_file_contents(&path)?;
@@ -226,7 +226,7 @@
 		Ok(self.data().str_files.get(&path).cloned().unwrap())
 	}
 
-	fn evaluate_loaded_file_raw(&self, name: &PathBuf) -> Result<Val> {
+	fn evaluate_loaded_file_raw(&self, name: &Path) -> Result<Val> {
 		let expr: LocExpr = {
 			let ro_map = &self.data().files;
 			let value = ro_map
@@ -252,7 +252,7 @@
 	/// Adds standard library global variable (std) to this evaluator
 	pub fn with_stdlib(&self) -> &Self {
 		use jrsonnet_stdlib::STDLIB_STR;
-		let std_path = Rc::new(PathBuf::from("std.jsonnet"));
+		let std_path: Rc<Path> = PathBuf::from("std.jsonnet").into();
 		self.run_in_state(|| {
 			self.add_parsed_file(
 				std_path.clone(),
@@ -380,14 +380,14 @@
 
 /// Raw methods evaluate passed values but don't perform TLA execution
 impl EvaluationState {
-	pub fn evaluate_file_raw(&self, name: &PathBuf) -> Result<Val> {
+	pub fn evaluate_file_raw(&self, name: &Path) -> Result<Val> {
 		self.run_in_state(|| self.import_file(&std::env::current_dir().expect("cwd"), name))
 	}
-	pub fn evaluate_file_raw_nocwd(&self, name: &PathBuf) -> Result<Val> {
+	pub fn evaluate_file_raw_nocwd(&self, name: &Path) -> Result<Val> {
 		self.run_in_state(|| self.import_file(&PathBuf::from("."), name))
 	}
 	/// Parses and evaluates the given snippet
-	pub fn evaluate_snippet_raw(&self, source: Rc<PathBuf>, code: IStr) -> Result<Val> {
+	pub fn evaluate_snippet_raw(&self, source: Rc<Path>, code: IStr) -> Result<Val> {
 		let parsed = parse(
 			&code,
 			&ParserSettings {
@@ -419,7 +419,7 @@
 	}
 	pub fn add_ext_code(&self, name: IStr, code: IStr) -> Result<()> {
 		let value =
-			self.evaluate_snippet_raw(Rc::new(PathBuf::from(format!("ext_code {}", name))), code)?;
+			self.evaluate_snippet_raw(PathBuf::from(format!("ext_code {}", name)).into(), code)?;
 		self.add_ext_var(name, value);
 		Ok(())
 	}
@@ -432,15 +432,15 @@
 	}
 	pub fn add_tla_code(&self, name: IStr, code: IStr) -> Result<()> {
 		let value =
-			self.evaluate_snippet_raw(Rc::new(PathBuf::from(format!("tla_code {}", name))), code)?;
+			self.evaluate_snippet_raw(PathBuf::from(format!("tla_code {}", name)).into(), code)?;
 		self.add_tla(name, value);
 		Ok(())
 	}
 
-	pub fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {
+	pub fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
 		self.settings().import_resolver.resolve_file(from, path)
 	}
-	pub fn load_file_contents(&self, path: &PathBuf) -> Result<IStr> {
+	pub fn load_file_contents(&self, path: &Path) -> Result<IStr> {
 		self.settings().import_resolver.load_file_contents(path)
 	}
 
@@ -490,7 +490,10 @@
 	use crate::{error::Error::*, primitive_equals, EvaluationState};
 	use jrsonnet_interner::IStr;
 	use jrsonnet_parser::*;
-	use std::{path::PathBuf, rc::Rc};
+	use std::{
+		path::{Path, PathBuf},
+		rc::Rc,
+	};
 
 	#[test]
 	#[should_panic]
@@ -499,19 +502,11 @@
 		state.run_in_state(|| {
 			state
 				.push(
-					Some(&ExprLocation(
-						Rc::new(PathBuf::from("test1.jsonnet")),
-						10,
-						20,
-					)),
+					Some(&ExprLocation(PathBuf::from("test1.jsonnet").into(), 10, 20)),
 					|| "outer".to_owned(),
 					|| {
 						state.push(
-							Some(&ExprLocation(
-								Rc::new(PathBuf::from("test2.jsonnet")),
-								30,
-								40,
-							)),
+							Some(&ExprLocation(PathBuf::from("test2.jsonnet").into(), 30, 40)),
 							|| "inner".to_owned(),
 							|| Err(RuntimeError("".into()).into()),
 						)?;
@@ -529,7 +524,7 @@
 		assert!(primitive_equals(
 			&state
 				.evaluate_snippet_raw(
-					Rc::new(PathBuf::from("raw.jsonnet")),
+					PathBuf::from("raw.jsonnet").into(),
 					r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#.into()
 				)
 				.unwrap(),
@@ -542,7 +537,7 @@
 		($str: expr) => {
 			EvaluationState::default()
 				.with_stdlib()
-				.evaluate_snippet_raw(Rc::new(PathBuf::from("raw.jsonnet")), $str.into())
+				.evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())
 				.unwrap()
 		};
 	}
@@ -552,7 +547,7 @@
 			evaluator.with_stdlib();
 			evaluator.run_in_state(|| {
 				evaluator
-					.evaluate_snippet_raw(Rc::new(PathBuf::from("raw.jsonnet")), $str.into())
+					.evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())
 					.unwrap()
 					.to_json(0)
 					.unwrap()
@@ -909,7 +904,10 @@
 			"{:?}",
 			jrsonnet_parser::parse(
 				"{ x: 1, y: 2 } == { x: 1, y: 2 }",
-				&ParserSettings::default()
+				&ParserSettings {
+					file_name: PathBuf::from("equality").into(),
+					loc_data: true,
+				}
 			)
 		);
 		assert_eval!("{ x: 1, y: 2 } == { x: 1, y: 2 }")
@@ -930,8 +928,8 @@
 				])),
 				|caller, args| {
 					assert_eq!(
-						caller.unwrap(),
-						Rc::new(PathBuf::from("native_caller.jsonnet"))
+						&caller.unwrap() as &Path,
+						&PathBuf::from("native_caller.jsonnet")
 					);
 					match (&args[0], &args[1]) {
 						(Val::Num(a), Val::Num(b)) => Ok(Val::Num(a + b)),
@@ -941,7 +939,7 @@
 			)),
 		);
 		evaluator.evaluate_snippet_raw(
-			Rc::new(PathBuf::from("native_caller.jsonnet")),
+			PathBuf::from("native_caller.jsonnet").into(),
 			"std.assertEqual(std.native(\"native_add\")(1, 2), 3)".into(),
 		)?;
 		Ok(())
@@ -993,11 +991,11 @@
 
 	struct TestImportResolver(IStr);
 	impl crate::import::ImportResolver for TestImportResolver {
-		fn resolve_file(&self, _: &PathBuf, _: &PathBuf) -> crate::error::Result<Rc<PathBuf>> {
-			Ok(Rc::new(PathBuf::from("/test")))
+		fn resolve_file(&self, _: &Path, _: &Path) -> crate::error::Result<Rc<Path>> {
+			Ok(PathBuf::from("/test").into())
 		}
 
-		fn load_file_contents(&self, _: &PathBuf) -> crate::error::Result<IStr> {
+		fn load_file_contents(&self, _: &Path) -> crate::error::Result<IStr> {
 			Ok(self.0.clone())
 		}
 
@@ -1020,7 +1018,7 @@
 
 		let error = state
 			.evaluate_snippet_raw(
-				Rc::new(PathBuf::from("issue40.jsonnet")),
+				PathBuf::from("issue40.jsonnet").into(),
 				r#"
 				local conf = {
 					n: ""
modifiedcrates/jrsonnet-evaluator/src/native.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/native.rs
+++ b/crates/jrsonnet-evaluator/src/native.rs
@@ -3,24 +3,24 @@
 use crate::{error::Result, Val};
 use jrsonnet_parser::ParamsDesc;
 use std::fmt::Debug;
-use std::path::PathBuf;
+use std::path::Path;
 use std::rc::Rc;
 
 pub struct NativeCallback {
 	pub params: ParamsDesc,
-	handler: Box<dyn Fn(Option<Rc<PathBuf>>, &[Val]) -> Result<Val>>,
+	handler: Box<dyn Fn(Option<Rc<Path>>, &[Val]) -> Result<Val>>,
 }
 impl NativeCallback {
 	pub fn new(
 		params: ParamsDesc,
-		handler: impl Fn(Option<Rc<PathBuf>>, &[Val]) -> Result<Val> + 'static,
+		handler: impl Fn(Option<Rc<Path>>, &[Val]) -> Result<Val> + 'static,
 	) -> Self {
 		Self {
 			params,
 			handler: Box::new(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)(caller, args)
 	}
 }
modifiedcrates/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,
modifiedcrates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -6,7 +6,7 @@
 use std::{
 	fmt::{Debug, Display},
 	ops::Deref,
-	path::PathBuf,
+	path::{Path, PathBuf},
 	rc::Rc,
 };
 
@@ -320,7 +320,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 Debug for ExprLocation {
 	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 		write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)
modifiedcrates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-parser/src/lib.rs
1#![allow(clippy::redundant_closure_call)]23use peg::parser;4use std::{path::PathBuf, rc::Rc};5mod expr;6pub use expr::*;7pub use peg;89#[derive(Default)]10pub struct ParserSettings {11	pub loc_data: bool,12	pub file_name: Rc<PathBuf>,13}1415parser! {16	grammar jsonnet_parser() for str {17		use peg::ParseLiteral;1819		/// Standard C-like comments20		rule comment()21			= "//" (!['\n'][_])* "\n"22			/ "/*" ("\\*/" / "\\\\" / (!("*/")[_]))* "*/"23			/ "#" (!['\n'][_])* "\n"2425		rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")26		rule _() = single_whitespace()*2728		/// For comma-delimited elements29		rule comma() = quiet!{_ "," _} / expected!("<comma>")30		rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}31		rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}32		rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']33		/// Sequence of digits34		rule uint() -> u64 = a:$(digit()+) { a.parse().unwrap() }35		/// Number in scientific notation format36		rule number() -> f64 = quiet!{a:$(uint() ("." uint())? (['e'|'E'] (s:['+'|'-'])? uint())?) { a.parse().unwrap() }} / expected!("<number>")3738		/// Reserved word followed by any non-alphanumberic39		rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()40		rule id() = quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")4142		rule keyword(id: &'static str) -> ()43			= ##parse_string_literal(id) end_of_ident()44		// Adds location data information to existing expression45		rule l(s: &ParserSettings, x: rule<Expr>) -> LocExpr46			= start:position!() v:x() end:position!() {loc_expr!(v, s.loc_data, (s.file_name.clone(), start, end))}4748		pub rule param(s: &ParserSettings) -> expr::Param = name:$(id()) expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name.into(), expr) }49		pub rule params(s: &ParserSettings) -> expr::ParamsDesc50			= params:param(s) ** comma() comma()? {51				let mut defaults_started = false;52				for param in &params {53					defaults_started = defaults_started || param.1.is_some();54					assert_eq!(defaults_started, param.1.is_some(), "defauld parameters should be used after all positionals");55				}56				expr::ParamsDesc(Rc::new(params))57			}58			/ { expr::ParamsDesc(Rc::new(Vec::new())) }5960		pub rule arg(s: &ParserSettings) -> expr::Arg61			= name:$(id()) _ "=" _ expr:expr(s) {expr::Arg(Some(name.into()), expr)}62			/ expr:expr(s) {expr::Arg(None, expr)}63		pub rule args(s: &ParserSettings) -> expr::ArgsDesc64			= args:arg(s) ** comma() comma()? {65				let mut named_started = false;66				for arg in &args {67					named_started = named_started || arg.0.is_some();68					assert_eq!(named_started, arg.0.is_some(), "named args should be used after all positionals");69				}70				expr::ArgsDesc(args)71			}72			/ { expr::ArgsDesc(Vec::new()) }7374		pub rule bind(s: &ParserSettings) -> expr::BindSpec75			= name:$(id()) _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: None, value: expr}}76			/ name:$(id()) _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: Some(params), value: expr}}77		pub rule assertion(s: &ParserSettings) -> expr::AssertStmt78			= keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }7980		pub rule whole_line() -> &'input str81			= str:$((!['\n'][_])* "\n") {str}82		pub rule string_block() -> String83			= "|||" (!['\n']single_whitespace())* "\n"84			  empty_lines:$(['\n']*)85			  prefix:[' ' | '\t']+ first_line:whole_line()86			  lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*87			  [' ' | '\t']*<, {prefix.len() - 1}> "|||"88			  {let mut l = empty_lines.to_owned(); l.push_str(first_line); l.extend(lines); l}89		pub rule string() -> String90			= quiet!{ "\"" str:$(("\\\"" / "\\\\" / (!['"'][_]))*) "\"" {unescape::unescape(str).unwrap()}91			/ "'" str:$(("\\'" / "\\\\" / (!['\''][_]))*) "'" {unescape::unescape(str).unwrap()}92			/ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}93			/ "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}94			/ string_block() } / expected!("<string>")9596		pub rule field_name(s: &ParserSettings) -> expr::FieldName97			= name:$(id()) {expr::FieldName::Fixed(name.into())}98			/ name:string() {expr::FieldName::Fixed(name.into())}99			/ "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}100		pub rule visibility() -> expr::Visibility101			= ":::" {expr::Visibility::Unhide}102			/ "::" {expr::Visibility::Hidden}103			/ ":" {expr::Visibility::Normal}104		pub rule field(s: &ParserSettings) -> expr::FieldMember105			= name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{106				name,107				plus: plus.is_some(),108				params: None,109				visibility,110				value,111			}}112			/ name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{113				name,114				plus: false,115				params: Some(params),116				visibility,117				value,118			}}119		pub rule obj_local(s: &ParserSettings) -> BindSpec120			= keyword("local") _ bind:bind(s) {bind}121		pub rule member(s: &ParserSettings) -> expr::Member122			= bind:obj_local(s) {expr::Member::BindStmt(bind)}123			/ assertion:assertion(s) {expr::Member::AssertStmt(assertion)}124			/ field:field(s) {expr::Member::Field(field)}125		pub rule objinside(s: &ParserSettings) -> expr::ObjBody126			= pre_locals:(b: obj_local(s) comma() {b})* "[" _ key:expr(s) _ "]" _ ":" _ value:expr(s) post_locals:(comma() b:obj_local(s) {b})* _ forspec:forspec(s) others:(_ rest:compspec(s) {rest})? {127				let mut compspecs = vec![CompSpec::ForSpec(forspec)];128				compspecs.extend(others.unwrap_or_default());129				expr::ObjBody::ObjComp(expr::ObjComp{130					pre_locals,131					key,132					value,133					post_locals,134					compspecs,135				})136			}137			/ members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}138		pub rule ifspec(s: &ParserSettings) -> IfSpecData139			= keyword("if") _ expr:expr(s) {IfSpecData(expr)}140		pub rule forspec(s: &ParserSettings) -> ForSpecData141			= keyword("for") _ id:$(id()) _ keyword("in") _ cond:expr(s) {ForSpecData(id.into(), cond)}142		pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>143			= s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}144		pub rule local_expr(s: &ParserSettings) -> LocExpr145			= l(s,<keyword("local") _ binds:bind(s) ** comma() _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }>)146		pub rule string_expr(s: &ParserSettings) -> LocExpr147			= l(s, <s:string() {Expr::Str(s.into())}>)148		pub rule obj_expr(s: &ParserSettings) -> LocExpr149			= l(s,<"{" _ body:objinside(s) _ "}" {Expr::Obj(body)}>)150		pub rule array_expr(s: &ParserSettings) -> LocExpr151			= l(s,<"[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}>)152		pub rule array_comp_expr(s: &ParserSettings) -> LocExpr153			= l(s,<"[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {154				let mut specs = vec![CompSpec::ForSpec(forspec)];155				specs.extend(others.unwrap_or_default());156				Expr::ArrComp(expr, specs)157			}>)158		pub rule number_expr(s: &ParserSettings) -> LocExpr159			= l(s,<n:number() { expr::Expr::Num(n) }>)160		pub rule var_expr(s: &ParserSettings) -> LocExpr161			= l(s,<n:$(id()) { expr::Expr::Var(n.into()) }>)162		pub rule if_then_else_expr(s: &ParserSettings) -> LocExpr163			= l(s,<cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{164				cond,165				cond_then,166				cond_else,167			}}>)168169		pub rule literal(s: &ParserSettings) -> LocExpr170			= l(s,<v:(171				keyword("null") {LiteralType::Null}172				/ keyword("true") {LiteralType::True}173				/ keyword("false") {LiteralType::False}174				/ keyword("self") {LiteralType::This}175				/ keyword("$") {LiteralType::Dollar}176				/ keyword("super") {LiteralType::Super}177			) {Expr::Literal(v)}>)178179		pub rule expr_basic(s: &ParserSettings) -> LocExpr180			= literal(s)181182			/ string_expr(s) / number_expr(s)183			/ array_expr(s)184			/ obj_expr(s)185			/ array_expr(s)186			/ array_comp_expr(s)187188			/ l(s,<keyword("importstr") _ path:string() {Expr::ImportStr(PathBuf::from(path))}>)189			/ l(s,<keyword("import") _ path:string() {Expr::Import(PathBuf::from(path))}>)190191			/ var_expr(s)192			/ local_expr(s)193			/ if_then_else_expr(s)194195			/ l(s,<keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}>)196			/ l(s,<assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }>)197198			/ l(s,<keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }>)199200		rule slice_part(s: &ParserSettings) -> Option<LocExpr>201			= e:(_ e:expr(s) _{e})? {e}202		pub rule slice_desc(s: &ParserSettings) -> SliceDesc203			= start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {204				let (end, step) = if let Some((end, step)) = pair {205					(end, step)206				}else{207					(None, None)208				};209210				SliceDesc { start, end, step }211			}212213		rule binop(x: rule<()>) -> ()214			= quiet!{ x() } / expected!("<binary op>")215		rule unaryop(x: rule<()>) -> ()216			= quiet!{ x() } / expected!("<unary op>")217218		rule expr(s: &ParserSettings) -> LocExpr219			= start:position!() a:precedence! {220				a:(@) _ binop(<"||">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Or, b))}221				--222				a:(@) _ binop(<"&&">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::And, b))}223				--224				a:(@) _ binop(<"|">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitOr, b))}225				--226				a:@ _ binop(<"^">) _ b:(@) {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitXor, b))}227				--228				a:(@) _ binop(<"&">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitAnd, b))}229				--230				a:(@) _ binop(<"==">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Eq, b))}231				a:(@) _ binop(<"!=">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Neq, b))}232				--233				a:(@) _ binop(<"<">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lt, b))}234				a:(@) _ binop(<">">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gt, b))}235				a:(@) _ binop(<"<=">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lte, b))}236				a:(@) _ binop(<">=">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gte, b))}237				a:(@) _ binop(<keyword("in")>) _ b:@ {loc_expr_todo!(Expr::Apply(238					el!(Expr::Intrinsic("objectHasEx".into())), ArgsDesc(vec![Arg(None, b), Arg(None, a), Arg(None, el!(Expr::Literal(LiteralType::True)))]),239					true240				))}241				--242				a:(@) _ binop(<"<<">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lhs, b))}243				a:(@) _ binop(<">>">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Rhs, b))}244				--245				a:(@) _ binop(<"+">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Add, b))}246				a:(@) _ binop(<"-">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Sub, b))}247				--248				a:(@) _ binop(<"*">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Mul, b))}249				a:(@) _ binop(<"/">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Div, b))}250				a:(@) _ binop(<"%">) _ b:@ {loc_expr_todo!(Expr::Apply(251					el!(Expr::Intrinsic("mod".into())), ArgsDesc(vec![Arg(None, a), Arg(None, b)]),252					false253				))}254				--255						unaryop(<"-">) _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Minus, b))}256						unaryop(<"!">) _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, b))}257						unaryop(<"~">) _ b:@ { loc_expr_todo!(Expr::UnaryOp(UnaryOpType::BitNot, b)) }258				--259				a:(@) _ "[" _ s:slice_desc(s) _ "]" {loc_expr_todo!(Expr::Apply(260					el!(Expr::Intrinsic("slice".into())),261					ArgsDesc(vec![262						Arg(None, a),263						Arg(None, s.start.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),264						Arg(None, s.end.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),265						Arg(None, s.step.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),266					]),267					true,268				))}269				a:(@) _ "." _ s:$(id()) {loc_expr_todo!(Expr::Index(a, el!(Expr::Str(s.into()))))}270				a:(@) _ "[" _ s:expr(s) _ "]" {loc_expr_todo!(Expr::Index(a, s))}271				a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {loc_expr_todo!(Expr::Apply(a, args, ts.is_some()))}272				a:(@) _ "{" _ body:objinside(s) _ "}" {loc_expr_todo!(Expr::ObjExtend(a, body))}273				--274				e:expr_basic(s) {e}275				"(" _ e:expr(s) _ ")" {loc_expr_todo!(Expr::Parened(e))}276			} end:position!() {277				let LocExpr(e, _) = a;278				LocExpr(e, if s.loc_data {279					Some(ExprLocation(s.file_name.clone(), start, end))280				} else {281					None282				})283			}284			/ e:expr_basic(s) {e}285286		pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}287	}288}289290pub type ParseError = peg::error::ParseError<peg::str::LineCol>;291pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {292	jsonnet_parser::jsonnet(str, settings)293}294295#[macro_export]296macro_rules! el {297	($expr:expr) => {298		LocExpr(std::rc::Rc::new($expr), None)299	};300}301302#[cfg(test)]303pub mod tests {304	use super::{expr::*, parse};305	use crate::ParserSettings;306	use std::path::PathBuf;307	use std::rc::Rc;308309	macro_rules! parse {310		($s:expr) => {311			parse(312				$s,313				&ParserSettings {314					loc_data: false,315					file_name: Rc::new(PathBuf::from("/test.jsonnet")),316				},317			)318			.unwrap()319		};320	}321322	mod expressions {323		use super::*;324325		pub fn basic_math() -> LocExpr {326			el!(Expr::BinaryOp(327				el!(Expr::Num(2.0)),328				BinaryOpType::Add,329				el!(Expr::BinaryOp(330					el!(Expr::Num(2.0)),331					BinaryOpType::Mul,332					el!(Expr::Num(2.0)),333				)),334			))335		}336	}337338	#[test]339	fn multiline_string() {340		assert_eq!(341			parse!("|||\n    Hello world!\n     a\n|||"),342			el!(Expr::Str("Hello world!\n a\n".into())),343		);344		assert_eq!(345			parse!("|||\n  Hello world!\n   a\n|||"),346			el!(Expr::Str("Hello world!\n a\n".into())),347		);348		assert_eq!(349			parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),350			el!(Expr::Str("Hello world!\n\ta\n".into())),351		);352		assert_eq!(353			parse!("|||\n   Hello world!\n    a\n |||"),354			el!(Expr::Str("Hello world!\n a\n".into())),355		);356	}357358	#[test]359	fn slice() {360		parse!("a[1:]");361		parse!("a[1::]");362		parse!("a[:1:]");363		parse!("a[::1]");364		parse!("str[:len - 1]");365	}366367	#[test]368	fn string_escaping() {369		assert_eq!(370			parse!(r#""Hello, \"world\"!""#),371			el!(Expr::Str(r#"Hello, "world"!"#.into())),372		);373		assert_eq!(374			parse!(r#"'Hello \'world\'!'"#),375			el!(Expr::Str("Hello 'world'!".into())),376		);377		assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into())),);378	}379380	#[test]381	fn string_unescaping() {382		assert_eq!(383			parse!(r#""Hello\nWorld""#),384			el!(Expr::Str("Hello\nWorld".into())),385		);386	}387388	#[test]389	fn string_verbantim() {390		assert_eq!(391			parse!(r#"@"Hello\n""World""""#),392			el!(Expr::Str("Hello\\n\"World\"".into())),393		);394	}395396	#[test]397	fn imports() {398		assert_eq!(399			parse!("import \"hello\""),400			el!(Expr::Import(PathBuf::from("hello"))),401		);402		assert_eq!(403			parse!("importstr \"garnish.txt\""),404			el!(Expr::ImportStr(PathBuf::from("garnish.txt")))405		);406	}407408	#[test]409	fn empty_object() {410		assert_eq!(parse!("{}"), el!(Expr::Obj(ObjBody::MemberList(vec![]))));411	}412413	#[test]414	fn basic_math() {415		assert_eq!(416			parse!("2+2*2"),417			el!(Expr::BinaryOp(418				el!(Expr::Num(2.0)),419				BinaryOpType::Add,420				el!(Expr::BinaryOp(421					el!(Expr::Num(2.0)),422					BinaryOpType::Mul,423					el!(Expr::Num(2.0))424				))425			))426		);427	}428429	#[test]430	fn basic_math_with_indents() {431		assert_eq!(parse!("2	+ 	  2	  *	2   	"), expressions::basic_math());432	}433434	#[test]435	fn basic_math_parened() {436		assert_eq!(437			parse!("2+(2+2*2)"),438			el!(Expr::BinaryOp(439				el!(Expr::Num(2.0)),440				BinaryOpType::Add,441				el!(Expr::Parened(expressions::basic_math())),442			))443		);444	}445446	/// Comments should not affect parsing447	#[test]448	fn comments() {449		assert_eq!(450			parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),451			el!(Expr::BinaryOp(452				el!(Expr::Num(2.0)),453				BinaryOpType::Add,454				el!(Expr::BinaryOp(455					el!(Expr::Num(3.0)),456					BinaryOpType::Mul,457					el!(Expr::Num(4.0))458				))459			))460		);461	}462463	/// Comments should be able to be escaped464	#[test]465	fn comment_escaping() {466		assert_eq!(467			parse!("2/*\\*/+*/ - 22"),468			el!(Expr::BinaryOp(469				el!(Expr::Num(2.0)),470				BinaryOpType::Sub,471				el!(Expr::Num(22.0))472			))473		);474	}475476	#[test]477	fn suffix() {478		// assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));479		// assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));480		// assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));481		// assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))482	}483484	#[test]485	fn array_comp() {486		use Expr::*;487		assert_eq!(488			parse!("[std.deepJoin(x) for x in arr]"),489			el!(ArrComp(490				el!(Apply(491					el!(Index(el!(Var("std".into())), el!(Str("deepJoin".into())))),492					ArgsDesc(vec![Arg(None, el!(Var("x".into())))]),493					false,494				)),495				vec![CompSpec::ForSpec(ForSpecData(496					"x".into(),497					el!(Var("arr".into()))498				))]499			)),500		)501	}502503	#[test]504	fn reserved() {505		use Expr::*;506		assert_eq!(parse!("null"), el!(Literal(LiteralType::Null)));507		assert_eq!(parse!("nulla"), el!(Var("nulla".into())));508	}509510	#[test]511	fn multiple_args_buf() {512		parse!("a(b, null_fields)");513	}514515	#[test]516	fn infix_precedence() {517		use Expr::*;518		assert_eq!(519			parse!("!a && !b"),520			el!(BinaryOp(521				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into())))),522				BinaryOpType::And,523				el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()))))524			))525		);526	}527528	#[test]529	fn infix_precedence_division() {530		use Expr::*;531		assert_eq!(532			parse!("!a / !b"),533			el!(BinaryOp(534				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into())))),535				BinaryOpType::Div,536				el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()))))537			))538		);539	}540541	#[test]542	fn double_negation() {543		use Expr::*;544		assert_eq!(545			parse!("!!a"),546			el!(UnaryOp(547				UnaryOpType::Not,548				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()))))549			))550		)551	}552553	#[test]554	fn array_test_error() {555		parse!("[a for a in b if c for e in f]");556		//                    ^^^^ failed code557	}558559	#[test]560	fn can_parse_stdlib() {561		parse!(jrsonnet_stdlib::STDLIB_STR);562	}563564	// From source code565	/*566	#[bench]567	fn bench_parse_peg(b: &mut Bencher) {568		b.iter(|| parse!(jrsonnet_stdlib::STDLIB_STR))569	}570	*/571}