1use crate::{evaluate_add_op, LazyBinding, Result, Val};2use indexmap::IndexMap;3use jrsonnet_parser::Visibility;4use std::{5 cell::RefCell,6 collections::{BTreeMap, HashMap},7 fmt::Debug,8 rc::Rc,9};1011#[derive(Debug)]12pub struct ObjMember {13 pub add: bool,14 pub visibility: Visibility,15 pub invoke: LazyBinding,16}1718#[derive(Debug)]19pub struct ObjValueInternals {20 super_obj: Option<ObjValue>,21 this_entries: Rc<BTreeMap<Rc<str>, ObjMember>>,22 value_cache: RefCell<HashMap<Rc<str>, Val>>,23}24#[derive(Clone)]25pub struct ObjValue(pub(crate) Rc<ObjValueInternals>);26impl Debug for ObjValue {27 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {28 if let Some(super_obj) = self.0.super_obj.as_ref() {29 if f.alternate() {30 write!(f, "{:#?}", super_obj)?;31 } else {32 write!(f, "{:?}", super_obj)?;33 }34 write!(f, " + ")?;35 }36 let mut debug = f.debug_struct("ObjValue");37 for (name, member) in self.0.this_entries.iter() {38 debug.field(name, member);39 }40 debug.finish_non_exhaustive()41 }42}4344impl ObjValue {45 pub fn new(46 super_obj: Option<ObjValue>,47 this_entries: Rc<BTreeMap<Rc<str>, ObjMember>>,48 ) -> ObjValue {49 ObjValue(Rc::new(ObjValueInternals {50 super_obj,51 this_entries,52 value_cache: RefCell::new(HashMap::new()),53 }))54 }55 pub fn new_empty() -> ObjValue {56 Self::new(None, Rc::new(BTreeMap::new()))57 }58 pub fn with_super(&self, super_obj: ObjValue) -> ObjValue {59 match &self.0.super_obj {60 None => ObjValue::new(Some(super_obj), self.0.this_entries.clone()),61 Some(v) => ObjValue::new(Some(v.with_super(super_obj)), self.0.this_entries.clone()),62 }63 }64 pub fn enum_fields(&self, handler: &impl Fn(&Rc<str>, &Visibility)) {65 if let Some(s) = &self.0.super_obj {66 s.enum_fields(handler);67 }68 for (name, member) in self.0.this_entries.iter() {69 handler(&name, &member.visibility);70 }71 }72 pub fn fields_visibility(&self) -> IndexMap<Rc<str>, bool> {73 let out = Rc::new(RefCell::new(IndexMap::new()));74 self.enum_fields(&|name, visibility| {75 let mut out = out.borrow_mut();76 match visibility {77 Visibility::Normal => {78 if !out.contains_key(name) {79 out.insert(name.to_owned(), true);80 }81 }82 Visibility::Hidden => {83 out.insert(name.to_owned(), false);84 }85 Visibility::Unhide => {86 out.insert(name.to_owned(), true);87 }88 };89 });90 Rc::try_unwrap(out).unwrap().into_inner()91 }92 pub fn visible_fields(&self) -> Vec<Rc<str>> {93 self.fields_visibility()94 .into_iter()95 .filter(|(_k, v)| *v)96 .map(|(k, _)| k)97 .collect()98 }99 pub fn get(&self, key: Rc<str>) -> Result<Option<Val>> {100 if let Some(v) = self.0.value_cache.borrow().get(&key) {101 return Ok(Some(v.clone()));102 }103 if let Some(v) = self.get_raw(&key, self)? {104 let v = v.unwrap_if_lazy()?;105 self.0.value_cache.borrow_mut().insert(key, v.clone());106 Ok(Some(v))107 } else {108 Ok(None)109 }110 }111 pub(crate) fn get_raw(&self, key: &str, real_this: &ObjValue) -> Result<Option<Val>> {112 match (self.0.this_entries.get(key), &self.0.super_obj) {113 (Some(k), None) => Ok(Some(114 k.invoke115 .evaluate(Some(real_this.clone()), self.0.super_obj.clone())?116 .evaluate()?,117 )),118 (Some(k), Some(s)) => {119 let lazy = k120 .invoke121 .evaluate(Some(real_this.clone()), self.0.super_obj.clone())?;122 let our = lazy.evaluate()?;123 if k.add {124 s.get_raw(key, real_this)?125 .map_or(Ok(Some(our.clone())), |v| {126 Ok(Some(evaluate_add_op(&v, &our)?))127 })128 } else {129 Ok(Some(our))130 }131 }132 (None, Some(s)) => s.get_raw(key, real_this),133 (None, None) => Ok(None),134 }135 }136}137impl PartialEq for ObjValue {138 fn eq(&self, other: &Self) -> bool {139 Rc::ptr_eq(&self.0, &other.0)140 }141}