Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Fix function name generator #48

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 36 additions & 35 deletions wasm-rpc-stubgen/src/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,8 @@ pub fn generate_stub_source(def: &StubDefinition) -> anyhow::Result<()> {
fn_impls.push(generate_function_stub_source(
def,
function,
if interface.global {
None
} else {
match &interface.owner_interface {
Some(owner) => Some(format!("{owner}/{}", &interface.name)),
None => Some(interface.name.clone()),
}
},
interface.interface_name(),
interface.resource_name(),
if interface.is_resource() {
FunctionMode::Method
} else {
Expand All @@ -105,14 +99,8 @@ pub fn generate_stub_source(def: &StubDefinition) -> anyhow::Result<()> {
fn_impls.push(generate_function_stub_source(
def,
function,
if interface.global {
None
} else {
match &interface.owner_interface {
Some(owner) => Some(format!("{owner}/{}", &interface.name)),
None => Some(interface.name.clone()),
}
},
interface.interface_name(),
interface.resource_name(),
FunctionMode::Static,
)?);
}
Expand All @@ -132,11 +120,8 @@ pub fn generate_stub_source(def: &StubDefinition) -> anyhow::Result<()> {
generate_function_stub_source(
def,
&constructor_stub,
Some(format!(
"{}/{}",
interface.owner_interface.clone().unwrap_or_default(),
&interface.name
)),
interface.interface_name(),
interface.resource_name(),
FunctionMode::Constructor,
)?
} else {
Expand All @@ -158,16 +143,13 @@ pub fn generate_stub_source(def: &StubDefinition) -> anyhow::Result<()> {
}
});

let remote_function_name = get_remote_function_name(
def,
"drop",
interface.interface_name().as_ref(),
interface.resource_name().as_ref(),
);
if interface.is_resource() {
let remote_function_name = get_remote_function_name(
def,
"drop",
Some(&format!(
"{}/{}",
interface.owner_interface.clone().unwrap_or_default(),
&interface.name
)),
);
interface_impls.push(quote! {
impl Drop for #interface_name {
fn drop(&mut self) {
Expand Down Expand Up @@ -220,6 +202,7 @@ fn generate_function_stub_source(
def: &StubDefinition,
function: &FunctionStub,
interface_name: Option<String>,
resource_name: Option<String>,
mode: FunctionMode,
) -> anyhow::Result<TokenStream> {
let function_name = Ident::new(&to_rust_ident(&function.name), Span::call_site());
Expand Down Expand Up @@ -323,8 +306,12 @@ fn generate_function_stub_source(
}
}

let remote_function_name =
get_remote_function_name(def, &function.name, interface_name.as_ref());
let remote_function_name = get_remote_function_name(
def,
&function.name,
interface_name.as_ref(),
resource_name.as_ref(),
);

let rpc = match mode {
FunctionMode::Static => {
Expand Down Expand Up @@ -403,16 +390,30 @@ fn get_remote_function_name(
def: &StubDefinition,
function_name: &str,
interface_name: Option<&String>,
resource_name: Option<&String>,
) -> String {
match interface_name {
Some(remote_interface) => format!(
match (interface_name, resource_name) {
(Some(remote_interface), None) => format!(
"{}:{}/{}.{{{}}}",
def.root_package_name.namespace,
def.root_package_name.name,
remote_interface,
function_name
),
None => function_name.to_string(),
(Some(remote_interface), Some(resource)) => {
format!(
"{}:{}/{}.{{{}.{}}}",
def.root_package_name.namespace,
def.root_package_name.name,
remote_interface,
resource,
function_name
)
}
(None, Some(resource)) => {
format!("{}.{}", resource, function_name)
}
(None, None) => function_name.to_string(),
}
}

Expand Down
19 changes: 19 additions & 0 deletions wasm-rpc-stubgen/src/stub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,25 @@ impl InterfaceStub {
pub fn is_resource(&self) -> bool {
self.constructor_params.is_some()
}

pub fn interface_name(&self) -> Option<String> {
if self.global {
None
} else {
match &self.owner_interface {
Some(iface) => Some(iface.clone()),
None => Some(self.name.clone()),
}
}
}

pub fn resource_name(&self) -> Option<String> {
if self.is_resource() {
Some(self.name.clone())
} else {
None
}
}
}

#[derive(Debug, Clone, PartialEq)]
Expand Down
Loading