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

View file

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

View file

@ -204,7 +204,7 @@ pub enum AtRulePrelude {
/// A @document rule, with its conditional. /// A @document rule, with its conditional.
Document(DocumentCondition), Document(DocumentCondition),
/// A @import rule prelude. /// 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. /// A @namespace rule prelude.
Namespace(Option<Prefix>, Namespace), Namespace(Option<Prefix>, Namespace),
/// A @layer rule prelude. /// 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::{DataValidity, DocumentStylesheetSet, SheetRebuildKind};
use crate::stylesheet_set::{DocumentStylesheetFlusher, SheetCollectionFlusher}; use crate::stylesheet_set::{DocumentStylesheetFlusher, SheetCollectionFlusher};
use crate::stylesheets::container_rule::ContainerCondition; use crate::stylesheets::container_rule::ContainerCondition;
use crate::stylesheets::import_rule::ImportLayer;
use crate::stylesheets::keyframes_rule::KeyframesAnimation; use crate::stylesheets::keyframes_rule::KeyframesAnimation;
use crate::stylesheets::layer_rule::{LayerName, LayerOrder}; use crate::stylesheets::layer_rule::{LayerName, LayerOrder};
use crate::stylesheets::viewport_rule::{self, MaybeNew, ViewportRule}; use crate::stylesheets::viewport_rule::{self, MaybeNew, ViewportRule};
@ -2944,8 +2945,10 @@ impl CascadeData {
self.effective_media_query_results self.effective_media_query_results
.saw_effective(import_rule); .saw_effective(import_rule);
} }
if let Some(ref layer) = import_rule.layer { match import_rule.layer {
maybe_register_layers(self, layer.name.as_ref(), containing_rule_state); 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) => { CssRule::Media(ref lock) => {