difftreelog
feat(evaluator) custom source paths
in: master
5 files changed
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth1use std::{fmt::Debug, path::PathBuf};23use jrsonnet_gcmodule::Trace;4use jrsonnet_interner::IStr;5use jrsonnet_parser::{BinaryOpType, ExprLocation, Source, SourcePath, UnaryOpType};6use jrsonnet_types::ValType;7use thiserror::Error;89use crate::{stdlib::format::FormatError, typed::TypeLocError};1011fn format_found(list: &[IStr], what: &str) -> String {12 if list.is_empty() {13 return String::new();14 }15 let mut out = String::new();16 out.push_str("\nThere is ");17 out.push_str(what);18 if list.len() > 1 {19 out.push('s');20 }21 out.push_str(" with similar name");22 if list.len() > 1 {23 out.push('s');24 }25 out.push_str(" present: ");26 for (i, v) in list.iter().enumerate() {27 if i != 0 {28 out.push_str(", ");29 }30 out.push_str(v as &str);31 }32 out33}3435fn format_signature(sig: &FunctionSignature) -> String {36 let mut out = String::new();37 out.push_str("\nFunction has the following signature: ");38 out.push('(');39 if sig.is_empty() {40 out.push_str("/*no arguments*/");41 } else {42 for (i, (name, has_default)) in sig.iter().enumerate() {43 if i != 0 {44 out.push_str(", ");45 }46 if let Some(name) = name {47 out.push_str(name);48 } else {49 out.push_str("<unnamed>");50 }51 if *has_default {52 out.push_str(" = <default>");53 }54 }55 }56 out.push(')');57 out58}5960const fn format_empty_str(str: &str) -> &str {61 if str.is_empty() {62 "\"\" (empty string)"63 } else {64 str65 }66}6768type FunctionSignature = Vec<(Option<IStr>, bool)>;6970#[derive(Error, Debug, Clone, Trace)]71pub enum Error {72 #[error("intrinsic not found: {0}")]73 IntrinsicNotFound(IStr),7475 #[error("operator {0} does not operate on type {1}")]76 UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),77 #[error("binary operation {1} {0} {2} is not implemented")]78 BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),7980 #[error("no top level object in this context")]81 NoTopLevelObjectFound,82 #[error("self is only usable inside objects")]83 CantUseSelfOutsideOfObject,84 #[error("no super found")]85 NoSuperFound,8687 #[error("for loop can only iterate over arrays")]88 InComprehensionCanOnlyIterateOverArray,8990 #[error("array out of bounds: {0} is not within [0,{1})")]91 ArrayBoundsError(usize, usize),92 #[error("string out of bounds: {0} is not within [0,{1})")]93 StringBoundsError(usize, usize),9495 #[error("assert failed: {}", format_empty_str(.0))]96 AssertionFailed(IStr),9798 #[error("variable is not defined: {0}{}", format_found(.1, "variable"))]99 VariableIsNotDefined(IStr, Vec<IStr>),100 #[error("duplicate local var: {0}")]101 DuplicateLocalVar(IStr),102103 #[error("type mismatch: expected {}, got {2} {0}", .1.iter().map(|e| format!("{}", e)).collect::<Vec<_>>().join(", "))]104 TypeMismatch(&'static str, Vec<ValType>, ValType),105 #[error("no such field: {}{}", format_empty_str(.0), format_found(.1, "field"))]106 NoSuchField(IStr, Vec<IStr>),107108 #[error("only functions can be called, got {0}")]109 OnlyFunctionsCanBeCalledGot(ValType),110 #[error("parameter {0} is not defined")]111 UnknownFunctionParameter(String),112 #[error("argument {0} is already bound")]113 BindingParameterASecondTime(IStr),114 #[error("too many args, function has {0}{}", format_signature(.1))]115 TooManyArgsFunctionHas(usize, FunctionSignature),116 #[error("function argument is not passed: {0}{}", format_signature(.1))]117 FunctionParameterNotBoundInCall(IStr, FunctionSignature),118119 #[error("external variable is not defined: {0}")]120 UndefinedExternalVariable(IStr),121122 #[error("field name should be string, got {0}")]123 FieldMustBeStringGot(ValType),124 #[error("duplicate field name: {}", format_empty_str(.0))]125 DuplicateFieldName(IStr),126127 #[error("attempted to index array with string {}", format_empty_str(.0))]128 AttemptedIndexAnArrayWithString(IStr),129 #[error("{0} index type should be {1}, got {2}")]130 ValueIndexMustBeTypeGot(ValType, ValType, ValType),131 #[error("cant index into {0}")]132 CantIndexInto(ValType),133 #[error("{0} is not indexable")]134 ValueIsNotIndexable(ValType),135136 #[error("super can't be used standalone")]137 StandaloneSuper,138139 #[error("can't resolve {1} from {0}")]140 ImportFileNotFound(PathBuf, String),141 #[error("resolved file not found: {:?}", .0)]142 ResolvedFileNotFound(SourcePath),143 #[error("imported file is not valid utf-8: {0:?}")]144 ImportBadFileUtf8(SourcePath),145 #[error("import io error: {0}")]146 ImportIo(String),147 #[error("tried to import {1} from {0}, but imports is not supported")]148 ImportNotSupported(PathBuf, PathBuf),149 #[error("can't import from virtual file")]150 CantImportFromVirtualFile,151 #[error(152 "syntax error: expected {}, got {:?}",153 .error.expected,154 .path.code().chars().nth(error.location.offset)155 .map_or_else(|| "EOF".into(), |c| c.to_string())156 )]157 ImportSyntaxError {158 path: Source,159 #[trace(skip)]160 error: Box<jrsonnet_parser::ParseError>,161 },162163 #[error("runtime error: {}", format_empty_str(.0))]164 RuntimeError(IStr),165 #[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]166 StackOverflow,167 #[error("infinite recursion detected")]168 InfiniteRecursionDetected,169 #[error("tried to index by fractional value")]170 FractionalIndex,171 #[error("attempted to divide by zero")]172 DivisionByZero,173174 #[error("string manifest output is not an string")]175 StringManifestOutputIsNotAString,176 #[error("stream manifest output is not an array")]177 StreamManifestOutputIsNotAArray,178 #[error("multi manifest output is not an object")]179 MultiManifestOutputIsNotAObject,180181 #[error("cant recurse stream manifest")]182 StreamManifestOutputCannotBeRecursed,183 #[error("stream manifest output cannot consist of raw strings")]184 StreamManifestCannotNestString,185186 #[error("{}", format_empty_str(.0))]187 ImportCallbackError(String),188 #[error("invalid unicode codepoint: {0}")]189 InvalidUnicodeCodepointGot(u32),190191 #[error("format error: {0}")]192 Format(#[from] FormatError),193 #[error("type error: {0}")]194 TypeError(TypeLocError),195196 #[cfg(feature = "anyhow-error")]197 #[error(transparent)]198 Other(Rc<anyhow::Error>),199}200201#[cfg(feature = "anyhow-error")]202impl From<anyhow::Error> for LocError {203 fn from(e: anyhow::Error) -> Self {204 Self::new(Error::Other(Rc::new(e)))205 }206}207208impl From<Error> for LocError {209 fn from(e: Error) -> Self {210 Self::new(e)211 }212}213214#[derive(Clone, Debug, Trace)]215pub struct StackTraceElement {216 pub location: Option<ExprLocation>,217 pub desc: String,218}219#[derive(Debug, Clone, Trace)]220pub struct StackTrace(pub Vec<StackTraceElement>);221222#[derive(Clone, Trace)]223pub struct LocError(Box<(Error, StackTrace)>);224impl LocError {225 pub fn new(e: Error) -> Self {226 Self(Box::new((e, StackTrace(vec![]))))227 }228229 pub const fn error(&self) -> &Error {230 &(self.0).0231 }232 pub fn error_mut(&mut self) -> &mut Error {233 &mut (self.0).0234 }235 pub const fn trace(&self) -> &StackTrace {236 &(self.0).1237 }238 pub fn trace_mut(&mut self) -> &mut StackTrace {239 &mut (self.0).1240 }241}242impl Debug for LocError {243 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {244 writeln!(f, "{}", self.0 .0)?;245 for el in &self.0 .1 .0 {246 writeln!(f, "\t{:?}", el)?;247 }248 Ok(())249 }250}251252pub type Result<V, E = LocError> = std::result::Result<V, E>;253254#[macro_export]255macro_rules! throw {256 ($e: expr) => {257 return Err($e.into())258 };259}260261#[macro_export]262macro_rules! throw_runtime {263 ($($tt:tt)*) => {264 return Err($crate::error::Error::RuntimeError(format!($($tt)*).into()).into())265 };266}1use std::{fmt::Debug, path::PathBuf};23use jrsonnet_gcmodule::Trace;4use jrsonnet_interner::IStr;5use jrsonnet_parser::{BinaryOpType, ExprLocation, Source, SourcePath, UnaryOpType};6use jrsonnet_types::ValType;7use thiserror::Error;89use crate::{stdlib::format::FormatError, typed::TypeLocError};1011fn format_found(list: &[IStr], what: &str) -> String {12 if list.is_empty() {13 return String::new();14 }15 let mut out = String::new();16 out.push_str("\nThere is ");17 out.push_str(what);18 if list.len() > 1 {19 out.push('s');20 }21 out.push_str(" with similar name");22 if list.len() > 1 {23 out.push('s');24 }25 out.push_str(" present: ");26 for (i, v) in list.iter().enumerate() {27 if i != 0 {28 out.push_str(", ");29 }30 out.push_str(v as &str);31 }32 out33}3435fn format_signature(sig: &FunctionSignature) -> String {36 let mut out = String::new();37 out.push_str("\nFunction has the following signature: ");38 out.push('(');39 if sig.is_empty() {40 out.push_str("/*no arguments*/");41 } else {42 for (i, (name, has_default)) in sig.iter().enumerate() {43 if i != 0 {44 out.push_str(", ");45 }46 if let Some(name) = name {47 out.push_str(name);48 } else {49 out.push_str("<unnamed>");50 }51 if *has_default {52 out.push_str(" = <default>");53 }54 }55 }56 out.push(')');57 out58}5960const fn format_empty_str(str: &str) -> &str {61 if str.is_empty() {62 "\"\" (empty string)"63 } else {64 str65 }66}6768type FunctionSignature = Vec<(Option<IStr>, bool)>;6970#[derive(Error, Debug, Clone, Trace)]71pub enum Error {72 #[error("intrinsic not found: {0}")]73 IntrinsicNotFound(IStr),7475 #[error("operator {0} does not operate on type {1}")]76 UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),77 #[error("binary operation {1} {0} {2} is not implemented")]78 BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),7980 #[error("no top level object in this context")]81 NoTopLevelObjectFound,82 #[error("self is only usable inside objects")]83 CantUseSelfOutsideOfObject,84 #[error("no super found")]85 NoSuperFound,8687 #[error("for loop can only iterate over arrays")]88 InComprehensionCanOnlyIterateOverArray,8990 #[error("array out of bounds: {0} is not within [0,{1})")]91 ArrayBoundsError(usize, usize),92 #[error("string out of bounds: {0} is not within [0,{1})")]93 StringBoundsError(usize, usize),9495 #[error("assert failed: {}", format_empty_str(.0))]96 AssertionFailed(IStr),9798 #[error("variable is not defined: {0}{}", format_found(.1, "variable"))]99 VariableIsNotDefined(IStr, Vec<IStr>),100 #[error("duplicate local var: {0}")]101 DuplicateLocalVar(IStr),102103 #[error("type mismatch: expected {}, got {2} {0}", .1.iter().map(|e| format!("{}", e)).collect::<Vec<_>>().join(", "))]104 TypeMismatch(&'static str, Vec<ValType>, ValType),105 #[error("no such field: {}{}", format_empty_str(.0), format_found(.1, "field"))]106 NoSuchField(IStr, Vec<IStr>),107108 #[error("only functions can be called, got {0}")]109 OnlyFunctionsCanBeCalledGot(ValType),110 #[error("parameter {0} is not defined")]111 UnknownFunctionParameter(String),112 #[error("argument {0} is already bound")]113 BindingParameterASecondTime(IStr),114 #[error("too many args, function has {0}{}", format_signature(.1))]115 TooManyArgsFunctionHas(usize, FunctionSignature),116 #[error("function argument is not passed: {0}{}", format_signature(.1))]117 FunctionParameterNotBoundInCall(IStr, FunctionSignature),118119 #[error("external variable is not defined: {0}")]120 UndefinedExternalVariable(IStr),121122 #[error("field name should be string, got {0}")]123 FieldMustBeStringGot(ValType),124 #[error("duplicate field name: {}", format_empty_str(.0))]125 DuplicateFieldName(IStr),126127 #[error("attempted to index array with string {}", format_empty_str(.0))]128 AttemptedIndexAnArrayWithString(IStr),129 #[error("{0} index type should be {1}, got {2}")]130 ValueIndexMustBeTypeGot(ValType, ValType, ValType),131 #[error("cant index into {0}")]132 CantIndexInto(ValType),133 #[error("{0} is not indexable")]134 ValueIsNotIndexable(ValType),135136 #[error("super can't be used standalone")]137 StandaloneSuper,138139 #[error("can't resolve {1} from {0}")]140 ImportFileNotFound(SourcePath, String),141 #[error("can't resolve absolute {0}")]142 AbsoluteImportFileNotFound(PathBuf),143 #[error("resolved file not found: {:?}", .0)]144 ResolvedFileNotFound(SourcePath),145 #[error("can't import {0}: is a directory")]146 ImportIsADirectory(SourcePath),147 #[error("imported file is not valid utf-8: {0:?}")]148 ImportBadFileUtf8(SourcePath),149 #[error("import io error: {0}")]150 ImportIo(String),151 #[error("tried to import {1} from {0}, but imports are not supported")]152 ImportNotSupported(SourcePath, String),153 #[error("tried to import {0}, but absolute imports are not supported")]154 AbsoluteImportNotSupported(PathBuf),155 #[error("can't import from virtual file")]156 CantImportFromVirtualFile,157 #[error(158 "syntax error: expected {}, got {:?}",159 .error.expected,160 .path.code().chars().nth(error.location.offset)161 .map_or_else(|| "EOF".into(), |c| c.to_string())162 )]163 ImportSyntaxError {164 path: Source,165 #[trace(skip)]166 error: Box<jrsonnet_parser::ParseError>,167 },168169 #[error("runtime error: {}", format_empty_str(.0))]170 RuntimeError(IStr),171 #[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]172 StackOverflow,173 #[error("infinite recursion detected")]174 InfiniteRecursionDetected,175 #[error("tried to index by fractional value")]176 FractionalIndex,177 #[error("attempted to divide by zero")]178 DivisionByZero,179180 #[error("string manifest output is not an string")]181 StringManifestOutputIsNotAString,182 #[error("stream manifest output is not an array")]183 StreamManifestOutputIsNotAArray,184 #[error("multi manifest output is not an object")]185 MultiManifestOutputIsNotAObject,186187 #[error("cant recurse stream manifest")]188 StreamManifestOutputCannotBeRecursed,189 #[error("stream manifest output cannot consist of raw strings")]190 StreamManifestCannotNestString,191192 #[error("{}", format_empty_str(.0))]193 ImportCallbackError(String),194 #[error("invalid unicode codepoint: {0}")]195 InvalidUnicodeCodepointGot(u32),196197 #[error("format error: {0}")]198 Format(#[from] FormatError),199 #[error("type error: {0}")]200 TypeError(TypeLocError),201202 #[cfg(feature = "anyhow-error")]203 #[error(transparent)]204 Other(Rc<anyhow::Error>),205}206207#[cfg(feature = "anyhow-error")]208impl From<anyhow::Error> for LocError {209 fn from(e: anyhow::Error) -> Self {210 Self::new(Error::Other(Rc::new(e)))211 }212}213214impl From<Error> for LocError {215 fn from(e: Error) -> Self {216 Self::new(e)217 }218}219220#[derive(Clone, Debug, Trace)]221pub struct StackTraceElement {222 pub location: Option<ExprLocation>,223 pub desc: String,224}225#[derive(Debug, Clone, Trace)]226pub struct StackTrace(pub Vec<StackTraceElement>);227228#[derive(Clone, Trace)]229pub struct LocError(Box<(Error, StackTrace)>);230impl LocError {231 pub fn new(e: Error) -> Self {232 Self(Box::new((e, StackTrace(vec![]))))233 }234235 pub const fn error(&self) -> &Error {236 &(self.0).0237 }238 pub fn error_mut(&mut self) -> &mut Error {239 &mut (self.0).0240 }241 pub const fn trace(&self) -> &StackTrace {242 &(self.0).1243 }244 pub fn trace_mut(&mut self) -> &mut StackTrace {245 &mut (self.0).1246 }247}248impl Debug for LocError {249 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {250 writeln!(f, "{}", self.0 .0)?;251 for el in &self.0 .1 .0 {252 writeln!(f, "\t{:?}", el)?;253 }254 Ok(())255 }256}257258pub type Result<V, E = LocError> = std::result::Result<V, E>;259260#[macro_export]261macro_rules! throw {262 ($e: expr) => {263 return Err($e.into())264 };265}266267#[macro_export]268macro_rules! throw_runtime {269 ($($tt:tt)*) => {270 return Err($crate::error::Error::RuntimeError(format!($($tt)*).into()).into())271 };272}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.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 {