@@ -5,12 +5,17 @@ use extractor::{ExtractOption, ImportAlias, extract, has_devup_ui};
55use rustc_hash:: FxHashSet ;
66use sheet:: StyleSheet ;
77use std:: collections:: HashMap ;
8+ use std:: fmt:: Display ;
89use std:: sync:: { LazyLock , Mutex } ;
910use wasm_bindgen:: prelude:: * ;
1011
1112static GLOBAL_STYLE_SHEET : LazyLock < Mutex < StyleSheet > > =
1213 LazyLock :: new ( || Mutex :: new ( StyleSheet :: default ( ) ) ) ;
1314
15+ fn js_error ( message : impl Display ) -> JsValue {
16+ js_sys:: Error :: new ( & message. to_string ( ) ) . into ( )
17+ }
18+
1419#[ wasm_bindgen]
1520pub struct Output {
1621 code : String ,
@@ -159,8 +164,7 @@ pub fn import_sheet_internal(sheet: StyleSheet) {
159164#[ wasm_bindgen( js_name = "importSheet" ) ]
160165#[ cfg( not( tarpaulin_include) ) ]
161166pub fn import_sheet ( sheet_object : JsValue ) -> Result < ( ) , JsValue > {
162- let sheet: StyleSheet = serde_wasm_bindgen:: from_value ( sheet_object)
163- . map_err ( |e| JsValue :: from_str ( & e. to_string ( ) ) ) ?;
167+ let sheet: StyleSheet = serde_wasm_bindgen:: from_value ( sheet_object) . map_err ( js_error) ?;
164168 import_sheet_internal ( sheet) ;
165169 Ok ( ( ) )
166170}
@@ -173,7 +177,7 @@ pub fn export_sheet_internal() -> Result<String, String> {
173177#[ wasm_bindgen( js_name = "exportSheet" ) ]
174178#[ cfg( not( tarpaulin_include) ) ]
175179pub fn export_sheet ( ) -> Result < String , JsValue > {
176- export_sheet_internal ( ) . map_err ( |e| JsValue :: from_str ( & e ) )
180+ export_sheet_internal ( ) . map_err ( js_error )
177181}
178182
179183/// Internal function to export class map as JSON string (testable without JsValue)
@@ -184,17 +188,14 @@ pub fn export_class_map_internal() -> Result<String, String> {
184188#[ wasm_bindgen( js_name = "importClassMap" ) ]
185189#[ cfg( not( tarpaulin_include) ) ]
186190pub fn import_class_map ( sheet_object : JsValue ) -> Result < ( ) , JsValue > {
187- set_class_map (
188- serde_wasm_bindgen:: from_value ( sheet_object)
189- . map_err ( |e| JsValue :: from_str ( & e. to_string ( ) ) ) ?,
190- ) ;
191+ set_class_map ( serde_wasm_bindgen:: from_value ( sheet_object) . map_err ( js_error) ?) ;
191192 Ok ( ( ) )
192193}
193194
194195#[ wasm_bindgen( js_name = "exportClassMap" ) ]
195196#[ cfg( not( tarpaulin_include) ) ]
196197pub fn export_class_map ( ) -> Result < String , JsValue > {
197- export_class_map_internal ( ) . map_err ( |e| JsValue :: from_str ( & e ) )
198+ export_class_map_internal ( ) . map_err ( js_error )
198199}
199200
200201/// Internal function to export file map as JSON string (testable without JsValue)
@@ -205,17 +206,14 @@ pub fn export_file_map_internal() -> Result<String, String> {
205206#[ wasm_bindgen( js_name = "importFileMap" ) ]
206207#[ cfg( not( tarpaulin_include) ) ]
207208pub fn import_file_map ( sheet_object : JsValue ) -> Result < ( ) , JsValue > {
208- set_file_map (
209- serde_wasm_bindgen:: from_value ( sheet_object)
210- . map_err ( |e| JsValue :: from_str ( & e. to_string ( ) ) ) ?,
211- ) ;
209+ set_file_map ( serde_wasm_bindgen:: from_value ( sheet_object) . map_err ( js_error) ?) ;
212210 Ok ( ( ) )
213211}
214212
215213#[ wasm_bindgen( js_name = "exportFileMap" ) ]
216214#[ cfg( not( tarpaulin_include) ) ]
217215pub fn export_file_map ( ) -> Result < String , JsValue > {
218- export_file_map_internal ( ) . map_err ( |e| JsValue :: from_str ( & e ) )
216+ export_file_map_internal ( ) . map_err ( js_error )
219217}
220218
221219/// Internal function to extract code (testable without JsValue)
@@ -270,7 +268,7 @@ pub fn code_extract(
270268 // Deserialize import_aliases from JsValue
271269 // Format: { "package": "namedExport" } or { "package": null } for named exports
272270 let aliases: HashMap < String , Option < String > > =
273- serde_wasm_bindgen:: from_value ( import_aliases) . unwrap_or_default ( ) ;
271+ serde_wasm_bindgen:: from_value ( import_aliases) . map_err ( js_error ) ? ;
274272
275273 // Convert to ImportAlias enum
276274 let import_aliases: HashMap < String , ImportAlias > = aliases
@@ -294,7 +292,7 @@ pub fn code_extract(
294292 import_main_css_in_css,
295293 import_aliases,
296294 )
297- . map_err ( |e| JsValue :: from_str ( & e ) )
295+ . map_err ( js_error )
298296}
299297
300298/// Internal function to register theme (testable without JsValue)
@@ -305,8 +303,8 @@ pub fn register_theme_internal(theme: sheet::theme::Theme) {
305303#[ wasm_bindgen( js_name = "registerTheme" ) ]
306304#[ cfg( not( tarpaulin_include) ) ]
307305pub fn register_theme ( theme_object : JsValue ) -> Result < ( ) , JsValue > {
308- let theme: sheet:: theme:: Theme = serde_wasm_bindgen :: from_value ( theme_object )
309- . map_err ( |e| JsValue :: from_str ( e . to_string ( ) . as_str ( ) ) ) ?;
306+ let theme: sheet:: theme:: Theme =
307+ serde_wasm_bindgen :: from_value ( theme_object ) . map_err ( js_error ) ?;
310308 register_theme_internal ( theme) ;
311309 Ok ( ( ) )
312310}
0 commit comments