From aaa966d9d6ae70c4b8a62bb5e3a14c068bb7dff0 Mon Sep 17 00:00:00 2001 From: Zach Hoffman Date: Tue, 14 Nov 2023 07:46:37 -0700 Subject: [PATCH] Replace TokenSerializationType with TokenSerializationTypeVariants (#369) * Replace TokenSerializationType with TokenSerializationTypeVariants Making TokenSerializationType public is useful to describe a CSS string that is already known to be valid in cases when the TokenSerializationTypes of the start and end of the CSS string is needed, but Tokens are not. This helps with creating a custom_properties::VariableValue from the CSS string of a computed value for CSS Properties and Values, see Mozilla bug 1858305 [1]. [1]: https://bugzilla.mozilla.org/show_bug.cgi?id=1858305 * Derive Default trait for TokenSerializationType * Deprecate TokenSerializationType::nothing() --- src/serializer.rs | 160 ++++++++++++++++++++++++++++++++-------------- src/tests.rs | 2 +- 2 files changed, 112 insertions(+), 50 deletions(-) diff --git a/src/serializer.rs b/src/serializer.rs index 19f01456..09c22402 100644 --- a/src/serializer.rs +++ b/src/serializer.rs @@ -378,19 +378,110 @@ impl_tocss_for_float!(f32); impl_tocss_for_float!(f64); /// A category of token. See the `needs_separator_when_before` method. -#[derive(Copy, Clone, Eq, PartialEq, Debug)] -pub struct TokenSerializationType(TokenSerializationTypeVariants); +#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)] +pub enum TokenSerializationType { + /// No token serialization type. + #[default] + Nothing, + + /// The [``](https://drafts.csswg.org/css-syntax/#whitespace-token-diagram) + /// type. + WhiteSpace, + + /// The [``](https://drafts.csswg.org/css-syntax/#at-keyword-token-diagram) + /// type, the "[``](https://drafts.csswg.org/css-syntax/#hash-token-diagram) with + /// the type flag set to 'unrestricted'" type, or the + /// "[``](https://drafts.csswg.org/css-syntax/#hash-token-diagram) with the type + /// flag set to 'id'" type. + AtKeywordOrHash, + + /// The [``](https://drafts.csswg.org/css-syntax/#number-token-diagram) type. + Number, + + /// The [``](https://drafts.csswg.org/css-syntax/#dimension-token-diagram) + /// type. + Dimension, + + /// The [``](https://drafts.csswg.org/css-syntax/#percentage-token-diagram) + /// type. + Percentage, + + /// The [``](https://drafts.csswg.org/css-syntax/#url-token-diagram) or + /// `` type. + UrlOrBadUrl, + + /// The [``](https://drafts.csswg.org/css-syntax/#function-token-diagram) type. + Function, + + /// The [``](https://drafts.csswg.org/css-syntax/#ident-token-diagram) type. + Ident, + + /// The `-->` [``](https://drafts.csswg.org/css-syntax/#CDC-token-diagram) type. + CDC, + + /// The `|=` + /// [``](https://drafts.csswg.org/css-syntax/#dash-match-token-diagram) type. + DashMatch, + + /// The `*=` + /// [``](https://drafts.csswg.org/css-syntax/#substring-match-token-diagram) + /// type. + SubstringMatch, + + /// The `<(-token>` type. + OpenParen, + + /// The `#` `` type. + DelimHash, + + /// The `@` `` type. + DelimAt, + + /// The `.` or `+` `` type. + DelimDotOrPlus, + + /// The `-` `` type. + DelimMinus, + + /// The `?` `` type. + DelimQuestion, + + /// The `$`, `^`, or `~` `` type. + DelimAssorted, + + /// The `=` `` type. + DelimEquals, + + /// The `|` `` type. + DelimBar, + + /// The `/` `` type. + DelimSlash, + + /// The `*` `` type. + DelimAsterisk, + + /// The `%` `` type. + DelimPercent, + + /// A type indicating any other token. + Other, +} impl TokenSerializationType { /// Return a value that represents the absence of a token, e.g. before the start of the input. + #[deprecated( + since = "0.32.1", + note = "use TokenSerializationType::Nothing or TokenSerializationType::default() instead" + )] pub fn nothing() -> TokenSerializationType { - TokenSerializationType(TokenSerializationTypeVariants::Nothing) + Default::default() } - /// If this value is `TokenSerializationType::nothing()`, set it to the given value instead. + /// If this value is `TokenSerializationType::Nothing`, set it to the given value instead. pub fn set_if_nothing(&mut self, new_value: TokenSerializationType) { - if self.0 == TokenSerializationTypeVariants::Nothing { - self.0 = new_value.0 + if matches!(self, TokenSerializationType::Nothing) { + *self = new_value } } @@ -404,10 +495,10 @@ impl TokenSerializationType { /// See https://github.com/w3c/csswg-drafts/issues/4088 for the /// `DelimPercent` bits. pub fn needs_separator_when_before(self, other: TokenSerializationType) -> bool { - use self::TokenSerializationTypeVariants::*; - match self.0 { + use self::TokenSerializationType::*; + match self { Ident => matches!( - other.0, + other, Ident | Function | UrlOrBadUrl @@ -419,15 +510,15 @@ impl TokenSerializationType { | OpenParen ), AtKeywordOrHash | Dimension => matches!( - other.0, + other, Ident | Function | UrlOrBadUrl | DelimMinus | Number | Percentage | Dimension | CDC ), DelimHash | DelimMinus => matches!( - other.0, + other, Ident | Function | UrlOrBadUrl | DelimMinus | Number | Percentage | Dimension ), Number => matches!( - other.0, + other, Ident | Function | UrlOrBadUrl @@ -437,11 +528,11 @@ impl TokenSerializationType { | DelimPercent | Dimension ), - DelimAt => matches!(other.0, Ident | Function | UrlOrBadUrl | DelimMinus), - DelimDotOrPlus => matches!(other.0, Number | Percentage | Dimension), - DelimAssorted | DelimAsterisk => matches!(other.0, DelimEquals), - DelimBar => matches!(other.0, DelimEquals | DelimBar | DashMatch), - DelimSlash => matches!(other.0, DelimAsterisk | SubstringMatch), + DelimAt => matches!(other, Ident | Function | UrlOrBadUrl | DelimMinus), + DelimDotOrPlus => matches!(other, Number | Percentage | Dimension), + DelimAssorted | DelimAsterisk => matches!(other, DelimEquals), + DelimBar => matches!(other, DelimEquals | DelimBar | DashMatch), + DelimSlash => matches!(other, DelimAsterisk | SubstringMatch), Nothing | WhiteSpace | Percentage | UrlOrBadUrl | Function | CDC | OpenParen | DashMatch | SubstringMatch | DelimQuestion | DelimEquals | DelimPercent | Other => { false @@ -450,43 +541,14 @@ impl TokenSerializationType { } } -#[derive(Copy, Clone, Eq, PartialEq, Debug)] -enum TokenSerializationTypeVariants { - Nothing, - WhiteSpace, - AtKeywordOrHash, - Number, - Dimension, - Percentage, - UrlOrBadUrl, - Function, - Ident, - CDC, - DashMatch, - SubstringMatch, - OpenParen, // '(' - DelimHash, // '#' - DelimAt, // '@' - DelimDotOrPlus, // '.', '+' - DelimMinus, // '-' - DelimQuestion, // '?' - DelimAssorted, // '$', '^', '~' - DelimEquals, // '=' - DelimBar, // '|' - DelimSlash, // '/' - DelimAsterisk, // '*' - DelimPercent, // '%' - Other, // anything else -} - impl<'a> Token<'a> { /// Categorize a token into a type that determines when `/**/` needs to be inserted /// between two tokens when serialized next to each other without whitespace in between. /// /// See the `TokenSerializationType::needs_separator_when_before` method. pub fn serialization_type(&self) -> TokenSerializationType { - use self::TokenSerializationTypeVariants::*; - TokenSerializationType(match *self { + use self::TokenSerializationType::*; + match self { Token::Ident(_) => Ident, Token::AtKeyword(_) | Token::Hash(_) | Token::IDHash(_) => AtKeywordOrHash, Token::UnquotedUrl(_) | Token::BadUrl(_) => UrlOrBadUrl, @@ -526,6 +588,6 @@ impl<'a> Token<'a> { | Token::IncludeMatch | Token::PrefixMatch | Token::SuffixMatch => Other, - }) + } } } diff --git a/src/tests.rs b/src/tests.rs index d4bc5f51..f9dea193 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -466,7 +466,7 @@ fn serializer(preserve_comments: bool) { } let mut serialized = String::new(); write_to( - TokenSerializationType::nothing(), + TokenSerializationType::Nothing, input, &mut serialized, preserve_comments,