style: Introduce CustomPropertiesBuilder.

I'm about to introduce more state here to implement optimizations for custom
property cascading, so this abstraction is useful to encapsulate that state.
This commit is contained in:
Emilio Cobos Álvarez 2017-10-08 14:58:14 +02:00
parent 47efcd5e52
commit d0f080d5dd
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
4 changed files with 73 additions and 63 deletions

View file

@ -457,9 +457,59 @@ fn parse_var_function<'i, 't>(
Ok(())
}
/// A struct that takes care of encapsulating the cascade process for custom
/// properties.
pub struct CustomPropertiesBuilder<'a> {
seen: PrecomputedHashSet<&'a Name>,
custom_properties: Option<OrderedMap<&'a Name, BorrowedSpecifiedValue<'a>>>,
inherited: Option<&'a Arc<CustomPropertiesMap>>,
}
impl<'a> CustomPropertiesBuilder<'a> {
/// Create a new builder, inheriting from a given custom properties map.
pub fn new(inherited: Option<&'a Arc<CustomPropertiesMap>>) -> Self {
Self {
seen: PrecomputedHashSet::default(),
custom_properties: None,
inherited,
}
}
/// Cascade a given custom property declaration.
pub fn cascade(
&mut self,
name: &'a Name,
specified_value: DeclaredValue<'a, Box<SpecifiedValue>>,
) {
cascade(
&mut self.custom_properties,
self.inherited,
&mut self.seen,
name,
specified_value,
)
}
/// Returns the final map of applicable custom properties.
///
/// If there was any specified property, we've created a new map and now we need
/// to remove any potential cycles, and wrap it in an arc.
///
/// Otherwise, just use the inherited custom properties map.
pub fn build(mut self) -> Option<Arc<CustomPropertiesMap>> {
let mut map = match self.custom_properties.take() {
Some(map) => map,
None => return self.inherited.cloned(),
};
remove_cycles(&mut map);
Some(Arc::new(substitute_all(map)))
}
}
/// Add one custom property declaration to a map, unless another with the same
/// name was already there.
pub fn cascade<'a>(
fn cascade<'a>(
custom_properties: &mut Option<OrderedMap<&'a Name, BorrowedSpecifiedValue<'a>>>,
inherited: Option<&'a Arc<CustomPropertiesMap>>,
seen: &mut PrecomputedHashSet<&'a Name>,
@ -509,24 +559,6 @@ pub fn cascade<'a>(
}
}
/// Returns the final map of applicable custom properties.
///
/// If there was any specified property, we've created a new map and now we need
/// to remove any potential cycles, and wrap it in an arc.
///
/// Otherwise, just use the inherited custom properties map.
pub fn finish_cascade(
specified_values_map: Option<OrderedMap<&Name, BorrowedSpecifiedValue>>,
inherited: Option<&Arc<CustomPropertiesMap>>,
) -> Option<Arc<CustomPropertiesMap>> {
if let Some(mut map) = specified_values_map {
remove_cycles(&mut map);
Some(Arc::new(substitute_all(map)))
} else {
inherited.cloned()
}
}
/// https://drafts.csswg.org/css-variables/#cycles
///
/// The initial value of a custom property is represented by this property not