git.delta.rocks / jrsonnet / refs/commits / 7c9753954b3b

difftreelog

source

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