diff --git a/rsjsonnet-lang/src/program/data.rs b/rsjsonnet-lang/src/program/data.rs index cdf7685..38178a0 100644 --- a/rsjsonnet-lang/src/program/data.rs +++ b/rsjsonnet-lang/src/program/data.rs @@ -500,6 +500,15 @@ impl<'p> ObjectData<'p> { }) } + #[inline] + pub(super) fn get_visible_fields_order( + &self, + ) -> impl DoubleEndedIterator> + '_ { + self.get_fields_order() + .iter() + .filter_map(|&(name, visible)| visible.then_some(name)) + } + pub(super) fn field_is_visible(&self, name: InternedStr<'p>) -> bool { if let Some(field) = self.self_layer.fields.get(&name) { match field.visibility { diff --git a/rsjsonnet-lang/src/program/eval/manifest.rs b/rsjsonnet-lang/src/program/eval/manifest.rs index 03e615f..cc3653d 100644 --- a/rsjsonnet-lang/src/program/eval/manifest.rs +++ b/rsjsonnet-lang/src/program/eval/manifest.rs @@ -60,12 +60,8 @@ impl<'p> Evaluator<'_, 'p> { }; let object = object.view(); - let visible_fields: Vec<_> = object - .get_fields_order() - .iter() - .filter_map(|&(name, visible)| visible.then_some(name)) - .collect(); - for &field_name in visible_fields.iter().rev() { + let visible_fields = object.get_visible_fields_order(); + for field_name in visible_fields.rev() { let field_thunk = self .program .find_object_field_thunk(&object, 0, field_name) @@ -141,11 +137,7 @@ impl<'p> Evaluator<'_, 'p> { } ValueData::Object(object) => { let object = object.view(); - let visible_fields: Vec<_> = object - .get_fields_order() - .iter() - .filter_map(|&(name, visible)| visible.then_some(name)) - .collect(); + let visible_fields: Vec<_> = object.get_visible_fields_order().collect(); if visible_fields.is_empty() { result.push_str("{}"); } else { @@ -253,11 +245,7 @@ impl<'p> Evaluator<'_, 'p> { } ValueData::Object(object) => { let object = object.view(); - let visible_fields: Vec<_> = object - .get_fields_order() - .iter() - .filter_map(|&(name, visible)| visible.then_some(name)) - .collect(); + let visible_fields: Vec<_> = object.get_visible_fields_order().collect(); if visible_fields.is_empty() { if let Some(ref empty_object) = format.empty_object { result.push_str(empty_object); @@ -427,11 +415,7 @@ impl<'p> Evaluator<'_, 'p> { } ValueData::Object(object) => { let object = object.view(); - let visible_fields: Vec<_> = object - .get_fields_order() - .iter() - .filter_map(|&(name, visible)| visible.then_some(name)) - .collect(); + let visible_fields: Vec<_> = object.get_visible_fields_order().collect(); if visible_fields.is_empty() { if parent_is_array || parent_is_object { result.push(' '); @@ -538,12 +522,8 @@ impl<'p> Evaluator<'_, 'p> { indent, }); - let visible_fields: Vec<_> = object - .get_fields_order() - .iter() - .filter_map(|&(name, visible)| visible.then_some(name)) - .collect(); - for &field_name in visible_fields.iter().rev() { + let visible_fields = object.get_visible_fields_order(); + for field_name in visible_fields.rev() { let field_thunk = self .program .find_object_field_thunk(&object, 0, field_name) @@ -563,10 +543,8 @@ impl<'p> Evaluator<'_, 'p> { indent: Rc, ) { let visible_fields: Vec<_> = object - .get_fields_order() - .iter() - .filter(|&&(_, visible)| visible) - .map(|&(name, _)| { + .get_visible_fields_order() + .map(|name| { // Check if the field is an object or an array of objects let (_, field) = object.find_field(0, name).unwrap(); let field_value = field.thunk.get().unwrap().view().get_value().unwrap(); @@ -785,11 +763,7 @@ impl<'p> Evaluator<'_, 'p> { } ValueData::Object(object) => { let object = object.view(); - let visible_fields: Vec<_> = object - .get_fields_order() - .iter() - .filter_map(|&(name, visible)| visible.then_some(name)) - .collect(); + let visible_fields: Vec<_> = object.get_visible_fields_order().collect(); if visible_fields.is_empty() { result.push_str("{ }"); } else { diff --git a/rsjsonnet-lang/src/program/eval/mod.rs b/rsjsonnet-lang/src/program/eval/mod.rs index 3f13c1d..5410a0f 100644 --- a/rsjsonnet-lang/src/program/eval/mod.rs +++ b/rsjsonnet-lang/src/program/eval/mod.rs @@ -997,20 +997,8 @@ impl<'p, 'a> Evaluator<'a, 'p> { (ValueData::Object(lhs), ValueData::Object(rhs)) => { let lhs = lhs.view(); let rhs = rhs.view(); - let lhs_fields: Vec<_> = lhs - .get_fields_order() - .iter() - .rev() - .filter_map(|(name, visible)| visible.then_some(name)) - .cloned() - .collect(); - let rhs_fields: Vec<_> = rhs - .get_fields_order() - .iter() - .rev() - .filter_map(|(name, visible)| visible.then_some(name)) - .cloned() - .collect(); + let lhs_fields: Vec<_> = lhs.get_visible_fields_order().rev().collect(); + let rhs_fields: Vec<_> = rhs.get_visible_fields_order().rev().collect(); if lhs_fields == rhs_fields { let mut fields = lhs_fields; diff --git a/rsjsonnet-lang/src/program/eval/stdlib.rs b/rsjsonnet-lang/src/program/eval/stdlib.rs index c4c553f..5a424be 100644 --- a/rsjsonnet-lang/src/program/eval/stdlib.rs +++ b/rsjsonnet-lang/src/program/eval/stdlib.rs @@ -88,11 +88,7 @@ impl<'p> Evaluator<'_, 'p> { ValueData::Array(array) => array.view().len(), ValueData::Object(object) => { let object = object.view(); - object - .get_fields_order() - .iter() - .filter_map(|(name, visible)| visible.then_some(name)) - .count() + object.get_visible_fields_order().count() } ValueData::Function(func) => func.view().params.order.len(), _ => { @@ -129,10 +125,7 @@ impl<'p> Evaluator<'_, 'p> { } ValueData::Object(object) => { let object = object.view(); - let visible_fields = object - .get_fields_order() - .iter() - .filter_map(|&(name, visible)| visible.then_some(name)); + let visible_fields = object.get_visible_fields_order(); self.object_stack .push(ObjectData::new_simple(FHashMap::default())); @@ -1043,12 +1036,8 @@ impl<'p> Evaluator<'_, 'p> { }; let object = object.view(); - let visible_fields: Vec<_> = object - .get_fields_order() - .iter() - .filter_map(|&(name, visible)| visible.then_some(name)) - .collect(); - for &field_name in visible_fields.iter().rev() { + let visible_fields = object.get_visible_fields_order(); + for field_name in visible_fields.rev() { let field_thunk = self .program .find_object_field_thunk(&object, 0, field_name) @@ -1078,13 +1067,9 @@ impl<'p> Evaluator<'_, 'p> { self.string_stack.push(String::new()); self.state_stack.push(State::StringToValue); - let visible_fields: Vec<_> = object - .get_fields_order() - .iter() - .filter_map(|&(name, visible)| visible.then_some(name)) - .collect(); + let visible_fields = object.get_visible_fields_order(); - for &field_name in visible_fields.iter().rev() { + for field_name in visible_fields.rev() { self.state_stack.push(State::AppendToString('\n'.into())); self.push_trace_item(TraceItem::ManifestObjectField { name: field_name }); @@ -1271,13 +1256,9 @@ impl<'p> Evaluator<'_, 'p> { self.state_stack.push(State::AppendToString('>'.into())); let object = object.view(); - let visible_fields: Vec<_> = object - .get_fields_order() - .iter() - .filter_map(|&(name, visible)| visible.then_some(name)) - .collect(); + let visible_fields = object.get_visible_fields_order(); - for &field_name in visible_fields.iter().rev() { + for field_name in visible_fields.rev() { let field_thunk = self .program .find_object_field_thunk(&object, 0, field_name)