feat(rust): implement resource

This commit is contained in:
Jonas Kruckenberg
2023-09-10 14:29:14 +02:00
parent a40ec95b2d
commit 4954d865f1
8 changed files with 654 additions and 572 deletions

View File

@@ -73,7 +73,10 @@ pub struct TypeInfos {
impl TypeInfos {
#[must_use]
pub fn collect_from_functions(typedefs: &TypeDefArena, functions: &[Function]) -> Self {
pub fn collect_from_functions<'a>(
typedefs: &TypeDefArena,
functions: impl Iterator<Item = &'a Function>,
) -> Self {
let mut this = Self::default();
for func in functions {

View File

@@ -23,7 +23,22 @@ pub struct Builder {
impl GeneratorBuilder for Builder {
fn build(self, interface: Interface) -> Box<dyn Generate> {
let infos = TypeInfos::collect_from_functions(&interface.typedefs, &interface.functions);
let methods = interface
.typedefs
.iter()
.filter_map(|(_, typedef)| {
if let TypeDefKind::Resource(methods) = &typedef.kind {
Some(methods.iter())
} else {
None
}
})
.flatten();
let infos = TypeInfos::collect_from_functions(
&interface.typedefs,
interface.functions.iter().chain(methods),
);
let serde_utils =
SerdeUtils::collect_from_functions(&interface.typedefs, &interface.functions);

View File

@@ -14,6 +14,7 @@ use tauri_bindgen_core::TypeInfo;
use tauri_bindgen_core::TypeInfos;
use tauri_bindgen_gen_rust::FnSig;
use tauri_bindgen_gen_rust::{BorrowMode, RustGenerator};
use wit_parser::TypeDefKind;
use wit_parser::{Function, Interface};
#[derive(Default, Debug, Clone)]
@@ -35,7 +36,22 @@ pub struct Builder {
impl GeneratorBuilder for Builder {
fn build(self, interface: Interface) -> Box<dyn Generate> {
let infos = TypeInfos::collect_from_functions(&interface.typedefs, &interface.functions);
let methods = interface
.typedefs
.iter()
.filter_map(|(_, typedef)| {
if let TypeDefKind::Resource(methods) = &typedef.kind {
Some(methods.iter())
} else {
None
}
})
.flatten();
let infos = TypeInfos::collect_from_functions(
&interface.typedefs,
interface.functions.iter().chain(methods),
);
Box::new(RustWasm {
opts: self,
@@ -117,6 +133,7 @@ impl RustGenerator for RustWasm {
fn print_resource(
&self,
mod_ident: &str,
docs: &str,
ident: &proc_macro2::Ident,
functions: &[Function],
@@ -139,9 +156,17 @@ impl RustGenerator for RustWasm {
&BorrowMode::Owned,
);
let mod_ident = format!("{mod_ident}::resource::{}", ident.to_string().to_snake_case());
let ident = func.ident.to_snake_case();
let param_idents = func
.params
.iter()
.map(|(ident, _)| format_ident!("{}", ident));
quote! {
#sig {
todo!()
::tauri_bindgen_guest_rust::invoke(#mod_ident, #ident, &(self.0, #(#param_idents),*)).await.unwrap()
}
}
});
@@ -149,9 +174,7 @@ impl RustGenerator for RustWasm {
quote! {
#docs
#additional_attrs
pub struct #ident {
id: u64
}
pub struct #ident(u32);
impl #ident {
#(#functions)*

View File

@@ -3,34 +3,42 @@
pub mod resources {
use ::tauri_bindgen_guest_rust::serde;
use ::tauri_bindgen_guest_rust::bitflags;
#[derive(serde::Deserialize)]
pub struct A {
id: u64,
}
#[derive(serde::Serialize, serde::Deserialize)]
pub struct A(u32);
impl A {
pub async fn f1(&self) {
todo!()
::tauri_bindgen_guest_rust::invoke("resources;::resource::A", "f1", &())
.await
.unwrap()
}
pub async fn f2(&self, a: u32) {
todo!()
::tauri_bindgen_guest_rust::invoke("resources;::resource::A", "f2", &(a))
.await
.unwrap()
}
pub async fn f3(&self, a: u32, b: u32) {
todo!()
::tauri_bindgen_guest_rust::invoke("resources;::resource::A", "f3", &(a, b))
.await
.unwrap()
}
}
#[derive(serde::Deserialize)]
pub struct B {
id: u64,
}
pub struct B(u32);
impl B {
pub async fn f1(&self) -> A {
todo!()
::tauri_bindgen_guest_rust::invoke("resources;::resource::B", "f1", &())
.await
.unwrap()
}
pub async fn f2(&self, x: A) -> Result<u32, ()> {
todo!()
::tauri_bindgen_guest_rust::invoke("resources;::resource::B", "f2", &(x))
.await
.unwrap()
}
pub async fn f3(&self, x: Option<&'_ [A]>) -> Result<A, ()> {
todo!()
::tauri_bindgen_guest_rust::invoke("resources;::resource::B", "f3", &(x))
.await
.unwrap()
}
}
pub async fn constructor_a() -> A {

View File

@@ -31,7 +31,22 @@ pub struct Builder {
impl GeneratorBuilder for Builder {
fn build(self, interface: Interface) -> Box<dyn Generate> {
let infos = TypeInfos::collect_from_functions(&interface.typedefs, &interface.functions);
let methods = interface
.typedefs
.iter()
.filter_map(|(_, typedef)| {
if let TypeDefKind::Resource(methods) = &typedef.kind {
Some(methods.iter())
} else {
None
}
})
.flatten();
let infos = TypeInfos::collect_from_functions(
&interface.typedefs,
interface.functions.iter().chain(methods),
);
let serde_utils =
SerdeUtils::collect_from_functions(&interface.typedefs, &interface.functions);

File diff suppressed because it is too large Load Diff

View File

@@ -34,7 +34,22 @@ pub struct Builder {
impl GeneratorBuilder for Builder {
fn build(self, interface: Interface) -> Box<dyn Generate> {
let infos = TypeInfos::collect_from_functions(&interface.typedefs, &interface.functions);
let methods = interface
.typedefs
.iter()
.filter_map(|(_, typedef)| {
if let TypeDefKind::Resource(methods) = &typedef.kind {
Some(methods.iter())
} else {
None
}
})
.flatten();
let infos = TypeInfos::collect_from_functions(
&interface.typedefs,
interface.functions.iter().chain(methods),
);
Box::new(Host {
opts: self,
@@ -85,6 +100,7 @@ impl RustGenerator for Host {
fn print_resource(
&self,
_mod_ident: &str,
docs: &str,
ident: &proc_macro2::Ident,
functions: &[Function],

View File

@@ -19,6 +19,7 @@ pub trait RustGenerator {
fn default_param_mode(&self) -> BorrowMode;
fn print_resource(
&self,
print_resource: &str,
docs: &str,
ident: &Ident,
functions: &[Function],
@@ -63,7 +64,7 @@ pub trait RustGenerator {
self.print_union(docs, &ident, cases, info, &borrow_mode)
}
TypeDefKind::Resource(functions) => {
self.print_resource(docs, &ident, functions, info)
self.print_resource(&self.interface().ident, docs, &ident, functions, info)
}
};