From af0fd0a324458366e44b65d5a16e4762becc56f4 Mon Sep 17 00:00:00 2001 From: Лач Date: Sun, 28 Jun 2020 12:36:27 +0000 Subject: [PATCH] docs(evaluator): document public api --- --- a/crates/jrsonnet-evaluator/src/import.rs +++ b/crates/jrsonnet-evaluator/src/import.rs @@ -8,12 +8,23 @@ use std::io::Read; use std::{any::Any, cell::RefCell, collections::HashMap, path::PathBuf, rc::Rc}; +/// Implements file resolution logic for `import` and `importStr` pub trait ImportResolver { + /// Resolve real file path, i.e + /// `(/home/user/manifests, b.libsonnet)` can resolve to both `/home/user/manifests/b.libsonnet` and to `/home/user/vendor/b.libsonnet` + /// (Where vendor is a library path) fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result>; + /// Reads file from filesystem, should be used only with path received from `resolve_file` fn load_file_contents(&self, resolved: &PathBuf) -> Result>; + /// # Safety + /// + /// For use in bindings, do not try to use it elsewhere + /// Implementations, which are not intended to be + /// used in bindings, should panic in this method unsafe fn as_any(&self) -> &dyn Any; } +/// Dummy resolver, can't resolve/load any file pub struct DummyImportResolver; impl ImportResolver for DummyImportResolver { fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result> { @@ -33,7 +44,11 @@ } } +/// File resolver, can load file from both FS and library paths +#[derive(Default)] pub struct FileImportResolver { + /// Library directories to search for file + /// In original jsonnet referred as jpath pub library_paths: Vec, } impl ImportResolver for FileImportResolver { @@ -67,6 +82,8 @@ } type ResolutionData = (PathBuf, PathBuf); + +/// Caches results of underlying resolver implementation pub struct CachingImportResolver { resolution_cache: RefCell>>>, loading_cache: RefCell>>>, --- a/crates/jrsonnet-evaluator/src/lib.rs +++ b/crates/jrsonnet-evaluator/src/lib.rs @@ -311,6 +311,7 @@ Context::new().extend_unbound(new_bindings, None, None, None) } + /// Executes code, creating new stack frame pub fn push( &self, e: ExprLocation, @@ -332,11 +333,8 @@ self.data_mut().stack.pop(); result } - pub fn print_stack_trace(&self) { - for e in self.stack_trace().0 { - println!("{:?} - {:?}", e.0, e.1) - } - } + + /// Returns current stack trace pub fn stack_trace(&self) -> StackTrace { StackTrace( self.data() @@ -348,10 +346,13 @@ .collect(), ) } + + /// Creates error with stack trace pub fn error(&self, err: Error) -> LocError { LocError(err, self.stack_trace()) } + /// Runs passed function in state (required, if function needs to modify stack trace) pub fn run_in_state(&self, f: impl FnOnce() -> T) -> T { EVAL_STATE.with(|v| { let has_state = v.borrow().is_some(); @@ -386,7 +387,6 @@ ExprLocation(Rc::new(PathBuf::from("test2.jsonnet")), 30, 40), "inner".to_owned(), || { - state.print_stack_trace(); Ok(()) }, )?; -- gitstuff