Skip to content

Commit 0639df7

Browse files
committed
implement From<u8> for #enum_name instead of classify()
There is a tiny problem with the custom trait which exposes `classify()`. The moment you have 2 enums with `ClassifyBytes` in the same file, it means that the `trait U8Ext` was duplicated resulting in errors. To solve this, we can also implement the `From<u8> for #enum_name` trait. This allows us to just use `b'a'.into()` in a context where you are comparing it with an `Class::AlphaLower` for example.
1 parent 2f77d99 commit 0639df7

11 files changed

+57
-63
lines changed

crates/classification-macros/src/lib.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use syn::{
2929
/// Other,
3030
/// }
3131
/// ```
32-
/// Then call `b'a'.classify()` to get `Example::SomeLetters`.
32+
/// Then call `b'a'.into()` to get `Example::SomeLetters`.
3333
#[proc_macro_derive(ClassifyBytes, attributes(bytes, bytes_range, fallback))]
3434
pub fn classify_bytes_derive(input: TokenStream) -> TokenStream {
3535
let ast = parse_macro_input!(input as DeriveInput);
@@ -103,15 +103,9 @@ pub fn classify_bytes_derive(input: TokenStream) -> TokenStream {
103103
];
104104
}
105105

106-
// Extend u8 with a `classify` method
107-
trait U8Ext {
108-
fn classify(&self) -> #enum_name;
109-
}
110-
111-
impl U8Ext for u8 {
112-
#[inline(always)]
113-
fn classify(&self) -> #enum_name {
114-
#enum_name::TABLE[*self as usize]
106+
impl From<u8> for #enum_name {
107+
fn from(byte: u8) -> Self {
108+
#enum_name::TABLE[byte as usize]
115109
}
116110
}
117111
};

