MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / name_package_module

Method name_package_module

crates/wit-bindgen/src/lib.rs:315–354  ·  view source on GitHub ↗

If the package `id` is the only package with its namespace/name combo then pass through the name unmodified. If, however, there are multiple versions of this package then the package module is going to get version information.

(&self, resolve: &Resolve, id: PackageId)

Source from the content-addressed store, hash-verified

313 /// versions of this package then the package module is going to get version
314 /// information.
315 fn name_package_module(&self, resolve: &Resolve, id: PackageId) -> String {
316 let pkg = &resolve.packages[id];
317 let versions_with_same_name = resolve
318 .packages
319 .iter()
320 .filter_map(|(_, p)| {
321 if p.name.namespace == pkg.name.namespace && p.name.name == pkg.name.name {
322 Some(&p.name.version)
323 } else {
324 None
325 }
326 })
327 .collect::<Vec<_>>();
328 let base = pkg.name.name.to_snake_case();
329 if versions_with_same_name.len() == 1 {
330 return base;
331 }
332
333 let version = match &pkg.name.version {
334 Some(version) => version,
335 // If this package didn't have a version then don't mangle its name
336 // and other packages with the same name but with versions present
337 // will have their names mangled.
338 None => return base,
339 };
340
341 // Here there's multiple packages with the same name that differ only in
342 // version, so the version needs to be mangled into the Rust module name
343 // that we're generating. This in theory could look at all of
344 // `versions_with_same_name` and produce a minimal diff, e.g. for 0.1.0
345 // and 0.2.0 this could generate "foo1" and "foo2", but for now
346 // a simpler path is chosen to generate "foo0_1_0" and "foo0_2_0".
347 let version = version
348 .to_string()
349 .replace('.', "_")
350 .replace('-', "_")
351 .replace('+', "_")
352 .to_snake_case();
353 format!("{base}{version}")
354 }
355
356 fn generate(&mut self, resolve: &Resolve, id: WorldId) -> anyhow::Result<String> {
357 self.types.analyze(resolve, id);

Callers 1

Calls 4

iterMethod · 0.45
lenMethod · 0.45
replaceMethod · 0.45
to_stringMethod · 0.45

Tested by

no test coverage detected