Skip to content

Commit 1cef2b5

Browse files
chore: fix clippy lints
1 parent c8a54cf commit 1cef2b5

File tree

5 files changed

+83
-101
lines changed

5 files changed

+83
-101
lines changed

crates/spidermonkey-embedding-splicer/src/bin/splicer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ enum Commands {
7070
/// random,
7171
/// http,
7272
///}
73-
fn map_features(features: &Vec<String>) -> Vec<Features> {
73+
fn map_features(features: &[String]) -> Vec<Features> {
7474
features
7575
.iter()
7676
.map(|f| match f.as_str() {
7777
"stdio" => Features::Stdio,
7878
"clocks" => Features::Clocks,
7979
"random" => Features::Random,
8080
"http" => Features::Http,
81-
_ => panic!("Unknown feature: {}", f),
81+
_ => panic!("Unknown feature: {f}"),
8282
})
8383
.collect()
8484
}
@@ -131,13 +131,13 @@ fn main() -> Result<()> {
131131

132132
let result = splice::splice_bindings(engine, world_name, wit_path_str, None, debug)
133133
.map_err(|e| anyhow::anyhow!(e))?;
134-
fs::write(&out_dir.join("component.wasm"), result.wasm).with_context(|| {
134+
fs::write(out_dir.join("component.wasm"), result.wasm).with_context(|| {
135135
format!(
136136
"Failed to write output file: {}",
137137
out_dir.join("component.wasm").display()
138138
)
139139
})?;
140-
fs::write(&out_dir.join("initializer.js"), result.js_bindings).with_context(|| {
140+
fs::write(out_dir.join("initializer.js"), result.js_bindings).with_context(|| {
141141
format!(
142142
"Failed to write output file: {}",
143143
out_dir.join("initializer.js").display()

crates/spidermonkey-embedding-splicer/src/bindgen.rs

Lines changed: 54 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ pub fn componentize_bindgen(resolve: &Resolve, wid: WorldId) -> Result<Component
191191
for (specifier, by_resource) in by_specifier_by_resource {
192192
let mut specifier_list = Vec::new();
193193
for (resource, items) in by_resource {
194-
let item = items.iter().next().unwrap();
194+
let item = items.first().unwrap();
195195
if let Some(resource) = resource {
196196
let export_name = resource.to_upper_camel_case();
197197
let binding_name = binding_name(&export_name, &item.iface_name);
@@ -241,7 +241,7 @@ pub fn componentize_bindgen(resolve: &Resolve, wid: WorldId) -> Result<Component
241241
if let TypeDefKind::Resource = &ty.kind {
242242
let iface_prefix = interface_name(resolve, *iface_id)
243243
.map(|s| format!("{s}$"))
244-
.unwrap_or_else(String::new);
244+
.unwrap_or_default();
245245
let resource_name_camel = ty.name.as_ref().unwrap().to_lower_camel_case();
246246
let resource_name_kebab = ty.name.as_ref().unwrap().to_kebab_case();
247247
let module_name = format!("[export]{key_name}");
@@ -295,11 +295,8 @@ pub fn componentize_bindgen(resolve: &Resolve, wid: WorldId) -> Result<Component
295295
WorldItem::Function(_) => {}
296296
WorldItem::Type(id) => {
297297
let ty = &resolve.types[*id];
298-
match ty.kind {
299-
TypeDefKind::Resource => {
300-
imported_resource_modules.insert(*id, key_name.clone());
301-
}
302-
_ => {}
298+
if ty.kind == TypeDefKind::Resource {
299+
imported_resource_modules.insert(*id, key_name.clone());
303300
}
304301
}
305302
}
@@ -390,7 +387,7 @@ pub fn componentize_bindgen(resolve: &Resolve, wid: WorldId) -> Result<Component
390387
impl JsBindgen<'_> {
391388
fn intrinsic(&mut self, intrinsic: Intrinsic) -> String {
392389
self.all_intrinsics.insert(intrinsic);
393-
return intrinsic.name().to_string();
390+
intrinsic.name().to_string()
394391
}
395392

396393
fn exports_bindgen(
@@ -449,7 +446,7 @@ impl JsBindgen<'_> {
449446
WorldItem::Function(func) => {
450447
let local_name = self.local_names.create_once(&func.name).to_string();
451448
self.export_bindgen(
452-
name.into(),
449+
name,
453450
false,
454451
None,
455452
&local_name,
@@ -484,7 +481,7 @@ impl JsBindgen<'_> {
484481
interface_name(self.resolve, *id),
485482
&local_name,
486483
StringEncoding::UTF8,
487-
&func,
484+
func,
488485
);
489486
self.esm_bindgen.add_export_func(
490487
Some(name),
@@ -501,7 +498,7 @@ impl JsBindgen<'_> {
501498
let local_name = self
502499
.local_names
503500
.get_or_create(
504-
&format!("resource:{resource_name}"),
501+
format!("resource:{resource_name}"),
505502
&resource_name,
506503
)
507504
.0
@@ -512,10 +509,10 @@ impl JsBindgen<'_> {
512509
interface_name(self.resolve, *id),
513510
&local_name,
514511
StringEncoding::UTF8,
515-
&func,
512+
func,
516513
);
517514
self.esm_bindgen.ensure_exported_resource(
518-
Some(&name),
515+
Some(name),
519516
local_name,
520517
resource_name,
521518
);
@@ -547,7 +544,7 @@ impl JsBindgen<'_> {
547544
.as_ref()
548545
.unwrap()
549546
.to_upper_camel_case(),
550-
&iface_name,
547+
iface_name,
551548
);
552549

553550
uwriteln!(self.src, "\nclass import_{name} {{");
@@ -568,7 +565,7 @@ impl JsBindgen<'_> {
568565
let prefix = iface_name
569566
.as_deref()
570567
.map(|s| format!("{s}$"))
571-
.unwrap_or(String::new());
568+
.unwrap_or_default();
572569

573570
let resource_symbol = self.intrinsic(Intrinsic::SymbolResourceHandle);
574571
let dispose_symbol = self.intrinsic(Intrinsic::SymbolDispose);
@@ -646,44 +643,38 @@ impl JsBindgen<'_> {
646643
}
647644
WorldItem::Type(id) => {
648645
let ty = &self.resolve.types[*id];
649-
match ty.kind {
650-
TypeDefKind::Resource => {
651-
self.resource_directions
652-
.insert(*id, AbiVariant::GuestImport);
653-
654-
let resource_name = ty.name.as_ref().unwrap();
655-
656-
let mut resource_fns = Vec::new();
657-
for (_, impt) in &self.resolve.worlds[self.world].imports {
658-
match impt {
659-
WorldItem::Function(function) => {
660-
let stripped = if let Some(stripped) =
661-
function.name.strip_prefix("[constructor]")
662-
{
663-
stripped
664-
} else if let Some(stripped) =
665-
function.name.strip_prefix("[method]")
666-
{
667-
stripped
668-
} else if let Some(stripped) =
669-
function.name.strip_prefix("[static]")
670-
{
671-
stripped
672-
} else {
673-
continue;
674-
};
675-
676-
if stripped.starts_with(resource_name) {
677-
resource_fns.push((function.name.as_str(), function));
678-
}
679-
}
680-
_ => {}
646+
if ty.kind == TypeDefKind::Resource {
647+
self.resource_directions
648+
.insert(*id, AbiVariant::GuestImport);
649+
650+
let resource_name = ty.name.as_ref().unwrap();
651+
652+
let mut resource_fns = Vec::new();
653+
for (_, impt) in &self.resolve.worlds[self.world].imports {
654+
if let WorldItem::Function(function) = impt {
655+
let stripped = if let Some(stripped) =
656+
function.name.strip_prefix("[constructor]")
657+
{
658+
stripped
659+
} else if let Some(stripped) =
660+
function.name.strip_prefix("[method]")
661+
{
662+
stripped
663+
} else if let Some(stripped) =
664+
function.name.strip_prefix("[static]")
665+
{
666+
stripped
667+
} else {
668+
continue;
669+
};
670+
671+
if stripped.starts_with(resource_name) {
672+
resource_fns.push((function.name.as_str(), function));
681673
}
682674
}
683-
684-
self.resource_bindgen(*id, "$root", &None, resource_fns);
685675
}
686-
_ => {}
676+
677+
self.resource_bindgen(*id, "$root", &None, resource_fns);
687678
}
688679
}
689680
};
@@ -1112,8 +1103,7 @@ impl EsmBindgen {
11121103
iface = match iface.get_mut(&iface_id_or_kebab).unwrap() {
11131104
Binding::Interface(iface) => iface,
11141105
Binding::Resource(_) | Binding::Local(_) => panic!(
1115-
"Exported interface {} cannot be both a function and an interface or resource",
1116-
iface_id_or_kebab
1106+
"Exported interface {iface_id_or_kebab} cannot be both a function and an interface or resource"
11171107
),
11181108
};
11191109
}
@@ -1143,8 +1133,7 @@ impl EsmBindgen {
11431133
iface = match iface.get_mut(&iface_id_or_kebab).unwrap() {
11441134
Binding::Interface(iface) => iface,
11451135
Binding::Resource(_) | Binding::Local(_) => panic!(
1146-
"Exported interface {} cannot be both a function and an interface or resource",
1147-
iface_id_or_kebab
1136+
"Exported interface {iface_id_or_kebab} cannot be both a function and an interface or resource"
11481137
),
11491138
};
11501139
}
@@ -1158,7 +1147,7 @@ impl EsmBindgen {
11581147
let expt_name_sans_version = if let Some(version_idx) = expt_name.find('@') {
11591148
&expt_name[0..version_idx]
11601149
} else {
1161-
&expt_name
1150+
expt_name
11621151
};
11631152
if let Some(alias) = interface_name_from_string(expt_name_sans_version) {
11641153
if !self.exports.contains_key(&alias)
@@ -1178,7 +1167,7 @@ impl EsmBindgen {
11781167
) {
11791168
// TODO: bring back these validations of imports
11801169
// including using the flattened bindings
1181-
if self.exports.len() > 0 {
1170+
if !self.exports.is_empty() {
11821171
// error handling
11831172
uwriteln!(output, "
11841173
class BindingsError extends Error {{
@@ -1328,11 +1317,11 @@ fn interface_name_from_string(name: &str) -> Option<String> {
13281317
let path_idx = name.rfind('/')?;
13291318
let name = &name[path_idx + 1..];
13301319
let at_idx = name.rfind('@');
1331-
let alias = name[..at_idx.unwrap_or_else(|| name.len())].to_lower_camel_case();
1320+
let alias = name[..at_idx.unwrap_or(name.len())].to_lower_camel_case();
13321321
let iface_name = Some(if let Some(at_idx) = at_idx {
13331322
format!(
13341323
"{alias}_{}",
1335-
name[at_idx + 1..].replace('.', "_").replace('-', "_")
1324+
name[at_idx + 1..].replace(['.', '-'], "_")
13361325
)
13371326
} else {
13381327
alias
@@ -1343,23 +1332,21 @@ fn interface_name_from_string(name: &str) -> Option<String> {
13431332
fn binding_name(func_name: &str, iface_name: &Option<String>) -> String {
13441333
match iface_name {
13451334
Some(iface_name) => format!("{iface_name}${func_name}"),
1346-
None => format!("{func_name}"),
1335+
None => func_name.to_string(),
13471336
}
13481337
}
13491338

13501339
/// Extract success and error types from a given optional type, if it is a Result
1351-
pub fn get_result_types<'a>(
1352-
resolve: &'a Resolve,
1340+
pub fn get_result_types(
1341+
resolve: &Resolve,
13531342
return_type: Option<Type>,
1354-
) -> Option<(Option<&'a Type>, Option<&'a Type>)> {
1343+
) -> Option<(Option<&Type>, Option<&Type>)> {
13551344
match return_type {
13561345
None => None,
1357-
Some(ty) => match ty {
1358-
Type::Id(id) => match &resolve.types[id].kind {
1359-
TypeDefKind::Result(r) => Some((r.ok.as_ref(), r.err.as_ref())),
1360-
_ => None,
1361-
},
1346+
Some(Type::Id(id)) => match &resolve.types[id].kind {
1347+
TypeDefKind::Result(r) => Some((r.ok.as_ref(), r.err.as_ref())),
13621348
_ => None,
13631349
},
1350+
_ => None,
13641351
}
13651352
}

crates/spidermonkey-embedding-splicer/src/lib.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::wit::{CoreFn, CoreTy};
99
use wit_parser::{PackageId, Resolve};
1010

1111
pub mod wit {
12+
#![allow(clippy::too_many_arguments)]
1213
wit_bindgen::generate!({
1314
world: "spidermonkey-embedding-splicer",
1415
pub_export_macro: true
@@ -60,10 +61,7 @@ fn map_core_fn(cfn: &bindgen::CoreFn) -> CoreFn {
6061
} = cfn;
6162
CoreFn {
6263
params: params.iter().map(&map_core_ty).collect(),
63-
ret: match ret {
64-
Some(ref core_ty) => Some(map_core_ty(core_ty)),
65-
None => None,
66-
},
64+
ret: ret.as_ref().map(map_core_ty),
6765
retptr: *retptr,
6866
retsize: *retsize,
6967
paramptr: *paramptr,
@@ -75,17 +73,17 @@ fn parse_wit(path: impl AsRef<Path>) -> Result<(Resolve, PackageId)> {
7573
let path = path.as_ref();
7674
let id = if path.is_dir() {
7775
resolve
78-
.push_dir(&path)
76+
.push_dir(path)
7977
.with_context(|| format!("resolving WIT in {}", path.display()))?
8078
.0
8179
} else {
8280
let contents =
83-
std::fs::read(&path).with_context(|| format!("reading file {}", path.display()))?;
81+
std::fs::read(path).with_context(|| format!("reading file {}", path.display()))?;
8482
let text = match std::str::from_utf8(&contents) {
8583
Ok(s) => s,
8684
Err(_) => bail!("input file is not valid utf-8"),
8785
};
88-
resolve.push_str(&path, text)?
86+
resolve.push_str(path, text)?
8987
};
9088
Ok((resolve, id))
9189
}

0 commit comments

Comments
 (0)