difftreelog
docs(evaluator) document public api
in: master
2 files changed
crates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth8use std::io::Read;8use std::io::Read;9use std::{any::Any, cell::RefCell, collections::HashMap, path::PathBuf, rc::Rc};9use std::{any::Any, cell::RefCell, collections::HashMap, path::PathBuf, rc::Rc};101011/// Implements file resolution logic for `import` and `importStr`11pub trait ImportResolver {12pub trait ImportResolver {13 /// Resolve real file path, i.e14 /// `(/home/user/manifests, b.libsonnet)` can resolve to both `/home/user/manifests/b.libsonnet` and to `/home/user/vendor/b.libsonnet`15 /// (Where vendor is a library path)12 fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>>;16 fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>>;17 /// Reads file from filesystem, should be used only with path received from `resolve_file`13 fn load_file_contents(&self, resolved: &PathBuf) -> Result<Rc<str>>;18 fn load_file_contents(&self, resolved: &PathBuf) -> Result<Rc<str>>;19 /// # Safety20 ///21 /// For use in bindings, do not try to use it elsewhere22 /// Implementations, which are not intended to be23 /// used in bindings, should panic in this method14 unsafe fn as_any(&self) -> &dyn Any;24 unsafe fn as_any(&self) -> &dyn Any;15}25}162627/// Dummy resolver, can't resolve/load any file17pub struct DummyImportResolver;28pub struct DummyImportResolver;18impl ImportResolver for DummyImportResolver {29impl ImportResolver for DummyImportResolver {19 fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {30 fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {33 }44 }34}45}354647/// File resolver, can load file from both FS and library paths48#[derive(Default)]36pub struct FileImportResolver {49pub struct FileImportResolver {50 /// Library directories to search for file51 /// In original jsonnet referred as jpath37 pub library_paths: Vec<PathBuf>,52 pub library_paths: Vec<PathBuf>,38}53}39impl ImportResolver for FileImportResolver {54impl ImportResolver for FileImportResolver {688369type ResolutionData = (PathBuf, PathBuf);84type ResolutionData = (PathBuf, PathBuf);8586/// Caches results of underlying resolver implementation70pub struct CachingImportResolver {87pub struct CachingImportResolver {71 resolution_cache: RefCell<HashMap<ResolutionData, Result<Rc<PathBuf>>>>,88 resolution_cache: RefCell<HashMap<ResolutionData, Result<Rc<PathBuf>>>>,72 loading_cache: RefCell<HashMap<PathBuf, Result<Rc<str>>>>,89 loading_cache: RefCell<HashMap<PathBuf, Result<Rc<str>>>>,crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth311 Context::new().extend_unbound(new_bindings, None, None, None)311 Context::new().extend_unbound(new_bindings, None, None, None)312 }312 }313313314 /// Executes code, creating new stack frame314 pub fn push<T>(315 pub fn push<T>(315 &self,316 &self,316 e: ExprLocation,317 e: ExprLocation,332 self.data_mut().stack.pop();333 self.data_mut().stack.pop();333 result334 result334 }335 }335 pub fn print_stack_trace(&self) {336336 for e in self.stack_trace().0 {337 /// Returns current stack trace337 println!("{:?} - {:?}", e.0, e.1)338 }339 }340 pub fn stack_trace(&self) -> StackTrace {338 pub fn stack_trace(&self) -> StackTrace {341 StackTrace(339 StackTrace(342 self.data()340 self.data()349 )347 )350 }348 }349350 /// Creates error with stack trace351 pub fn error(&self, err: Error) -> LocError {351 pub fn error(&self, err: Error) -> LocError {352 LocError(err, self.stack_trace())352 LocError(err, self.stack_trace())353 }353 }354354355 /// Runs passed function in state (required, if function needs to modify stack trace)355 pub fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {356 pub fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {356 EVAL_STATE.with(|v| {357 EVAL_STATE.with(|v| {357 let has_state = v.borrow().is_some();358 let has_state = v.borrow().is_some();386 ExprLocation(Rc::new(PathBuf::from("test2.jsonnet")), 30, 40),387 ExprLocation(Rc::new(PathBuf::from("test2.jsonnet")), 30, 40),387 "inner".to_owned(),388 "inner".to_owned(),388 || {389 || {389 state.print_stack_trace();390 Ok(())390 Ok(())391 },391 },392 )?;392 )?;