Skip to content

Commit 5cefd26

Browse files
chore: fix clippy
1 parent e783878 commit 5cefd26

File tree

5 files changed

+82
-111
lines changed

5 files changed

+82
-111
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ enum Commands {
7777
/// http,
7878
/// fetch-event,
7979
///}
80-
fn map_features(features: &Vec<String>) -> Result<Vec<Features>> {
80+
fn map_features(features: &[String]) -> Result<Vec<Features>> {
8181
features
8282
.iter()
8383
.map(|f| Features::from_str(f.as_str()))
@@ -137,13 +137,13 @@ fn main() -> Result<()> {
137137
splice::splice_bindings(engine, features, None, wit_path_str, world_name, debug)
138138
.map_err(|e| anyhow::anyhow!(e))?;
139139

140-
fs::write(&out_dir.join("component.wasm"), result.wasm).with_context(|| {
140+
fs::write(out_dir.join("component.wasm"), result.wasm).with_context(|| {
141141
format!(
142142
"Failed to write output file: {}",
143143
out_dir.join("component.wasm").display()
144144
)
145145
})?;
146-
fs::write(&out_dir.join("initializer.js"), result.js_bindings).with_context(|| {
146+
fs::write(out_dir.join("initializer.js"), result.js_bindings).with_context(|| {
147147
format!(
148148
"Failed to write output file: {}",
149149
out_dir.join("initializer.js").display()

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

Lines changed: 54 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ pub fn componentize_bindgen(
201201
for (specifier, by_resource) in by_specifier_by_resource {
202202
let mut specifier_list = Vec::new();
203203
for (resource, items) in by_resource {
204-
let item = items.iter().next().unwrap();
204+
let item = items.first().unwrap();
205205
if let Some(resource) = resource {
206206
let export_name = resource.to_upper_camel_case();
207207
let binding_name = binding_name(&export_name, &item.iface_name);
@@ -251,7 +251,7 @@ pub fn componentize_bindgen(
251251
if let TypeDefKind::Resource = &ty.kind {
252252
let iface_prefix = interface_name(resolve, *iface_id)
253253
.map(|s| format!("{s}$"))
254-
.unwrap_or_else(String::new);
254+
.unwrap_or_default();
255255
let resource_name_camel = ty.name.as_ref().unwrap().to_lower_camel_case();
256256
let resource_name_kebab = ty.name.as_ref().unwrap().to_kebab_case();
257257
let module_name = format!("[export]{key_name}");
@@ -305,11 +305,8 @@ pub fn componentize_bindgen(
305305
WorldItem::Function(_) => {}
306306
WorldItem::Type(id) => {
307307
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());
313310
}
314311
}
315312
}
@@ -400,7 +397,7 @@ pub fn componentize_bindgen(
400397
impl JsBindgen<'_> {
401398
fn intrinsic(&mut self, intrinsic: Intrinsic) -> String {
402399
self.all_intrinsics.insert(intrinsic);
403-
return intrinsic.name().to_string();
400+
intrinsic.name().to_string()
404401
}
405402

406403
fn exports_bindgen(&mut self) -> Result<()> {
@@ -418,14 +415,7 @@ impl JsBindgen<'_> {
418415
match export {
419416
WorldItem::Function(func) => {
420417
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);
429419
self.esm_bindgen.add_export_func(
430420
None,
431421
local_name.to_string(),
@@ -454,7 +444,7 @@ impl JsBindgen<'_> {
454444
interface_name(self.resolve, *id),
455445
&local_name,
456446
StringEncoding::UTF8,
457-
&func,
447+
func,
458448
);
459449
self.esm_bindgen.add_export_func(
460450
Some(name),
@@ -471,7 +461,7 @@ impl JsBindgen<'_> {
471461
let local_name = self
472462
.local_names
473463
.get_or_create(
474-
&format!("resource:{resource_name}"),
464+
format!("resource:{resource_name}"),
475465
&resource_name,
476466
)
477467
.0
@@ -482,10 +472,10 @@ impl JsBindgen<'_> {
482472
interface_name(self.resolve, *id),
483473
&local_name,
484474
StringEncoding::UTF8,
485-
&func,
475+
func,
486476
);
487477
self.esm_bindgen.ensure_exported_resource(
488-
Some(&name),
478+
Some(name),
489479
local_name,
490480
resource_name,
491481
);
@@ -517,7 +507,7 @@ impl JsBindgen<'_> {
517507
.as_ref()
518508
.unwrap()
519509
.to_upper_camel_case(),
520-
&iface_name,
510+
iface_name,
521511
);
522512

523513
uwriteln!(self.src, "\nclass import_{name} {{");
@@ -538,7 +528,7 @@ impl JsBindgen<'_> {
538528
let prefix = iface_name
539529
.as_deref()
540530
.map(|s| format!("{s}$"))
541-
.unwrap_or(String::new());
531+
.unwrap_or_default();
542532

543533
let resource_symbol = self.intrinsic(Intrinsic::SymbolResourceHandle);
544534
let dispose_symbol = self.intrinsic(Intrinsic::SymbolDispose);
@@ -616,44 +606,38 @@ impl JsBindgen<'_> {
616606
}
617607
WorldItem::Type(id) => {
618608
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));
651636
}
652637
}
653-
654-
self.resource_bindgen(*id, "$root", &None, resource_fns);
655638
}
656-
_ => {}
639+
640+
self.resource_bindgen(*id, "$root", &None, resource_fns);
657641
}
658642
}
659643
};
@@ -1082,8 +1066,7 @@ impl EsmBindgen {
10821066
iface = match iface.get_mut(&iface_id_or_kebab).unwrap() {
10831067
Binding::Interface(iface) => iface,
10841068
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"
10871070
),
10881071
};
10891072
}
@@ -1113,8 +1096,7 @@ impl EsmBindgen {
11131096
iface = match iface.get_mut(&iface_id_or_kebab).unwrap() {
11141097
Binding::Interface(iface) => iface,
11151098
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"
11181100
),
11191101
};
11201102
}
@@ -1128,7 +1110,7 @@ impl EsmBindgen {
11281110
let expt_name_sans_version = if let Some(version_idx) = expt_name.find('@') {
11291111
&expt_name[0..version_idx]
11301112
} else {
1131-
&expt_name
1113+
expt_name
11321114
};
11331115
if let Some(alias) = interface_name_from_string(expt_name_sans_version) {
11341116
if !self.exports.contains_key(&alias)
@@ -1148,7 +1130,7 @@ impl EsmBindgen {
11481130
) {
11491131
// TODO: bring back these validations of imports
11501132
// including using the flattened bindings
1151-
if self.exports.len() > 0 {
1133+
if !self.exports.is_empty() {
11521134
// error handling
11531135
uwriteln!(output, "
11541136
class BindingsError extends Error {{
@@ -1298,12 +1280,9 @@ fn interface_name_from_string(name: &str) -> Option<String> {
12981280
let path_idx = name.rfind('/')?;
12991281
let name = &name[path_idx + 1..];
13001282
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();
13021284
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(['.', '-'], "_"))
13071286
} else {
13081287
alias
13091288
});
@@ -1313,23 +1292,21 @@ fn interface_name_from_string(name: &str) -> Option<String> {
13131292
fn binding_name(func_name: &str, iface_name: &Option<String>) -> String {
13141293
match iface_name {
13151294
Some(iface_name) => format!("{iface_name}${func_name}"),
1316-
None => format!("{func_name}"),
1295+
None => func_name.to_string(),
13171296
}
13181297
}
13191298

13201299
/// 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,
13231302
return_type: Option<Type>,
1324-
) -> Option<(Option<&'a Type>, Option<&'a Type>)> {
1303+
) -> Option<(Option<&Type>, Option<&Type>)> {
13251304
match return_type {
13261305
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())),
13321308
_ => None,
13331309
},
1310+
_ => None,
13341311
}
13351312
}

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ fn map_core_fn(cfn: &bindgen::CoreFn) -> CoreFn {
5555
} = cfn;
5656
CoreFn {
5757
params: params.iter().map(&map_core_ty).collect(),
58-
ret: match ret {
59-
Some(ref core_ty) => Some(map_core_ty(core_ty)),
60-
None => None,
61-
},
58+
ret: ret.as_ref().map(map_core_ty),
6259
retptr: *retptr,
6360
retsize: *retsize,
6461
paramptr: *paramptr,
@@ -70,17 +67,17 @@ fn parse_wit(path: impl AsRef<Path>) -> Result<(Resolve, PackageId)> {
7067
let path = path.as_ref();
7168
let id = if path.is_dir() {
7269
resolve
73-
.push_dir(&path)
70+
.push_dir(path)
7471
.with_context(|| format!("resolving WIT in {}", path.display()))?
7572
.0
7673
} else {
7774
let contents =
78-
std::fs::read(&path).with_context(|| format!("reading file {}", path.display()))?;
75+
std::fs::read(path).with_context(|| format!("reading file {}", path.display()))?;
7976
let text = match std::str::from_utf8(&contents) {
8077
Ok(s) => s,
8178
Err(_) => bail!("input file is not valid utf-8"),
8279
};
83-
resolve.push_str(&path, text)?
80+
resolve.push_str(path, text)?
8481
};
8582
Ok((resolve, id))
8683
}

0 commit comments

Comments
 (0)