difftreelog
feat(evaluator) custom source paths
in: master
5 files changed
crates/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(
crates/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),
crates/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)
}
}
crates/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)
}
crates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth1use std::path::{Path, PathBuf};23use jrsonnet_parser::{CodeLocation, Source};45use crate::{error::Error, LocError, State};67/// The way paths should be displayed8pub enum PathResolver {9 /// Only filename10 FileName,11 /// Absolute path12 Absolute,13 /// Path relative to base directory14 Relative(PathBuf),15}1617impl PathResolver {18 pub fn resolve(&self, from: &Path) -> String {19 match self {20 Self::FileName => from21 .file_name()22 .expect("file name exists")23 .to_string_lossy()24 .into_owned(),25 Self::Absolute => from.to_string_lossy().into_owned(),26 Self::Relative(base) => {27 if from.is_relative() {28 return from.to_string_lossy().into_owned();29 }30 pathdiff::diff_paths(from, base)31 .expect("base is absolute")32 .to_string_lossy()33 .into_owned()34 }35 }36 }37}3839/// Implements pretty-printing of traces40#[allow(clippy::module_name_repetitions)]41pub trait TraceFormat {42 fn write_trace(43 &self,44 out: &mut dyn std::fmt::Write,45 s: &State,46 error: &LocError,47 ) -> Result<(), std::fmt::Error>;48}4950fn print_code_location(51 out: &mut impl std::fmt::Write,52 start: &CodeLocation,53 end: &CodeLocation,54) -> Result<(), std::fmt::Error> {55 if start.line == end.line {56 if start.column == end.column {57 write!(out, "{}:{}", start.line, end.column.saturating_sub(1))?;58 } else {59 write!(out, "{}:{}-{}", start.line, start.column - 1, end.column)?;60 }61 } else {62 write!(63 out,64 "{}:{}-{}:{}",65 start.line,66 end.column.saturating_sub(1),67 start.line,68 end.column69 )?;70 }71 Ok(())72}7374/// vanilla-like jsonnet formatting75pub struct CompactFormat {76 pub resolver: PathResolver,77 pub padding: usize,78}7980impl TraceFormat for CompactFormat {81 fn write_trace(82 &self,83 out: &mut dyn std::fmt::Write,84 _s: &State,85 error: &LocError,86 ) -> Result<(), std::fmt::Error> {87 write!(out, "{}", error.error())?;88 if let Error::ImportSyntaxError { path, error } = error.error() {89 use std::fmt::Write;9091 writeln!(out)?;92 let mut n = match path.path() {93 Some(r) => self.resolver.resolve(r),94 None => path.short_display().to_string(),95 };96 let mut offset = error.location.offset;97 let is_eof = if offset >= path.code().len() {98 offset = path.code().len().saturating_sub(1);99 true100 } else {101 false102 };103 let mut location = path104 .map_source_locations(&[offset as u32])105 .into_iter()106 .next()107 .unwrap();108 if is_eof {109 location.column += 1;110 }111112 write!(n, ":").unwrap();113 print_code_location(&mut n, &location, &location).unwrap();114 write!(out, "{:<p$}{}", "", n, p = self.padding,)?;115 }116 let file_names = error117 .trace()118 .0119 .iter()120 .map(|el| &el.location)121 .map(|location| {122 use std::fmt::Write;123 #[allow(clippy::option_if_let_else)]124 if let Some(location) = location {125 let mut resolved_path = match location.0.path() {126 Some(r) => self.resolver.resolve(r),127 None => location.0.short_display().to_string(),128 };129 // TODO: Process all trace elements first130 let location = location.0.map_source_locations(&[location.1, location.2]);131 write!(resolved_path, ":").unwrap();132 print_code_location(&mut resolved_path, &location[0], &location[1]).unwrap();133 write!(resolved_path, ":").unwrap();134 Some(resolved_path)135 } else {136 None137 }138 })139 .collect::<Vec<_>>();140 let align = file_names141 .iter()142 .flatten()143 .map(String::len)144 .max()145 .unwrap_or(0);146 for (el, file) in error.trace().0.iter().zip(file_names) {147 writeln!(out)?;148 if let Some(file) = file {149 write!(150 out,151 "{:<p$}{:<w$} {}",152 "",153 file,154 el.desc,155 p = self.padding,156 w = align157 )?;158 } else {159 write!(out, "{:<p$}{}", "", el.desc, p = self.padding,)?;160 }161 }162 Ok(())163 }164}165166pub struct JsFormat;167impl TraceFormat for JsFormat {168 fn write_trace(169 &self,170 out: &mut dyn std::fmt::Write,171 _s: &State,172 error: &LocError,173 ) -> Result<(), std::fmt::Error> {174 write!(out, "{}", error.error())?;175 for item in &error.trace().0 {176 writeln!(out)?;177 let desc = &item.desc;178 if let Some(source) = &item.location {179 let start_end = source.0.map_source_locations(&[source.1, source.2]);180 let resolved_path = match source.0.path() {181 Some(r) => r.display().to_string(),182 None => source.0.short_display().to_string(),183 };184185 write!(186 out,187 " at {} ({}:{}:{})",188 desc, resolved_path, start_end[0].line, start_end[0].column,189 )?;190 } else {191 write!(out, " during {}", desc)?;192 }193 }194 Ok(())195 }196}197198/// rustc-like trace displaying199#[cfg(feature = "explaining-traces")]200pub struct ExplainingFormat {201 pub resolver: PathResolver,202}203#[cfg(feature = "explaining-traces")]204impl TraceFormat for ExplainingFormat {205 fn write_trace(206 &self,207 out: &mut dyn std::fmt::Write,208 _s: &State,209 error: &LocError,210 ) -> Result<(), std::fmt::Error> {211 write!(out, "{}", error.error())?;212 if let Error::ImportSyntaxError { path, error } = error.error() {213 writeln!(out)?;214 let offset = error.location.offset;215 let location = path216 .map_source_locations(&[offset as u32])217 .into_iter()218 .next()219 .unwrap();220 let mut end_location = location.clone();221 end_location.offset += 1;222223 self.print_snippet(224 out,225 path.code(),226 path,227 &location,228 &end_location,229 "syntax error",230 )?;231 }232 let trace = &error.trace();233 for item in &trace.0 {234 writeln!(out)?;235 let desc = &item.desc;236 if let Some(source) = &item.location {237 let start_end = source.0.map_source_locations(&[source.1, source.2]);238 self.print_snippet(239 out,240 source.0.code(),241 &source.0,242 &start_end[0],243 &start_end[1],244 desc,245 )?;246 } else {247 write!(out, "{}", desc)?;248 }249 }250 Ok(())251 }252}253254impl ExplainingFormat {255 fn print_snippet(256 &self,257 out: &mut dyn std::fmt::Write,258 source: &str,259 origin: &Source,260 start: &CodeLocation,261 end: &CodeLocation,262 desc: &str,263 ) -> Result<(), std::fmt::Error> {264 use annotate_snippets::{265 display_list::{DisplayList, FormatOptions},266 snippet::{AnnotationType, Slice, Snippet, SourceAnnotation},267 };268269 let source_fragment: String = source270 .chars()271 .skip(start.line_start_offset)272 .take(end.line_end_offset - end.line_start_offset)273 .collect();274275 let origin = match origin.path() {276 Some(r) => self.resolver.resolve(r),277 None => origin.short_display().to_string(),278 };279 let snippet = Snippet {280 opt: FormatOptions {281 color: true,282 ..FormatOptions::default()283 },284 title: None,285 footer: vec![],286 slices: vec![Slice {287 source: &source_fragment,288 line_start: start.line,289 origin: Some(&origin),290 fold: false,291 annotations: vec![SourceAnnotation {292 label: desc,293 annotation_type: AnnotationType::Error,294 range: (295 start.offset - start.line_start_offset,296 (end.offset - start.line_start_offset).min(source_fragment.len()),297 ),298 }],299 }],300 };301302 let dl = DisplayList::from(snippet);303 write!(out, "{}", dl)?;304305 Ok(())306 }307}