style: Refactor ImportLayer into enum

Refactored ImportLayer into an enum instead of a struct and using
Option everywhere.

Differential Revision: https://phabricator.services.mozilla.com/D176793
This commit is contained in:
CanadaHonk 2023-05-01 21:56:33 +00:00 committed by Martin Robinson
parent 2e713d4366
commit a5b85ec834
4 changed files with 28 additions and 18 deletions

View file

@ -169,11 +169,17 @@ impl DeepCloneWithLock for ImportSheet {
}
}
/// The layer keyword or function in an import rule.
/// The layer specified in an import rule (can be none, anonymous, or named).
#[derive(Debug, Clone)]
pub struct ImportLayer {
/// The layer name, or None for an anonymous layer.
pub name: Option<LayerName>,
pub enum ImportLayer {
/// No layer specified
None,
/// Anonymous layer (`layer`)
Anonymous,
/// Named layer (`layer(name)`)
Named(LayerName),
}
/// The supports condition in an import rule.
@ -191,9 +197,10 @@ impl ToCss for ImportLayer {
where
W: Write,
{
match self.name {
None => dest.write_str("layer"),
Some(ref name) => {
match *self {
ImportLayer::None => Ok(()),
ImportLayer::Anonymous => dest.write_str("layer"),
ImportLayer::Named(ref name) => {
dest.write_str("layer(")?;
name.to_css(dest)?;
dest.write_char(')')
@ -219,7 +226,7 @@ pub struct ImportRule {
pub supports: Option<ImportSupportsCondition>,
/// A `layer()` function name.
pub layer: Option<ImportLayer>,
pub layer: ImportLayer,
/// The line and column of the rule's source code.
pub source_location: SourceLocation,
@ -238,21 +245,21 @@ impl ImportRule {
input: &mut Parser<'i, 't>,
context: &ParserContext,
namespaces: &Namespaces,
) -> (Option<ImportLayer>, Option<ImportSupportsCondition>) {
) -> (ImportLayer, Option<ImportSupportsCondition>) {
let layer = if input
.try_parse(|input| input.expect_ident_matching("layer"))
.is_ok()
{
Some(ImportLayer { name: None })
ImportLayer::Anonymous
} else {
input
.try_parse(|input| {
input.expect_function_matching("layer")?;
input
.parse_nested_block(|input| LayerName::parse(context, input))
.map(|name| ImportLayer { name: Some(name) })
.map(|name| ImportLayer::Named(name))
})
.ok()
.ok().unwrap_or(ImportLayer::None)
};
#[cfg(feature = "gecko")]
@ -308,9 +315,9 @@ impl ToCssWithGuard for ImportRule {
dest.write_str("@import ")?;
self.url.to_css(&mut CssWriter::new(dest))?;
if let Some(ref layer) = self.layer {
if !matches!(self.layer, ImportLayer::None) {
dest.write_char(' ')?;
layer.to_css(&mut CssWriter::new(dest))?;
self.layer.to_css(&mut CssWriter::new(dest))?;
}
if let Some(ref supports) = self.supports {

View file

@ -26,6 +26,6 @@ pub trait StylesheetLoader {
lock: &SharedRwLock,
media: Arc<Locked<MediaList>>,
supports: Option<ImportSupportsCondition>,
layer: Option<ImportLayer>,
layer: ImportLayer,
) -> Arc<Locked<ImportRule>>;
}

View file

@ -204,7 +204,7 @@ pub enum AtRulePrelude {
/// A @document rule, with its conditional.
Document(DocumentCondition),
/// A @import rule prelude.
Import(CssUrl, Arc<Locked<MediaList>>, Option<ImportSupportsCondition>, Option<ImportLayer>),
Import(CssUrl, Arc<Locked<MediaList>>, Option<ImportSupportsCondition>, ImportLayer),
/// A @namespace rule prelude.
Namespace(Option<Prefix>, Namespace),
/// A @layer rule prelude.

View file

@ -28,6 +28,7 @@ use crate::shared_lock::{Locked, SharedRwLockReadGuard, StylesheetGuards};
use crate::stylesheet_set::{DataValidity, DocumentStylesheetSet, SheetRebuildKind};
use crate::stylesheet_set::{DocumentStylesheetFlusher, SheetCollectionFlusher};
use crate::stylesheets::container_rule::ContainerCondition;
use crate::stylesheets::import_rule::ImportLayer;
use crate::stylesheets::keyframes_rule::KeyframesAnimation;
use crate::stylesheets::layer_rule::{LayerName, LayerOrder};
use crate::stylesheets::viewport_rule::{self, MaybeNew, ViewportRule};
@ -2944,8 +2945,10 @@ impl CascadeData {
self.effective_media_query_results
.saw_effective(import_rule);
}
if let Some(ref layer) = import_rule.layer {
maybe_register_layers(self, layer.name.as_ref(), containing_rule_state);
match import_rule.layer {
ImportLayer::Named(ref name) => maybe_register_layers(self, Some(name), containing_rule_state),
ImportLayer::Anonymous => maybe_register_layers(self, None, containing_rule_state),
ImportLayer::None => {},
}
},
CssRule::Media(ref lock) => {