Skip to content

Commit aaa966d

Browse files
authored
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()
1 parent cab31ea commit aaa966d

File tree

2 files changed

+112
-50
lines changed

2 files changed

+112
-50
lines changed

src/serializer.rs

+111-49
Original file line numberDiff line numberDiff line change
@@ -378,19 +378,110 @@ impl_tocss_for_float!(f32);
378378
impl_tocss_for_float!(f64);
379379

380380
/// A category of token. See the `needs_separator_when_before` method.
381-
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
382-
pub struct TokenSerializationType(TokenSerializationTypeVariants);
381+
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]
382+
pub enum TokenSerializationType {
383+
/// No token serialization type.
384+
#[default]
385+
Nothing,
386+
387+
/// The [`<whitespace-token>`](https://drafts.csswg.org/css-syntax/#whitespace-token-diagram)
388+
/// type.
389+
WhiteSpace,
390+
391+
/// The [`<at-keyword-token>`](https://drafts.csswg.org/css-syntax/#at-keyword-token-diagram)
392+
/// type, the "[`<hash-token>`](https://drafts.csswg.org/css-syntax/#hash-token-diagram) with
393+
/// the type flag set to 'unrestricted'" type, or the
394+
/// "[`<hash-token>`](https://drafts.csswg.org/css-syntax/#hash-token-diagram) with the type
395+
/// flag set to 'id'" type.
396+
AtKeywordOrHash,
397+
398+
/// The [`<number-token>`](https://drafts.csswg.org/css-syntax/#number-token-diagram) type.
399+
Number,
400+
401+
/// The [`<dimension-token>`](https://drafts.csswg.org/css-syntax/#dimension-token-diagram)
402+
/// type.
403+
Dimension,
404+
405+
/// The [`<percentage-token>`](https://drafts.csswg.org/css-syntax/#percentage-token-diagram)
406+
/// type.
407+
Percentage,
408+
409+
/// The [`<url-token>`](https://drafts.csswg.org/css-syntax/#url-token-diagram) or
410+
/// `<bad-url-token>` type.
411+
UrlOrBadUrl,
412+
413+
/// The [`<function-token>`](https://drafts.csswg.org/css-syntax/#function-token-diagram) type.
414+
Function,
415+
416+
/// The [`<ident-token>`](https://drafts.csswg.org/css-syntax/#ident-token-diagram) type.
417+
Ident,
418+
419+
/// The `-->` [`<CDC-token>`](https://drafts.csswg.org/css-syntax/#CDC-token-diagram) type.
420+
CDC,
421+
422+
/// The `|=`
423+
/// [`<dash-match-token>`](https://drafts.csswg.org/css-syntax/#dash-match-token-diagram) type.
424+
DashMatch,
425+
426+
/// The `*=`
427+
/// [`<substring-match-token>`](https://drafts.csswg.org/css-syntax/#substring-match-token-diagram)
428+
/// type.
429+
SubstringMatch,
430+
431+
/// The `<(-token>` type.
432+
OpenParen,
433+
434+
/// The `#` `<delim-token>` type.
435+
DelimHash,
436+
437+
/// The `@` `<delim-token>` type.
438+
DelimAt,
439+
440+
/// The `.` or `+` `<delim-token>` type.
441+
DelimDotOrPlus,
442+
443+
/// The `-` `<delim-token>` type.
444+
DelimMinus,
445+
446+
/// The `?` `<delim-token>` type.
447+
DelimQuestion,
448+
449+
/// The `$`, `^`, or `~` `<delim-token>` type.
450+
DelimAssorted,
451+
452+
/// The `=` `<delim-token>` type.
453+
DelimEquals,
454+
455+
/// The `|` `<delim-token>` type.
456+
DelimBar,
457+
458+
/// The `/` `<delim-token>` type.
459+
DelimSlash,
460+
461+
/// The `*` `<delim-token>` type.
462+
DelimAsterisk,
463+
464+
/// The `%` `<delim-token>` type.
465+
DelimPercent,
466+
467+
/// A type indicating any other token.
468+
Other,
469+
}
383470

384471
impl TokenSerializationType {
385472
/// Return a value that represents the absence of a token, e.g. before the start of the input.
473+
#[deprecated(
474+
since = "0.32.1",
475+
note = "use TokenSerializationType::Nothing or TokenSerializationType::default() instead"
476+
)]
386477
pub fn nothing() -> TokenSerializationType {
387-
TokenSerializationType(TokenSerializationTypeVariants::Nothing)
478+
Default::default()
388479
}
389480

