git.delta.rocks / jrsonnet / refs/commits / 56fe3090ef64

difftreelog

source

crates/jsonnet-evaluator/src/obj.rs2.9 KiBsourcehistory
1use crate::{evaluate_binary_op, Binding, Val};2use jsonnet_parser::{BinaryOpType, Visibility};3use std::{4	cell::RefCell,5	collections::{BTreeMap, BTreeSet, HashMap},6	fmt::Debug,7	rc::Rc,8};910#[derive(Debug)]11pub struct ObjMember {12	pub add: bool,13	pub visibility: Visibility,14	pub invoke: Binding,15}1617#[derive(Debug)]18pub struct ObjValueInternals {19	super_obj: Option<ObjValue>,20	this_entries: Rc<BTreeMap<String, ObjMember>>,21	value_cache: RefCell<HashMap<String, Val>>,22}23pub struct ObjValue(pub(crate) Rc<ObjValueInternals>);24impl Debug for ObjValue {25	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {26		if let Some(super_obj) = self.0.super_obj.as_ref() {27			if f.alternate() {28				write!(f, "{:#?}", super_obj)?;29			} else {30				write!(f, "{:?}", super_obj)?;31			}32			write!(f, " + ")?;33		}34		let mut debug = f.debug_struct("ObjValue");35		debug.field("$ptr", &Rc::as_ptr(&self.0));36		for (name, member) in self.0.this_entries.iter() {37			debug.field(name, member);38		}39		debug.finish_non_exhaustive()40		// .field("fields", &self.fields())41		// .finish_non_exhaustive()42	}43}4445impl ObjValue {46	pub fn new(47		super_obj: Option<ObjValue>,48		this_entries: Rc<BTreeMap<String, ObjMember>>,49	) -> ObjValue {50		ObjValue(Rc::new(ObjValueInternals {51			super_obj,52			this_entries,53			value_cache: RefCell::new(HashMap::new()),54		}))55	}56	pub fn with_super(&self, super_obj: ObjValue) -> ObjValue {57		match &self.0.super_obj {58			None => ObjValue::new(Some(super_obj), self.0.this_entries.clone()),59			Some(v) => ObjValue::new(Some(v.with_super(super_obj)), self.0.this_entries.clone()),60		}61	}62	pub fn fields(&self) -> BTreeSet<String> {63		let mut fields = BTreeSet::new();64		self.0.this_entries.keys().for_each(|k| {65			fields.insert(k.clone());66		});67		if self.0.super_obj.is_some() {68			for field in self.0.super_obj.clone().unwrap().fields() {69				fields.insert(field);70			}71		}72		fields73	}74	pub fn get(&self, key: &str) -> Option<Val> {75		if let Some(v) = self.0.value_cache.borrow().get(key) {76			return Some(v.clone());77		}78		let v = self.get_raw(key, self).map(|v| v.unwrap_if_lazy());79		if let Some(v) = v.clone() {80			self.0.value_cache.borrow_mut().insert(key.to_owned(), v);81		}82		v83	}84	fn get_raw(&self, key: &str, real_this: &ObjValue) -> Option<Val> {85		match (self.0.this_entries.get(key), &self.0.super_obj) {86			(Some(k), None) => Some(k.invoke.0(87				Some(real_this.clone()),88				self.0.super_obj.clone(),89			)),90			(Some(k), Some(s)) => {91				let our = k.invoke.0(Some(real_this.clone()), self.0.super_obj.clone());92				if k.add {93					s.get_raw(key, real_this).map_or(Some(our.clone()), |v| {94						Some(evaluate_binary_op(&v, BinaryOpType::Add, &our))95					})96				} else {97					Some(our)98				}99			}100			(None, Some(s)) => s.get_raw(key, real_this),101			(None, None) => None,102		}103	}104}105impl Clone for ObjValue {106	fn clone(&self) -> Self {107		ObjValue(self.0.clone())108	}109}110impl PartialEq for ObjValue {111	fn eq(&self, other: &Self) -> bool {112		Rc::ptr_eq(&self.0, &other.0)113	}114}