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
1use std::{1use std::{
2 any::Any,2 any::Any,
3 cell::RefCell,
4 env::current_dir,
3 fs,5 fs,
4 io::Read,6 io::{ErrorKind, Read},
5 path::{Path, PathBuf},7 path::{Path, PathBuf},
6};8};
79
8use fs::File;10use fs::File;
9use jrsonnet_parser::SourcePath;11use jrsonnet_parser::{SourceDirectory, SourceFile, SourcePath};
1012
11use crate::{13use crate::{
12 error::{Error::*, Result},14 error::{
15 Error::{self, *},
16 Result,
17 },
13 throw,18 throw,
14};19};
1520
16/// Implements file resolution logic for `import` and `importStr`21/// Implements file resolution logic for `import` and `importStr`
17pub trait ImportResolver {22pub trait ImportResolver {
18 /// Resolves real file path, e.g. `(/home/user/manifests, b.libjsonnet)` can correspond23 /// Resolves file path, e.g. `(/home/user/manifests, b.libjsonnet)` can correspond
19 /// both to `/home/user/manifests/b.libjsonnet` and to `/home/user/${vendor}/b.libjsonnet`24 /// both to `/home/user/manifests/b.libjsonnet` and to `/home/user/${vendor}/b.libjsonnet`
20 /// where `${vendor}` is a library path.25 /// where `${vendor}` is a library path.
26 ///
27 /// `from` should only be returned from [`ImportResolver::resolve`], or from other defined file, any other value
28 /// may result in panic
21 fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath>;29 fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
30 throw!(ImportNotSupported(from.clone(), path.into()))
31 }
32 fn resolve_from_default(&self, path: &str) -> Result<SourcePath> {
33 self.resolve_from(&SourcePath::default(), path)
34 }
35 /// Resolves absolute path, doesn't supports jpath and other fancy things
36 fn resolve(&self, path: &Path) -> Result<SourcePath> {
37 throw!(AbsoluteImportNotSupported(path.to_owned()))
38 }
2239
23 /// Load resolved file40 /// Load resolved file
24 /// This should only be called with value returned from `resolve_file`, this cannot be resolved using associated type,41 /// This should only be called with value returned from [`ImportResolver::resolve_file`]/[`ImportResolver::resolve`],
25 /// as evaluator uses object instead of generic for [`ImportResolver`]42 /// this cannot be resolved using associated type, as evaluator uses object instead of generic for [`ImportResolver`]
26 fn load_file_contents(&self, resolved: &SourcePath) -> Result<Vec<u8>>;43 fn load_file_contents(&self, resolved: &SourcePath) -> Result<Vec<u8>>;
2744
28 /// # Safety45 /// For downcasts
29 ///
30 /// For use only in bindings, should not be used elsewhere.
31 /// Implementations which are not intended to be used in bindings
32 /// should panic on call to this method.
33 unsafe fn as_any(&self) -> &dyn Any;46 fn as_any(&self) -> &dyn Any;
34}47}
3548
36/// Dummy resolver, can't resolve/load any file49/// Dummy resolver, can't resolve/load any file
37pub struct DummyImportResolver;50pub struct DummyImportResolver;
38impl ImportResolver for DummyImportResolver {51impl ImportResolver for DummyImportResolver {
39 fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {
40 throw!(ImportNotSupported(from.into(), path.into()))
41 }
42
43 fn load_file_contents(&self, _resolved: &SourcePath) -> Result<Vec<u8>> {52 fn load_file_contents(&self, _resolved: &SourcePath) -> Result<Vec<u8>> {
44 panic!("dummy resolver can't load any file")53 panic!("dummy resolver can't load any file")
45 }54 }
4655
47 unsafe fn as_any(&self) -> &dyn Any {56 fn as_any(&self) -> &dyn Any {
48 panic!("`as_any(&self)` is not supported by dummy resolver")57 self
49 }58 }
50}59}
51#[allow(clippy::use_self)]60#[allow(clippy::use_self)]
60pub struct FileImportResolver {69pub struct FileImportResolver {
61 /// Library directories to search for file.70 /// Library directories to search for file.
62 /// Referred to as `jpath` in original jsonnet implementation.71 /// Referred to as `jpath` in original jsonnet implementation.
63 pub library_paths: Vec<PathBuf>,72 library_paths: RefCell<Vec<PathBuf>>,
64}73}
74impl FileImportResolver {
75 pub fn new(jpath: Vec<PathBuf>) -> Self {
76 Self {
77 library_paths: RefCell::new(jpath),
78 }
79 }
80 /// Dynamically add new jpath, used by bindings
81 pub fn add_jpath(&self, path: PathBuf) {
82 self.library_paths.borrow_mut().push(path);
83 }
84}
65impl ImportResolver for FileImportResolver {85impl ImportResolver for FileImportResolver {
66 fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {86 fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
67 let mut direct = from.to_path_buf();87 let mut direct = if let Some(f) = from.downcast_ref::<SourceFile>() {
88 let mut o = f.path().to_owned();
89 o.pop();
90 o
91 } else if let Some(d) = from.downcast_ref::<SourceDirectory>() {
92 d.path().to_owned()
93 } else if from.is_default() {
94 current_dir().map_err(|e| Error::ImportIo(e.to_string()))?
95 } else {
96 unreachable!("resolver can't return this path")
97 };
68 direct.push(path);98 direct.push(path);
69 if direct.exists() {99 if direct.is_file() {
70 Ok(SourcePath::Path(100 Ok(SourcePath::new(SourceFile::new(
71 direct.canonicalize().map_err(|e| ImportIo(e.to_string()))?,101 direct.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
72 ))102 )))
73 } else {103 } else {
74 for library_path in &self.library_paths {104 for library_path in self.library_paths.borrow().iter() {
75 let mut cloned = library_path.clone();105 let mut cloned = library_path.clone();
76 cloned.push(path);106 cloned.push(path);
77 if cloned.exists() {107 if cloned.exists() {
78 return Ok(SourcePath::Path(108 return Ok(SourcePath::new(SourceFile::new(
79 cloned.canonicalize().map_err(|e| ImportIo(e.to_string()))?,109 cloned.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
80 ));110 )));
81 }111 }
82 }112 }
83 throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))113 throw!(ImportFileNotFound(from.clone(), path.to_owned()))
84 }114 }
85 }115 }
116 fn resolve(&self, path: &Path) -> Result<SourcePath> {
117 let meta = match fs::metadata(path) {
118 Ok(v) => v,
119 Err(e) if e.kind() == ErrorKind::NotFound => {
120 throw!(AbsoluteImportFileNotFound(path.to_owned()))
121 }
122 Err(e) => throw!(Error::ImportIo(e.to_string())),
123 };
124 if meta.is_file() {
125 Ok(SourcePath::new(SourceFile::new(
126 path.canonicalize()
127 .map_err(|e| ImportIo(e.to_string()))?
128 .to_owned(),
129 )))
130 } else if meta.is_dir() {
131 Ok(SourcePath::new(SourceDirectory::new(
132 path.canonicalize()
133 .map_err(|e| ImportIo(e.to_string()))?
134 .to_owned(),
135 )))
136 } else {
137 unreachable!("this can't be a symlink")
138 }
139 }
86140
87 fn load_file_contents(&self, id: &SourcePath) -> Result<Vec<u8>> {141 fn load_file_contents(&self, id: &SourcePath) -> Result<Vec<u8>> {
88 let path = match id {142 let path = if let Some(f) = id.downcast_ref::<SourceFile>() {
89 SourcePath::Path(path) => path,143 f.path()
144 } else if id.downcast_ref::<SourceDirectory>().is_some() || id.is_default() {
145 throw!(Error::ImportIsADirectory(id.clone()))
90 _ => {146 } else {
91 panic!("this resolver can only resolve to path")147 unreachable!("other types are not supported in resolve");
92 }148 };
93 };
94 let mut file = File::open(path).map_err(|_e| ResolvedFileNotFound(id.clone()))?;149 let mut file = File::open(path).map_err(|_e| ResolvedFileNotFound(id.clone()))?;
95 let mut out = Vec::new();150 let mut out = Vec::new();
96 file.read_to_end(&mut out)151 file.read_to_end(&mut out)
97 .map_err(|e| ImportIo(e.to_string()))?;152 .map_err(|e| ImportIo(e.to_string()))?;
98 Ok(out)153 Ok(out)
99 }154 }
155
100 unsafe fn as_any(&self) -> &dyn Any {156 fn as_any(&self) -> &dyn Any {
157 self
158 }
159
160 fn resolve_from_default(&self, path: &str) -> Result<SourcePath> {
101 panic!("this resolver can't be used as any")161 self.resolve_from(&SourcePath::default(), path)
102 }162 }
103}163}
104164
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -44,7 +44,6 @@
 
 use std::{
 	any::Any,
-	borrow::Cow,
 	cell::{Ref, RefCell, RefMut},
 	collections::HashMap,
 	fmt::{self, Debug},
@@ -103,12 +102,7 @@
 pub trait ContextInitializer {
 	fn initialize(&self, state: State, for_file: Source) -> Context;
 
-	/// # 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;
+	fn as_any(&self) -> &dyn Any;
 }
 
 /// Context initializer, which adds noth
@@ -117,8 +111,8 @@
 	fn initialize(&self, _state: State, _for_file: Source) -> Context {
 		Context::default()
 	}
-	unsafe fn as_any(&self) -> &dyn Any {
-		panic!("`as_any(&self)` is not supported by dummy initializer")
+	fn as_any(&self) -> &dyn Any {
+		self
 	}
 }
 
@@ -343,8 +337,7 @@
 			);
 		}
 		let code = file.string.as_ref().expect("just set");
