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

difftreelog

feat(evaluator) custom source paths

Yaroslav Bolyukin2022-08-27parent: #a7e60a9.patch.diff
in: master

5 files changed

modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -137,15 +137,21 @@
 	StandaloneSuper,
 
 	#[error("can't resolve {1} from {0}")]
-	ImportFileNotFound(PathBuf, String),
+	ImportFileNotFound(SourcePath, String),
+	#[error("can't resolve absolute {0}")]
+	AbsoluteImportFileNotFound(PathBuf),
 	#[error("resolved file not found: {:?}", .0)]
 	ResolvedFileNotFound(SourcePath),
+	#[error("can't import {0}: is a directory")]
+	ImportIsADirectory(SourcePath),
 	#[error("imported file is not valid utf-8: {0:?}")]
 	ImportBadFileUtf8(SourcePath),
 	#[error("import io error: {0}")]
 	ImportIo(String),
-	#[error("tried to import {1} from {0}, but imports is not supported")]
-	ImportNotSupported(PathBuf, PathBuf),
+	#[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),
 	#[error("can't import from virtual file")]
 	CantImportFromVirtualFile,
 	#[error(
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -630,15 +630,7 @@
 		}
 		i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {
 			let tmp = loc.clone().0;
-			let import_location = tmp
-				.path()
-				.map(|p| {
-					let mut p = p.to_owned();
-					p.pop();
-					p
-				})
-				.unwrap_or_default();
-			let resolved_path = s.resolve_file(&import_location, path as &str)?;
+			let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;
 			match i {
 				Import(_) => s.push(
 					CallLocation::new(loc),
modifiedcrates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -1,51 +1,60 @@
 use std::{
 	any::Any,
+	cell::RefCell,
+	env::current_dir,
 	fs,
-	io::Read,
+	io::{ErrorKind, Read},
 	path::{Path, PathBuf},
 };
 
 use fs::File;
-use jrsonnet_parser::SourcePath;
+use jrsonnet_parser::{SourceDirectory, SourceFile, SourcePath};
 
 use crate::{
-	error::{Error::*, Result},
+	error::{
+		Error::{self, *},
+		Result,
+	},
 	throw,
 };
 
 /// Implements file resolution logic for `import` and `importStr`
 pub trait ImportResolver {
-	/// Resolves real file path, e.g. `(/home/user/manifests, b.libjsonnet)` can correspond
+	/// Resolves 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_relative(&self, from: &Path, path: &str) -> Result<SourcePath>;
+	///
+	/// `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> {
+		throw!(ImportNotSupported(from.clone(), path.into()))
+	}
+	fn resolve_from_default(&self, path: &str) -> 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> {
+		throw!(AbsoluteImportNotSupported(path.to_owned()))
+	}
 
 	/// Load resolved file
-	/// This should only be called with value returned from `resolve_file`, this cannot be resolved using associated type,
-	/// as evaluator uses object instead of generic for [`ImportResolver`]
+	/// This should only be called with value returned from [`ImportResolver::resolve_file`]/[`ImportResolver::resolve`],
+	/// this cannot be resolved using associated type, as evaluator uses object instead of generic for [`ImportResolver`]
 	fn load_file_contents(&self, resolved: &SourcePath) -> Result<Vec<u8>>;
 
-	/// # Safety
-	///
-	/// For use only in bindings, should not be used elsewhere.
-	/// Implementations which are not intended to be used in bindings
-	/// should panic on call to this method.
-	unsafe fn as_any(&self) -> &dyn Any;
+	/// For downcasts
+	fn as_any(&self) -> &dyn Any;
 }
 
 /// Dummy resolver, can't resolve/load any file
 pub struct DummyImportResolver;
 impl ImportResolver for DummyImportResolver {
-	fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {
-		throw!(ImportNotSupported(from.into(), path.into()))
-	}
-
 	fn load_file_contents(&self, _resolved: &SourcePath) -> Result<Vec<u8>> {
 		panic!("dummy resolver can't load any file")
 	}
 
-	unsafe fn as_any(&self) -> &dyn Any {
-		panic!("`as_any(&self)` is not supported by dummy resolver")
+	fn as_any(&self) -> &dyn Any {
+		self
 	}
 }
 #[allow(clippy::use_self)]
@@ -60,36 +69,82 @@
 pub struct FileImportResolver {
 	/// Library directories to search for file.
 	/// Referred to as `jpath` in original jsonnet implementation.
-	pub library_paths: Vec<PathBuf>,
+	library_paths: RefCell<Vec<PathBuf>>,
 }
+impl FileImportResolver {
+	pub fn new(jpath: Vec<PathBuf>) -> Self {
+		Self {
+			library_paths: RefCell::new(jpath),
+		}
+	}
+	/// Dynamically add new jpath, used by bindings
+	pub fn add_jpath(&self, path: PathBuf) {
+		self.library_paths.borrow_mut().push(path);
+	}
+}
 impl ImportResolver for FileImportResolver {
-	fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {
-		let mut direct = from.to_path_buf();
+	fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
+		let mut direct = if let Some(f) = from.downcast_ref::<SourceFile>() {
+			let mut o = f.path().to_owned();
+			o.pop();
+			o
+		} else if let Some(d) = from.downcast_ref::<SourceDirectory>() {
+			d.path().to_owned()
+		} else if from.is_default() {
+			current_dir().map_err(|e| Error::ImportIo(e.to_string()))?
+		} else {
+			unreachable!("resolver can't return this path")
+		};
 		direct.push(path);
-		if direct.exists() {
-			Ok(SourcePath::Path(
+		if direct.is_file() {
+			Ok(SourcePath::new(SourceFile::new(
 				direct.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
-			))
+			)))
 		} else {
-			for library_path in &self.library_paths {
+			for library_path in self.library_paths.borrow().iter() {
 				let mut cloned = library_path.clone();
 				cloned.push(path);
 				if cloned.exists() {
-					return Ok(SourcePath::Path(
+					return Ok(SourcePath::new(SourceFile::new(
 						cloned.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
-					));
+					)));
 				}
 			}
-			throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))
+			throw!(ImportFileNotFound(from.clone(), path.to_owned()))
+		}
+	}
+	fn resolve(&self, path: &Path) -> Result<SourcePath> {
+		let meta = match fs::metadata(path) {
+			Ok(v) => v,
+			Err(e) if e.kind() == ErrorKind::NotFound => {
+				throw!(AbsoluteImportFileNotFound(path.to_owned()))
+			}
+			Err(e) => throw!(Error::ImportIo(e.to_string())),
+		};
+		if meta.is_file() {
+			Ok(SourcePath::new(SourceFile::new(
+				path.canonicalize()
+					.map_err(|e| ImportIo(e.to_string()))?
+					.to_owned(),
+			)))
+		} else if meta.is_dir() {
+			Ok(SourcePath::new(SourceDirectory::new(
+				path.canonicalize()
+					.map_err(|e| ImportIo(e.to_string()))?
+					.to_owned(),
+			)))
+		} else {
+			unreachable!("this can't be a symlink")
 		}
 	}
 
 	fn load_file_contents(&self, id: &SourcePath) -> Result<Vec<u8>> {
-		let path = match id {
-			SourcePath::Path(path) => path,
-			_ => {
-				panic!("this resolver can only resolve to path")
-			}
+		let path = if let Some(f) = id.downcast_ref::<SourceFile>() {
+			f.path()
+		} else if id.downcast_ref::<SourceDirectory>().is_some() || id.is_default() {
+			throw!(Error::ImportIsADirectory(id.clone()))
+		} else {
+			unreachable!("other types are not supported in resolve");
 		};
 		let mut file = File::open(path).map_err(|_e| ResolvedFileNotFound(id.clone()))?;
 		let mut out = Vec::new();
@@ -97,7 +152,12 @@
 			.map_err(|e| ImportIo(e.to_string()))?;
 		Ok(out)
 	}
-	unsafe fn as_any(&self) -> &dyn Any {
-		panic!("this resolver can't be used as any")
+
+	fn as_any(&self) -> &dyn Any {
+		self
+	}
+
+	fn resolve_from_default(&self, path: &str) -> Result<SourcePath> {
+		self.resolve_from(&SourcePath::default(), path)
 	}
 }
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
4444
45use std::{45use std::{
46 any::Any,46 any::Any,
47 borrow::Cow,
48 cell::{Ref, RefCell, RefMut},47 cell::{Ref, RefCell, RefMut},
49 collections::HashMap,48 collections::HashMap,
50 fmt::{self, Debug},49 fmt::{self, Debug},
103pub trait ContextInitializer {102pub trait ContextInitializer {
104 fn initialize(&self, state: State, for_file: Source) -> Context;103 fn initialize(&self, state: State, for_file: Source) -> Context;
105104
106 /// # Safety
107 ///
108 /// For use only in bindings, should not be used elsewhere.
109 /// Implementations which are not intended to be used in bindings
110 /// should panic on call to this method.
111 unsafe fn as_any(&self) -> &dyn Any;105 fn as_any(&self) -> &dyn Any;
112}106}
113107
114/// Context initializer, which adds noth108/// Context initializer, which adds noth
117 fn initialize(&self, _state: State, _for_file: Source) -> Context {111 fn initialize(&self, _state: State, _for_file: Source) -> Context {
118 Context::default()112 Context::default()
119 }113 }
120 unsafe fn as_any(&self) -> &dyn Any {114 fn as_any(&self) -> &dyn Any {
121 panic!("`as_any(&self)` is not supported by dummy initializer")115 self
122 }116 }
123}117}
124118
344 }338 }
345 let code = file.string.as_ref().expect("just set");339 let code = file.string.as_ref().expect("just set");
346 let file_name =340 let file_name = Source::new(path.clone(), code.clone());
347 Source::new(path.clone(), code.clone()).expect("resolver should return correct name");
348 if file.parsed.is_none() {341 if file.parsed.is_none() {
349 file.parsed = Some(342 file.parsed = Some(
350 jrsonnet_parser::parse(343 jrsonnet_parser::parse(
389 }382 }
390 }383 }
384
385 /// Has same semantics as `import 'path'` called from `from` file
391 pub fn import(&self, from: &Path, path: &str) -> Result<Val> {386 pub fn import_from(&self, from: &SourcePath, path: &str) -> Result<Val> {
392 let resolved = self.resolve_file(from, path)?;387 let resolved = self.resolve_from(from, path)?;
393 self.import_resolved(resolved)388 self.import_resolved(resolved)
394 }389 }
390 pub fn import(&self, path: &impl AsRef<Path>) -> Result<Val> {
391 let resolved = self.resolve(path)?;
392 self.import_resolved(resolved)
393 }
395394
396 /// Creates context with all passed global variables395 /// Creates context with all passed global variables
397 pub fn create_default_context(&self, source: Source) -> Context {396 pub fn create_default_context(&self, source: Source) -> Context {
532 func.evaluate(531 func.evaluate(
533 self.clone(),532 self.clone(),
534 self.create_default_context(Source::new_virtual(533 self.create_default_context(Source::new_virtual(
535 Cow::Borrowed("<tla>"),534 "<tla>".into(),
536 IStr::empty(),535 IStr::empty(),
537 )),536 )),
538 CallLocation::native(),537 CallLocation::native(),
565/// Raw methods evaluate passed values but don't perform TLA execution564/// Raw methods evaluate passed values but don't perform TLA execution
566impl State {565impl State {
567 /// Parses and evaluates the given snippet566 /// Parses and evaluates the given snippet
568 pub fn evaluate_snippet(&self, name: String, code: impl Into<IStr>) -> Result<Val> {567 pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {
569 let code = code.into();568 let code = code.into();
570 let source = Source::new_virtual(Cow::Owned(name), code.clone());569 let source = Source::new_virtual(name.into(), code.clone());
571 let parsed = jrsonnet_parser::parse(570 let parsed = jrsonnet_parser::parse(
572 &code,571 &code,
573 &ParserSettings {572 &ParserSettings {
596 }595 }
597 pub fn add_tla_code(&self, name: IStr, code: &str) -> Result<()> {596 pub fn add_tla_code(&self, name: IStr, code: &str) -> Result<()> {
598 let source_name = format!("<top-level-arg:{}>", name);597 let source_name = format!("<top-level-arg:{}>", name);
599 let source = Source::new_virtual(Cow::Owned(source_name), code.into());598 let source = Source::new_virtual(source_name.into(), code.into());
600 let parsed = jrsonnet_parser::parse(599 let parsed = jrsonnet_parser::parse(
601 code,600 code,
602 &ParserSettings {601 &ParserSettings {
613 Ok(())612 Ok(())
614 }613 }
615614
615 // Only panics in case of [`ImportResolver`] contract violation
616 #[allow(clippy::missing_panics_doc)]
616 pub fn resolve_file(&self, from: &Path, path: &str) -> Result<SourcePath> {617 pub fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
617 self.settings()618 self.import_resolver().resolve_from(from, path.as_ref())
618 .import_resolver
619 .resolve_file_relative(from, path.as_ref())
620 }619 }
621620
621 // Only panics in case of [`ImportResolver`] contract violation
622 #[allow(clippy::missing_panics_doc)]
623 pub fn resolve(&self, path: &impl AsRef<Path>) -> Result<SourcePath> {
624 self.import_resolver().resolve(path.as_ref())
625 }
622 pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {626 pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {
623 Ref::map(self.settings(), |s| &*s.import_resolver)627 Ref::map(self.settings(), |s| &*s.import_resolver)
624 }628 }
modifiedcrates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -5,6 +5,7 @@
 use crate::{error::Error, LocError, State};
 
 /// The way paths should be displayed
+#[derive(Clone)]
 pub enum PathResolver {
 	/// Only filename
 	FileName,
@@ -15,6 +16,13 @@
 }
 
 impl PathResolver {
+	/// Will return Self::Relative(cwd), or Self::Absolute on cwd failure
+	pub fn new_cwd_fallback() -> Self {
+		match std::env::current_dir() {
+			Ok(v) => Self::Relative(v),
+			Err(_) => Self::Absolute,
+		}
+	}
 	pub fn resolve(&self, from: &Path) -> String {
 		match self {
 			Self::FileName => from
@@ -89,9 +97,9 @@
 			use std::fmt::Write;
 
 			writeln!(out)?;
-			let mut n = match path.path() {
+			let mut n = match path.source_path().path() {
 				Some(r) => self.resolver.resolve(r),
-				None => path.short_display().to_string(),
+				None => path.source_path().to_string(),
 			};
 			let mut offset = error.location.offset;
 			let is_eof = if offset >= path.code().len() {
@@ -122,9 +130,9 @@
 				use std::fmt::Write;
 				#[allow(clippy::option_if_let_else)]
 				if let Some(location) = location {
-					let mut resolved_path = match location.0.path() {
+					let mut resolved_path = match location.0.source_path().path() {
 						Some(r) => self.resolver.resolve(r),
-						None => location.0.short_display().to_string(),
+						None => location.0.source_path().to_string(),
 					};
 					// TODO: Process all trace elements first
 					let location = location.0.map_source_locations(&[location.1, location.2]);
@@ -177,9 +185,9 @@
 			let desc = &item.desc;
 			if let Some(source) = &item.location {
 				let start_end = source.0.map_source_locations(&[source.1, source.2]);
-				let resolved_path = match source.0.path() {
+				let resolved_path = match source.0.source_path().path() {
 					Some(r) => r.display().to_string(),
-					None => source.0.short_display().to_string(),
+					None => source.0.source_path().to_string(),
 				};
 
 				write!(
@@ -272,9 +280,9 @@
 			.take(end.line_end_offset - end.line_start_offset)
 			.collect();
 
-		let origin = match origin.path() {
+		let origin = match origin.source_path().path() {
 			Some(r) => self.resolver.resolve(r),
-			None => origin.short_display().to_string(),
+			None => origin.source_path().to_string(),
 		};
 		let snippet = Snippet {
 			opt: FormatOptions {