@@ -5,13 +5,56 @@ use std::fmt;
55use std:: fmt:: { Display , Formatter } ;
66use std:: sync:: Mutex ;
77
8- #[ derive( Debug , PartialEq , Clone , Hash , Eq , Ord , PartialOrd ) ]
8+ static SELECTOR_ORDER_MAP : Lazy < HashMap < String , u8 > > = Lazy :: new ( || {
9+ let mut map = HashMap :: new ( ) ;
10+ map. insert ( "disabled" . to_string ( ) , 5 ) ;
11+ map. insert ( "selected" . to_string ( ) , 4 ) ;
12+ map. insert ( "active" . to_string ( ) , 3 ) ;
13+ map. insert ( "focus" . to_string ( ) , 2 ) ;
14+ map. insert ( "hover" . to_string ( ) , 1 ) ;
15+ map
16+ } ) ;
17+
18+ #[ derive( Debug , PartialEq , Clone , Hash , Eq ) ]
919pub enum StyleSelector {
1020 Postfix ( String ) ,
1121 Prefix ( String ) ,
1222 Dual ( String , String ) ,
1323}
1424
25+ impl PartialOrd for StyleSelector {
26+ fn partial_cmp ( & self , other : & Self ) -> Option < std:: cmp:: Ordering > {
27+ Some ( self . cmp ( other) )
28+ }
29+ }
30+
31+ impl Ord for StyleSelector {
32+ fn cmp ( & self , other : & Self ) -> std:: cmp:: Ordering {
33+ match ( self , other) {
34+ ( Postfix ( a) , Postfix ( b) ) => SELECTOR_ORDER_MAP
35+ . get ( a)
36+ . unwrap_or ( & 0 )
37+ . cmp ( SELECTOR_ORDER_MAP . get ( b) . unwrap_or ( & 0 ) ) ,
38+ ( Prefix ( a) , Prefix ( b) ) => a. cmp ( b) ,
39+ ( Dual ( p1, a) , Dual ( p2, b) ) => {
40+ if p1 == p2 {
41+ SELECTOR_ORDER_MAP
42+ . get ( a)
43+ . unwrap_or ( & 0 )
44+ . cmp ( SELECTOR_ORDER_MAP . get ( b) . unwrap_or ( & 0 ) )
45+ } else {
46+ p1. cmp ( p2)
47+ }
48+ }
49+ ( Postfix ( _) , _) => std:: cmp:: Ordering :: Less ,
50+ ( Prefix ( _) , Postfix ( _) ) => std:: cmp:: Ordering :: Greater ,
51+ ( Prefix ( _) , _) => std:: cmp:: Ordering :: Less ,
52+ ( Dual ( _, _) , Postfix ( _) ) => std:: cmp:: Ordering :: Greater ,
53+ ( Dual ( _, _) , Prefix ( _) ) => std:: cmp:: Ordering :: Greater ,
54+ }
55+ }
56+ }
57+
1558impl From < & str > for StyleSelector {
1659 fn from ( value : & str ) -> Self {
1760 if let Some ( s) = value. strip_prefix ( "group" ) {
0 commit comments