@@ -8,7 +8,8 @@ use std::collections::HashSet;
88use std:: sync:: Mutex ;
99use wasm_bindgen:: prelude:: * ;
1010
11- static GLOBAL_STYLE_SHEET : Lazy < Mutex < StyleSheet > > = Lazy :: new ( || Mutex :: new ( StyleSheet :: default ( ) ) ) ;
11+ static GLOBAL_STYLE_SHEET : Lazy < Mutex < StyleSheet > > =
12+ Lazy :: new ( || Mutex :: new ( StyleSheet :: default ( ) ) ) ;
1213
1314#[ wasm_bindgen]
1415pub struct Output {
@@ -32,11 +33,34 @@ pub struct Output {
3233
3334#[ wasm_bindgen]
3435impl Output {
35- fn new ( code : String , styles : HashSet < ExtractStyleValue > , map : Option < String > , single_css : bool , filename : String , css_file : Option < String > , import_main_css : bool ) -> Self {
36+ fn new (
37+ code : String ,
38+ styles : HashSet < ExtractStyleValue > ,
39+ map : Option < String > ,
40+ single_css : bool ,
41+ filename : String ,
42+ css_file : Option < String > ,
43+ import_main_css : bool ,
44+ ) -> Self {
3645 let mut sheet = GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) ;
3746 let default_collected = sheet. rm_global_css ( & filename, single_css) ;
3847 let ( collected, updated_base_style) = sheet. update_styles ( & styles, & filename, single_css) ;
39- Self { code, map, css_file, updated_base_style : updated_base_style || default_collected, css : { if !collected && !default_collected { None } else { Some ( sheet. create_css ( if !single_css { Some ( & filename) } else { None } , import_main_css) ) } } }
48+ Self {
49+ code,
50+ map,
51+ css_file,
52+ updated_base_style : updated_base_style || default_collected,
53+ css : {
54+ if !collected && !default_collected {
55+ None
56+ } else {
57+ Some ( sheet. create_css (
58+ if !single_css { Some ( & filename) } else { None } ,
59+ import_main_css,
60+ ) )
61+ }
62+ } ,
63+ }
4064 }
4165
4266 /// Get the code
@@ -79,18 +103,23 @@ pub fn is_debug() -> bool {
79103
80104#[ wasm_bindgen( js_name = "importSheet" ) ]
81105pub fn import_sheet ( sheet_object : JsValue ) -> Result < ( ) , JsValue > {
82- * GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) = serde_wasm_bindgen:: from_value ( sheet_object) . map_err ( |e| JsValue :: from_str ( & e. to_string ( ) ) ) ?;
106+ * GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) = serde_wasm_bindgen:: from_value ( sheet_object)
107+ . map_err ( |e| JsValue :: from_str ( & e. to_string ( ) ) ) ?;
83108 Ok ( ( ) )
84109}
85110
86111#[ wasm_bindgen( js_name = "exportSheet" ) ]
87112pub fn export_sheet ( ) -> Result < String , JsValue > {
88- serde_json:: to_string ( & * GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) ) . map_err ( |e| JsValue :: from_str ( & e. to_string ( ) ) )
113+ serde_json:: to_string ( & * GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) )
114+ . map_err ( |e| JsValue :: from_str ( & e. to_string ( ) ) )
89115}
90116
91117#[ wasm_bindgen( js_name = "importClassMap" ) ]
92118pub fn import_class_map ( sheet_object : JsValue ) -> Result < ( ) , JsValue > {
93- set_class_map ( serde_wasm_bindgen:: from_value ( sheet_object) . map_err ( |e| JsValue :: from_str ( & e. to_string ( ) ) ) ?) ;
119+ set_class_map (
120+ serde_wasm_bindgen:: from_value ( sheet_object)
121+ . map_err ( |e| JsValue :: from_str ( & e. to_string ( ) ) ) ?,
122+ ) ;
94123 Ok ( ( ) )
95124}
96125
@@ -101,7 +130,10 @@ pub fn export_class_map() -> Result<String, JsValue> {
101130
102131#[ wasm_bindgen( js_name = "importFileMap" ) ]
103132pub fn import_file_map ( sheet_object : JsValue ) -> Result < ( ) , JsValue > {
104- set_file_map ( serde_wasm_bindgen:: from_value ( sheet_object) . map_err ( |e| JsValue :: from_str ( & e. to_string ( ) ) ) ?) ;
133+ set_file_map (
134+ serde_wasm_bindgen:: from_value ( sheet_object)
135+ . map_err ( |e| JsValue :: from_str ( & e. to_string ( ) ) ) ?,
136+ ) ;
105137 Ok ( ( ) )
106138}
107139
@@ -111,16 +143,44 @@ pub fn export_file_map() -> Result<String, JsValue> {
111143}
112144
113145#[ wasm_bindgen( js_name = "codeExtract" ) ]
114- pub fn code_extract ( filename : & str , code : & str , package : & str , css_dir : String , single_css : bool , import_main_css_in_code : bool , import_main_css_in_css : bool ) -> Result < Output , JsValue > {
115- match extract ( filename, code, ExtractOption { package : package. to_string ( ) , css_dir, single_css, import_main_css : import_main_css_in_code } ) {
116- Ok ( output) => Ok ( Output :: new ( output. code , output. styles , output. map , single_css, filename. to_string ( ) , output. css_file , import_main_css_in_css) ) ,
146+ pub fn code_extract (
147+ filename : & str ,
148+ code : & str ,
149+ package : & str ,
150+ css_dir : String ,
151+ single_css : bool ,
152+ import_main_css_in_code : bool ,
153+ import_main_css_in_css : bool ,
154+ ) -> Result < Output , JsValue > {
155+ match extract (
156+ filename,
157+ code,
158+ ExtractOption {
159+ package : package. to_string ( ) ,
160+ css_dir,
161+ single_css,
162+ import_main_css : import_main_css_in_code,
163+ } ,
164+ ) {
165+ Ok ( output) => Ok ( Output :: new (
166+ output. code ,
167+ output. styles ,
168+ output. map ,
169+ single_css,
170+ filename. to_string ( ) ,
171+ output. css_file ,
172+ import_main_css_in_css,
173+ ) ) ,
117174 Err ( error) => Err ( JsValue :: from_str ( error. to_string ( ) . as_str ( ) ) ) ,
118175 }
119176}
120177
121178#[ wasm_bindgen( js_name = "registerTheme" ) ]
122179pub fn register_theme ( theme_object : JsValue ) -> Result < ( ) , JsValue > {
123- GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) . set_theme ( serde_wasm_bindgen:: from_value ( theme_object) . map_err ( |e| JsValue :: from_str ( e. to_string ( ) . as_str ( ) ) ) ?) ;
180+ GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) . set_theme (
181+ serde_wasm_bindgen:: from_value ( theme_object)
182+ . map_err ( |e| JsValue :: from_str ( e. to_string ( ) . as_str ( ) ) ) ?,
183+ ) ;
124184 Ok ( ( ) )
125185}
126186
@@ -133,13 +193,26 @@ pub fn get_default_theme() -> Result<Option<String>, JsValue> {
133193#[ wasm_bindgen( js_name = "getCss" ) ]
134194pub fn get_css ( file_num : Option < usize > , import_main_css : bool ) -> Result < String , JsValue > {
135195 let sheet = GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) ;
136- Ok ( sheet. create_css ( file_num. map ( get_filename_by_file_num) . as_deref ( ) , import_main_css) )
196+ Ok ( sheet. create_css (
197+ file_num. map ( get_filename_by_file_num) . as_deref ( ) ,
198+ import_main_css,
199+ ) )
137200}
138201
139202#[ wasm_bindgen( js_name = "getThemeInterface" ) ]
140- pub fn get_theme_interface ( package_name : & str , color_interface_name : & str , typography_interface_name : & str , theme_interface_name : & str ) -> String {
203+ pub fn get_theme_interface (
204+ package_name : & str ,
205+ color_interface_name : & str ,
206+ typography_interface_name : & str ,
207+ theme_interface_name : & str ,
208+ ) -> String {
141209 let sheet = GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) ;
142- sheet. create_interface ( package_name, color_interface_name, typography_interface_name, theme_interface_name)
210+ sheet. create_interface (
211+ package_name,
212+ color_interface_name,
213+ typography_interface_name,
214+ theme_interface_name,
215+ )
143216}
144217#[ cfg( test) ]
145218mod tests {
@@ -158,7 +231,10 @@ mod tests {
158231 let mut sheet = GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) ;
159232 * sheet = StyleSheet :: default ( ) ;
160233 }
161- assert_eq ! ( get_css( None , false ) . unwrap( ) . split( "*/" ) . nth( 1 ) . unwrap( ) , "" ) ;
234+ assert_eq ! (
235+ get_css( None , false ) . unwrap( ) . split( "*/" ) . nth( 1 ) . unwrap( ) ,
236+ ""
237+ ) ;
162238
163239 {
164240 let mut sheet = GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) ;
@@ -271,15 +347,48 @@ mod tests {
271347 let mut color_theme = ColorTheme :: default ( ) ;
272348 color_theme. add_color ( "primary" , "#fff" ) ;
273349 theme. add_color_theme ( "dark" , color_theme) ;
274- theme. add_typography ( "default" , vec ! [ Some ( Typography :: new( Some ( "Arial" . to_string( ) ) , Some ( "16px" . to_string( ) ) , Some ( "400" . to_string( ) ) , Some ( "1.5" . to_string( ) ) , Some ( "0.5" . to_string( ) ) ) ) , Some ( Typography :: new( Some ( "Arial" . to_string( ) ) , Some ( "24px" . to_string( ) ) , Some ( "400" . to_string( ) ) , Some ( "1.5" . to_string( ) ) , Some ( "0.5" . to_string( ) ) ) ) ] ) ;
350+ theme. add_typography (
351+ "default" ,
352+ vec ! [
353+ Some ( Typography :: new(
354+ Some ( "Arial" . to_string( ) ) ,
355+ Some ( "16px" . to_string( ) ) ,
356+ Some ( "400" . to_string( ) ) ,
357+ Some ( "1.5" . to_string( ) ) ,
358+ Some ( "0.5" . to_string( ) ) ,
359+ ) ) ,
360+ Some ( Typography :: new(
361+ Some ( "Arial" . to_string( ) ) ,
362+ Some ( "24px" . to_string( ) ) ,
363+ Some ( "400" . to_string( ) ) ,
364+ Some ( "1.5" . to_string( ) ) ,
365+ Some ( "0.5" . to_string( ) ) ,
366+ ) ) ,
367+ ] ,
368+ ) ;
275369
276- theme. add_typography ( "default1" , vec ! [ None , Some ( Typography :: new( Some ( "Arial" . to_string( ) ) , Some ( "24px" . to_string( ) ) , Some ( "400" . to_string( ) ) , Some ( "1.5" . to_string( ) ) , Some ( "0.5" . to_string( ) ) ) ) ] ) ;
370+ theme. add_typography (
371+ "default1" ,
372+ vec ! [
373+ None ,
374+ Some ( Typography :: new(
375+ Some ( "Arial" . to_string( ) ) ,
376+ Some ( "24px" . to_string( ) ) ,
377+ Some ( "400" . to_string( ) ) ,
378+ Some ( "1.5" . to_string( ) ) ,
379+ Some ( "0.5" . to_string( ) ) ,
380+ ) ) ,
381+ ] ,
382+ ) ;
277383 let css = theme. to_css ( ) ;
278384 assert_debug_snapshot ! ( css) ;
279385
280386 assert_eq ! ( Theme :: default ( ) . to_css( ) , "" ) ;
281387 let mut theme = Theme :: default ( ) ;
282- theme. add_typography ( "default" , vec ! [ Some ( Typography :: new( None , None , None , None , None ) ) ] ) ;
388+ theme. add_typography (
389+ "default" ,
390+ vec ! [ Some ( Typography :: new( None , None , None , None , None ) ) ] ,
391+ ) ;
283392 assert_eq ! ( theme. to_css( ) , "" ) ;
284393
285394 let mut theme = Theme :: default ( ) ;
@@ -453,25 +562,58 @@ mod tests {
453562 #[ serial]
454563 fn test_get_theme_interface ( ) {
455564 let sheet = StyleSheet :: default ( ) ;
456- assert_eq ! ( sheet. create_interface( "package" , "ColorInterface" , "TypographyInterface" , "ThemeInterface" ) , "" ) ;
565+ assert_eq ! (
566+ sheet. create_interface(
567+ "package" ,
568+ "ColorInterface" ,
569+ "TypographyInterface" ,
570+ "ThemeInterface"
571+ ) ,
572+ ""
573+ ) ;
457574
458575 let mut theme = Theme :: default ( ) ;
459576 let mut color_theme = ColorTheme :: default ( ) ;
460577 color_theme. add_color ( "primary" , "#000" ) ;
461578 theme. add_color_theme ( "dark" , color_theme) ;
462579 GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) . set_theme ( theme) ;
463- assert_eq ! ( get_theme_interface( "package" , "ColorInterface" , "TypographyInterface" , "ThemeInterface" ) , "import \" package\" ;declare module \" package\" {interface ColorInterface{$primary:null;}interface TypographyInterface{}interface ThemeInterface{dark:null;}}" ) ;
580+ assert_eq ! (
581+ get_theme_interface(
582+ "package" ,
583+ "ColorInterface" ,
584+ "TypographyInterface" ,
585+ "ThemeInterface"
586+ ) ,
587+ "import \" package\" ;declare module \" package\" {interface ColorInterface{$primary:null;}interface TypographyInterface{}interface ThemeInterface{dark:null;}}"
588+ ) ;
464589
465590 // test wrong case
466591 let mut sheet = StyleSheet :: default ( ) ;
467592 let mut theme = Theme :: default ( ) ;
468593 let mut color_theme = ColorTheme :: default ( ) ;
469594 color_theme. add_color ( "(primary)" , "#000" ) ;
470595 theme. add_color_theme ( "dark" , color_theme) ;
471- theme. add_typography ( "prim``ary" , vec ! [ Some ( Typography :: new( Some ( "Arial" . to_string( ) ) , Some ( "16px" . to_string( ) ) , Some ( "400" . to_string( ) ) , Some ( "1.5" . to_string( ) ) , Some ( "0.5" . to_string( ) ) ) ) ] ) ;
596+ theme. add_typography (
597+ "prim``ary" ,
598+ vec ! [ Some ( Typography :: new(
599+ Some ( "Arial" . to_string( ) ) ,
600+ Some ( "16px" . to_string( ) ) ,
601+ Some ( "400" . to_string( ) ) ,
602+ Some ( "1.5" . to_string( ) ) ,
603+ Some ( "0.5" . to_string( ) ) ,
604+ ) ) ] ,
605+ ) ;
472606 sheet. set_theme ( theme) ;
473607 * GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) = sheet;
474- assert_eq ! ( get_theme_interface( "package" , "ColorInterface" , "TypographyInterface" , "ThemeInterface" ) , "import \" package\" ;declare module \" package\" {interface ColorInterface{[`$(primary)`]:null;}interface TypographyInterface{[`prim\\ `\\ `ary`]:null;}interface ThemeInterface{dark:null;}}" ) ;
608+ assert_eq ! (
609+ get_theme_interface(
610+ "package" ,
611+ "ColorInterface" ,
612+ "TypographyInterface" ,
613+ "ThemeInterface"
614+ ) ,
615+ "import \" package\" ;declare module \" package\" {interface ColorInterface{[`$(primary)`]:null;}interface TypographyInterface{[`prim\\ `\\ `ary`]:null;}interface ThemeInterface{dark:null;}}"
616+ ) ;
475617 }
476618
477619 #[ test]
0 commit comments