difftreelog
feat library paths
in: master
5 files changed
cmds/jrsonnet/src/main.rsdiffbeforeafterboth99 )]99 )]100 max_trace: usize,100 max_trace: usize,101102 #[clap(long, short = "J", about = "Library search dir")]103 jpath: Vec<PathBuf>,101104102 #[clap(105 #[clap(103 long,106 long,112115113fn main() {116fn main() {114 let opts: Opts = Opts::parse();117 let opts: Opts = Opts::parse();115 let evaluator = jsonnet_evaluator::EvaluationState::new(EvaluationSettings {118 let evaluator = jsonnet_evaluator::EvaluationState::new(119 EvaluationSettings {120 max_stack_trace_size: opts.max_trace,121 max_stack_frames: opts.max_stack,122 },116 import_resolver: Box::new(|path| String::from_utf8(std::fs::read(path).unwrap()).unwrap()),123 Box::new(jsonnet_evaluator::FileImportResolver {117 ..Default::default()124 library_paths: opts.jpath.clone(),125 }),118 });126 );119 if !opts.no_stdlib {127 if !opts.no_stdlib {120 evaluator.with_stdlib();128 evaluator.with_stdlib();121 }129 }crates/jsonnet-evaluator/src/error.rsdiffbeforeafterboth1use crate::ValType;1use crate::ValType;2use jsonnet_parser::LocExpr;2use jsonnet_parser::LocExpr;3use std::path::PathBuf;344#[derive(Debug)]5#[derive(Debug)]5pub enum Error {6pub enum Error {242525 StandaloneSuper,26 StandaloneSuper,2728 ImportFileNotFound(PathBuf, PathBuf),29 ResolvedFileNotFound(PathBuf),30 ImportBadFileUtf8(PathBuf),31 ImportNotSupported(PathBuf, PathBuf),32 ImportSyntaxError(jsonnet_parser::ParseError),263327 RuntimeError(String),34 RuntimeError(String),28 StackOverflow,35 StackOverflow,crates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth785 }785 }786 }786 }787 Import(path) => {787 Import(path) => {788 let mut lib_path = loc788 let mut import_location = loc789 .clone()789 .clone()790 .expect("imports can't be used without loc_data")790 .expect("imports can't be used without loc_data")791 .0791 .0792 .clone();792 .clone();793 lib_path.pop();793 import_location.pop();794 lib_path.push(path);795 with_state(|s| s.import_file(&lib_path))?794 with_state(|s| s.import_file(&import_location, path))?796 }795 }797 ImportStr(path) => {796 ImportStr(path) => {798 let mut file_path = loc797 let mut import_location = loc799 .clone()798 .clone()800 .expect("imports can't be used without loc_data")799 .expect("imports can't be used without loc_data")801 .0800 .0802 .clone();801 .clone();803 file_path.pop();802 import_location.pop();804 file_path.push(path);805 Val::Str(with_state(|s| s.import_file_str(&file_path))?)803 Val::Str(with_state(|s| s.import_file_str(&import_location, path))?)806 }804 }807 Literal(LiteralType::Super) => return create_error(crate::error::Error::StandaloneSuper),805 Literal(LiteralType::Super) => return create_error(crate::error::Error::StandaloneSuper),808 })806 })crates/jsonnet-evaluator/src/import.rsdiffbeforeafterbothno changes
crates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth8mod error;8mod error;9mod evaluate;9mod evaluate;10mod function;10mod function;11mod import;11mod map;12mod map;12mod obj;13mod obj;13mod val;14mod val;17pub use error::*;18pub use error::*;18pub use evaluate::*;19pub use evaluate::*;19pub use function::parse_function_call;20pub use function::parse_function_call;21pub use import::*;20use jsonnet_parser::*;22use jsonnet_parser::*;21pub use obj::*;23pub use obj::*;22use std::{cell::RefCell, collections::HashMap, fmt::Debug, path::PathBuf, rc::Rc};24use std::{cell::RefCell, collections::HashMap, fmt::Debug, path::PathBuf, rc::Rc};46pub struct EvaluationSettings {48pub struct EvaluationSettings {47 pub max_stack_frames: usize,49 pub max_stack_frames: usize,48 pub max_stack_trace_size: usize,50 pub max_stack_trace_size: usize,49 pub import_resolver: Box<dyn Fn(&PathBuf) -> String>,50}51}51impl Default for EvaluationSettings {52impl Default for EvaluationSettings {52 fn default() -> Self {53 fn default() -> Self {53 EvaluationSettings {54 EvaluationSettings {54 max_stack_frames: 200,55 max_stack_frames: 200,55 max_stack_trace_size: 20,56 max_stack_trace_size: 20,56 import_resolver: Box::new(|path| {57 panic!("default EvaluationSettings have no support for import resolution, can't import {:?}", path)58 }),59 }57 }60 }58 }61}59}75 ext_vars: RefCell<HashMap<String, Val>>,73 ext_vars: RefCell<HashMap<String, Val>>,767477 settings: EvaluationSettings,75 settings: EvaluationSettings,76 import_resolver: Box<dyn ImportResolver>,78}77}797880thread_local! {79thread_local! {101#[derive(Default, Clone)]100#[derive(Default, Clone)]102pub struct EvaluationState(Rc<EvaluationStateInternals>);101pub struct EvaluationState(Rc<EvaluationStateInternals>);103impl EvaluationState {102impl EvaluationState {104 pub fn new(settings: EvaluationSettings) -> Self {103 pub fn new(settings: EvaluationSettings, import_resolver: Box<dyn ImportResolver>) -> Self {105 EvaluationState(Rc::new(EvaluationStateInternals {104 EvaluationState(Rc::new(EvaluationStateInternals {106 settings,105 settings,106 import_resolver,107 ..Default::default()107 ..Default::default()108 }))108 }))109 }109 }171 }171 }172 Ok(value)172 Ok(value)173 }173 }174 pub(crate) fn import_file(&self, path: &PathBuf) -> Result<Val> {174 pub(crate) fn import_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Val> {175 let file_path = self.0.import_resolver.resolve_file(from, path)?;176 {175 if !self.0.files.borrow().contains_key(path) {177 let files = self.0.files.borrow();178 if files.contains_key(&file_path) {179 return self.evaluate_file(&file_path);180 }181 }176 let file_str = (self.0.settings.import_resolver)(path);182 let contents = self.0.import_resolver.load_file_contents(&file_path)?;177 self.add_file(path.clone(), file_str).unwrap();183 self.add_file(file_path.clone(), contents).map_err(|e| {178 }184 create_error::<()>(Error::ImportSyntaxError(e))185 .err()186 .unwrap()187 })?;179 self.evaluate_file_in_current_state(path)188 self.evaluate_file(&file_path)180 }189 }181 pub(crate) fn import_file_str(&self, path: &PathBuf) -> Result<String> {190 pub(crate) fn import_file_str(&self, from: &PathBuf, path: &PathBuf) -> Result<String> {191 let path = self.0.import_resolver.resolve_file(from, path)?;182 if !self.0.str_files.borrow().contains_key(path) {192 if !self.0.str_files.borrow().contains_key(&path) {183 let file_str = (self.0.settings.import_resolver)(path);193 let file_str = self.0.import_resolver.load_file_contents(&path)?;184 self.0.str_files.borrow_mut().insert(path.clone(), file_str);194 self.0.str_files.borrow_mut().insert(path.clone(), file_str);185 }195 }186 Ok(self.0.str_files.borrow().get(path).cloned().unwrap())196 Ok(self.0.str_files.borrow().get(&path).cloned().unwrap())187 }197 }188198189 pub fn parse_evaluate_raw(&self, code: &str) -> Result<Val> {199 pub fn parse_evaluate_raw(&self, code: &str) -> Result<Val> {