difftreelog
refactor reuse code between ObjValue::get and get_for
in: master
1 file changed
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth17 operator::evaluate_add_op,17 operator::evaluate_add_op,18 tb, throw,18 tb, throw,19 val::ThunkValue,19 val::ThunkValue,20 MaybeUnbound, Result, State, Thunk, Unbound, Val,20 MaybeUnbound, Result, ResultExt, State, Thunk, Unbound, Val,21};21};222223#[cfg(not(feature = "exp-preserve-order"))]23#[cfg(not(feature = "exp-preserve-order"))]413 }))413 }))414 }414 }415 pub fn get(&self, key: IStr) -> Result<Option<Val>> {415 pub fn get(&self, key: IStr) -> Result<Option<Val>> {416 self.run_assertions()?;417 let cache_key = (key.clone(), None);418 if let Some(v) = self.0.value_cache.borrow().get(&cache_key) {419 return Ok(match v {420 CacheValue::Cached(v) => Some(v.clone()),421 CacheValue::NotFound => None,422 CacheValue::Pending => throw!(InfiniteRecursionDetected),423 CacheValue::Errored(e) => return Err(e.clone()),424 });425 }426 self.0427 .value_cache428 .borrow_mut()429 .insert(cache_key.clone(), CacheValue::Pending);430 let value = self416 self.get_for(key, self.0.this.clone().unwrap_or_else(|| self.clone()))431 .get_raw(key, self.0.this.clone().unwrap_or_else(|| self.clone()))432 .map_err(|e| {433 self.0434 .value_cache435 .borrow_mut()436 .insert(cache_key.clone(), CacheValue::Errored(e.clone()));437 e438 })?;439 self.0.value_cache.borrow_mut().insert(440 cache_key,441 value442 .as_ref()443 .map_or(CacheValue::NotFound, |v| CacheValue::Cached(v.clone())),444 );445 Ok(value)446 }417 }447 pub fn get_for(&self, key: IStr, this: Self) -> Result<Option<Val>> {418 pub fn get_for(&self, key: IStr, this: Self) -> Result<Option<Val>> {448 self.run_assertions()?;419 self.run_assertions()?;449 let cache_key = (key.clone(), Some(this.clone().downgrade()));420 let cache_key = (421 key.clone(),422 (!ObjValue::ptr_eq(&this, self)).then(|| this.clone().downgrade()),423 );450 if let Some(v) = self.0.value_cache.borrow().get(&cache_key) {424 if let Some(v) = self.0.value_cache.borrow().get(&cache_key) {451 return Ok(match v {425 return Ok(match v {