difftreelog
style fix clippy warnings
in: master
2 files changed
crates/jrsonnet-evaluator/src/evaluate/destructure.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
@@ -128,8 +128,7 @@
new_bindings,
)?;
}
- Some(DestructRest::Drop) => {}
- None => {}
+ Some(DestructRest::Drop) | None => {}
}
{
@@ -202,7 +201,7 @@
.collect();
let full = Thunk::new(tb!(DataThunk {
parent,
- field_names: field_names.clone(),
+ field_names,
has_rest: rest.is_some()
}));
@@ -222,7 +221,7 @@
Ok(field)
} else {
let (fctx, expr) = self.default.as_ref().expect("shape is checked");
- Ok(evaluate(fctx.clone().unwrap(), &expr)?)
+ Ok(evaluate(fctx.clone().unwrap(), expr)?)
}
}
}
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth1//! jsonnet interpreter implementation2#![cfg_attr(feature = "nightly", feature(thread_local, type_alias_impl_trait))]3#![deny(unsafe_op_in_unsafe_fn)]4#![warn(5 clippy::all,6 clippy::nursery,7 clippy::pedantic,8 // missing_docs,9 elided_lifetimes_in_paths,10 explicit_outlives_requirements,11 noop_method_call,12 single_use_lifetimes,13 variant_size_differences,14 rustdoc::all15)]16#![allow(17 macro_expanded_macro_exports_accessed_by_absolute_paths,18 clippy::ptr_arg,19 // Too verbose20 clippy::must_use_candidate,21 // A lot of functions pass around errors thrown by code22 clippy::missing_errors_doc,23 // A lot of pointers have interior Rc24 clippy::needless_pass_by_value,25 // Its fine26 clippy::wildcard_imports,27 clippy::enum_glob_use,28 clippy::module_name_repetitions,29 // TODO: fix individual issues, however this works as intended almost everywhere30 clippy::cast_precision_loss,31 clippy::cast_possible_wrap,32 clippy::cast_possible_truncation,33 clippy::cast_sign_loss,34 // False positives35 // https://github.com/rust-lang/rust-clippy/issues/690236 clippy::use_self,37 // https://github.com/rust-lang/rust-clippy/issues/853938 clippy::iter_with_drain,39 // ci is being run with nightly, but library should work on stable40 clippy::missing_const_for_fn,41)]4243// For jrsonnet-macros44extern crate self as jrsonnet_evaluator;4546mod arr;47mod ctx;48mod dynamic;49pub mod error;50mod evaluate;51pub mod function;52pub mod gc;53mod import;54mod integrations;55pub mod manifest;56mod map;57mod obj;58pub mod stack;59pub mod stdlib;60mod tla;61pub mod trace;62pub mod typed;63pub mod val;6465use std::{66 any::Any,67 cell::{Ref, RefCell, RefMut},68 fmt::{self, Debug},69 path::Path,70};7172pub use ctx::*;73pub use dynamic::*;74pub use error::{Error, ErrorKind::*, Result, ResultExt};75pub use evaluate::*;76use function::CallLocation;77use gc::{GcHashMap, TraceBox};78use hashbrown::hash_map::RawEntryMut;79pub use import::*;80use jrsonnet_gcmodule::{Cc, Trace};81pub use jrsonnet_interner::{IBytes, IStr};82pub use jrsonnet_parser as parser;83use jrsonnet_parser::*;84pub use obj::*;85use stack::check_depth;86pub use tla::apply_tla;87pub use val::{Thunk, Val};8889/// Thunk without bound `super`/`this`90/// object inheritance may be overriden multiple times, and will be fixed only on field read91pub trait Unbound: Trace {92 /// Type of value after object context is bound93 type Bound;94 /// Create value bound to specified object context95 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;96}9798/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code99/// Standard jsonnet fields are always unbound100#[derive(Clone, Trace)]101pub enum MaybeUnbound {102 /// Value needs to be bound to `this`/`super`103 Unbound(Cc<TraceBox<dyn Unbound<Bound = Val>>>),104 /// Value is object-independent105 Bound(Thunk<Val>),106}107108impl Debug for MaybeUnbound {109 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {110 write!(f, "MaybeUnbound")111 }112}113impl MaybeUnbound {114 /// Attach object context to value, if required115 pub fn evaluate(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {116 match self {117 Self::Unbound(v) => v.bind(sup, this),118 Self::Bound(v) => Ok(v.evaluate()?),119 }120 }121}122123/// During import, this trait will be called to create initial context for file.124/// It may initialize global variables, stdlib for example.125pub trait ContextInitializer: Trace {126 /// Initialize default file context.127 fn initialize(&self, state: State, for_file: Source) -> Context;128 /// Allows upcasting from abstract to concrete context initializer.129 /// jrsonnet by itself doesn't use this method, it is allowed for it to panic.130 fn as_any(&self) -> &dyn Any;131}132133/// Context initializer which adds nothing.134#[derive(Trace)]135pub struct DummyContextInitializer;136impl ContextInitializer for DummyContextInitializer {137 fn initialize(&self, state: State, _for_file: Source) -> Context {138 ContextBuilder::new(state).build()139 }140 fn as_any(&self) -> &dyn Any {141 self142 }143}144145/// Dynamically reconfigurable evaluation settings146#[derive(Trace)]147pub struct EvaluationSettings {148 /// Context initializer, which will be used for imports and everything149 /// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`150 pub context_initializer: TraceBox<dyn ContextInitializer>,151 /// Used to resolve file locations/contents152 pub import_resolver: TraceBox<dyn ImportResolver>,153}154impl Default for EvaluationSettings {155 fn default() -> Self {156 Self {157 context_initializer: tb!(DummyContextInitializer),158 import_resolver: tb!(DummyImportResolver),159 }160 }161}162163#[derive(Trace)]164struct FileData {165 string: Option<IStr>,166 bytes: Option<IBytes>,167 parsed: Option<LocExpr>,168 evaluated: Option<Val>,169170 evaluating: bool,171}172impl FileData {173 fn new_string(data: IStr) -> Self {174 Self {175 string: Some(data),176 bytes: None,177 parsed: None,178 evaluated: None,179 evaluating: false,180 }181 }182 fn new_bytes(data: IBytes) -> Self {183 Self {184 string: None,185 bytes: Some(data),186 parsed: None,187 evaluated: None,188 evaluating: false,189 }190 }191}192193#[derive(Default, Trace)]194pub struct EvaluationStateInternals {195 /// Internal state196 file_cache: RefCell<GcHashMap<SourcePath, FileData>>,197 /// Settings, safe to change at runtime198 settings: RefCell<EvaluationSettings>,199}200201/// Maintains stack trace and import resolution202#[derive(Default, Clone, Trace)]203pub struct State(Cc<EvaluationStateInternals>);204205impl State {206 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise207 pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {208 let mut file_cache = self.file_cache();209 let mut file = file_cache.raw_entry_mut().from_key(&path);210211 let file = match file {212 RawEntryMut::Occupied(ref mut d) => d.get_mut(),213 RawEntryMut::Vacant(v) => {214 let data = self.settings().import_resolver.load_file_contents(&path)?;215 v.insert(216 path.clone(),217 FileData::new_string(218 std::str::from_utf8(&data)219 .map_err(|_| ImportBadFileUtf8(path.clone()))?220 .into(),221 ),222 )223 .1224 }225 };226 if let Some(str) = &file.string {227 return Ok(str.clone());228 }229 if file.string.is_none() {230 file.string = Some(231 file.bytes232 .as_ref()233 .expect("either string or bytes should be set")234 .clone()235 .cast_str()236 .ok_or_else(|| ImportBadFileUtf8(path.clone()))?,237 );238 }239 Ok(file.string.as_ref().expect("just set").clone())240 }241 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise242 pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {243 let mut file_cache = self.file_cache();244 let mut file = file_cache.raw_entry_mut().from_key(&path);245246 let file = match file {247 RawEntryMut::Occupied(ref mut d) => d.get_mut(),248 RawEntryMut::Vacant(v) => {249 let data = self.settings().import_resolver.load_file_contents(&path)?;250 v.insert(path.clone(), FileData::new_bytes(data.as_slice().into()))251 .1252 }253 };254 if let Some(str) = &file.bytes {255 return Ok(str.clone());256 }257 if file.bytes.is_none() {258 file.bytes = Some(259 file.string260 .as_ref()261 .expect("either string or bytes should be set")262 .clone()263 .cast_bytes(),264 );265 }266 Ok(file.bytes.as_ref().expect("just set").clone())267 }268 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise269 pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {270 let mut file_cache = self.file_cache();271 let mut file = file_cache.raw_entry_mut().from_key(&path);272273 let file = match file {274 RawEntryMut::Occupied(ref mut d) => d.get_mut(),275 RawEntryMut::Vacant(v) => {276 let data = self.settings().import_resolver.load_file_contents(&path)?;277 v.insert(278 path.clone(),279 FileData::new_string(280 std::str::from_utf8(&data)281 .map_err(|_| ImportBadFileUtf8(path.clone()))?282 .into(),283 ),284 )285 .1286 }287 };288 if let Some(val) = &file.evaluated {289 return Ok(val.clone());290 }291 if file.string.is_none() {292 file.string = Some(293 std::str::from_utf8(294 file.bytes295 .as_ref()296 .expect("either string or bytes should be set"),297 )298 .map_err(|_| ImportBadFileUtf8(path.clone()))?299 .into(),300 );301 }302 let code = file.string.as_ref().expect("just set");303 let file_name = Source::new(path.clone(), code.clone());304 if file.parsed.is_none() {305 file.parsed = Some(306 jrsonnet_parser::parse(307 code,308 &ParserSettings {309 source: file_name.clone(),310 },311 )312 .map_err(|e| ImportSyntaxError {313 path: file_name.clone(),314 error: Box::new(e),315 })?,316 );317 }318 let parsed = file.parsed.as_ref().expect("just set").clone();319 if file.evaluating {320 throw!(InfiniteRecursionDetected)321 }322 file.evaluating = true;323 // Dropping file cache guard here, as evaluation may use this map too324 drop(file_cache);325 let res = evaluate(self.create_default_context(file_name), &parsed);326327 let mut file_cache = self.file_cache();328 let mut file = file_cache.raw_entry_mut().from_key(&path);329330 let RawEntryMut::Occupied(file) = &mut file else {331 unreachable!("this file was just here!")332 };333 let file = file.get_mut();334 file.evaluating = false;335 match res {336 Ok(v) => {337 file.evaluated = Some(v.clone());338 Ok(v)339 }340 Err(e) => Err(e),341 }342 }343344 /// Has same semantics as `import 'path'` called from `from` file345 pub fn import_from(&self, from: &SourcePath, path: &str) -> Result<Val> {346 let resolved = self.resolve_from(from, path)?;347 self.import_resolved(resolved)348 }349 pub fn import(&self, path: impl AsRef<Path>) -> Result<Val> {350 let resolved = self.resolve(path)?;351 self.import_resolved(resolved)352 }353354 /// Creates context with all passed global variables355 pub fn create_default_context(&self, source: Source) -> Context {356 let context_initializer = &self.settings().context_initializer;357 context_initializer.initialize(self.clone(), source)358 }359360 /// Executes code creating a new stack frame361 pub fn push<T>(362 e: CallLocation<'_>,363 frame_desc: impl FnOnce() -> String,364 f: impl FnOnce() -> Result<T>,365 ) -> Result<T> {366 let _guard = check_depth()?;367368 f().with_description_src(e, frame_desc)369 }370371 /// Executes code creating a new stack frame372 pub fn push_val(373 &self,374 e: &ExprLocation,375 frame_desc: impl FnOnce() -> String,376 f: impl FnOnce() -> Result<Val>,377 ) -> Result<Val> {378 let _guard = check_depth()?;379380 f().with_description_src(e, frame_desc)381 }382 /// Executes code creating a new stack frame383 pub fn push_description<T>(384 frame_desc: impl FnOnce() -> String,385 f: impl FnOnce() -> Result<T>,386 ) -> Result<T> {387 let _guard = check_depth()?;388389 f().with_description(frame_desc)390 }391}392393/// Internals394impl State {395 fn file_cache(&self) -> RefMut<'_, GcHashMap<SourcePath, FileData>> {396 self.0.file_cache.borrow_mut()397 }398 pub fn settings(&self) -> Ref<'_, EvaluationSettings> {399 self.0.settings.borrow()400 }401 pub fn settings_mut(&self) -> RefMut<'_, EvaluationSettings> {402 self.0.settings.borrow_mut()403 }404}405406/// Raw methods evaluate passed values but don't perform TLA execution407impl State {408 /// Parses and evaluates the given snippet409 pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {410 let code = code.into();411 let source = Source::new_virtual(name.into(), code.clone());412 let parsed = jrsonnet_parser::parse(413 &code,414 &ParserSettings {415 source: source.clone(),416 },417 )418 .map_err(|e| ImportSyntaxError {419 path: source.clone(),420 error: Box::new(e),421 })?;422 evaluate(self.create_default_context(source), &parsed)423 }424}425426/// Settings utilities427impl State {428 // Only panics in case of [`ImportResolver`] contract violation429 #[allow(clippy::missing_panics_doc)]430 pub fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {431 self.import_resolver().resolve_from(from, path.as_ref())432 }433434 // Only panics in case of [`ImportResolver`] contract violation435 #[allow(clippy::missing_panics_doc)]436 pub fn resolve(&self, path: impl AsRef<Path>) -> Result<SourcePath> {437 self.import_resolver().resolve(path.as_ref())438 }439 pub fn import_resolver(&self) -> Ref<'_, dyn ImportResolver> {440 Ref::map(self.settings(), |s| &*s.import_resolver)441 }442 pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {443 self.settings_mut().import_resolver = TraceBox(resolver);444 }445 pub fn context_initializer(&self) -> Ref<'_, dyn ContextInitializer> {446 Ref::map(self.settings(), |s| &*s.context_initializer)447 }448}