difftreelog
feat(evaluator) custom source paths
in: master
5 files changed
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth137 StandaloneSuper,137 StandaloneSuper,138138139 #[error("can't resolve {1} from {0}")]139 #[error("can't resolve {1} from {0}")]140 ImportFileNotFound(PathBuf, String),140 ImportFileNotFound(SourcePath, String),141 #[error("can't resolve absolute {0}")]142 AbsoluteImportFileNotFound(PathBuf),141 #[error("resolved file not found: {:?}", .0)]143 #[error("resolved file not found: {:?}", .0)]142 ResolvedFileNotFound(SourcePath),144 ResolvedFileNotFound(SourcePath),145 #[error("can't import {0}: is a directory")]146 ImportIsADirectory(SourcePath),143 #[error("imported file is not valid utf-8: {0:?}")]147 #[error("imported file is not valid utf-8: {0:?}")]144 ImportBadFileUtf8(SourcePath),148 ImportBadFileUtf8(SourcePath),145 #[error("import io error: {0}")]149 #[error("import io error: {0}")]146 ImportIo(String),150 ImportIo(String),147 #[error("tried to import {1} from {0}, but imports is not supported")]151 #[error("tried to import {1} from {0}, but imports are not supported")]148 ImportNotSupported(PathBuf, PathBuf),152 ImportNotSupported(SourcePath, String),153 #[error("tried to import {0}, but absolute imports are not supported")]154 AbsoluteImportNotSupported(PathBuf),149 #[error("can't import from virtual file")]155 #[error("can't import from virtual file")]150 CantImportFromVirtualFile,156 CantImportFromVirtualFile,151 #[error(157 #[error(crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth630 }630 }631 i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {631 i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {632 let tmp = loc.clone().0;632 let tmp = loc.clone().0;633 let import_location = tmp634 .path()635 .map(|p| {636 let mut p = p.to_owned();637 p.pop();638 p639 })640 .unwrap_or_default();641 let resolved_path = s.resolve_file(&import_location, path as &str)?;633 let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;642 match i {634 match i {643 Import(_) => s.push(635 Import(_) => s.push(644 CallLocation::new(loc),636 CallLocation::new(loc),crates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth1use 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};798use fs::File;10use fs::File;9use jrsonnet_parser::SourcePath;11use jrsonnet_parser::{SourceDirectory, SourceFile, SourcePath};101211use crate::{13use crate::{12 error::{Error::*, Result},14 error::{15 Error::{self, *},16 Result,17 },13 throw,18 throw,14};19};152016/// 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 correspond19 /// 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 value28 /// may result in panic21 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 things36 fn resolve(&self, path: &Path) -> Result<SourcePath> {37 throw!(AbsoluteImportNotSupported(path.to_owned()))38 }223923 /// Load resolved file40 /// Load resolved file24 /// 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>>;274428 /// # Safety45 /// For downcasts29 ///30 /// For use only in bindings, should not be used elsewhere.31 /// Implementations which are not intended to be used in bindings32 /// should panic on call to this method.33 unsafe fn as_any(&self) -> &dyn Any;46 fn as_any(&self) -> &dyn Any;34}47}354836/// Dummy resolver, can't resolve/load any file49/// Dummy resolver, can't resolve/load any file37pub 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 }4243 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 }465547 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 self49 }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 bindings81 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 o91 } 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 }8614087 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 }155100 unsafe fn as_any(&self) -> &dyn Any {156 fn as_any(&self) -> &dyn Any {157 self158 }159160 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}104164crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth444445use 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;105104106 /// # Safety107 ///108 /// For use only in bindings, should not be used elsewhere.109 /// Implementations which are not intended to be used in bindings110 /// should panic on call to this method.111 unsafe fn as_any(&self) -> &dyn Any;105 fn as_any(&self) -> &dyn Any;112}106}113107114/// Context initializer, which adds noth108/// Context initializer, which adds noth117 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 self122 }116 }123}117}124118344 }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 }384385 /// Has same semantics as `import 'path'` called from `from` file391 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 }395394396 /// Creates context with all passed global variables395 /// Creates context with all passed global variables397 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 execution566impl State {565impl State {567 /// Parses and evaluates the given snippet566 /// Parses and evaluates the given snippet568 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 }615614615 // Only panics in case of [`ImportResolver`] contract violation616 #[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_resolver619 .resolve_file_relative(from, path.as_ref())620 }619 }621620621 // Only panics in case of [`ImportResolver`] contract violation622 #[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 }crates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth5use crate::{error::Error, LocError, State};5use crate::{error::Error, LocError, State};667/// The way paths should be displayed7/// The way paths should be displayed8#[derive(Clone)]8pub enum PathResolver {9pub enum PathResolver {9 /// Only filename10 /// Only filename10 FileName,11 FileName,15}16}161717impl PathResolver {18impl PathResolver {19 /// Will return Self::Relative(cwd), or Self::Absolute on cwd failure20 pub fn new_cwd_fallback() -> Self {21 match std::env::current_dir() {22 Ok(v) => Self::Relative(v),23 Err(_) => Self::Absolute,24 }25 }18 pub fn resolve(&self, from: &Path) -> String {26 pub fn resolve(&self, from: &Path) -> String {19 match self {27 match self {20 Self::FileName => from28 Self::FileName => from89 use std::fmt::Write;97 use std::fmt::Write;909891 writeln!(out)?;99 writeln!(out)?;92 let mut n = match path.path() {100 let mut n = match path.source_path().path() {93 Some(r) => self.resolver.resolve(r),101 Some(r) => self.resolver.resolve(r),94 None => path.short_display().to_string(),102 None => path.source_path().to_string(),95 };103 };96 let mut offset = error.location.offset;104 let mut offset = error.location.offset;97 let is_eof = if offset >= path.code().len() {105 let is_eof = if offset >= path.code().len() {122 use std::fmt::Write;130 use std::fmt::Write;123 #[allow(clippy::option_if_let_else)]131 #[allow(clippy::option_if_let_else)]124 if let Some(location) = location {132 if let Some(location) = location {125 let mut resolved_path = match location.0.path() {133 let mut resolved_path = match location.0.source_path().path() {126 Some(r) => self.resolver.resolve(r),134 Some(r) => self.resolver.resolve(r),127 None => location.0.short_display().to_string(),135 None => location.0.source_path().to_string(),128 };136 };129 // TODO: Process all trace elements first137 // TODO: Process all trace elements first130 let location = location.0.map_source_locations(&[location.1, location.2]);138 let location = location.0.map_source_locations(&[location.1, location.2]);177 let desc = &item.desc;185 let desc = &item.desc;178 if let Some(source) = &item.location {186 if let Some(source) = &item.location {179 let start_end = source.0.map_source_locations(&[source.1, source.2]);187 let start_end = source.0.map_source_locations(&[source.1, source.2]);180 let resolved_path = match source.0.path() {188 let resolved_path = match source.0.source_path().path() {181 Some(r) => r.display().to_string(),189 Some(r) => r.display().to_string(),182 None => source.0.short_display().to_string(),190 None => source.0.source_path().to_string(),183 };191 };184192185 write!(193 write!(272 .take(end.line_end_offset - end.line_start_offset)280 .take(end.line_end_offset - end.line_start_offset)273 .collect();281 .collect();274282275 let origin = match origin.path() {283 let origin = match origin.source_path().path() {276 Some(r) => self.resolver.resolve(r),284 Some(r) => self.resolver.resolve(r),277 None => origin.short_display().to_string(),285 None => origin.source_path().to_string(),278 };286 };279 let snippet = Snippet {287 let snippet = Snippet {280 opt: FormatOptions {288 opt: FormatOptions {