@@ -201,7 +201,7 @@ pub fn componentize_bindgen(
201
201
for ( specifier, by_resource) in by_specifier_by_resource {
202
202
let mut specifier_list = Vec :: new ( ) ;
203
203
for ( resource, items) in by_resource {
204
- let item = items. iter ( ) . next ( ) . unwrap ( ) ;
204
+ let item = items. first ( ) . unwrap ( ) ;
205
205
if let Some ( resource) = resource {
206
206
let export_name = resource. to_upper_camel_case ( ) ;
207
207
let binding_name = binding_name ( & export_name, & item. iface_name ) ;
@@ -251,7 +251,7 @@ pub fn componentize_bindgen(
251
251
if let TypeDefKind :: Resource = & ty. kind {
252
252
let iface_prefix = interface_name ( resolve, * iface_id)
253
253
. map ( |s| format ! ( "{s}$" ) )
254
- . unwrap_or_else ( String :: new ) ;
254
+ . unwrap_or_default ( ) ;
255
255
let resource_name_camel = ty. name . as_ref ( ) . unwrap ( ) . to_lower_camel_case ( ) ;
256
256
let resource_name_kebab = ty. name . as_ref ( ) . unwrap ( ) . to_kebab_case ( ) ;
257
257
let module_name = format ! ( "[export]{key_name}" ) ;
@@ -305,11 +305,8 @@ pub fn componentize_bindgen(
305
305
WorldItem :: Function ( _) => { }
306
306
WorldItem :: Type ( id) => {
307
307
let ty = & resolve. types [ * id] ;
308
- match ty. kind {
309
- TypeDefKind :: Resource => {
310
- imported_resource_modules. insert ( * id, key_name. clone ( ) ) ;
311
- }
312
- _ => { }
308
+ if ty. kind == TypeDefKind :: Resource {
309
+ imported_resource_modules. insert ( * id, key_name. clone ( ) ) ;
313
310
}
314
311
}
315
312
}
@@ -400,7 +397,7 @@ pub fn componentize_bindgen(
400
397
impl JsBindgen < ' _ > {
401
398
fn intrinsic ( & mut self , intrinsic : Intrinsic ) -> String {
402
399
self . all_intrinsics . insert ( intrinsic) ;
403
- return intrinsic. name ( ) . to_string ( ) ;
400
+ intrinsic. name ( ) . to_string ( )
404
401
}
405
402
406
403
fn exports_bindgen ( & mut self ) -> Result < ( ) > {
@@ -418,14 +415,7 @@ impl JsBindgen<'_> {
418
415
match export {
419
416
WorldItem :: Function ( func) => {
420
417
let local_name = self . local_names . create_once ( & func. name ) . to_string ( ) ;
421
- self . export_bindgen (
422
- name. into ( ) ,
423
- false ,
424
- None ,
425
- & local_name,
426
- StringEncoding :: UTF8 ,
427
- func,
428
- ) ;
418
+ self . export_bindgen ( name, false , None , & local_name, StringEncoding :: UTF8 , func) ;
429
419
self . esm_bindgen . add_export_func (
430
420
None ,
431
421
local_name. to_string ( ) ,
@@ -454,7 +444,7 @@ impl JsBindgen<'_> {
454
444
interface_name ( self . resolve , * id) ,
455
445
& local_name,
456
446
StringEncoding :: UTF8 ,
457
- & func,
447
+ func,
458
448
) ;
459
449
self . esm_bindgen . add_export_func (
460
450
Some ( name) ,
@@ -471,7 +461,7 @@ impl JsBindgen<'_> {
471
461
let local_name = self
472
462
. local_names
473
463
. get_or_create (
474
- & format ! ( "resource:{resource_name}" ) ,
464
+ format ! ( "resource:{resource_name}" ) ,
475
465
& resource_name,
476
466
)
477
467
. 0
@@ -482,10 +472,10 @@ impl JsBindgen<'_> {
482
472
interface_name ( self . resolve , * id) ,
483
473
& local_name,
484
474
StringEncoding :: UTF8 ,
485
- & func,
475
+ func,
486
476
) ;
487
477
self . esm_bindgen . ensure_exported_resource (
488
- Some ( & name) ,
478
+ Some ( name) ,
489
479
local_name,
490
480
resource_name,
491
481
) ;
@@ -517,7 +507,7 @@ impl JsBindgen<'_> {
517
507
. as_ref ( )
518
508
. unwrap ( )
519
509
. to_upper_camel_case ( ) ,
520
- & iface_name,
510
+ iface_name,
521
511
) ;
522
512
523
513
uwriteln ! ( self . src, "\n class import_{name} {{" ) ;
@@ -538,7 +528,7 @@ impl JsBindgen<'_> {
538
528
let prefix = iface_name
539
529
. as_deref ( )
540
530
. map ( |s| format ! ( "{s}$" ) )
541
- . unwrap_or ( String :: new ( ) ) ;
531
+ . unwrap_or_default ( ) ;
542
532
543
533
let resource_symbol = self . intrinsic ( Intrinsic :: SymbolResourceHandle ) ;
544
534
let dispose_symbol = self . intrinsic ( Intrinsic :: SymbolDispose ) ;
@@ -616,44 +606,38 @@ impl JsBindgen<'_> {
616
606
}
617
607
WorldItem :: Type ( id) => {
618
608
let ty = & self . resolve . types [ * id] ;
619
- match ty. kind {
620
- TypeDefKind :: Resource => {
621
- self . resource_directions
622
- . insert ( * id, AbiVariant :: GuestImport ) ;
623
-
624
- let resource_name = ty. name . as_ref ( ) . unwrap ( ) ;
625
-
626
- let mut resource_fns = Vec :: new ( ) ;
627
- for ( _, impt) in & self . resolve . worlds [ self . world ] . imports {
628
- match impt {
629
- WorldItem :: Function ( function) => {
630
- let stripped = if let Some ( stripped) =
631
- function. name . strip_prefix ( "[constructor]" )
632
- {
633
- stripped
634
- } else if let Some ( stripped) =
635
- function. name . strip_prefix ( "[method]" )
636
- {
637
- stripped
638
- } else if let Some ( stripped) =
639
- function. name . strip_prefix ( "[static]" )
640
- {
641
- stripped
642
- } else {
643
- continue ;
644
- } ;
645
-
646
- if stripped. starts_with ( resource_name) {
647
- resource_fns. push ( ( function. name . as_str ( ) , function) ) ;
648
- }
649
- }
650
- _ => { }
609
+ if ty. kind == TypeDefKind :: Resource {
610
+ self . resource_directions
611
+ . insert ( * id, AbiVariant :: GuestImport ) ;
612
+
613
+ let resource_name = ty. name . as_ref ( ) . unwrap ( ) ;
614
+
615
+ let mut resource_fns = Vec :: new ( ) ;
616
+ for ( _, impt) in & self . resolve . worlds [ self . world ] . imports {
617
+ if let WorldItem :: Function ( function) = impt {
618
+ let stripped = if let Some ( stripped) =
619
+ function. name . strip_prefix ( "[constructor]" )
620
+ {
621
+ stripped
622
+ } else if let Some ( stripped) =
623
+ function. name . strip_prefix ( "[method]" )
624
+ {
625
+ stripped
626
+ } else if let Some ( stripped) =
627
+ function. name . strip_prefix ( "[static]" )
628
+ {
629
+ stripped
630
+ } else {
631
+ continue ;
632
+ } ;
633
+
634
+ if stripped. starts_with ( resource_name) {
635
+ resource_fns. push ( ( function. name . as_str ( ) , function) ) ;
651
636
}
652
637
}
653
-
654
- self . resource_bindgen ( * id, "$root" , & None , resource_fns) ;
655
638
}
656
- _ => { }
639
+
640
+ self . resource_bindgen ( * id, "$root" , & None , resource_fns) ;
657
641
}
658
642
}
659
643
} ;
@@ -1082,8 +1066,7 @@ impl EsmBindgen {
1082
1066
iface = match iface. get_mut ( & iface_id_or_kebab) . unwrap ( ) {
1083
1067
Binding :: Interface ( iface) => iface,
1084
1068
Binding :: Resource ( _) | Binding :: Local ( _) => panic ! (
1085
- "Exported interface {} cannot be both a function and an interface or resource" ,
1086
- iface_id_or_kebab
1069
+ "Exported interface {iface_id_or_kebab} cannot be both a function and an interface or resource"
1087
1070
) ,
1088
1071
} ;
1089
1072
}
@@ -1113,8 +1096,7 @@ impl EsmBindgen {
1113
1096
iface = match iface. get_mut ( & iface_id_or_kebab) . unwrap ( ) {
1114
1097
Binding :: Interface ( iface) => iface,
1115
1098
Binding :: Resource ( _) | Binding :: Local ( _) => panic ! (
1116
- "Exported interface {} cannot be both a function and an interface or resource" ,
1117
- iface_id_or_kebab
1099
+ "Exported interface {iface_id_or_kebab} cannot be both a function and an interface or resource"
1118
1100
) ,
1119
1101
} ;
1120
1102
}
@@ -1128,7 +1110,7 @@ impl EsmBindgen {
1128
1110
let expt_name_sans_version = if let Some ( version_idx) = expt_name. find ( '@' ) {
1129
1111
& expt_name[ 0 ..version_idx]
1130
1112
} else {
1131
- & expt_name
1113
+ expt_name
1132
1114
} ;
1133
1115
if let Some ( alias) = interface_name_from_string ( expt_name_sans_version) {
1134
1116
if !self . exports . contains_key ( & alias)
@@ -1148,7 +1130,7 @@ impl EsmBindgen {
1148
1130
) {
1149
1131
// TODO: bring back these validations of imports
1150
1132
// including using the flattened bindings
1151
- if self . exports . len ( ) > 0 {
1133
+ if ! self . exports . is_empty ( ) {
1152
1134
// error handling
1153
1135
uwriteln ! ( output, "
1154
1136
class BindingsError extends Error {{
@@ -1298,12 +1280,9 @@ fn interface_name_from_string(name: &str) -> Option<String> {
1298
1280
let path_idx = name. rfind ( '/' ) ?;
1299
1281
let name = & name[ path_idx + 1 ..] ;
1300
1282
let at_idx = name. rfind ( '@' ) ;
1301
- let alias = name[ ..at_idx. unwrap_or_else ( || name. len ( ) ) ] . to_lower_camel_case ( ) ;
1283
+ let alias = name[ ..at_idx. unwrap_or ( name. len ( ) ) ] . to_lower_camel_case ( ) ;
1302
1284
let iface_name = Some ( if let Some ( at_idx) = at_idx {
1303
- format ! (
1304
- "{alias}_{}" ,
1305
- name[ at_idx + 1 ..] . replace( '.' , "_" ) . replace( '-' , "_" )
1306
- )
1285
+ format ! ( "{alias}_{}" , name[ at_idx + 1 ..] . replace( [ '.' , '-' ] , "_" ) )
1307
1286
} else {
1308
1287
alias
1309
1288
} ) ;
@@ -1313,23 +1292,21 @@ fn interface_name_from_string(name: &str) -> Option<String> {
1313
1292
fn binding_name ( func_name : & str , iface_name : & Option < String > ) -> String {
1314
1293
match iface_name {
1315
1294
Some ( iface_name) => format ! ( "{iface_name}${func_name}" ) ,
1316
- None => format ! ( "{ func_name}" ) ,
1295
+ None => func_name. to_string ( ) ,
1317
1296
}
1318
1297
}
1319
1298
1320
1299
/// Extract success and error types from a given optional type, if it is a Result
1321
- pub fn get_result_types < ' a > (
1322
- resolve : & ' a Resolve ,
1300
+ pub fn get_result_types (
1301
+ resolve : & Resolve ,
1323
1302
return_type : Option < Type > ,
1324
- ) -> Option < ( Option < & ' a Type > , Option < & ' a Type > ) > {
1303
+ ) -> Option < ( Option < & Type > , Option < & Type > ) > {
1325
1304
match return_type {
1326
1305
None => None ,
1327
- Some ( ty) => match ty {
1328
- Type :: Id ( id) => match & resolve. types [ id] . kind {
1329
- TypeDefKind :: Result ( r) => Some ( ( r. ok . as_ref ( ) , r. err . as_ref ( ) ) ) ,
1330
- _ => None ,
1331
- } ,
1306
+ Some ( Type :: Id ( id) ) => match & resolve. types [ id] . kind {
1307
+ TypeDefKind :: Result ( r) => Some ( ( r. ok . as_ref ( ) , r. err . as_ref ( ) ) ) ,
1332
1308
_ => None ,
1333
1309
} ,
1310
+ _ => None ,
1334
1311
}
1335
1312
}
0 commit comments