390-
/// If this value is `TokenSerializationType::nothing()`, set it to the given value instead.
481+
/// If this value is `TokenSerializationType::Nothing`, set it to the given value instead.
391482
pub fn set_if_nothing(&mut self, new_value: TokenSerializationType) {
392-
if self.0 == TokenSerializationTypeVariants::Nothing {
393-
self.0 = new_value.0
483+
if matches!(self, TokenSerializationType::Nothing) {
484+
*self = new_value
394485
}
395486
}
396487

@@ -404,10 +495,10 @@ impl TokenSerializationType {
404495
/// See https://github.com/w3c/csswg-drafts/issues/4088 for the
405496
/// `DelimPercent` bits.
406497
pub fn needs_separator_when_before(self, other: TokenSerializationType) -> bool {
407-
use self::TokenSerializationTypeVariants::*;
408-
match self.0 {
498+
use self::TokenSerializationType::*;
499+
match self {
409500
Ident => matches!(
410-
other.0,
501+
other,
411502
Ident
412503
| Function
413504
| UrlOrBadUrl
@@ -419,15 +510,15 @@ impl TokenSerializationType {
419510
| OpenParen
420511
),
421512
AtKeywordOrHash | Dimension => matches!(
422-
other.0,
513+
other,
423514
Ident | Function | UrlOrBadUrl | DelimMinus | Number | Percentage | Dimension | CDC
424515
),
425516
DelimHash | DelimMinus => matches!(
426-
other.0,
517+
other,
427518
Ident | Function | UrlOrBadUrl | DelimMinus | Number | Percentage | Dimension
428519
),
429520
Number => matches!(
430-
other.0,
521+
other,
431522
Ident
432523
| Function
433524
| UrlOrBadUrl
@@ -437,11 +528,11 @@ impl TokenSerializationType {
437528
| DelimPercent
438529
| Dimension
439530
),
440-
DelimAt => matches!(other.0, Ident | Function | UrlOrBadUrl | DelimMinus),
441-
DelimDotOrPlus => matches!(other.0, Number | Percentage | Dimension),
442-
DelimAssorted | DelimAsterisk => matches!(other.0, DelimEquals),
443-
DelimBar => matches!(other.0, DelimEquals | DelimBar | DashMatch),
444-
DelimSlash => matches!(other.0, DelimAsterisk | SubstringMatch),
531+
DelimAt => matches!(other, Ident | Function | UrlOrBadUrl | DelimMinus),
532+
DelimDotOrPlus => matches!(other, Number | Percentage | Dimension),
533+
DelimAssorted | DelimAsterisk => matches!(other, DelimEquals),
534+
DelimBar => matches!(other, DelimEquals | DelimBar | DashMatch),
535+
DelimSlash => matches!(other, DelimAsterisk | SubstringMatch),
445536
Nothing | WhiteSpace | Percentage | UrlOrBadUrl | Function | CDC | OpenParen
446537
| DashMatch | SubstringMatch | DelimQuestion | DelimEquals | DelimPercent | Other => {
447538
false
@@ -450,43 +541,14 @@ impl TokenSerializationType {
450541
}
451542
}
452543

453-
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
454-
enum TokenSerializationTypeVariants {
455-
Nothing,
456-
WhiteSpace,
457-
AtKeywordOrHash,
458-
Number,
459-
Dimension,
460-
Percentage,
461-
UrlOrBadUrl,
462-
Function,
463-
Ident,
464-
CDC,
465-
DashMatch,
466-
SubstringMatch,
467-
OpenParen, // '('
468-
DelimHash, // '#'
469-
DelimAt, // '@'
470-
DelimDotOrPlus, // '.', '+'
471-
DelimMinus, // '-'
472-
DelimQuestion, // '?'
473-
DelimAssorted, // '$', '^', '~'
474-
DelimEquals, // '='
475-
DelimBar, // '|'
476-
DelimSlash, // '/'
477-
DelimAsterisk, // '*'
478-
DelimPercent, // '%'
479-
Other, // anything else
480-
}
481-
482544
impl<'a> Token<'a> {
483545
/// Categorize a token into a type that determines when `/**/` needs to be inserted
484546
/// between two tokens when serialized next to each other without whitespace in between.
485547
///
486548
/// See the `TokenSerializationType::needs_separator_when_before` method.
487549
pub fn serialization_type(&self) -> TokenSerializationType {
488-
use self::TokenSerializationTypeVariants::*;
489-
TokenSerializationType(match *self {
550+
use self::TokenSerializationType::*;
551+
match self {
490552
Token::Ident(_) => Ident,
491553
Token::AtKeyword(_) | Token::Hash(_) | Token::IDHash(_) => AtKeywordOrHash,
492554
Token::UnquotedUrl(_) | Token::BadUrl(_) => UrlOrBadUrl,
@@ -526,6 +588,6 @@ impl<'a> Token<'a> {
526588
| Token::IncludeMatch
527589
| Token::PrefixMatch
528590
| Token::SuffixMatch => Other,
529-
})
591+
}
530592
}
531593
}

src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ fn serializer(preserve_comments: bool) {
466466
}
467467
let mut serialized = String::new();
468468
write_to(
469-
TokenSerializationType::nothing(),
469+
TokenSerializationType::Nothing,
470470
input,
471471
&mut serialized,
472472
preserve_comments,

0 commit comments

Comments
 (0)