diff --git a/crates/gen-guest-js/src/lib.rs b/crates/gen-guest-js/src/lib.rs index 5e09bbd..62f8f92 100644 --- a/crates/gen-guest-js/src/lib.rs +++ b/crates/gen-guest-js/src/lib.rs @@ -50,6 +50,33 @@ impl JavaScript { ) } + fn print_resource(&self, docs: &str, ident: &str, functions: &[Function]) -> String { + let functions: String = functions + .iter() + .map(|func| { + let docs = self.print_docs(&func); + let ident = func.ident.to_lower_camel_case(); + let params = self.print_function_params(&func.params); + + format!( + r#" + {docs} + async {ident} ({params}) {{ + }} + "# + ) + }) + .collect(); + + format!( + "{docs}\nclass {ident} {{ + #id: number; + + {functions} + }}" + ) + } + fn print_docs(&self, func: &Function) -> String { let docs = func .docs @@ -169,13 +196,26 @@ impl JavaScript { impl Generate for JavaScript { fn to_file(&self) -> (std::path::PathBuf, String) { - let mut contents = self + let functions: String = self .interface .functions .iter() .map(|func| self.print_function(func)) - .collect::>() - .join("\n"); + .collect(); + let resources: String = self + .interface + .typedefs + .iter() + .filter_map(|(_, typedef)| { + if let TypeDefKind::Resource(functions) = &typedef.kind { + Some(self.print_resource(&typedef.docs, &typedef.ident, &functions)) + } else { + None + } + }) + .collect(); + + let mut contents = format!("{}\n{}", functions, resources); if self.opts.prettier { postprocess(&mut contents, "prettier", ["--parser=babel"]) diff --git a/crates/gen-guest-js/tests/chars.js b/crates/gen-guest-js/tests/chars.js index 1bfe160..cf91b3f 100644 --- a/crates/gen-guest-js/tests/chars.js +++ b/crates/gen-guest-js/tests/chars.js @@ -2,16 +2,14 @@ /** * A function that accepts a character * @param {string} x -* @returns {Promise<[]>} */ export async function takeChar (x) { } - /** * A function that returns a character * @returns {Promise} */ export async function returnChar () { } - \ No newline at end of file + diff --git a/crates/gen-guest-js/tests/conventions.js b/crates/gen-guest-js/tests/conventions.js index 8616c93..3e49070 100644 --- a/crates/gen-guest-js/tests/conventions.js +++ b/crates/gen-guest-js/tests/conventions.js @@ -1,85 +1,62 @@ /** -* @returns {Promise<[]>} */ export async function kebabCase () { } - /** * @param {LudicrousSpeed} x -* @returns {Promise<[]>} */ export async function foo (x) { } - /** -* @returns {Promise<[]>} */ export async function functionWithUnderscores () { } - /** -* @returns {Promise<[]>} */ export async function functionWithNoWeirdCharacters () { } - /** -* @returns {Promise<[]>} */ export async function apple () { } - /** -* @returns {Promise<[]>} */ export async function applePear () { } - /** -* @returns {Promise<[]>} */ export async function applePearGrape () { } - /** -* @returns {Promise<[]>} */ export async function a0 () { } - /** -* @returns {Promise<[]>} */ export async function isXml () { } - /** -* @returns {Promise<[]>} */ export async function explicit () { } - /** -* @returns {Promise<[]>} */ export async function explicitSnake () { } - /** -* @returns {Promise<[]>} */ export async function bool () { } - \ No newline at end of file + diff --git a/crates/gen-guest-js/tests/empty.js b/crates/gen-guest-js/tests/empty.js index e69de29..8b13789 100644 --- a/crates/gen-guest-js/tests/empty.js +++ b/crates/gen-guest-js/tests/empty.js @@ -0,0 +1 @@ + diff --git a/crates/gen-guest-js/tests/flegs.js b/crates/gen-guest-js/tests/flegs.js index 746d531..53e36b7 100644 --- a/crates/gen-guest-js/tests/flegs.js +++ b/crates/gen-guest-js/tests/flegs.js @@ -6,7 +6,6 @@ export async function roundtripFlag1 (x) { } - /** * @param {Flag2} x * @returns {Promise} @@ -14,7 +13,6 @@ export async function roundtripFlag2 (x) { } - /** * @param {Flag4} x * @returns {Promise} @@ -22,7 +20,6 @@ export async function roundtripFlag4 (x) { } - /** * @param {Flag8} x * @returns {Promise} @@ -30,7 +27,6 @@ export async function roundtripFlag8 (x) { } - /** * @param {Flag16} x * @returns {Promise} @@ -38,7 +34,6 @@ export async function roundtripFlag16 (x) { } - /** * @param {Flag32} x * @returns {Promise} @@ -46,11 +41,10 @@ export async function roundtripFlag32 (x) { } - /** * @param {Flag64} x * @returns {Promise} */ export async function roundtripFlag64 (x) { } - \ No newline at end of file + diff --git a/crates/gen-guest-js/tests/floats.js b/crates/gen-guest-js/tests/floats.js index 6e67f68..7493ac6 100644 --- a/crates/gen-guest-js/tests/floats.js +++ b/crates/gen-guest-js/tests/floats.js @@ -1,30 +1,25 @@ /** * @param {number} x -* @returns {Promise<[]>} */ export async function float32Param (x) { } - /** * @param {number} x -* @returns {Promise<[]>} */ export async function float64Param (x) { } - /** * @returns {Promise} */ export async function float32Result () { } - /** * @returns {Promise} */ export async function float64Result () { } - \ No newline at end of file + diff --git a/crates/gen-guest-js/tests/integers.js b/crates/gen-guest-js/tests/integers.js index 7eaa8e7..c533b15 100644 --- a/crates/gen-guest-js/tests/integers.js +++ b/crates/gen-guest-js/tests/integers.js @@ -1,68 +1,52 @@ /** * @param {number} x -* @returns {Promise<[]>} */ export async function a1 (x) { } - /** * @param {number} x -* @returns {Promise<[]>} */ export async function a2 (x) { } - /** * @param {number} x -* @returns {Promise<[]>} */ export async function a3 (x) { } - /** * @param {number} x -* @returns {Promise<[]>} */ export async function a4 (x) { } - /** * @param {number} x -* @returns {Promise<[]>} */ export async function a5 (x) { } - /** * @param {number} x -* @returns {Promise<[]>} */ export async function a6 (x) { } - /** * @param {bigint} x -* @returns {Promise<[]>} */ export async function a7 (x) { } - /** * @param {bigint} x -* @returns {Promise<[]>} */ export async function a8 (x) { } - /** * @param {number} p1 * @param {number} p2 @@ -72,71 +56,61 @@ * @param {number} p6 * @param {bigint} p7 * @param {bigint} p8 -* @returns {Promise<[]>} */ export async function a9 (p1, p2, p3, p4, p5, p6, p7, p8) { } - /** * @returns {Promise} */ export async function r1 () { } - /** * @returns {Promise} */ export async function r2 () { } - /** * @returns {Promise} */ export async function r3 () { } - /** * @returns {Promise} */ export async function r4 () { } - /** * @returns {Promise} */ export async function r5 () { } - /** * @returns {Promise} */ export async function r6 () { } - /** * @returns {Promise} */ export async function r7 () { } - /** * @returns {Promise} */ export async function r8 () { } - /** * @returns {Promise<[bigint, number]>} */ export async function pairRet () { } - \ No newline at end of file + diff --git a/crates/gen-guest-js/tests/lists.js b/crates/gen-guest-js/tests/lists.js index da76d09..dc024af 100644 --- a/crates/gen-guest-js/tests/lists.js +++ b/crates/gen-guest-js/tests/lists.js @@ -1,154 +1,124 @@ /** * @param {Uint8Array[]} x -* @returns {Promise<[]>} */ export async function listU8Param (x) { } - /** * @param {Uint16Array[]} x -* @returns {Promise<[]>} */ export async function listU16Param (x) { } - /** * @param {Uint32Array[]} x -* @returns {Promise<[]>} */ export async function listU32Param (x) { } - /** * @param {BigUint64Array[]} x -* @returns {Promise<[]>} */ export async function listU64Param (x) { } - /** * @param {Int8Array[]} x -* @returns {Promise<[]>} */ export async function listS8Param (x) { } - /** * @param {Int16Array[]} x -* @returns {Promise<[]>} */ export async function listS16Param (x) { } - /** * @param {Int32Array[]} x -* @returns {Promise<[]>} */ export async function listS32Param (x) { } - /** * @param {BigInt64Array[]} x -* @returns {Promise<[]>} */ export async function listS64Param (x) { } - /** * @param {Float32Array[]} x -* @returns {Promise<[]>} */ export async function listFloat32Param (x) { } - /** * @param {Float64Array[]} x -* @returns {Promise<[]>} */ export async function listFloat64Param (x) { } - /** * @returns {Promise} */ export async function listU8Ret () { } - /** * @returns {Promise} */ export async function listU16Ret () { } - /** * @returns {Promise} */ export async function listU32Ret () { } - /** * @returns {Promise} */ export async function listU64Ret () { } - /** * @returns {Promise} */ export async function listS8Ret () { } - /** * @returns {Promise} */ export async function listS16Ret () { } - /** * @returns {Promise} */ export async function listS32Ret () { } - /** * @returns {Promise} */ export async function listS64Ret () { } - /** * @returns {Promise} */ export async function listFloat32Ret () { } - /** * @returns {Promise} */ export async function listFloat64Ret () { } - /** * @param {[number, number][]} x * @returns {Promise<[bigint, number][]>} @@ -156,22 +126,18 @@ export async function tupleList (x) { } - /** * @param {string[]} a -* @returns {Promise<[]>} */ export async function stringListArg (a) { } - /** * @returns {Promise} */ export async function stringListRet () { } - /** * @param {[number, string][]} x * @returns {Promise<[string, number][]>} @@ -179,7 +145,6 @@ export async function tupleStringList (x) { } - /** * @param {string[]} x * @returns {Promise} @@ -187,7 +152,6 @@ export async function stringList (x) { } - /** * @param {SomeRecord[]} x * @returns {Promise} @@ -195,7 +159,6 @@ export async function recordList (x) { } - /** * @param {OtherRecord[]} x * @returns {Promise} @@ -203,7 +166,6 @@ export async function recordListReverse (x) { } - /** * @param {SomeVariant[]} x * @returns {Promise} @@ -211,11 +173,10 @@ export async function variantList (x) { } - /** * @param {LoadStoreAllSizes} a * @returns {Promise} */ export async function loadStoreEverything (a) { } - \ No newline at end of file + diff --git a/crates/gen-guest-js/tests/many-arguments.js b/crates/gen-guest-js/tests/many-arguments.js index 7d5ed1e..6c5cfa1 100644 --- a/crates/gen-guest-js/tests/many-arguments.js +++ b/crates/gen-guest-js/tests/many-arguments.js @@ -16,16 +16,13 @@ * @param {bigint} a14 * @param {bigint} a15 * @param {bigint} a16 -* @returns {Promise<[]>} */ export async function manyArgs (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16) { } - /** * @param {BigStruct} x -* @returns {Promise<[]>} */ export async function bigArgument (x) { } - \ No newline at end of file + diff --git a/crates/gen-guest-js/tests/multi-return.js b/crates/gen-guest-js/tests/multi-return.js index fc24f0c..f4cfa8c 100644 --- a/crates/gen-guest-js/tests/multi-return.js +++ b/crates/gen-guest-js/tests/multi-return.js @@ -1,34 +1,30 @@ /** -* @returns {Promise<[]>} */ export async function mra () { } - /** +* @returns {Promise<[]>} */ export async function mrb () { } - /** * @returns {Promise} */ export async function mrc () { } - /** -* @returns {Promise} +* @returns {Promise<[number]>} */ export async function mrd () { } - /** * @returns {Promise<[number, number]>} */ export async function mre () { } - \ No newline at end of file + diff --git a/crates/gen-guest-js/tests/records.js b/crates/gen-guest-js/tests/records.js index 2253df6..b3cfd7b 100644 --- a/crates/gen-guest-js/tests/records.js +++ b/crates/gen-guest-js/tests/records.js @@ -1,83 +1,68 @@ /** * @param {[string, number]} x -* @returns {Promise<[]>} */ export async function tupleArg (x) { } - /** * @returns {Promise<[string, number]>} */ export async function tupleResult () { } - /** * @param {Empty} x -* @returns {Promise<[]>} */ export async function emptyArg (x) { } - /** * @returns {Promise} */ export async function emptyResult () { } - /** * @param {Scalars} x -* @returns {Promise<[]>} */ export async function scalarArg (x) { } - /** * @returns {Promise} */ export async function scalarResult () { } - /** * @param {ReallyFlags} x -* @returns {Promise<[]>} */ export async function flagsArg (x) { } - /** * @returns {Promise} */ export async function flagsResult () { } - /** * @param {Aggregates} x -* @returns {Promise<[]>} */ export async function aggregateArg (x) { } - /** * @returns {Promise} */ export async function aggregateResult () { } - /** * @param {TupleTypedef2} e * @returns {Promise} */ export async function typedefInout (e) { } - \ No newline at end of file + diff --git a/crates/gen-guest-js/tests/resources.js b/crates/gen-guest-js/tests/resources.js new file mode 100644 index 0000000..cf88232 --- /dev/null +++ b/crates/gen-guest-js/tests/resources.js @@ -0,0 +1,62 @@ + + /** +* @returns {Promise} +*/ + export async function constructorA () { + } + + /** +* @returns {Promise} +*/ + export async function constructorB () { + } + + +class a { + #id: number; + + + /** +*/ + async f1 () { + } + + /** +* @param {number} a +*/ + async f2 (a) { + } + + /** +* @param {number} a +* @param {number} b +*/ + async f3 (a, b) { + } + + } +class b { + #id: number; + + + /** +* @returns {Promise} +*/ + async f1 () { + } + + /** +* @param {A} x +* @returns {Promise>} +*/ + async f2 (x) { + } + + /** +* @param {A[] | null} x +* @returns {Promise>} +*/ + async f3 (x) { + } + + } \ No newline at end of file diff --git a/crates/gen-guest-js/tests/simple-functions.js b/crates/gen-guest-js/tests/simple-functions.js index 9259ada..5cc286d 100644 --- a/crates/gen-guest-js/tests/simple-functions.js +++ b/crates/gen-guest-js/tests/simple-functions.js @@ -1,42 +1,34 @@ /** -* @returns {Promise<[]>} */ export async function f1 () { } - /** * @param {number} a -* @returns {Promise<[]>} */ export async function f2 (a) { } - /** * @param {number} a * @param {number} b -* @returns {Promise<[]>} */ export async function f3 (a, b) { } - /** * @returns {Promise} */ export async function f4 () { } - /** * @returns {Promise<[number, number]>} */ export async function f5 () { } - /** * @param {number} a * @param {number} b @@ -45,4 +37,4 @@ */ export async function f6 (a, b, c) { } - \ No newline at end of file + diff --git a/crates/gen-guest-js/tests/simple-lists.js b/crates/gen-guest-js/tests/simple-lists.js index 66a06f3..ba76e45 100644 --- a/crates/gen-guest-js/tests/simple-lists.js +++ b/crates/gen-guest-js/tests/simple-lists.js @@ -1,19 +1,16 @@ /** * @param {Uint32Array[]} l -* @returns {Promise<[]>} */ export async function simpleList1 (l) { } - /** * @returns {Promise} */ export async function simpleList2 () { } - /** * @param {Uint32Array[]} a * @param {Uint32Array[]} b @@ -22,11 +19,10 @@ export async function simpleList3 (a, b) { } - /** * @param {Uint32Array[][]} l * @returns {Promise} */ export async function simpleList4 (l) { } - \ No newline at end of file + diff --git a/crates/gen-guest-js/tests/small-anonymous.js b/crates/gen-guest-js/tests/small-anonymous.js index 1a31431..2829c50 100644 --- a/crates/gen-guest-js/tests/small-anonymous.js +++ b/crates/gen-guest-js/tests/small-anonymous.js @@ -4,4 +4,4 @@ */ export async function optionTest () { } - \ No newline at end of file + diff --git a/crates/gen-guest-js/tests/strings.js b/crates/gen-guest-js/tests/strings.js index 175e609..cf3c3b3 100644 --- a/crates/gen-guest-js/tests/strings.js +++ b/crates/gen-guest-js/tests/strings.js @@ -1,19 +1,16 @@ /** * @param {string} x -* @returns {Promise<[]>} */ export async function a (x) { } - /** * @returns {Promise} */ export async function b () { } - /** * @param {string} a * @param {string} b @@ -21,4 +18,4 @@ */ export async function c (a, b) { } - \ No newline at end of file + diff --git a/crates/gen-guest-js/tests/unions.js b/crates/gen-guest-js/tests/unions.js index 952a049..d17b73c 100644 --- a/crates/gen-guest-js/tests/unions.js +++ b/crates/gen-guest-js/tests/unions.js @@ -6,7 +6,6 @@ export async function addOneInteger (num) { } - /** * @param {AllFloats} num * @returns {Promise} @@ -14,7 +13,6 @@ export async function addOneFloat (num) { } - /** * @param {AllText} text * @param {string} letter @@ -23,7 +21,6 @@ export async function replaceFirstChar (text, letter) { } - /** * @param {AllIntegers} num * @returns {Promise} @@ -31,7 +28,6 @@ export async function identifyInteger (num) { } - /** * @param {AllFloats} num * @returns {Promise} @@ -39,7 +35,6 @@ export async function identifyFloat (num) { } - /** * @param {AllText} text * @returns {Promise} @@ -47,7 +42,6 @@ export async function identifyText (text) { } - /** * @param {DuplicatedS32} num * @returns {Promise} @@ -55,7 +49,6 @@ export async function addOneDuplicated (num) { } - /** * @param {DuplicatedS32} num * @returns {Promise} @@ -63,7 +56,6 @@ export async function identifyDuplicated (num) { } - /** * @param {DistinguishableNum} num * @returns {Promise} @@ -71,11 +63,10 @@ export async function addOneDistinguishableNum (num) { } - /** * @param {DistinguishableNum} num * @returns {Promise} */ export async function identifyDistinguishableNum (num) { } - \ No newline at end of file + diff --git a/crates/gen-guest-js/tests/variants.js b/crates/gen-guest-js/tests/variants.js index 171f681..4cb0b03 100644 --- a/crates/gen-guest-js/tests/variants.js +++ b/crates/gen-guest-js/tests/variants.js @@ -1,64 +1,52 @@ /** * @param {E1} x -* @returns {Promise<[]>} */ export async function e1Arg (x) { } - /** * @returns {Promise} */ export async function e1Result () { } - /** * @param {U1} x -* @returns {Promise<[]>} */ export async function u1Arg (x) { } - /** * @returns {Promise} */ export async function u1Result () { } - /** * @param {V1} x -* @returns {Promise<[]>} */ export async function v1Arg (x) { } - /** * @returns {Promise} */ export async function v1Result () { } - /** * @param {boolean} x -* @returns {Promise<[]>} */ export async function boolArg (x) { } - /** * @returns {Promise} */ export async function boolResult () { } - /** * @param {boolean | null} a * @param {[] | null} b @@ -67,19 +55,16 @@ * @param {number | null} e * @param {U1 | null} f * @param {boolean | null | null} g -* @returns {Promise<[]>} */ export async function optionArg (a, b, c, d, e, f, g) { } - /** * @returns {Promise<[boolean | null, [] | null, number | null, E1 | null, number | null, U1 | null, boolean | null | null]>} */ export async function optionResult () { } - /** * @param {Casts1} a * @param {Casts2} b @@ -92,7 +77,6 @@ export async function casts (a, b, c, d, e, f) { } - /** * @param {Result<_, _>} a * @param {Result<_, E1>} b @@ -100,93 +84,79 @@ * @param {Result<[], []>} d * @param {Result} e * @param {Result} f -* @returns {Promise<[]>} */ export async function resultArg (a, b, c, d, e, f) { } - /** * @returns {Promise<[Result<_, _>, Result<_, E1>, Result, Result<[], []>, Result, Result]>} */ export async function resultResult () { } - /** * @returns {Promise>} */ export async function returnResultSugar () { } - /** * @returns {Promise>} */ export async function returnResultSugar2 () { } - /** * @returns {Promise>} */ export async function returnResultSugar3 () { } - /** * @returns {Promise>} */ export async function returnResultSugar4 () { } - /** * @returns {Promise} */ export async function returnOptionSugar () { } - /** * @returns {Promise} */ export async function returnOptionSugar2 () { } - /** * @returns {Promise>} */ export async function resultSimple () { } - /** * @param {IsClone} a -* @returns {Promise<[]>} */ export async function isCloneArg (a) { } - /** * @returns {Promise} */ export async function isCloneReturn () { } - /** -* @returns {Promise} +* @returns {Promise<[number | null]>} */ export async function returnNamedOption () { } - /** -* @returns {Promise>} +* @returns {Promise<[Result]>} */ export async function returnNamedResult () { } - \ No newline at end of file + diff --git a/crates/gen-guest-rust/src/lib.rs b/crates/gen-guest-rust/src/lib.rs index 83e87bb..8dc711f 100644 --- a/crates/gen-guest-rust/src/lib.rs +++ b/crates/gen-guest-rust/src/lib.rs @@ -116,6 +116,50 @@ impl RustGenerator for RustWasm { fn default_param_mode(&self) -> BorrowMode { BorrowMode::AllBorrowed(parse_quote!('a)) } + + fn print_resource( + &self, + docs: &str, + ident: &proc_macro2::Ident, + functions: &[Function], + info: TypeInfo, + ) -> TokenStream { + let docs = self.print_docs(docs); + let additional_attrs = self.additional_attrs(&ident.to_string(), info); + let functions = functions.iter().map(|func| { + let sig = FnSig { + async_: true, + unsafe_: false, + private: false, + self_arg: Some(quote!(&self)), + func, + }; + + let sig = self.print_function_signature( + &sig, + &BorrowMode::AllBorrowed(parse_quote!('_)), + &BorrowMode::Owned, + ); + + quote! { + #sig { + todo!() + } + } + }); + + quote! { + #docs + #additional_attrs + struct #ident { + id: u64 + } + + impl #ident { + #(#functions)* + } + } + } } impl tauri_bindgen_core::Generate for RustWasm { diff --git a/crates/gen-guest-rust/tests/chars.rs b/crates/gen-guest-rust/tests/chars.rs index a1103ed..b5f2f3f 100644 --- a/crates/gen-guest-rust/tests/chars.rs +++ b/crates/gen-guest-rust/tests/chars.rs @@ -4,7 +4,7 @@ pub mod chars { use ::tauri_bindgen_guest_rust::serde; use ::tauri_bindgen_guest_rust::bitflags; ///A function that accepts a character - pub async fn take_char(x: char) -> () { + pub async fn take_char(x: char) { todo!() } ///A function that returns a character diff --git a/crates/gen-guest-rust/tests/conventions.rs b/crates/gen-guest-rust/tests/conventions.rs index cb4a057..a960b27 100644 --- a/crates/gen-guest-rust/tests/conventions.rs +++ b/crates/gen-guest-rust/tests/conventions.rs @@ -8,40 +8,40 @@ pub mod conventions { how_fast_are_you_going: u32, i_am_going_extremely_slow: u64, } - pub async fn kebab_case() -> () { + pub async fn kebab_case() { todo!() } - pub async fn foo(x: LudicrousSpeed) -> () { + pub async fn foo(x: LudicrousSpeed) { todo!() } - pub async fn function_with_underscores() -> () { + pub async fn function_with_underscores() { todo!() } - pub async fn function_with_no_weird_characters() -> () { + pub async fn function_with_no_weird_characters() { todo!() } - pub async fn apple() -> () { + pub async fn apple() { todo!() } - pub async fn apple_pear() -> () { + pub async fn apple_pear() { todo!() } - pub async fn apple_pear_grape() -> () { + pub async fn apple_pear_grape() { todo!() } - pub async fn a0() -> () { + pub async fn a0() { todo!() } - pub async fn is_xml() -> () { + pub async fn is_xml() { todo!() } - pub async fn explicit() -> () { + pub async fn explicit() { todo!() } - pub async fn explicit_snake() -> () { + pub async fn explicit_snake() { todo!() } - pub async fn bool() -> () { + pub async fn bool() { todo!() } } diff --git a/crates/gen-guest-rust/tests/floats.rs b/crates/gen-guest-rust/tests/floats.rs index e9fa448..f0b0e5b 100644 --- a/crates/gen-guest-rust/tests/floats.rs +++ b/crates/gen-guest-rust/tests/floats.rs @@ -3,10 +3,10 @@ pub mod floats { use ::tauri_bindgen_guest_rust::serde; use ::tauri_bindgen_guest_rust::bitflags; - pub async fn float32_param(x: f32) -> () { + pub async fn float32_param(x: f32) { todo!() } - pub async fn float64_param(x: f64) -> () { + pub async fn float64_param(x: f64) { todo!() } pub async fn float32_result() -> f32 { diff --git a/crates/gen-guest-rust/tests/integers.rs b/crates/gen-guest-rust/tests/integers.rs index fe1809a..50af87d 100644 --- a/crates/gen-guest-rust/tests/integers.rs +++ b/crates/gen-guest-rust/tests/integers.rs @@ -3,28 +3,28 @@ pub mod integers { use ::tauri_bindgen_guest_rust::serde; use ::tauri_bindgen_guest_rust::bitflags; - pub async fn a1(x: u8) -> () { + pub async fn a1(x: u8) { todo!() } - pub async fn a2(x: i8) -> () { + pub async fn a2(x: i8) { todo!() } - pub async fn a3(x: u16) -> () { + pub async fn a3(x: u16) { todo!() } - pub async fn a4(x: i16) -> () { + pub async fn a4(x: i16) { todo!() } - pub async fn a5(x: u32) -> () { + pub async fn a5(x: u32) { todo!() } - pub async fn a6(x: i32) -> () { + pub async fn a6(x: i32) { todo!() } - pub async fn a7(x: u64) -> () { + pub async fn a7(x: u64) { todo!() } - pub async fn a8(x: i64) -> () { + pub async fn a8(x: i64) { todo!() } pub async fn a9( @@ -36,7 +36,7 @@ pub mod integers { p6: i32, p7: u64, p8: i64, - ) -> () { + ) { todo!() } pub async fn r1() -> u8 { diff --git a/crates/gen-guest-rust/tests/lists.rs b/crates/gen-guest-rust/tests/lists.rs index 089937f..d2e1638 100644 --- a/crates/gen-guest-rust/tests/lists.rs +++ b/crates/gen-guest-rust/tests/lists.rs @@ -79,34 +79,34 @@ pub mod lists { pub type LoadStoreAllSizesResult = Vec< (String, u8, i8, u16, i16, u32, i32, u64, i64, f32, f64, char), >; - pub async fn list_u8_param(x: &'_ [u8]) -> () { + pub async fn list_u8_param(x: &'_ [u8]) { todo!() } - pub async fn list_u16_param(x: &'_ [u16]) -> () { + pub async fn list_u16_param(x: &'_ [u16]) { todo!() } - pub async fn list_u32_param(x: &'_ [u32]) -> () { + pub async fn list_u32_param(x: &'_ [u32]) { todo!() } - pub async fn list_u64_param(x: &'_ [u64]) -> () { + pub async fn list_u64_param(x: &'_ [u64]) { todo!() } - pub async fn list_s8_param(x: &'_ [i8]) -> () { + pub async fn list_s8_param(x: &'_ [i8]) { todo!() } - pub async fn list_s16_param(x: &'_ [i16]) -> () { + pub async fn list_s16_param(x: &'_ [i16]) { todo!() } - pub async fn list_s32_param(x: &'_ [i32]) -> () { + pub async fn list_s32_param(x: &'_ [i32]) { todo!() } - pub async fn list_s64_param(x: &'_ [i64]) -> () { + pub async fn list_s64_param(x: &'_ [i64]) { todo!() } - pub async fn list_float32_param(x: &'_ [f32]) -> () { + pub async fn list_float32_param(x: &'_ [f32]) { todo!() } - pub async fn list_float64_param(x: &'_ [f64]) -> () { + pub async fn list_float64_param(x: &'_ [f64]) { todo!() } pub async fn list_u8_ret() -> Vec { @@ -142,7 +142,7 @@ pub mod lists { pub async fn tuple_list(x: &'_ [(u8, i8)]) -> Vec<(i64, u32)> { todo!() } - pub async fn string_list_arg(a: &'_ [&'_ str]) -> () { + pub async fn string_list_arg(a: &'_ [&'_ str]) { todo!() } pub async fn string_list_ret() -> Vec { diff --git a/crates/gen-guest-rust/tests/many-arguments.rs b/crates/gen-guest-rust/tests/many-arguments.rs index 13c0518..8d5b9c5 100644 --- a/crates/gen-guest-rust/tests/many-arguments.rs +++ b/crates/gen-guest-rust/tests/many-arguments.rs @@ -43,10 +43,10 @@ pub mod many_arguments { a14: u64, a15: u64, a16: u64, - ) -> () { + ) { todo!() } - pub async fn big_argument(x: BigStruct<'_>) -> () { + pub async fn big_argument(x: BigStruct<'_>) { todo!() } } diff --git a/crates/gen-guest-rust/tests/multi-return.rs b/crates/gen-guest-rust/tests/multi-return.rs index 2b85cbe..d9bae8f 100644 --- a/crates/gen-guest-rust/tests/multi-return.rs +++ b/crates/gen-guest-rust/tests/multi-return.rs @@ -3,7 +3,7 @@ pub mod multi_return { use ::tauri_bindgen_guest_rust::serde; use ::tauri_bindgen_guest_rust::bitflags; - pub async fn mra() -> () { + pub async fn mra() { todo!() } pub async fn mrb() { diff --git a/crates/gen-guest-rust/tests/records.rs b/crates/gen-guest-rust/tests/records.rs index baad2a1..e14da3d 100644 --- a/crates/gen-guest-rust/tests/records.rs +++ b/crates/gen-guest-rust/tests/records.rs @@ -46,31 +46,31 @@ All of the fields are bool*/ } pub type IntTypedef = i32; pub type TupleTypedef2 = (IntTypedef,); - pub async fn tuple_arg(x: (char, u32)) -> () { + pub async fn tuple_arg(x: (char, u32)) { todo!() } pub async fn tuple_result() -> (char, u32) { todo!() } - pub async fn empty_arg(x: Empty) -> () { + pub async fn empty_arg(x: Empty) { todo!() } pub async fn empty_result() -> Empty { todo!() } - pub async fn scalar_arg(x: Scalars) -> () { + pub async fn scalar_arg(x: Scalars) { todo!() } pub async fn scalar_result() -> Scalars { todo!() } - pub async fn flags_arg(x: ReallyFlags) -> () { + pub async fn flags_arg(x: ReallyFlags) { todo!() } pub async fn flags_result() -> ReallyFlags { todo!() } - pub async fn aggregate_arg(x: AggregatesParam<'_>) -> () { + pub async fn aggregate_arg(x: AggregatesParam<'_>) { todo!() } pub async fn aggregate_result() -> AggregatesResult { diff --git a/crates/gen-guest-rust/tests/resources.rs b/crates/gen-guest-rust/tests/resources.rs new file mode 100644 index 0000000..f82033b --- /dev/null +++ b/crates/gen-guest-rust/tests/resources.rs @@ -0,0 +1,42 @@ +#[allow(unused_imports, unused_variables, dead_code)] +#[rustfmt::skip] +pub mod resources { + use ::tauri_bindgen_guest_rust::serde; + use ::tauri_bindgen_guest_rust::bitflags; + #[derive(serde::Deserialize)] + struct A { + id: u64, + } + impl A { + pub async fn f1(&self) { + todo!() + } + pub async fn f2(&self, a: u32) { + todo!() + } + pub async fn f3(&self, a: u32, b: u32) { + todo!() + } + } + #[derive(serde::Deserialize)] + struct B { + id: u64, + } + impl B { + pub async fn f1(&self) -> A { + todo!() + } + pub async fn f2(&self, x: A) -> Result { + todo!() + } + pub async fn f3(&self, x: Option<&'_ [A]>) -> Result { + todo!() + } + } + pub async fn constructor_a() -> A { + todo!() + } + pub async fn constructor_b() -> B { + todo!() + } +} diff --git a/crates/gen-guest-rust/tests/simple-functions.rs b/crates/gen-guest-rust/tests/simple-functions.rs index fbe2859..27d9d2d 100644 --- a/crates/gen-guest-rust/tests/simple-functions.rs +++ b/crates/gen-guest-rust/tests/simple-functions.rs @@ -3,13 +3,13 @@ pub mod simple_functions { use ::tauri_bindgen_guest_rust::serde; use ::tauri_bindgen_guest_rust::bitflags; - pub async fn f1() -> () { + pub async fn f1() { todo!() } - pub async fn f2(a: u32) -> () { + pub async fn f2(a: u32) { todo!() } - pub async fn f3(a: u32, b: u32) -> () { + pub async fn f3(a: u32, b: u32) { todo!() } pub async fn f4() -> u32 { diff --git a/crates/gen-guest-rust/tests/simple-lists.rs b/crates/gen-guest-rust/tests/simple-lists.rs index f2c7d9d..18bc75f 100644 --- a/crates/gen-guest-rust/tests/simple-lists.rs +++ b/crates/gen-guest-rust/tests/simple-lists.rs @@ -3,7 +3,7 @@ pub mod simple_lists { use ::tauri_bindgen_guest_rust::serde; use ::tauri_bindgen_guest_rust::bitflags; - pub async fn simple_list1(l: &'_ [u32]) -> () { + pub async fn simple_list1(l: &'_ [u32]) { todo!() } pub async fn simple_list2() -> Vec { diff --git a/crates/gen-guest-rust/tests/strings.rs b/crates/gen-guest-rust/tests/strings.rs index 9d0963b..3e19d4a 100644 --- a/crates/gen-guest-rust/tests/strings.rs +++ b/crates/gen-guest-rust/tests/strings.rs @@ -3,7 +3,7 @@ pub mod strings { use ::tauri_bindgen_guest_rust::serde; use ::tauri_bindgen_guest_rust::bitflags; - pub async fn a(x: &'_ str) -> () { + pub async fn a(x: &'_ str) { todo!() } pub async fn b() -> String { diff --git a/crates/gen-guest-rust/tests/variants.rs b/crates/gen-guest-rust/tests/variants.rs index a3a2837..2dbbc65 100644 --- a/crates/gen-guest-rust/tests/variants.rs +++ b/crates/gen-guest-rust/tests/variants.rs @@ -78,25 +78,25 @@ pub mod variants { pub struct IsCloneResult { v1: V1Result, } - pub async fn e1_arg(x: E1) -> () { + pub async fn e1_arg(x: E1) { todo!() } pub async fn e1_result() -> E1 { todo!() } - pub async fn u1_arg(x: U1) -> () { + pub async fn u1_arg(x: U1) { todo!() } pub async fn u1_result() -> U1 { todo!() } - pub async fn v1_arg(x: V1Param<'_>) -> () { + pub async fn v1_arg(x: V1Param<'_>) { todo!() } pub async fn v1_result() -> V1Result { todo!() } - pub async fn bool_arg(x: bool) -> () { + pub async fn bool_arg(x: bool) { todo!() } pub async fn bool_result() -> bool { @@ -110,7 +110,7 @@ pub mod variants { e: Option, f: Option, g: Option>, - ) -> () { + ) { todo!() } pub async fn option_result() -> ( @@ -141,7 +141,7 @@ pub mod variants { d: Result<(), ()>, e: Result>, f: Result<&'_ str, &'_ [u8]>, - ) -> () { + ) { todo!() } pub async fn result_result() -> ( @@ -175,7 +175,7 @@ pub mod variants { pub async fn result_simple() -> Result { todo!() } - pub async fn is_clone_arg(a: IsCloneParam<'_>) -> () { + pub async fn is_clone_arg(a: IsCloneParam<'_>) { todo!() } pub async fn is_clone_return() -> IsCloneResult { diff --git a/crates/gen-guest-ts/src/lib.rs b/crates/gen-guest-ts/src/lib.rs index aa79896..be65c19 100644 --- a/crates/gen-guest-ts/src/lib.rs +++ b/crates/gen-guest-ts/src/lib.rs @@ -46,7 +46,11 @@ impl TypeScript { let ident = func.ident.to_lower_camel_case(); let params = self.print_function_params(&func.params); - let result = func.result.as_ref().map(|result| self.print_function_result(result)).unwrap_or_default(); + let result = func + .result + .as_ref() + .map(|result| self.print_function_result(result)) + .unwrap_or_default(); format!( r#" @@ -235,6 +239,39 @@ impl TypeScript { format!("{docs}\nexport type {ident} = {cases};\n") } + TypeDefKind::Resource(functions) => { + let functions: String = functions + .iter() + .map(|func| { + let docs = print_docs(&func.docs); + + let ident = func.ident.to_lower_camel_case(); + + let params = self.print_function_params(&func.params); + let result = func + .result + .as_ref() + .map(|result| self.print_function_result(result)) + .unwrap_or_default(); + + format!( + r#" + {docs} + async {ident} ({params}) {result} {{ + }} + "# + ) + }) + .collect(); + + format!( + "{docs}\nclass {ident} {{ + #id: number; + + {functions} + }}" + ) + } } } diff --git a/crates/gen-guest-ts/tests/chars.ts b/crates/gen-guest-ts/tests/chars.ts index 27a2640..31d0164 100644 --- a/crates/gen-guest-ts/tests/chars.ts +++ b/crates/gen-guest-ts/tests/chars.ts @@ -4,7 +4,7 @@ /** * A function that accepts a character */ - export async function takeChar (x: string) : Promise<[]> { + export async function takeChar (x: string) { } /** diff --git a/crates/gen-guest-ts/tests/conventions.ts b/crates/gen-guest-ts/tests/conventions.ts index c63ce6c..c60b045 100644 --- a/crates/gen-guest-ts/tests/conventions.ts +++ b/crates/gen-guest-ts/tests/conventions.ts @@ -8,50 +8,50 @@ iAmGoingExtremelySlow: bigint, - export async function kebabCase () : Promise<[]> { + export async function kebabCase () { } - export async function foo (x: LudicrousSpeed) : Promise<[]> { + export async function foo (x: LudicrousSpeed) { } - export async function functionWithUnderscores () : Promise<[]> { + export async function functionWithUnderscores () { } - export async function functionWithNoWeirdCharacters () : Promise<[]> { + export async function functionWithNoWeirdCharacters () { } - export async function apple () : Promise<[]> { + export async function apple () { } - export async function applePear () : Promise<[]> { + export async function applePear () { } - export async function applePearGrape () : Promise<[]> { + export async function applePearGrape () { } - export async function a0 () : Promise<[]> { + export async function a0 () { } - export async function isXml () : Promise<[]> { + export async function isXml () { } - export async function explicit () : Promise<[]> { + export async function explicit () { } - export async function explicitSnake () : Promise<[]> { + export async function explicitSnake () { } - export async function bool () : Promise<[]> { + export async function bool () { } \ No newline at end of file diff --git a/crates/gen-guest-ts/tests/floats.ts b/crates/gen-guest-ts/tests/floats.ts index 8644616..2c76109 100644 --- a/crates/gen-guest-ts/tests/floats.ts +++ b/crates/gen-guest-ts/tests/floats.ts @@ -2,11 +2,11 @@ - export async function float32Param (x: number) : Promise<[]> { + export async function float32Param (x: number) { } - export async function float64Param (x: number) : Promise<[]> { + export async function float64Param (x: number) { } diff --git a/crates/gen-guest-ts/tests/integers.ts b/crates/gen-guest-ts/tests/integers.ts index 2274ab0..7fae174 100644 --- a/crates/gen-guest-ts/tests/integers.ts +++ b/crates/gen-guest-ts/tests/integers.ts @@ -2,39 +2,39 @@ - export async function a1 (x: number) : Promise<[]> { + export async function a1 (x: number) { } - export async function a2 (x: number) : Promise<[]> { + export async function a2 (x: number) { } - export async function a3 (x: number) : Promise<[]> { + export async function a3 (x: number) { } - export async function a4 (x: number) : Promise<[]> { + export async function a4 (x: number) { } - export async function a5 (x: number) : Promise<[]> { + export async function a5 (x: number) { } - export async function a6 (x: number) : Promise<[]> { + export async function a6 (x: number) { } - export async function a7 (x: bigint) : Promise<[]> { + export async function a7 (x: bigint) { } - export async function a8 (x: bigint) : Promise<[]> { + export async function a8 (x: bigint) { } - export async function a9 (p1: number, p2: number, p3: number, p4: number, p5: number, p6: number, p7: bigint, p8: bigint) : Promise<[]> { + export async function a9 (p1: number, p2: number, p3: number, p4: number, p5: number, p6: number, p7: bigint, p8: bigint) { } diff --git a/crates/gen-guest-ts/tests/lists.ts b/crates/gen-guest-ts/tests/lists.ts index fce0e74..b5c5735 100644 --- a/crates/gen-guest-ts/tests/lists.ts +++ b/crates/gen-guest-ts/tests/lists.ts @@ -61,43 +61,43 @@ export type LoadStoreAllSizes = [string, number, number, number, number, number, - export async function listU8Param (x: Uint8Array[]) : Promise<[]> { + export async function listU8Param (x: Uint8Array[]) { } - export async function listU16Param (x: Uint16Array[]) : Promise<[]> { + export async function listU16Param (x: Uint16Array[]) { } - export async function listU32Param (x: Uint32Array[]) : Promise<[]> { + export async function listU32Param (x: Uint32Array[]) { } - export async function listU64Param (x: BigUint64Array[]) : Promise<[]> { + export async function listU64Param (x: BigUint64Array[]) { } - export async function listS8Param (x: Int8Array[]) : Promise<[]> { + export async function listS8Param (x: Int8Array[]) { } - export async function listS16Param (x: Int16Array[]) : Promise<[]> { + export async function listS16Param (x: Int16Array[]) { } - export async function listS32Param (x: Int32Array[]) : Promise<[]> { + export async function listS32Param (x: Int32Array[]) { } - export async function listS64Param (x: BigInt64Array[]) : Promise<[]> { + export async function listS64Param (x: BigInt64Array[]) { } - export async function listFloat32Param (x: Float32Array[]) : Promise<[]> { + export async function listFloat32Param (x: Float32Array[]) { } - export async function listFloat64Param (x: Float64Array[]) : Promise<[]> { + export async function listFloat64Param (x: Float64Array[]) { } @@ -145,7 +145,7 @@ export type LoadStoreAllSizes = [string, number, number, number, number, number, } - export async function stringListArg (a: string[]) : Promise<[]> { + export async function stringListArg (a: string[]) { } diff --git a/crates/gen-guest-ts/tests/many-arguments.ts b/crates/gen-guest-ts/tests/many-arguments.ts index b6077d1..4f4f44c 100644 --- a/crates/gen-guest-ts/tests/many-arguments.ts +++ b/crates/gen-guest-ts/tests/many-arguments.ts @@ -44,10 +44,10 @@ a20: string, - export async function manyArgs (a1: bigint, a2: bigint, a3: bigint, a4: bigint, a5: bigint, a6: bigint, a7: bigint, a8: bigint, a9: bigint, a10: bigint, a11: bigint, a12: bigint, a13: bigint, a14: bigint, a15: bigint, a16: bigint) : Promise<[]> { + export async function manyArgs (a1: bigint, a2: bigint, a3: bigint, a4: bigint, a5: bigint, a6: bigint, a7: bigint, a8: bigint, a9: bigint, a10: bigint, a11: bigint, a12: bigint, a13: bigint, a14: bigint, a15: bigint, a16: bigint) { } - export async function bigArgument (x: BigStruct) : Promise<[]> { + export async function bigArgument (x: BigStruct) { } \ No newline at end of file diff --git a/crates/gen-guest-ts/tests/multi-return.ts b/crates/gen-guest-ts/tests/multi-return.ts index 611b2db..fe3418f 100644 --- a/crates/gen-guest-ts/tests/multi-return.ts +++ b/crates/gen-guest-ts/tests/multi-return.ts @@ -2,7 +2,7 @@ - export async function mra () : Promise<[]> { + export async function mra () { } diff --git a/crates/gen-guest-ts/tests/records.ts b/crates/gen-guest-ts/tests/records.ts index bf9937b..d916f21 100644 --- a/crates/gen-guest-ts/tests/records.ts +++ b/crates/gen-guest-ts/tests/records.ts @@ -56,7 +56,7 @@ export type TupleTypedef2 = [IntTypedef]; - export async function tupleArg (x: [string, number]) : Promise<[]> { + export async function tupleArg (x: [string, number]) { } @@ -64,7 +64,7 @@ export type TupleTypedef2 = [IntTypedef]; } - export async function emptyArg (x: Empty) : Promise<[]> { + export async function emptyArg (x: Empty) { } @@ -72,7 +72,7 @@ export type TupleTypedef2 = [IntTypedef]; } - export async function scalarArg (x: Scalars) : Promise<[]> { + export async function scalarArg (x: Scalars) { } @@ -80,7 +80,7 @@ export type TupleTypedef2 = [IntTypedef]; } - export async function flagsArg (x: ReallyFlags) : Promise<[]> { + export async function flagsArg (x: ReallyFlags) { } @@ -88,7 +88,7 @@ export type TupleTypedef2 = [IntTypedef]; } - export async function aggregateArg (x: Aggregates) : Promise<[]> { + export async function aggregateArg (x: Aggregates) { } diff --git a/crates/gen-guest-ts/tests/resources.ts b/crates/gen-guest-ts/tests/resources.ts new file mode 100644 index 0000000..4c2abc1 --- /dev/null +++ b/crates/gen-guest-ts/tests/resources.ts @@ -0,0 +1,45 @@ + + +class A { + #id: number; + + + + async f1 () { + } + + + async f2 (a: number) { + } + + + async f3 (a: number, b: number) { + } + + } +class B { + #id: number; + + + + async f1 () : Promise { + } + + + async f2 (x: A) : Promise> { + } + + + async f3 (x: A[] | null) : Promise> { + } + + } + + + export async function constructorA () : Promise { + } + + + export async function constructorB () : Promise { + } + \ No newline at end of file diff --git a/crates/gen-guest-ts/tests/simple-functions.ts b/crates/gen-guest-ts/tests/simple-functions.ts index fc15556..8486ebf 100644 --- a/crates/gen-guest-ts/tests/simple-functions.ts +++ b/crates/gen-guest-ts/tests/simple-functions.ts @@ -2,15 +2,15 @@ - export async function f1 () : Promise<[]> { + export async function f1 () { } - export async function f2 (a: number) : Promise<[]> { + export async function f2 (a: number) { } - export async function f3 (a: number, b: number) : Promise<[]> { + export async function f3 (a: number, b: number) { } diff --git a/crates/gen-guest-ts/tests/simple-lists.ts b/crates/gen-guest-ts/tests/simple-lists.ts index 6276667..dc5bff5 100644 --- a/crates/gen-guest-ts/tests/simple-lists.ts +++ b/crates/gen-guest-ts/tests/simple-lists.ts @@ -2,7 +2,7 @@ - export async function simpleList1 (l: Uint32Array[]) : Promise<[]> { + export async function simpleList1 (l: Uint32Array[]) { } diff --git a/crates/gen-guest-ts/tests/strings.ts b/crates/gen-guest-ts/tests/strings.ts index 1c783de..eb120dd 100644 --- a/crates/gen-guest-ts/tests/strings.ts +++ b/crates/gen-guest-ts/tests/strings.ts @@ -2,7 +2,7 @@ - export async function a (x: string) : Promise<[]> { + export async function a (x: string) { } diff --git a/crates/gen-guest-ts/tests/variants.ts b/crates/gen-guest-ts/tests/variants.ts index aace699..b7ea1e7 100644 --- a/crates/gen-guest-ts/tests/variants.ts +++ b/crates/gen-guest-ts/tests/variants.ts @@ -103,7 +103,7 @@ v1: V1, - export async function e1Arg (x: E1) : Promise<[]> { + export async function e1Arg (x: E1) { } @@ -111,7 +111,7 @@ v1: V1, } - export async function u1Arg (x: U1) : Promise<[]> { + export async function u1Arg (x: U1) { } @@ -119,7 +119,7 @@ v1: V1, } - export async function v1Arg (x: V1) : Promise<[]> { + export async function v1Arg (x: V1) { } @@ -127,7 +127,7 @@ v1: V1, } - export async function boolArg (x: boolean) : Promise<[]> { + export async function boolArg (x: boolean) { } @@ -135,7 +135,7 @@ v1: V1, } - export async function optionArg (a: boolean | null, b: [] | null, c: number | null, d: E1 | null, e: number | null, f: U1 | null, g: boolean | null | null) : Promise<[]> { + export async function optionArg (a: boolean | null, b: [] | null, c: number | null, d: E1 | null, e: number | null, f: U1 | null, g: boolean | null | null) { } @@ -147,7 +147,7 @@ v1: V1, } - export async function resultArg (a: Result<_, _>, b: Result<_, E1>, c: Result, d: Result<[], []>, e: Result, f: Result) : Promise<[]> { + export async function resultArg (a: Result<_, _>, b: Result<_, E1>, c: Result, d: Result<[], []>, e: Result, f: Result) { } @@ -183,7 +183,7 @@ v1: V1, } - export async function isCloneArg (a: IsClone) : Promise<[]> { + export async function isCloneArg (a: IsClone) { } diff --git a/crates/gen-host/src/lib.rs b/crates/gen-host/src/lib.rs index 8b71d5c..2550b06 100644 --- a/crates/gen-host/src/lib.rs +++ b/crates/gen-host/src/lib.rs @@ -5,6 +5,7 @@ clippy::unused_self )] +use std::collections::HashSet; use std::path::PathBuf; use heck::ToKebabCase; @@ -14,8 +15,10 @@ use quote::format_ident; use quote::quote; use syn::parse_quote; use tauri_bindgen_core::{Generate, GeneratorBuilder}; -use tauri_bindgen_gen_rust::{BorrowMode, FnSig, RustGenerator, TypeInfo, TypeInfos}; -use wit_parser::{Function, Interface}; +use tauri_bindgen_gen_rust::{ + print_generics, BorrowMode, FnSig, RustGenerator, TypeInfo, TypeInfos, +}; +use wit_parser::{Function, Interface, Type, TypeDefKind}; #[derive(Default, Debug, Clone)] #[cfg_attr(feature = "clap", derive(clap::Args))] @@ -90,6 +93,144 @@ impl RustGenerator for Host { fn default_param_mode(&self) -> BorrowMode { BorrowMode::Owned } + + fn print_resource( + &self, + docs: &str, + ident: &proc_macro2::Ident, + functions: &[Function], + _info: TypeInfo, + ) -> TokenStream { + let docs = self.print_docs(docs); + + let mut resources = HashSet::new(); + for func in functions { + for (_, ty) in &func.params { + self.extract_resources(ty, &mut resources); + } + if let Some(result) = &func.result { + for ty in result.types() { + self.extract_resources(ty, &mut resources); + } + } + } + + let resources = resources.iter().map(|r| { + let ident = format_ident!("{}", r.to_upper_camel_case()); + + quote! { type #ident: #ident; } + }); + + let trait_ = self.print_trait(&ident.to_string(), functions.iter(), resources, false); + + quote! { + #docs + #trait_ + } + } + + fn print_ty(&self, ty: &Type, mode: &BorrowMode) -> TokenStream { + match ty { + Type::Bool => quote! { bool }, + Type::U8 => quote! { u8 }, + Type::U16 => quote! { u16 }, + Type::U32 => quote! { u32 }, + Type::U64 => quote! { u64 }, + Type::S8 => quote! { i8 }, + Type::S16 => quote! { i16 }, + Type::S32 => quote! { i32 }, + Type::S64 => quote! { i64 }, + Type::Float32 => quote! { f32 }, + Type::Float64 => quote! { f64 }, + Type::Char => quote! { char }, + Type::String => match mode { + BorrowMode::Owned => quote! { String }, + BorrowMode::AllBorrowed(lt) | BorrowMode::LeafBorrowed(lt) => quote! { &#lt str }, + }, + Type::List(ty) => { + let is_primitive = matches!( + **ty, + Type::U8 + | Type::S8 + | Type::U16 + | Type::S16 + | Type::U32 + | Type::S32 + | Type::U64 + | Type::S64 + | Type::Float32 + | Type::Float64 + ); + + let ty = self.print_ty(ty, mode); + + match mode { + BorrowMode::Owned => quote! { Vec<#ty> }, + BorrowMode::AllBorrowed(lt) => quote! { &#lt [#ty] }, + BorrowMode::LeafBorrowed(lt) => { + if is_primitive { + quote! { &#lt [#ty] } + } else { + quote! { Vec<#ty> } + } + } + } + } + Type::Tuple(types) => { + if types.len() == 1 { + let ty = self.print_ty(&types[0], mode); + + quote! { (#ty,) } + } else { + let types = types.iter().map(|ty| self.print_ty(ty, mode)); + + quote! { (#(#types),*) } + } + } + Type::Option(ty) => { + let ty = self.print_ty(ty, mode); + + quote! { Option<#ty> } + } + Type::Result { ok, err } => { + let ok = ok + .as_ref() + .map(|ty| self.print_ty(ty, mode)) + .unwrap_or(quote! { () }); + let err = err + .as_ref() + .map(|ty| self.print_ty(ty, mode)) + .unwrap_or(quote! { () }); + + quote! { Result<#ok, #err> } + } + Type::Id(id) => { + let typedef = &self.interface().typedefs[*id]; + let info = self.infos()[*id]; + + if let TypeDefKind::Resource(_) = &typedef.kind { + return quote! { ::tauri_bindgen_host::ResourceId }; + } + + let ident = if self.uses_two_names(info) { + match mode { + BorrowMode::Owned => { + format_ident!("{}Result", typedef.ident.to_upper_camel_case()) + } + BorrowMode::AllBorrowed(_) | BorrowMode::LeafBorrowed(_) => { + format_ident!("{}Param", typedef.ident.to_upper_camel_case()) + } + } + } else { + format_ident!("{}", typedef.ident.to_upper_camel_case()) + }; + + let generics = print_generics(info, mode); + + quote! { #ident #generics } + } + } + } } impl Generate for Host { @@ -103,7 +244,27 @@ impl Generate for Host { &BorrowMode::LeafBorrowed(parse_quote!('a)), ); - let trait_ = self.print_trait(&self.interface.ident, self.interface.functions.iter()); + let resources = self.interface.typedefs.iter().filter_map(|(_, typedef)| { + if let TypeDefKind::Resource(_) = &typedef.kind { + let ident = format_ident!("{}", typedef.ident.to_upper_camel_case()); + let func_ident = format_ident!("get_{}_mut", typedef.ident.to_snake_case()); + + Some(quote! { + type #ident: #ident; + + fn #func_ident(&mut self, id: ::tauri_bindgen_host::ResourceId) -> &mut Self::#ident; + }) + } else { + None + } + }); + + let trait_ = self.print_trait( + &self.interface.ident, + self.interface.functions.iter(), + resources, + true, + ); let add_to_router = self.print_add_to_router( &self.interface.ident, /*, self.interface.functions.iter()*/ @@ -146,6 +307,8 @@ impl Host { &self, ident: &str, functions: impl Iterator, + additional_items: impl Iterator, + sized: bool, ) -> TokenStream { let ident = format_ident!("{}", ident.to_upper_camel_case()); @@ -167,13 +330,46 @@ impl Host { quote! { #sig; } }); + let sized = sized.then_some(quote!(: Sized)); + quote! { - pub trait #ident: Sized { + pub trait #ident #sized { + #(#additional_items)* #(#functions)* } } } + fn extract_resources<'a>(&'a self, ty: &Type, resources: &mut HashSet<&'a str>) { + match ty { + Type::List(ty) | Type::Option(ty) => { + self.extract_resources(ty, resources); + } + Type::Tuple(types) => { + for ty in types { + self.extract_resources(ty, resources) + } + } + Type::Result { ok, err } => { + if let Some(ok) = ok { + self.extract_resources(ok, resources); + } + + if let Some(err) = err { + self.extract_resources(err, resources); + } + } + Type::Id(id) => { + let typedef = &self.interface().typedefs[*id]; + + if let TypeDefKind::Resource(_) = &typedef.kind { + resources.insert(&typedef.ident); + } + } + _ => {} + } + } + fn print_add_to_router( &self, mod_ident: &str, diff --git a/crates/gen-host/tests/chars.rs b/crates/gen-host/tests/chars.rs index f9f26e0..15cdb1b 100644 --- a/crates/gen-host/tests/chars.rs +++ b/crates/gen-host/tests/chars.rs @@ -5,7 +5,7 @@ pub mod chars { use ::tauri_bindgen_host::bitflags; pub trait Chars: Sized { ///A function that accepts a character - fn take_char(&mut self, x: char) -> (); + fn take_char(&mut self, x: char); ///A function that returns a character fn return_char(&mut self) -> char; } diff --git a/crates/gen-host/tests/conventions.rs b/crates/gen-host/tests/conventions.rs index ac6a79c..c3e31dd 100644 --- a/crates/gen-host/tests/conventions.rs +++ b/crates/gen-host/tests/conventions.rs @@ -9,18 +9,18 @@ pub mod conventions { i_am_going_extremely_slow: u64, } pub trait Conventions: Sized { - fn kebab_case(&mut self) -> (); - fn foo(&mut self, x: LudicrousSpeed) -> (); - fn function_with_underscores(&mut self) -> (); - fn function_with_no_weird_characters(&mut self) -> (); - fn apple(&mut self) -> (); - fn apple_pear(&mut self) -> (); - fn apple_pear_grape(&mut self) -> (); - fn a0(&mut self) -> (); - fn is_xml(&mut self) -> (); - fn explicit(&mut self) -> (); - fn explicit_snake(&mut self) -> (); - fn bool(&mut self) -> (); + fn kebab_case(&mut self); + fn foo(&mut self, x: LudicrousSpeed); + fn function_with_underscores(&mut self); + fn function_with_no_weird_characters(&mut self); + fn apple(&mut self); + fn apple_pear(&mut self); + fn apple_pear_grape(&mut self); + fn a0(&mut self); + fn is_xml(&mut self); + fn explicit(&mut self); + fn explicit_snake(&mut self); + fn bool(&mut self); } pub fn add_to_router( router: &mut ::tauri_bindgen_host::ipc_router_wip::Router, diff --git a/crates/gen-host/tests/floats.rs b/crates/gen-host/tests/floats.rs index 54f4ea4..7eed05f 100644 --- a/crates/gen-host/tests/floats.rs +++ b/crates/gen-host/tests/floats.rs @@ -4,8 +4,8 @@ pub mod floats { use ::tauri_bindgen_host::serde; use ::tauri_bindgen_host::bitflags; pub trait Floats: Sized { - fn float32_param(&mut self, x: f32) -> (); - fn float64_param(&mut self, x: f64) -> (); + fn float32_param(&mut self, x: f32); + fn float64_param(&mut self, x: f64); fn float32_result(&mut self) -> f32; fn float64_result(&mut self) -> f64; } diff --git a/crates/gen-host/tests/integers.rs b/crates/gen-host/tests/integers.rs index 2c62003..9573eee 100644 --- a/crates/gen-host/tests/integers.rs +++ b/crates/gen-host/tests/integers.rs @@ -4,14 +4,14 @@ pub mod integers { use ::tauri_bindgen_host::serde; use ::tauri_bindgen_host::bitflags; pub trait Integers: Sized { - fn a1(&mut self, x: u8) -> (); - fn a2(&mut self, x: i8) -> (); - fn a3(&mut self, x: u16) -> (); - fn a4(&mut self, x: i16) -> (); - fn a5(&mut self, x: u32) -> (); - fn a6(&mut self, x: i32) -> (); - fn a7(&mut self, x: u64) -> (); - fn a8(&mut self, x: i64) -> (); + fn a1(&mut self, x: u8); + fn a2(&mut self, x: i8); + fn a3(&mut self, x: u16); + fn a4(&mut self, x: i16); + fn a5(&mut self, x: u32); + fn a6(&mut self, x: i32); + fn a7(&mut self, x: u64); + fn a8(&mut self, x: i64); fn a9( &mut self, p1: u8, @@ -22,7 +22,7 @@ pub mod integers { p6: i32, p7: u64, p8: i64, - ) -> (); + ); fn r1(&mut self) -> u8; fn r2(&mut self) -> i8; fn r3(&mut self) -> u16; diff --git a/crates/gen-host/tests/lists.rs b/crates/gen-host/tests/lists.rs index c9c24e0..e00e2c1 100644 --- a/crates/gen-host/tests/lists.rs +++ b/crates/gen-host/tests/lists.rs @@ -41,16 +41,16 @@ pub mod lists { (&'a str, u8, i8, u16, i16, u32, i32, u64, i64, f32, f64, char), >; pub trait Lists: Sized { - fn list_u8_param(&mut self, x: Vec) -> (); - fn list_u16_param(&mut self, x: Vec) -> (); - fn list_u32_param(&mut self, x: Vec) -> (); - fn list_u64_param(&mut self, x: Vec) -> (); - fn list_s8_param(&mut self, x: Vec) -> (); - fn list_s16_param(&mut self, x: Vec) -> (); - fn list_s32_param(&mut self, x: Vec) -> (); - fn list_s64_param(&mut self, x: Vec) -> (); - fn list_float32_param(&mut self, x: Vec) -> (); - fn list_float64_param(&mut self, x: Vec) -> (); + fn list_u8_param(&mut self, x: Vec); + fn list_u16_param(&mut self, x: Vec); + fn list_u32_param(&mut self, x: Vec); + fn list_u64_param(&mut self, x: Vec); + fn list_s8_param(&mut self, x: Vec); + fn list_s16_param(&mut self, x: Vec); + fn list_s32_param(&mut self, x: Vec); + fn list_s64_param(&mut self, x: Vec); + fn list_float32_param(&mut self, x: Vec); + fn list_float64_param(&mut self, x: Vec); fn list_u8_ret(&mut self) -> &'_ [u8]; fn list_u16_ret(&mut self) -> &'_ [u16]; fn list_u32_ret(&mut self) -> &'_ [u32]; @@ -62,7 +62,7 @@ pub mod lists { fn list_float32_ret(&mut self) -> &'_ [f32]; fn list_float64_ret(&mut self) -> &'_ [f64]; fn tuple_list(&mut self, x: Vec<(u8, i8)>) -> &'_ [(i64, u32)]; - fn string_list_arg(&mut self, a: Vec) -> (); + fn string_list_arg(&mut self, a: Vec); fn string_list_ret(&mut self) -> &'_ [&'_ str]; fn tuple_string_list(&mut self, x: Vec<(u8, String)>) -> &'_ [(&'_ str, u8)]; fn string_list(&mut self, x: Vec) -> &'_ [&'_ str]; diff --git a/crates/gen-host/tests/many-arguments.rs b/crates/gen-host/tests/many-arguments.rs index 8f11c98..c10ecc4 100644 --- a/crates/gen-host/tests/many-arguments.rs +++ b/crates/gen-host/tests/many-arguments.rs @@ -45,8 +45,8 @@ pub mod many_arguments { a14: u64, a15: u64, a16: u64, - ) -> (); - fn big_argument(&mut self, x: BigStruct) -> (); + ); + fn big_argument(&mut self, x: BigStruct); } pub fn add_to_router( router: &mut ::tauri_bindgen_host::ipc_router_wip::Router, diff --git a/crates/gen-host/tests/multi-return.rs b/crates/gen-host/tests/multi-return.rs index 38c75f9..6b7a89b 100644 --- a/crates/gen-host/tests/multi-return.rs +++ b/crates/gen-host/tests/multi-return.rs @@ -4,7 +4,7 @@ pub mod multi_return { use ::tauri_bindgen_host::serde; use ::tauri_bindgen_host::bitflags; pub trait MultiReturn: Sized { - fn mra(&mut self) -> (); + fn mra(&mut self); fn mrb(&mut self); fn mrc(&mut self) -> u32; fn mrd(&mut self) -> u32; diff --git a/crates/gen-host/tests/records.rs b/crates/gen-host/tests/records.rs index febe051..f2fdfd1 100644 --- a/crates/gen-host/tests/records.rs +++ b/crates/gen-host/tests/records.rs @@ -39,15 +39,15 @@ All of the fields are bool*/ pub type IntTypedef = i32; pub type TupleTypedef2 = (IntTypedef,); pub trait Records: Sized { - fn tuple_arg(&mut self, x: (char, u32)) -> (); + fn tuple_arg(&mut self, x: (char, u32)); fn tuple_result(&mut self) -> (char, u32); - fn empty_arg(&mut self, x: Empty) -> (); + fn empty_arg(&mut self, x: Empty); fn empty_result(&mut self) -> Empty; - fn scalar_arg(&mut self, x: Scalars) -> (); + fn scalar_arg(&mut self, x: Scalars); fn scalar_result(&mut self) -> Scalars; - fn flags_arg(&mut self, x: ReallyFlags) -> (); + fn flags_arg(&mut self, x: ReallyFlags); fn flags_result(&mut self) -> ReallyFlags; - fn aggregate_arg(&mut self, x: Aggregates) -> (); + fn aggregate_arg(&mut self, x: Aggregates); fn aggregate_result(&mut self) -> Aggregates<'_>; fn typedef_inout(&mut self, e: TupleTypedef2) -> i32; } diff --git a/crates/gen-host/tests/resources.rs b/crates/gen-host/tests/resources.rs new file mode 100644 index 0000000..340423c --- /dev/null +++ b/crates/gen-host/tests/resources.rs @@ -0,0 +1,37 @@ +#[allow(unused_imports, unused_variables, dead_code)] +#[rustfmt::skip] +pub mod resources { + use ::tauri_bindgen_host::serde; + use ::tauri_bindgen_host::bitflags; + pub trait A { + fn f1(&mut self); + fn f2(&mut self, a: u32); + fn f3(&mut self, a: u32, b: u32); + } + pub trait B { + type A: A; + fn f1(&mut self) -> ::tauri_bindgen_host::ResourceId; + fn f2(&mut self, x: ::tauri_bindgen_host::ResourceId) -> Result; + fn f3( + &mut self, + x: Option>, + ) -> Result<::tauri_bindgen_host::ResourceId, ()>; + } + pub trait Resources: Sized { + type A: A; + fn get_a_mut(&mut self, id: ::tauri_bindgen_host::ResourceId) -> &mut Self::A; + type B: B; + fn get_b_mut(&mut self, id: ::tauri_bindgen_host::ResourceId) -> &mut Self::B; + fn constructor_a(&mut self) -> ::tauri_bindgen_host::ResourceId; + fn constructor_b(&mut self) -> ::tauri_bindgen_host::ResourceId; + } + pub fn add_to_router( + router: &mut ::tauri_bindgen_host::ipc_router_wip::Router, + get_cx: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + ) -> Result<(), ::tauri_bindgen_host::ipc_router_wip::Error> + where + U: Resources, + { + Ok(()) + } +} diff --git a/crates/gen-host/tests/simple-functions.rs b/crates/gen-host/tests/simple-functions.rs index e8344be..a185f4e 100644 --- a/crates/gen-host/tests/simple-functions.rs +++ b/crates/gen-host/tests/simple-functions.rs @@ -4,9 +4,9 @@ pub mod simple_functions { use ::tauri_bindgen_host::serde; use ::tauri_bindgen_host::bitflags; pub trait SimpleFunctions: Sized { - fn f1(&mut self) -> (); - fn f2(&mut self, a: u32) -> (); - fn f3(&mut self, a: u32, b: u32) -> (); + fn f1(&mut self); + fn f2(&mut self, a: u32); + fn f3(&mut self, a: u32, b: u32); fn f4(&mut self) -> u32; fn f5(&mut self) -> (u32, u32); fn f6(&mut self, a: u32, b: u32, c: u32) -> (u32, u32, u32); diff --git a/crates/gen-host/tests/simple-lists.rs b/crates/gen-host/tests/simple-lists.rs index c6d60ce..77384e3 100644 --- a/crates/gen-host/tests/simple-lists.rs +++ b/crates/gen-host/tests/simple-lists.rs @@ -4,7 +4,7 @@ pub mod simple_lists { use ::tauri_bindgen_host::serde; use ::tauri_bindgen_host::bitflags; pub trait SimpleLists: Sized { - fn simple_list1(&mut self, l: Vec) -> (); + fn simple_list1(&mut self, l: Vec); fn simple_list2(&mut self) -> &'_ [u32]; fn simple_list3(&mut self, a: Vec, b: Vec) -> (&'_ [u32], &'_ [u32]); fn simple_list4(&mut self, l: Vec>) -> &'_ [&'_ [u32]]; diff --git a/crates/gen-host/tests/strings.rs b/crates/gen-host/tests/strings.rs index a3faff1..badb236 100644 --- a/crates/gen-host/tests/strings.rs +++ b/crates/gen-host/tests/strings.rs @@ -4,7 +4,7 @@ pub mod strings { use ::tauri_bindgen_host::serde; use ::tauri_bindgen_host::bitflags; pub trait Strings: Sized { - fn a(&mut self, x: String) -> (); + fn a(&mut self, x: String); fn b(&mut self) -> &'_ str; fn c(&mut self, a: String, b: String) -> &'_ str; } diff --git a/crates/gen-host/tests/variants.rs b/crates/gen-host/tests/variants.rs index 22f596c..e3ede17 100644 --- a/crates/gen-host/tests/variants.rs +++ b/crates/gen-host/tests/variants.rs @@ -65,13 +65,13 @@ pub mod variants { v1: V1<'a>, } pub trait Variants: Sized { - fn e1_arg(&mut self, x: E1) -> (); + fn e1_arg(&mut self, x: E1); fn e1_result(&mut self) -> E1; - fn u1_arg(&mut self, x: U1) -> (); + fn u1_arg(&mut self, x: U1); fn u1_result(&mut self) -> U1; - fn v1_arg(&mut self, x: V1) -> (); + fn v1_arg(&mut self, x: V1); fn v1_result(&mut self) -> V1<'_>; - fn bool_arg(&mut self, x: bool) -> (); + fn bool_arg(&mut self, x: bool); fn bool_result(&mut self) -> bool; fn option_arg( &mut self, @@ -82,7 +82,7 @@ pub mod variants { e: Option, f: Option, g: Option>, - ) -> (); + ); fn option_result( &mut self, ) -> ( @@ -111,7 +111,7 @@ pub mod variants { d: Result<(), ()>, e: Result, f: Result>, - ) -> (); + ); fn result_result( &mut self, ) -> ( @@ -129,7 +129,7 @@ pub mod variants { fn return_option_sugar(&mut self) -> Option; fn return_option_sugar2(&mut self) -> Option; fn result_simple(&mut self) -> Result; - fn is_clone_arg(&mut self, a: IsClone) -> (); + fn is_clone_arg(&mut self, a: IsClone); fn is_clone_return(&mut self) -> IsClone<'_>; fn return_named_option(&mut self) -> Option; fn return_named_result(&mut self) -> Result; diff --git a/crates/gen-markdown/src/lib.rs b/crates/gen-markdown/src/lib.rs index 268b6f9..9d94b85 100644 --- a/crates/gen-markdown/src/lib.rs +++ b/crates/gen-markdown/src/lib.rs @@ -156,6 +156,26 @@ impl Markdown { format!("## Union {ident}\n\n{docs}\n\n### Cases\n\n{cases}") } + wit_parser::TypeDefKind::Resource(functions) => { + let functions: String = functions + .iter() + .map(|func| { + format!( + "### Method {ident}\n\n`func {ident} ({params}){result}`\n\n{docs}", + ident = func.ident, + params = self.print_named_types(&func.params), + result = func + .result + .as_ref() + .map(|result| self.print_result(result)) + .unwrap_or_default(), + docs = func.docs + ) + }) + .collect(); + + format!("## Resource {ident}\n\n{docs}\n\n### Methods\n\n{functions}") + } } } @@ -164,7 +184,11 @@ impl Markdown { "### Function {ident}\n\n`func {ident} ({params}){result}`\n\n{docs}", ident = func.ident, params = self.print_named_types(&func.params), - result = func.result.as_ref().map(|result| self.print_result(result)).unwrap_or_default(), + result = func + .result + .as_ref() + .map(|result| self.print_result(result)) + .unwrap_or_default(), docs = func.docs ) } @@ -178,10 +202,6 @@ impl Markdown { } fn print_result(&self, result: &FunctionResult) -> String { - if result.is_empty() { - return String::new(); - } - if let Some(Type::Tuple(types)) = result.types().next() { if types.is_empty() { return String::new(); diff --git a/crates/gen-markdown/tests/multi-return.md b/crates/gen-markdown/tests/multi-return.md index 7e4883d..d790759 100644 --- a/crates/gen-markdown/tests/multi-return.md +++ b/crates/gen-markdown/tests/multi-return.md @@ -15,7 +15,7 @@ ### Function mrb -`func mrb ()` +`func mrb () -> ()` ### Function mrc diff --git a/crates/gen-markdown/tests/resources.md b/crates/gen-markdown/tests/resources.md new file mode 100644 index 0000000..9e7459f --- /dev/null +++ b/crates/gen-markdown/tests/resources.md @@ -0,0 +1,56 @@ +# resources + + + +## Type definitions + +## Resource a + + + +### Methods + +### Method f1 + +`func f1 ()` + +### Method f2 + +`func f2 (a: u32)` + +### Method f3 + +`func f3 (a: u32, b: u32)` + + +## Resource b + + + +### Methods + +### Method f1 + +`func f1 () -> [a](#a)` + +### Method f2 + +`func f2 (x: [a](#a)) -> result` + +### Method f3 + +`func f3 (x: option>) -> result<[a](#a), _>` + + + +## Functions + +### Function constructor_a + +`func constructor_a () -> [a](#a)` + + +### Function constructor_b + +`func constructor_b () -> [b](#b)` + diff --git a/crates/gen-rust/src/lib.rs b/crates/gen-rust/src/lib.rs index 2fb5968..acef801 100644 --- a/crates/gen-rust/src/lib.rs +++ b/crates/gen-rust/src/lib.rs @@ -16,6 +16,13 @@ pub trait RustGenerator { fn infos(&self) -> &TypeInfos; fn additional_attrs(&self, ident: &str, info: TypeInfo) -> Option; fn default_param_mode(&self) -> BorrowMode; + fn print_resource( + &self, + docs: &str, + ident: &Ident, + functions: &[Function], + info: TypeInfo, + ) -> TokenStream; fn print_typedefs( &self, @@ -54,6 +61,9 @@ pub trait RustGenerator { TypeDefKind::Union(cases) => { self.print_union(docs, &ident, cases, info, &borrow_mode) } + TypeDefKind::Resource(functions) => { + self.print_resource(docs, &ident, functions, info) + } }; typedefs_out.push(typedef); @@ -376,7 +386,11 @@ pub trait RustGenerator { let params = self.print_function_params(&sig.func.params, param_mode); - let result = sig.func.result.as_ref().map(|result| self.print_function_result(result, results_mode)); + let result = sig + .func + .result + .as_ref() + .map(|result| self.print_function_result(result, results_mode)); quote! { #docs @@ -460,6 +474,7 @@ pub trait RustGenerator { TypeDefKind::Variant(_) => "Variant".to_string(), TypeDefKind::Enum(_) => "Enum".to_string(), TypeDefKind::Union(_) => "Union".to_string(), + TypeDefKind::Resource(_) => "Resource".to_string(), }, } } diff --git a/crates/host/Cargo.toml b/crates/host/Cargo.toml index 4e18527..9e3bbcd 100644 --- a/crates/host/Cargo.toml +++ b/crates/host/Cargo.toml @@ -16,3 +16,4 @@ tracing = { version = "0.1.37", features = ["log", "log-always"] } anyhow = "1.0.69" serde.workspace = true ipc-router-wip = { path = "../ipc-router-wip" } +generational-arena = "0.2.8" diff --git a/crates/host/src/lib.rs b/crates/host/src/lib.rs index 9f4422a..dfff614 100644 --- a/crates/host/src/lib.rs +++ b/crates/host/src/lib.rs @@ -1,4 +1,30 @@ pub use tauri_bindgen_host_macro::*; +pub use generational_arena::{Arena as ResourceTable, Index as ResourceId}; #[doc(hidden)] pub use {anyhow, async_trait::async_trait, bitflags, ipc_router_wip, serde, tauri, tracing}; + +// #[derive(Debug)] +// pub struct ResourceId { +// id: generational_arena::Index, +// _m: PhantomData, +// } + +// impl Clone for ResourceId { +// fn clone(&self) -> Self { +// Self { +// id: self.id.clone(), +// _m: PhantomData, +// } +// } +// } + +// impl Copy for ResourceId {} + +// impl PartialEq for ResourceId { +// fn eq(&self, other: &Self) -> bool { +// self.id == other.id +// } +// } + +// impl Eq for ResourceId {} diff --git a/crates/wit-parser/src/lib.rs b/crates/wit-parser/src/lib.rs index a2fe971..a6e5670 100644 --- a/crates/wit-parser/src/lib.rs +++ b/crates/wit-parser/src/lib.rs @@ -105,6 +105,7 @@ pub enum TypeDefKind { Variant(Vec), Enum(Vec), Union(Vec), + Resource(Vec), } #[derive(Debug, Clone, PartialEq, Eq)] diff --git a/crates/wit-parser/src/parse.rs b/crates/wit-parser/src/parse.rs index e3be572..4abfca3 100644 --- a/crates/wit-parser/src/parse.rs +++ b/crates/wit-parser/src/parse.rs @@ -59,6 +59,7 @@ pub enum InterfaceItemInner { Enum(Vec), Union(Vec), Func(Func), + Resource(Vec), } #[derive(Debug, Clone, PartialEq, Eq)] @@ -95,7 +96,6 @@ pub struct VariantCase { pub ty: Option, } - #[derive(Debug, Clone, PartialEq, Eq)] pub struct EnumCase { pub ident: Span, @@ -108,6 +108,13 @@ pub struct UnionCase { pub ty: Type, } +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Method { + pub ident: Span, + pub docs: Vec, + pub inner: Func, +} + #[derive(Debug, Clone, PartialEq, Eq)] pub enum Type { Bool, @@ -161,27 +168,52 @@ impl<'a> FromTokens<'a> for InterfaceItem { let inner = match kind { Token::Record => { - let inner = parse_list(tokens, Token::LeftBrace, Token::RightBrace, Some(Token::Comma))?; + let inner = parse_list( + tokens, + Token::LeftBrace, + Token::RightBrace, + Some(Token::Comma), + )?; InterfaceItemInner::Record(inner) } Token::Enum => { - let inner = parse_list(tokens, Token::LeftBrace, Token::RightBrace, Some(Token::Comma))?; + let inner = parse_list( + tokens, + Token::LeftBrace, + Token::RightBrace, + Some(Token::Comma), + )?; InterfaceItemInner::Enum(inner) } Token::Flags => { - let inner = parse_list(tokens, Token::LeftBrace, Token::RightBrace, Some(Token::Comma))?; + let inner = parse_list( + tokens, + Token::LeftBrace, + Token::RightBrace, + Some(Token::Comma), + )?; InterfaceItemInner::Flags(inner) } Token::Variant => { - let inner = parse_list(tokens, Token::LeftBrace, Token::RightBrace, Some(Token::Comma))?; + let inner = parse_list( + tokens, + Token::LeftBrace, + Token::RightBrace, + Some(Token::Comma), + )?; InterfaceItemInner::Variant(inner) } Token::Union => { - let inner = parse_list(tokens, Token::LeftBrace, Token::RightBrace, Some(Token::Comma))?; + let inner = parse_list( + tokens, + Token::LeftBrace, + Token::RightBrace, + Some(Token::Comma), + )?; InterfaceItemInner::Union(inner) } @@ -195,7 +227,11 @@ impl<'a> FromTokens<'a> for InterfaceItem { InterfaceItemInner::Func(inner) } - Token::Resource => todo!(), + Token::Resource => { + let inner = parse_list(tokens, Token::LeftBrace, Token::RightBrace, None)?; + + InterfaceItemInner::Resource(inner) + } Token::Use => todo!(), found => { let suggestions = find_similar( @@ -239,7 +275,12 @@ impl<'a> FromTokens<'a> for Func { impl<'a> FromTokens<'a> for NamedTypeList { fn parse(tokens: &mut Tokens<'a>) -> Result { - parse_list(tokens, Token::LeftParen, Token::RightParen, Some(Token::Comma)) + parse_list( + tokens, + Token::LeftParen, + Token::RightParen, + Some(Token::Comma), + ) } } @@ -328,6 +369,18 @@ impl<'a> FromTokens<'a> for UnionCase { } } +impl<'a> FromTokens<'a> for Method { + fn parse(tokens: &mut Tokens<'a>) -> Result { + let docs = parse_docs(tokens); + + let _ = tokens.expect(Token::Func)?; + + let (_, ident) = tokens.expect(Token::Ident)?; + + let inner = Func::parse(tokens)?; + + Ok(Method { ident, docs, inner }) + } } impl<'a> FromTokens<'a> for Type { @@ -356,7 +409,12 @@ impl<'a> FromTokens<'a> for Type { Ok(Self::List(Box::new(ty))) } Token::Tuple => { - let types = parse_list(tokens, Token::LessThan, Token::GreaterThan, Some(Token::Comma))?; + let types = parse_list( + tokens, + Token::LessThan, + Token::GreaterThan, + Some(Token::Comma), + )?; Ok(Self::Tuple(types)) } diff --git a/crates/wit-parser/src/typecheck.rs b/crates/wit-parser/src/typecheck.rs index 68920a2..837753c 100644 --- a/crates/wit-parser/src/typecheck.rs +++ b/crates/wit-parser/src/typecheck.rs @@ -158,6 +158,14 @@ impl<'a> Resolver<'a> { TypeDefKind::Union(inner) } + parse::InterfaceItemInner::Resource(methods) => { + let functions = methods + .iter() + .map(|method| self.resolve_func(&method.docs, &method.ident, &method.inner)) + .partition_result::<_, Error>()?; + + TypeDefKind::Resource(functions) + } parse::InterfaceItemInner::Func(_) => unreachable!(), }; @@ -267,17 +275,16 @@ impl<'a> Resolver<'a> { let params = self.resolve_named_types(&func.params)?; - let result = match &func.result { None => None, Some(parse::FuncResult::Anon(ty)) => { let ty = self.resolve_type(ty)?; Some(FunctionResult::Anon(ty)) - }, + } Some(parse::FuncResult::Named(types)) => { let types = self.resolve_named_types(types)?; Some(FunctionResult::Named(types)) - }, + } }; Ok(Function { @@ -387,8 +394,7 @@ impl<'a> Resolver<'a> { if !self.iface_typedefs.is_empty() { // we use `partition_result` here to aggregate all errors before throwing them, // this way all errors are reported together instead of one by one. - self - .iface_typedefs + self.iface_typedefs .values() .map(|item| Err(Error::unused_type(item.ident.clone()))) .partition_result::<_, Error>()?; diff --git a/wit/resources.wit b/wit/resources.wit new file mode 100644 index 0000000..f223c3d --- /dev/null +++ b/wit/resources.wit @@ -0,0 +1,17 @@ +interface resources { + resource a { + func f1() + func f2(a: u32) + func f3(a: u32, b: u32) + } + + resource b { + func f1() -> a + + func f2(x: a) -> result + func f3(x: option>) -> result + } + + func constructor_a () -> a + func constructor_b () -> b +} \ No newline at end of file