-		let file_name =
-			Source::new(path.clone(), code.clone()).expect("resolver should return correct name");
+		let file_name = Source::new(path.clone(), code.clone());
 		if file.parsed.is_none() {
 			file.parsed = Some(
 				jrsonnet_parser::parse(
@@ -388,8 +381,14 @@
 			Err(e) => Err(e),
 		}
 	}
-	pub fn import(&self, from: &Path, path: &str) -> Result<Val> {
-		let resolved = self.resolve_file(from, path)?;
+
+	/// 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)?;
+		self.import_resolved(resolved)
+	}
+	pub fn import(&self, path: &impl AsRef<Path>) -> Result<Val> {
+		let resolved = self.resolve(path)?;
 		self.import_resolved(resolved)
 	}
 
@@ -532,7 +531,7 @@
 					func.evaluate(
 						self.clone(),
 						self.create_default_context(Source::new_virtual(
-							Cow::Borrowed("<tla>"),
+							"<tla>".into(),
 							IStr::empty(),
 						)),
 						CallLocation::native(),
@@ -565,9 +564,9 @@
 /// Raw methods evaluate passed values but don't perform TLA execution
 impl State {
 	/// Parses and evaluates the given snippet
-	pub fn evaluate_snippet(&self, name: String, code: impl Into<IStr>) -> Result<Val> {
+	pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {
 		let code = code.into();
-		let source = Source::new_virtual(Cow::Owned(name), code.clone());
+		let source = Source::new_virtual(name.into(), code.clone());
 		let parsed = jrsonnet_parser::parse(
 			&code,
 			&ParserSettings {
@@ -596,7 +595,7 @@
 	}
 	pub fn add_tla_code(&self, name: IStr, code: &str) -> Result<()> {
 		let source_name = format!("<top-level-arg:{}>", name);
-		let source = Source::new_virtual(Cow::Owned(source_name), code.into());
+		let source = Source::new_virtual(source_name.into(), code.into());
 		let parsed = jrsonnet_parser::parse(
 			code,
 			&ParserSettings {
@@ -613,12 +612,17 @@
 		Ok(())
 	}
 
-	pub fn resolve_file(&self, from: &Path, path: &str) -> Result<SourcePath> {
-		self.settings()
-			.import_resolver
-			.resolve_file_relative(from, path.as_ref())
+	// 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())
 	}
 
+	// 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 import_resolver(&self) -> Ref<dyn ImportResolver> {
 		Ref::map(self.settings(), |s| &*s.import_resolver)
 	}
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 {