crates/oxide/src/extractor/arbitrary_property_machine.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl Machine for ArbitraryPropertyMachine {
6969
let len = cursor.input.len();
7070

7171
match self.state {
72-
State::Idle => match cursor.curr.classify() {
72+
State::Idle => match cursor.curr.into() {
7373
// Start of an arbitrary property
7474
Class::OpenBracket => {
7575
self.start_pos = cursor.pos;
@@ -84,8 +84,8 @@ impl Machine for ArbitraryPropertyMachine {
8484

8585
State::ParsingProperty => {
8686
while cursor.pos < len {
87-
match cursor.curr.classify() {
88-
Class::Dash => match cursor.next.classify() {
87+
match cursor.curr.into() {
88+
Class::Dash => match cursor.next.into() {
8989
// Start of a CSS variable
9090
//
9191
// E.g.: `[--my-color:red]`
@@ -122,8 +122,8 @@ impl Machine for ArbitraryPropertyMachine {
122122

123123
State::ParsingValue => {
124124
while cursor.pos < len {
125-
match cursor.curr.classify() {
126-
Class::Escape => match cursor.next.classify() {
125+
match cursor.curr.into() {
126+
Class::Escape => match cursor.next.into() {
127127
// An escaped whitespace character is not allowed
128128
//
129129
// E.g.: `[color:var(--my-\ color)]`
@@ -196,7 +196,7 @@ impl ArbitraryPropertyMachine {
196196
fn parse_property_variable(&mut self, cursor: &mut cursor::Cursor<'_>) -> MachineState {
197197
match self.css_variable_machine.next(cursor) {
198198
MachineState::Idle => self.restart(),
199-
MachineState::Done(_) => match cursor.next.classify() {
199+
MachineState::Done(_) => match cursor.next.into() {
200200
// End of the CSS variable, must be followed by a `:`
201201
//
202202
// E.g.: `[--my-color:red]`

crates/oxide/src/extractor/arbitrary_value_machine.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl Machine for ArbitraryValueMachine {
3333
#[inline]
3434
fn next(&mut self, cursor: &mut cursor::Cursor<'_>) -> MachineState {
3535
// An arbitrary value must start with an open bracket
36-
if cursor.curr.classify() != Class::OpenBracket {
36+
if Class::OpenBracket != cursor.curr.into() {
3737
return MachineState::Idle;
3838
}
3939

@@ -43,8 +43,8 @@ impl Machine for ArbitraryValueMachine {
4343
let len = cursor.input.len();
4444

4545
while cursor.pos < len {
46-
match cursor.curr.classify() {
47-
Class::Escape => match cursor.next.classify() {
46+
match cursor.curr.into() {
47+
Class::Escape => match cursor.next.into() {
4848
// An escaped whitespace character is not allowed
4949
//
5050
// E.g.: `[color:var(--my-\ color)]`

crates/oxide/src/extractor/arbitrary_variable_machine.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl Machine for ArbitraryVariableMachine {
6464

6565
#[inline]
6666
fn next(&mut self, cursor: &mut cursor::Cursor<'_>) -> MachineState {
67-
let class_curr = cursor.curr.classify();
67+
let class_curr = cursor.curr.into();
6868
let len = cursor.input.len();
6969

7070
match self.state {
@@ -74,7 +74,7 @@ impl Machine for ArbitraryVariableMachine {
7474
// E.g.: `(--my-variable)`
7575
// ^^
7676
//
77-
Class::OpenParen => match cursor.next.classify() {
77+
Class::OpenParen => match cursor.next.into() {
7878
Class::Dash => {
7979
self.start_pos = cursor.pos;
8080
self.state = State::Parsing;
@@ -92,7 +92,7 @@ impl Machine for ArbitraryVariableMachine {
9292

9393
State::Parsing => match self.css_variable_machine.next(cursor) {
9494
MachineState::Idle => self.restart(),
95-
MachineState::Done(_) => match cursor.next.classify() {
95+
MachineState::Done(_) => match cursor.next.into() {
9696
// A CSS variable followed by a `,` means that there is a fallback
9797
//
9898
// E.g.: `(--my-color,red)`
@@ -110,7 +110,7 @@ impl Machine for ArbitraryVariableMachine {
110110
_ => {
111111
cursor.advance();
112112

113-
match cursor.curr.classify() {
113+
match cursor.curr.into() {
114114
// End of an arbitrary variable, must be followed by `)`
115115
Class::CloseParen => self.done(self.start_pos, cursor),
116116

@@ -123,8 +123,8 @@ impl Machine for ArbitraryVariableMachine {
123123

124124
State::ParsingFallback => {
125125
while cursor.pos < len {
126-
match cursor.curr.classify() {
127-
Class::Escape => match cursor.next.classify() {
126+
match cursor.curr.into() {
127+
Class::Escape => match cursor.next.into() {
128128
// An escaped whitespace character is not allowed
129129
//
130130
// E.g.: `(--my-\ color)`

crates/oxide/src/extractor/css_variable_machine.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl Machine for CssVariableMachine {
2020
#[inline]
2121
fn next(&mut self, cursor: &mut cursor::Cursor<'_>) -> MachineState {
2222
// CSS Variables must start with `--`
23-
if cursor.curr.classify() != Class::Dash || cursor.next.classify() != Class::Dash {
23+
if Class::Dash != cursor.curr.into() || Class::Dash != cursor.next.into() {
2424
return MachineState::Idle;
2525
}
2626

@@ -30,11 +30,11 @@ impl Machine for CssVariableMachine {
3030
cursor.advance_twice();
3131

3232
while cursor.pos < len {
33-
match cursor.curr.classify() {
33+
match cursor.curr.into() {
3434
// https://drafts.csswg.org/css-syntax-3/#ident-token-diagram
3535
//
3636
Class::AllowedCharacter | Class::Dash => {
37-
match cursor.next.classify() {
37+
match cursor.next.into() {
3838
// Valid character followed by a valid character or an escape character
3939
//
4040
// E.g.: `--my-variable`
@@ -58,7 +58,7 @@ impl Machine for CssVariableMachine {
5858
}
5959
}
6060

61-
Class::Escape => match cursor.next.classify() {
61+
Class::Escape => match cursor.next.into() {
6262
// An escaped whitespace character is not allowed
6363
//
6464
// In CSS it is allowed, but in the context of a class it's not because then we

crates/oxide/src/extractor/modifier_machine.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ impl Machine for ModifierMachine {
3232
#[inline]
3333
fn next(&mut self, cursor: &mut cursor::Cursor<'_>) -> MachineState {
3434
// A modifier must start with a `/`, everything else is not a valid start of a modifier
35-
if cursor.curr.classify() != Class::Slash {
35+
if Class::Slash != cursor.curr.into() {
3636
return MachineState::Idle;
3737
}
3838

3939
let start_pos = cursor.pos;
4040
cursor.advance();
4141

42-
match cursor.curr.classify() {
42+
match cursor.curr.into() {
4343
// Start of an arbitrary value:
4444
//
4545
// ```
@@ -71,9 +71,9 @@ impl Machine for ModifierMachine {
7171
Class::ValidStart => {
7272
let len = cursor.input.len();
7373
while cursor.pos < len {
74-
match cursor.curr.classify() {
74+
match cursor.curr.into() {
7575
Class::ValidStart | Class::ValidInside => {
76-
match cursor.next.classify() {
76+
match cursor.next.into() {
7777
// Only valid characters are allowed, if followed by another valid character
7878
Class::ValidStart | Class::ValidInside => cursor.advance(),
7979

crates/oxide/src/extractor/named_utility_machine.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ impl Machine for NamedUtilityMachine {
4949
let len = cursor.input.len();
5050

5151
match self.state {
52-
State::Idle => match cursor.curr.classify() {
53-
Class::AlphaLower => match cursor.next.classify() {
52+
State::Idle => match cursor.curr.into() {
53+
Class::AlphaLower => match cursor.next.into() {
5454
// Valid single character utility in between quotes
5555
//
5656
// E.g.: `<div class="a"></div>`
@@ -89,7 +89,7 @@ impl Machine for NamedUtilityMachine {
8989
//
9090
// E.g.: `-mx-2.5`
9191
// ^^
92-
Class::Dash => match cursor.next.classify() {
92+
Class::Dash => match cursor.next.into() {
9393
Class::AlphaLower => {
9494
self.start_pos = cursor.pos;
9595
self.state = State::Parsing;
@@ -107,7 +107,7 @@ impl Machine for NamedUtilityMachine {
107107

108108
State::Parsing => {
109109
while cursor.pos < len {
110-
match cursor.curr.classify() {
110+
match cursor.curr.into() {
111111
// Followed by a boundary character, we are at the end of the utility.
112112
//
113113
// E.g.: `'flex'`
@@ -121,7 +121,7 @@ impl Machine for NamedUtilityMachine {
121121
// E.g.: `:div="{ flex: true }"` (JavaScript object syntax)
122122
// ^
123123
Class::AlphaLower | Class::AlphaUpper => {
124-
match cursor.next.classify() {
124+
match cursor.next.into() {
125125
Class::Quote
126126
| Class::Whitespace
127127
| Class::CloseBracket
@@ -136,7 +136,7 @@ impl Machine for NamedUtilityMachine {
136136
}
137137
}
138138

139-
Class::Dash => match cursor.next.classify() {
139+
Class::Dash => match cursor.next.into() {
140140
// Start of an arbitrary value
141141
//
142142
// E.g.: `bg-[#0088cc]`
@@ -180,7 +180,7 @@ impl Machine for NamedUtilityMachine {
180180
_ => return self.restart(),
181181
},
182182

183-
Class::Underscore => match cursor.next.classify() {
183+
Class::Underscore => match cursor.next.into() {
184184
// Valid characters _if_ followed by another valid character. These characters are
185185
// only valid inside of the utility but not at the end of the utility.
186186
//
@@ -230,11 +230,11 @@ impl Machine for NamedUtilityMachine {
230230
// E.g.: `px-2.5`
231231
// ^^^
232232
Class::Dot => {
233-
if !matches!(cursor.prev.classify(), Class::Number) {
233+
if !matches!(cursor.prev.into(), Class::Number) {
234234
return self.restart();
235235
}
236236

237-
if !matches!(cursor.next.classify(), Class::Number) {
237+
if !matches!(cursor.next.into(), Class::Number) {
238238
return self.restart();
239239
}
240240

@@ -258,14 +258,14 @@ impl Machine for NamedUtilityMachine {
258258
//
259259
Class::Number => {
260260
if !matches!(
261-
cursor.prev.classify(),
261+
cursor.prev.into(),
262262
Class::Dash | Class::Dot | Class::Number | Class::AlphaLower
263263
) {
264264
return self.restart();
265265
}
266266

267267
if !matches!(
268-
cursor.next.classify(),
268+
cursor.next.into(),
269269
Class::Dot
270270
| Class::Number
271271
| Class::AlphaLower
@@ -287,7 +287,7 @@ impl Machine for NamedUtilityMachine {
287287
// ^^
288288
// ```
289289
Class::Percent => {
290-
if !matches!(cursor.prev.classify(), Class::Number) {
290+
if !matches!(cursor.prev.into(), Class::Number) {
291291
return self.restart();
292292
}
293293

crates/oxide/src/extractor/named_variant_machine.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ impl Machine for NamedVariantMachine {
7575
let len = cursor.input.len();
7676

7777
match self.state {
78-
State::Idle => match cursor.curr.classify() {
79-
Class::AlphaLower | Class::Star => match cursor.next.classify() {
78+
State::Idle => match cursor.curr.into() {
79+
Class::AlphaLower | Class::Star => match cursor.next.into() {
8080
// Valid single character variant, must be followed by a `:`
8181
//
8282
// E.g.: `<div class="x:flex"></div>`
@@ -122,8 +122,8 @@ impl Machine for NamedVariantMachine {
122122

123123
State::Parsing => {
124124
while cursor.pos < len {
125-
match cursor.curr.classify() {
126-
Class::Dash => match cursor.next.classify() {
125+
match cursor.curr.into() {
126+
Class::Dash => match cursor.next.into() {
127127
// Start of an arbitrary value
128128
//
129129
// E.g.: `data-[state=pending]:`.
@@ -170,7 +170,7 @@ impl Machine for NamedVariantMachine {
170170
_ => return self.restart(),
171171
},
172172

173-
Class::Underscore => match cursor.next.classify() {
173+
Class::Underscore => match cursor.next.into() {
174174
// Valid characters _if_ followed by another valid character. These characters are
175175
// only valid inside of the variant but not at the end of the variant.
176176
//
@@ -226,7 +226,7 @@ impl Machine for NamedVariantMachine {
226226

227227
State::ParsingModifier => match self.modifier_machine.next(cursor) {
228228
MachineState::Idle => self.restart(),
229-
MachineState::Done(_) => match cursor.next.classify() {
229+
MachineState::Done(_) => match cursor.next.into() {
230230
// Modifier must be followed by a `:`
231231
//
232232
// E.g.: `group-hover/name:`
@@ -242,7 +242,7 @@ impl Machine for NamedVariantMachine {
242242
},
243243
},
244244

245-
State::ParseEnd => match cursor.curr.classify() {
245+
State::ParseEnd => match cursor.curr.into() {
246246
// The end of a variant must be the `:`
247247
//
248248
// E.g.: `hover:`
@@ -259,7 +259,7 @@ impl Machine for NamedVariantMachine {
259259
impl NamedVariantMachine {
260260
#[inline(always)]
261261
fn parse_arbitrary_end(&mut self, cursor: &mut cursor::Cursor<'_>) -> MachineState {
262-
match cursor.next.classify() {
262+
match cursor.next.into() {
263263
Class::Slash => {
264264
self.state = State::ParsingModifier;
265265
cursor.advance();

crates/oxide/src/extractor/string_machine.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl Machine for StringMachine {
3030

3131
#[inline]
3232
fn next(&mut self, cursor: &mut cursor::Cursor<'_>) -> MachineState {
33-
if cursor.curr.classify() != Class::Quote {
33+
if Class::Quote != cursor.curr.into() {
3434
return MachineState::Idle;
3535
}
3636

@@ -42,8 +42,8 @@ impl Machine for StringMachine {
4242
cursor.advance();
4343

4444
while cursor.pos < len {
45-
match cursor.curr.classify() {
46-
Class::Escape => match cursor.next.classify() {
45+
match cursor.curr.into() {
46+
Class::Escape => match cursor.next.into() {
4747
// An escaped whitespace character is not allowed
4848
Class::Whitespace => return MachineState::Idle,
4949

0 commit comments

Comments
 (0)