diff --git a/bindings/jsonnet/src/import.rs b/bindings/jsonnet/src/import.rs index 1102d7f1..7d27adf0 100644 --- a/bindings/jsonnet/src/import.rs +++ b/bindings/jsonnet/src/import.rs @@ -12,7 +12,7 @@ use std::{ fs::File, io::Read, os::raw::{c_char, c_int}, - path::PathBuf, + path::{Path, PathBuf}, ptr::null_mut, rc::Rc, }; @@ -33,7 +33,7 @@ pub struct CallbackImportResolver { out: RefCell>, } impl ImportResolver for CallbackImportResolver { - fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result> { + fn resolve_file(&self, from: &Path, path: &Path) -> Result> { let base = CString::new(from.to_str().unwrap()).unwrap().into_raw(); let rel = CString::new(path.to_str().unwrap()).unwrap().into_raw(); let found_here: *mut c_char = null_mut(); @@ -73,9 +73,9 @@ impl ImportResolver for CallbackImportResolver { unsafe { CString::from_raw(result_ptr) }; } - Ok(Rc::new(found_here_buf)) + Ok(found_here_buf.into()) } - fn load_file_contents(&self, resolved: &PathBuf) -> Result { + fn load_file_contents(&self, resolved: &Path) -> Result { Ok(self.out.borrow().get(resolved).unwrap().clone()) } unsafe fn as_any(&self) -> &dyn Any { @@ -108,27 +108,27 @@ impl NativeImportResolver { } } impl ImportResolver for NativeImportResolver { - fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result> { - let mut new_path = from.clone(); + fn resolve_file(&self, from: &Path, path: &Path) -> Result> { + let mut new_path = from.to_owned(); new_path.push(path); if new_path.exists() { - Ok(Rc::new(new_path)) + Ok(new_path.into()) } else { for library_path in self.library_paths.borrow().iter() { let mut cloned = library_path.clone(); cloned.push(path); if cloned.exists() { - return Ok(Rc::new(cloned)); + return Ok(cloned.into()); } } - throw!(ImportFileNotFound(from.clone(), path.clone())) + throw!(ImportFileNotFound(from.to_owned(), path.to_owned())) } } - fn load_file_contents(&self, id: &PathBuf) -> Result { - let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.clone()))?; + fn load_file_contents(&self, id: &Path) -> Result { + let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.to_owned()))?; let mut out = String::new(); file.read_to_string(&mut out) - .map_err(|_e| ImportBadFileUtf8(id.clone()))?; + .map_err(|_e| ImportBadFileUtf8(id.to_owned()))?; Ok(out.into()) } unsafe fn as_any(&self) -> &dyn Any { diff --git a/bindings/jsonnet/src/lib.rs b/bindings/jsonnet/src/lib.rs index 5e7aa558..f1ab7bd3 100644 --- a/bindings/jsonnet/src/lib.rs +++ b/bindings/jsonnet/src/lib.rs @@ -15,7 +15,6 @@ use std::{ ffi::{CStr, CString}, os::raw::{c_char, c_double, c_int, c_uint}, path::PathBuf, - rc::Rc, }; /// WASM stub @@ -144,7 +143,7 @@ pub unsafe extern "C" fn jsonnet_evaluate_snippet( let snippet = CStr::from_ptr(snippet); match vm .evaluate_snippet_raw( - Rc::new(PathBuf::from(filename.to_str().unwrap())), + PathBuf::from(filename.to_str().unwrap()).into(), snippet.to_str().unwrap().into(), ) .and_then(|v| vm.with_tla(v)) @@ -220,7 +219,7 @@ pub unsafe extern "C" fn jsonnet_evaluate_snippet_multi( let snippet = CStr::from_ptr(snippet); match vm .evaluate_snippet_raw( - Rc::new(PathBuf::from(filename.to_str().unwrap())), + PathBuf::from(filename.to_str().unwrap()).into(), snippet.to_str().unwrap().into(), ) .and_then(|v| vm.with_tla(v)) @@ -294,7 +293,7 @@ pub unsafe extern "C" fn jsonnet_evaluate_snippet_stream( let snippet = CStr::from_ptr(snippet); match vm .evaluate_snippet_raw( - Rc::new(PathBuf::from(filename.to_str().unwrap())), + PathBuf::from(filename.to_str().unwrap()).into(), snippet.to_str().unwrap().into(), ) .and_then(|v| vm.with_tla(v)) diff --git a/cmds/jrsonnet/src/main.rs b/cmds/jrsonnet/src/main.rs index 29fbb632..47047b26 100644 --- a/cmds/jrsonnet/src/main.rs +++ b/cmds/jrsonnet/src/main.rs @@ -6,7 +6,6 @@ use std::{ io::Read, io::Write, path::PathBuf, - rc::Rc, str::FromStr, }; @@ -134,14 +133,14 @@ fn main_real(state: &EvaluationState, opts: Opts) -> Result<(), Error> { let val = if opts.input.exec { state.set_manifest_format(ManifestFormat::ToString); state.evaluate_snippet_raw( - Rc::new(PathBuf::from("args")), + PathBuf::from("args").into(), (&opts.input.input as &str).into(), )? } else if opts.input.input == "-" { let mut input = Vec::new(); std::io::stdin().read_to_end(&mut input)?; let input_str = std::str::from_utf8(&input)?.into(); - state.evaluate_snippet_raw(Rc::new(PathBuf::from("")), input_str)? + state.evaluate_snippet_raw(PathBuf::from("").into(), input_str)? } else { state.evaluate_file_raw(&PathBuf::from(opts.input.input))? }; diff --git a/crates/jrsonnet-evaluator/build.rs b/crates/jrsonnet-evaluator/build.rs index e37eda06..86597641 100644 --- a/crates/jrsonnet-evaluator/build.rs +++ b/crates/jrsonnet-evaluator/build.rs @@ -15,7 +15,7 @@ fn main() { let parsed = parse( STDLIB_STR, &ParserSettings { - file_name: Rc::new(PathBuf::from("std.jsonnet")), + file_name: PathBuf::from("std.jsonnet").into(), loc_data: true, }, ) diff --git a/crates/jrsonnet-evaluator/src/builtin/mod.rs b/crates/jrsonnet-evaluator/src/builtin/mod.rs index 8112bceb..393f31f8 100644 --- a/crates/jrsonnet-evaluator/src/builtin/mod.rs +++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs @@ -174,7 +174,7 @@ fn builtin_parse_json( 0, s: ty!(string) => Val::Str; ], { let state = EvaluationState::default(); - let path = Rc::new(PathBuf::from("std.parseJson")); + let path = PathBuf::from("std.parseJson").into(); state.evaluate_snippet_raw(path ,s) }) } diff --git a/crates/jrsonnet-evaluator/src/builtin/stdlib.rs b/crates/jrsonnet-evaluator/src/builtin/stdlib.rs index 05ddda18..2984552f 100644 --- a/crates/jrsonnet-evaluator/src/builtin/stdlib.rs +++ b/crates/jrsonnet-evaluator/src/builtin/stdlib.rs @@ -1,5 +1,5 @@ use jrsonnet_parser::{LocExpr, ParserSettings}; -use std::{path::PathBuf, rc::Rc}; +use std::path::PathBuf; thread_local! { /// To avoid parsing again when issued from the same thread @@ -25,7 +25,7 @@ thread_local! { jrsonnet_stdlib::STDLIB_STR, &ParserSettings { loc_data: true, - file_name: Rc::new(PathBuf::from("std.jsonnet")), + file_name: PathBuf::from("std.jsonnet").into(), }, ) .unwrap() diff --git a/crates/jrsonnet-evaluator/src/error.rs b/crates/jrsonnet-evaluator/src/error.rs index 99d808e1..09c60c46 100644 --- a/crates/jrsonnet-evaluator/src/error.rs +++ b/crates/jrsonnet-evaluator/src/error.rs @@ -5,7 +5,10 @@ use crate::{ use jrsonnet_interner::IStr; use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType}; use jrsonnet_types::ValType; -use std::{path::PathBuf, rc::Rc}; +use std::{ + path::{Path, PathBuf}, + rc::Rc, +}; use thiserror::Error; #[derive(Error, Debug, Clone)] @@ -86,7 +89,7 @@ pub enum Error { .source_code.chars().nth(error.location.offset).map(|c| c.to_string()).unwrap_or_else(|| "EOF".into()) )] ImportSyntaxError { - path: Rc, + path: Rc, source_code: IStr, error: Box, }, diff --git a/crates/jrsonnet-evaluator/src/evaluate.rs b/crates/jrsonnet-evaluator/src/evaluate.rs index 755c5c23..3040b5a1 100644 --- a/crates/jrsonnet-evaluator/src/evaluate.rs +++ b/crates/jrsonnet-evaluator/src/evaluate.rs @@ -609,26 +609,26 @@ pub fn evaluate(context: Context, expr: &LocExpr) -> Result { } } Import(path) => { - let mut tmp = loc + let tmp = loc .clone() .expect("imports cannot be used without loc_data") .0; - let import_location = Rc::make_mut(&mut tmp); + let mut import_location = tmp.to_path_buf(); import_location.pop(); push( loc.as_ref(), || format!("import {:?}", path), - || with_state(|s| s.import_file(import_location, path)), + || with_state(|s| s.import_file(&import_location, path)), )? } ImportStr(path) => { - let mut tmp = loc + let tmp = loc .clone() .expect("imports cannot be used without loc_data") .0; - let import_location = Rc::make_mut(&mut tmp); + let mut import_location = tmp.to_path_buf(); import_location.pop(); - Val::Str(with_state(|s| s.import_file_str(import_location, path))?) + Val::Str(with_state(|s| s.import_file_str(&import_location, path))?) } }) } diff --git a/crates/jrsonnet-evaluator/src/import.rs b/crates/jrsonnet-evaluator/src/import.rs index 956f34a4..1134a3a9 100644 --- a/crates/jrsonnet-evaluator/src/import.rs +++ b/crates/jrsonnet-evaluator/src/import.rs @@ -6,17 +6,23 @@ use fs::File; use jrsonnet_interner::IStr; use std::fs; use std::io::Read; -use std::{any::Any, cell::RefCell, collections::HashMap, path::PathBuf, rc::Rc}; +use std::{ + any::Any, + cell::RefCell, + collections::HashMap, + path::{Path, PathBuf}, + rc::Rc, +}; /// Implements file resolution logic for `import` and `importStr` pub trait ImportResolver { /// Resolves real 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(&self, from: &PathBuf, path: &PathBuf) -> Result>; + fn resolve_file(&self, from: &Path, path: &Path) -> Result>; /// Reads file from filesystem, should be used only with path received from `resolve_file` - fn load_file_contents(&self, resolved: &PathBuf) -> Result; + fn load_file_contents(&self, resolved: &Path) -> Result; /// # Safety /// @@ -29,11 +35,11 @@ pub trait ImportResolver { /// Dummy resolver, can't resolve/load any file pub struct DummyImportResolver; impl ImportResolver for DummyImportResolver { - fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result> { - throw!(ImportNotSupported(from.clone(), path.clone())) + fn resolve_file(&self, from: &Path, path: &Path) -> Result> { + throw!(ImportNotSupported(from.into(), path.into())) } - fn load_file_contents(&self, _resolved: &PathBuf) -> Result { + fn load_file_contents(&self, _resolved: &Path) -> Result { // Can be only caused by library direct consumer, not by supplied jsonnet panic!("dummy resolver can't load any file") } @@ -57,27 +63,27 @@ pub struct FileImportResolver { pub library_paths: Vec, } impl ImportResolver for FileImportResolver { - fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result> { - let mut new_path = from.clone(); - new_path.push(path); - if new_path.exists() { - Ok(Rc::new(new_path)) + fn resolve_file(&self, from: &Path, path: &Path) -> Result> { + let mut direct = from.to_path_buf(); + direct.push(path); + if direct.exists() { + Ok(direct.into()) } else { for library_path in self.library_paths.iter() { let mut cloned = library_path.clone(); cloned.push(path); if cloned.exists() { - return Ok(Rc::new(cloned)); + return Ok(cloned.into()); } } - throw!(ImportFileNotFound(from.clone(), path.clone())) + throw!(ImportFileNotFound(from.to_owned(), path.to_owned())) } } - fn load_file_contents(&self, id: &PathBuf) -> Result { - let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.clone()))?; + fn load_file_contents(&self, id: &Path) -> Result { + let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.to_owned()))?; let mut out = String::new(); file.read_to_string(&mut out) - .map_err(|_e| ImportBadFileUtf8(id.clone()))?; + .map_err(|_e| ImportBadFileUtf8(id.to_owned()))?; Ok(out.into()) } unsafe fn as_any(&self) -> &dyn Any { @@ -89,23 +95,23 @@ type ResolutionData = (PathBuf, PathBuf); /// Caches results of the underlying resolver pub struct CachingImportResolver { - resolution_cache: RefCell>>>, + resolution_cache: RefCell>>>, loading_cache: RefCell>>, inner: Box, } impl ImportResolver for CachingImportResolver { - fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result> { + fn resolve_file(&self, from: &Path, path: &Path) -> Result> { self.resolution_cache .borrow_mut() - .entry((from.clone(), path.clone())) + .entry((from.to_owned(), path.to_owned())) .or_insert_with(|| self.inner.resolve_file(from, path)) .clone() } - fn load_file_contents(&self, resolved: &PathBuf) -> Result { + fn load_file_contents(&self, resolved: &Path) -> Result { self.loading_cache .borrow_mut() - .entry(resolved.clone()) + .entry(resolved.to_owned()) .or_insert_with(|| self.inner.load_file_contents(resolved)) .clone() } diff --git a/crates/jrsonnet-evaluator/src/lib.rs b/crates/jrsonnet-evaluator/src/lib.rs index 0412de01..7dc53eb9 100644 --- a/crates/jrsonnet-evaluator/src/lib.rs +++ b/crates/jrsonnet-evaluator/src/lib.rs @@ -35,7 +35,7 @@ use std::{ collections::HashMap, fmt::Debug, hash::BuildHasherDefault, - path::PathBuf, + path::{Path, PathBuf}, rc::Rc, }; use trace::{offset_to_location, CodeLocation, CompactFormat, TraceFormat}; @@ -109,8 +109,8 @@ struct EvaluationData { /// Used for stack overflow detection, stacktrace is populated on unwind stack_depth: usize, /// Contains file source codes and evaluation results for imports and pretty-printed stacktraces - files: HashMap, FileData>, - str_files: HashMap, IStr>, + files: HashMap, FileData>, + str_files: HashMap, IStr>, } pub struct FileData { @@ -156,7 +156,7 @@ pub struct EvaluationState(Rc); impl EvaluationState { /// Parses and adds file as loaded - pub fn add_file(&self, path: Rc, source_code: IStr) -> Result<()> { + pub fn add_file(&self, path: Rc, source_code: IStr) -> Result<()> { self.add_parsed_file( path.clone(), source_code.clone(), @@ -169,7 +169,7 @@ impl EvaluationState { ) .map_err(|error| ImportSyntaxError { error: Box::new(error), - path, + path: path.to_owned(), source_code, })?, )?; @@ -180,7 +180,7 @@ impl EvaluationState { /// Adds file by source code and parsed expr pub fn add_parsed_file( &self, - name: Rc, + name: Rc, source_code: IStr, parsed: LocExpr, ) -> Result<()> { @@ -195,20 +195,20 @@ impl EvaluationState { Ok(()) } - pub fn get_source(&self, name: &PathBuf) -> Option { + pub fn get_source(&self, name: &Path) -> Option { let ro_map = &self.data().files; ro_map.get(name).map(|value| value.source_code.clone()) } - pub fn map_source_locations(&self, file: &PathBuf, locs: &[usize]) -> Vec { + pub fn map_source_locations(&self, file: &Path, locs: &[usize]) -> Vec { offset_to_location(&self.get_source(file).unwrap(), locs) } - pub(crate) fn import_file(&self, from: &PathBuf, path: &PathBuf) -> Result { + pub(crate) fn import_file(&self, from: &Path, path: &Path) -> Result { let file_path = self.resolve_file(from, path)?; { let data = self.data(); let files = &data.files; - if files.contains_key(&file_path) { + if files.contains_key(&file_path as &Path) { drop(data); return self.evaluate_loaded_file_raw(&file_path); } @@ -217,7 +217,7 @@ impl EvaluationState { self.add_file(file_path.clone(), contents)?; self.evaluate_loaded_file_raw(&file_path) } - pub(crate) fn import_file_str(&self, from: &PathBuf, path: &PathBuf) -> Result { + pub(crate) fn import_file_str(&self, from: &Path, path: &Path) -> Result { let path = self.resolve_file(from, path)?; if !self.data().str_files.contains_key(&path) { let file_str = self.load_file_contents(&path)?; @@ -226,7 +226,7 @@ impl EvaluationState { Ok(self.data().str_files.get(&path).cloned().unwrap()) } - fn evaluate_loaded_file_raw(&self, name: &PathBuf) -> Result { + fn evaluate_loaded_file_raw(&self, name: &Path) -> Result { let expr: LocExpr = { let ro_map = &self.data().files; let value = ro_map @@ -252,7 +252,7 @@ impl EvaluationState { /// Adds standard library global variable (std) to this evaluator pub fn with_stdlib(&self) -> &Self { use jrsonnet_stdlib::STDLIB_STR; - let std_path = Rc::new(PathBuf::from("std.jsonnet")); + let std_path: Rc = PathBuf::from("std.jsonnet").into(); self.run_in_state(|| { self.add_parsed_file( std_path.clone(), @@ -380,14 +380,14 @@ impl EvaluationState { /// Raw methods evaluate passed values but don't perform TLA execution impl EvaluationState { - pub fn evaluate_file_raw(&self, name: &PathBuf) -> Result { + pub fn evaluate_file_raw(&self, name: &Path) -> Result { self.run_in_state(|| self.import_file(&std::env::current_dir().expect("cwd"), name)) } - pub fn evaluate_file_raw_nocwd(&self, name: &PathBuf) -> Result { + pub fn evaluate_file_raw_nocwd(&self, name: &Path) -> Result { self.run_in_state(|| self.import_file(&PathBuf::from("."), name)) } /// Parses and evaluates the given snippet - pub fn evaluate_snippet_raw(&self, source: Rc, code: IStr) -> Result { + pub fn evaluate_snippet_raw(&self, source: Rc, code: IStr) -> Result { let parsed = parse( &code, &ParserSettings { @@ -419,7 +419,7 @@ impl EvaluationState { } pub fn add_ext_code(&self, name: IStr, code: IStr) -> Result<()> { let value = - self.evaluate_snippet_raw(Rc::new(PathBuf::from(format!("ext_code {}", name))), code)?; + self.evaluate_snippet_raw(PathBuf::from(format!("ext_code {}", name)).into(), code)?; self.add_ext_var(name, value); Ok(()) } @@ -432,15 +432,15 @@ impl EvaluationState { } pub fn add_tla_code(&self, name: IStr, code: IStr) -> Result<()> { let value = - self.evaluate_snippet_raw(Rc::new(PathBuf::from(format!("tla_code {}", name))), code)?; + self.evaluate_snippet_raw(PathBuf::from(format!("tla_code {}", name)).into(), code)?; self.add_tla(name, value); Ok(()) } - pub fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result> { + pub fn resolve_file(&self, from: &Path, path: &Path) -> Result> { self.settings().import_resolver.resolve_file(from, path) } - pub fn load_file_contents(&self, path: &PathBuf) -> Result { + pub fn load_file_contents(&self, path: &Path) -> Result { self.settings().import_resolver.load_file_contents(path) } @@ -490,7 +490,10 @@ pub mod tests { use crate::{error::Error::*, primitive_equals, EvaluationState}; use jrsonnet_interner::IStr; use jrsonnet_parser::*; - use std::{path::PathBuf, rc::Rc}; + use std::{ + path::{Path, PathBuf}, + rc::Rc, + }; #[test] #[should_panic] @@ -499,19 +502,11 @@ pub mod tests { state.run_in_state(|| { state .push( - Some(&ExprLocation( - Rc::new(PathBuf::from("test1.jsonnet")), - 10, - 20, - )), + Some(&ExprLocation(PathBuf::from("test1.jsonnet").into(), 10, 20)), || "outer".to_owned(), || { state.push( - Some(&ExprLocation( - Rc::new(PathBuf::from("test2.jsonnet")), - 30, - 40, - )), + Some(&ExprLocation(PathBuf::from("test2.jsonnet").into(), 30, 40)), || "inner".to_owned(), || Err(RuntimeError("".into()).into()), )?; @@ -529,7 +524,7 @@ pub mod tests { assert!(primitive_equals( &state .evaluate_snippet_raw( - Rc::new(PathBuf::from("raw.jsonnet")), + PathBuf::from("raw.jsonnet").into(), r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#.into() ) .unwrap(), @@ -542,7 +537,7 @@ pub mod tests { ($str: expr) => { EvaluationState::default() .with_stdlib() - .evaluate_snippet_raw(Rc::new(PathBuf::from("raw.jsonnet")), $str.into()) + .evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into()) .unwrap() }; } @@ -552,7 +547,7 @@ pub mod tests { evaluator.with_stdlib(); evaluator.run_in_state(|| { evaluator - .evaluate_snippet_raw(Rc::new(PathBuf::from("raw.jsonnet")), $str.into()) + .evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into()) .unwrap() .to_json(0) .unwrap() @@ -909,7 +904,10 @@ pub mod tests { "{:?}", jrsonnet_parser::parse( "{ x: 1, y: 2 } == { x: 1, y: 2 }", - &ParserSettings::default() + &ParserSettings { + file_name: PathBuf::from("equality").into(), + loc_data: true, + } ) ); assert_eval!("{ x: 1, y: 2 } == { x: 1, y: 2 }") @@ -930,8 +928,8 @@ pub mod tests { ])), |caller, args| { assert_eq!( - caller.unwrap(), - Rc::new(PathBuf::from("native_caller.jsonnet")) + &caller.unwrap() as &Path, + &PathBuf::from("native_caller.jsonnet") ); match (&args[0], &args[1]) { (Val::Num(a), Val::Num(b)) => Ok(Val::Num(a + b)), @@ -941,7 +939,7 @@ pub mod tests { )), ); evaluator.evaluate_snippet_raw( - Rc::new(PathBuf::from("native_caller.jsonnet")), + PathBuf::from("native_caller.jsonnet").into(), "std.assertEqual(std.native(\"native_add\")(1, 2), 3)".into(), )?; Ok(()) @@ -993,11 +991,11 @@ pub mod tests { struct TestImportResolver(IStr); impl crate::import::ImportResolver for TestImportResolver { - fn resolve_file(&self, _: &PathBuf, _: &PathBuf) -> crate::error::Result> { - Ok(Rc::new(PathBuf::from("/test"))) + fn resolve_file(&self, _: &Path, _: &Path) -> crate::error::Result> { + Ok(PathBuf::from("/test").into()) } - fn load_file_contents(&self, _: &PathBuf) -> crate::error::Result { + fn load_file_contents(&self, _: &Path) -> crate::error::Result { Ok(self.0.clone()) } @@ -1020,7 +1018,7 @@ pub mod tests { let error = state .evaluate_snippet_raw( - Rc::new(PathBuf::from("issue40.jsonnet")), + PathBuf::from("issue40.jsonnet").into(), r#" local conf = { n: "" diff --git a/crates/jrsonnet-evaluator/src/native.rs b/crates/jrsonnet-evaluator/src/native.rs index a6673c48..580a9537 100644 --- a/crates/jrsonnet-evaluator/src/native.rs +++ b/crates/jrsonnet-evaluator/src/native.rs @@ -3,24 +3,24 @@ use crate::{error::Result, Val}; use jrsonnet_parser::ParamsDesc; use std::fmt::Debug; -use std::path::PathBuf; +use std::path::Path; use std::rc::Rc; pub struct NativeCallback { pub params: ParamsDesc, - handler: Box>, &[Val]) -> Result>, + handler: Box>, &[Val]) -> Result>, } impl NativeCallback { pub fn new( params: ParamsDesc, - handler: impl Fn(Option>, &[Val]) -> Result + 'static, + handler: impl Fn(Option>, &[Val]) -> Result + 'static, ) -> Self { Self { params, handler: Box::new(handler), } } - pub fn call(&self, caller: Option>, args: &[Val]) -> Result { + pub fn call(&self, caller: Option>, args: &[Val]) -> Result { (self.handler)(caller, args) } } diff --git a/crates/jrsonnet-evaluator/src/trace/mod.rs b/crates/jrsonnet-evaluator/src/trace/mod.rs index 337c7abb..a4365ab7 100644 --- a/crates/jrsonnet-evaluator/src/trace/mod.rs +++ b/crates/jrsonnet-evaluator/src/trace/mod.rs @@ -2,7 +2,7 @@ mod location; use crate::{error::Error, EvaluationState, LocError}; pub use location::*; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; /// The way paths should be displayed pub enum PathResolver { @@ -15,7 +15,7 @@ pub enum PathResolver { } impl PathResolver { - pub fn resolve(&self, from: &PathBuf) -> String { + pub fn resolve(&self, from: &Path) -> String { match self { Self::FileName => from.file_name().unwrap().to_string_lossy().into_owned(), Self::Absolute => from.to_string_lossy().into_owned(), @@ -255,7 +255,7 @@ impl ExplainingFormat { &self, out: &mut dyn std::fmt::Write, source: &str, - origin: &PathBuf, + origin: &Path, start: &CodeLocation, end: &CodeLocation, desc: &str, diff --git a/crates/jrsonnet-parser/src/expr.rs b/crates/jrsonnet-parser/src/expr.rs index f3662fe6..d42a2d4d 100644 --- a/crates/jrsonnet-parser/src/expr.rs +++ b/crates/jrsonnet-parser/src/expr.rs @@ -6,7 +6,7 @@ use serde::Serialize; use std::{ fmt::{Debug, Display}, ops::Deref, - path::PathBuf, + path::{Path, PathBuf}, rc::Rc, }; @@ -320,7 +320,7 @@ pub enum Expr { #[cfg_attr(feature = "serialize", derive(Serialize))] #[cfg_attr(feature = "deserialize", derive(Deserialize))] #[derive(Clone, PartialEq)] -pub struct ExprLocation(pub Rc, pub usize, pub usize); +pub struct ExprLocation(pub Rc, pub usize, pub usize); impl Debug for ExprLocation { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2) diff --git a/crates/jrsonnet-parser/src/lib.rs b/crates/jrsonnet-parser/src/lib.rs index 38beda7d..c107f42c 100644 --- a/crates/jrsonnet-parser/src/lib.rs +++ b/crates/jrsonnet-parser/src/lib.rs @@ -1,15 +1,17 @@ #![allow(clippy::redundant_closure_call)] use peg::parser; -use std::{path::PathBuf, rc::Rc}; +use std::{ + path::{Path, PathBuf}, + rc::Rc, +}; mod expr; pub use expr::*; pub use peg; -#[derive(Default)] pub struct ParserSettings { pub loc_data: bool, - pub file_name: Rc, + pub file_name: Rc, } parser! { @@ -304,7 +306,6 @@ pub mod tests { use super::{expr::*, parse}; use crate::ParserSettings; use std::path::PathBuf; - use std::rc::Rc; macro_rules! parse { ($s:expr) => { @@ -312,7 +313,7 @@ pub mod tests { $s, &ParserSettings { loc_data: false, - file_name: Rc::new(PathBuf::from("/test.jsonnet")), + file_name: PathBuf::from("/test.jsonnet").into(), }, ) .unwrap()