@@ -21,18 +21,11 @@ use std::sync::LazyLock;
2121
2222macro_rules! push_fmt {
2323 ( $target: expr, $( $arg: tt) * ) => { {
24- std:: fmt:: Write :: write_fmt( $target , format_args! ( $ ( $arg ) * ) )
25- . unwrap_or_else ( |err| panic! ( "failed to write CSS into string: {err}" ) ) ;
24+ // ` std::fmt::Write::write_fmt` on `&mut String` is infallible; discard result.
25+ let _ = std :: fmt :: Write :: write_fmt ( $target , format_args! ( $ ( $arg ) * ) ) ;
2626 } } ;
2727}
2828
29- trait ExtractStyle {
30- fn extract ( & self ) -> String ;
31- fn write_extract ( & self , css : & mut String ) {
32- css. push_str ( & self . extract ( ) ) ;
33- }
34- }
35-
3629#[ derive( Debug , Hash , Eq , PartialEq , Deserialize , Serialize , Clone ) ]
3730#[ serde( rename_all = "camelCase" ) ]
3831pub struct StyleSheetProperty {
@@ -81,15 +74,7 @@ impl Ord for StyleSheetProperty {
8174 }
8275}
8376
84- impl ExtractStyle for StyleSheetProperty {
85- fn extract ( & self ) -> String {
86- let mut css = String :: with_capacity (
87- self . class_name . len ( ) + self . property . len ( ) + self . value . len ( ) + 4 ,
88- ) ;
89- self . write_extract ( & mut css) ;
90- css
91- }
92-
77+ impl StyleSheetProperty {
9378 fn write_extract ( & self , css : & mut String ) {
9479 css. push_str ( & merge_selector ( & self . class_name , self . selector . as_ref ( ) ) ) ;
9580 css. push ( '{' ) ;
@@ -150,16 +135,6 @@ pub struct StyleSheetCss {
150135 pub css : String ,
151136}
152137
153- impl ExtractStyle for StyleSheetCss {
154- fn extract ( & self ) -> String {
155- self . css . clone ( )
156- }
157-
158- fn write_extract ( & self , css : & mut String ) {
159- css. push_str ( & self . css ) ;
160- }
161- }
162-
163138type PropertyMap = BTreeMap < u8 , BTreeMap < u8 , FxHashSet < StyleSheetProperty > > > ;
164139type KeyframesMap = BTreeMap < String , BTreeMap < String , BTreeMap < String , Vec < ( String , String ) > > > > ;
165140
@@ -2064,6 +2039,37 @@ mod tests {
20642039 "ShadowsInterface" ,
20652040 "ThemeInterface"
20662041 ) ) ;
2042+
2043+ // Multiple typography keys + multiple color themes exercise the
2044+ // `plain_keys` semicolon separator (joins 2+ entries).
2045+ let mut sheet = StyleSheet :: default ( ) ;
2046+ let mut theme = Theme :: default ( ) ;
2047+ let mut light_theme = ColorTheme :: default ( ) ;
2048+ light_theme. add_color ( "primary" , "#000" ) ;
2049+ let mut dark_theme = ColorTheme :: default ( ) ;
2050+ dark_theme. add_color ( "primary" , "#fff" ) ;
2051+ theme. add_color_theme ( "default" , light_theme) ;
2052+ theme. add_color_theme ( "dark" , dark_theme) ;
2053+ let make_typography = || {
2054+ Typography :: new (
2055+ Some ( "Arial" . to_string ( ) ) ,
2056+ Some ( "16px" . to_string ( ) ) ,
2057+ Some ( "400" . to_string ( ) ) ,
2058+ Some ( "1.5" . to_string ( ) ) ,
2059+ Some ( "0.5" . to_string ( ) ) ,
2060+ )
2061+ } ;
2062+ theme. add_typography ( "heading" , vec ! [ Some ( make_typography( ) ) ] ) ;
2063+ theme. add_typography ( "body" , vec ! [ Some ( make_typography( ) ) ] ) ;
2064+ sheet. set_theme ( theme) ;
2065+ assert_debug_snapshot ! ( sheet. create_interface(
2066+ "package" ,
2067+ "ColorInterface" ,
2068+ "TypographyInterface" ,
2069+ "LengthInterface" ,
2070+ "ShadowsInterface" ,
2071+ "ThemeInterface"
2072+ ) ) ;
20672073 }
20682074
20692075 #[ test]
@@ -2299,14 +2305,30 @@ mod tests {
22992305 }
23002306
23012307 #[ test]
2302- fn test_stylesheet_css_extract ( ) {
2308+ fn test_stylesheet_css_struct ( ) {
23032309 let css_entry = StyleSheetCss {
23042310 css : "div{display:flex}" . to_string ( ) ,
23052311 } ;
2306- assert_eq ! ( css_entry. extract ( ) , "div{display:flex}" ) ;
2312+ assert_eq ! ( css_entry. css , "div{display:flex}" ) ;
23072313
23082314 let empty = StyleSheetCss { css : String :: new ( ) } ;
2309- assert_eq ! ( empty. extract( ) , "" ) ;
2315+ assert_eq ! ( empty. css, "" ) ;
2316+ }
2317+
2318+ #[ test]
2319+ fn test_stylesheet_property_ord_no_selectors ( ) {
2320+ // Both sides without selectors: branches on property then value.
2321+ let make = |property : & str , value : & str | StyleSheetProperty {
2322+ class_name : "a" . to_string ( ) ,
2323+ property : property. to_string ( ) ,
2324+ value : value. to_string ( ) ,
2325+ selector : None ,
2326+ layer : None ,
2327+ } ;
2328+ assert_eq ! ( make( "color" , "red" ) . cmp( & make( "color" , "red" ) ) , Equal ) ;
2329+ assert ! ( make( "color" , "red" ) < make( "color" , "white" ) ) ;
2330+ assert ! ( make( "color" , "red" ) < make( "display" , "block" ) ) ;
2331+ assert ! ( make( "display" , "block" ) > make( "color" , "white" ) ) ;
23102332 }
23112333
23122334 #[ test]
0 commit comments