mirror of
https://github.com/tauri-apps/tauri-bindgen.git
synced 2026-01-31 00:45:21 +01:00
wip: initial resource implementation
This commit is contained in:
@@ -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::<Vec<_>>()
|
||||
.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"])
|
||||
|
||||
@@ -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<string>}
|
||||
*/
|
||||
export async function returnChar () {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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 () {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
export async function roundtripFlag1 (x) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {Flag2} x
|
||||
* @returns {Promise<Flag2>}
|
||||
@@ -14,7 +13,6 @@
|
||||
export async function roundtripFlag2 (x) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {Flag4} x
|
||||
* @returns {Promise<Flag4>}
|
||||
@@ -22,7 +20,6 @@
|
||||
export async function roundtripFlag4 (x) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {Flag8} x
|
||||
* @returns {Promise<Flag8>}
|
||||
@@ -30,7 +27,6 @@
|
||||
export async function roundtripFlag8 (x) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {Flag16} x
|
||||
* @returns {Promise<Flag16>}
|
||||
@@ -38,7 +34,6 @@
|
||||
export async function roundtripFlag16 (x) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {Flag32} x
|
||||
* @returns {Promise<Flag32>}
|
||||
@@ -46,11 +41,10 @@
|
||||
export async function roundtripFlag32 (x) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {Flag64} x
|
||||
* @returns {Promise<Flag64>}
|
||||
*/
|
||||
export async function roundtripFlag64 (x) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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<number>}
|
||||
*/
|
||||
export async function float32Result () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<number>}
|
||||
*/
|
||||
export async function float64Result () {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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<number>}
|
||||
*/
|
||||
export async function r1 () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<number>}
|
||||
*/
|
||||
export async function r2 () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<number>}
|
||||
*/
|
||||
export async function r3 () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<number>}
|
||||
*/
|
||||
export async function r4 () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<number>}
|
||||
*/
|
||||
export async function r5 () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<number>}
|
||||
*/
|
||||
export async function r6 () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<bigint>}
|
||||
*/
|
||||
export async function r7 () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<bigint>}
|
||||
*/
|
||||
export async function r8 () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<[bigint, number]>}
|
||||
*/
|
||||
export async function pairRet () {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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<Uint8Array[]>}
|
||||
*/
|
||||
export async function listU8Ret () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<Uint16Array[]>}
|
||||
*/
|
||||
export async function listU16Ret () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<Uint32Array[]>}
|
||||
*/
|
||||
export async function listU32Ret () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<BigUint64Array[]>}
|
||||
*/
|
||||
export async function listU64Ret () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<Int8Array[]>}
|
||||
*/
|
||||
export async function listS8Ret () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<Int16Array[]>}
|
||||
*/
|
||||
export async function listS16Ret () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<Int32Array[]>}
|
||||
*/
|
||||
export async function listS32Ret () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<BigInt64Array[]>}
|
||||
*/
|
||||
export async function listS64Ret () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<Float32Array[]>}
|
||||
*/
|
||||
export async function listFloat32Ret () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<Float64Array[]>}
|
||||
*/
|
||||
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<string[]>}
|
||||
*/
|
||||
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<string[]>}
|
||||
@@ -187,7 +152,6 @@
|
||||
export async function stringList (x) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {SomeRecord[]} x
|
||||
* @returns {Promise<OtherRecord[]>}
|
||||
@@ -195,7 +159,6 @@
|
||||
export async function recordList (x) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {OtherRecord[]} x
|
||||
* @returns {Promise<SomeRecord[]>}
|
||||
@@ -203,7 +166,6 @@
|
||||
export async function recordListReverse (x) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {SomeVariant[]} x
|
||||
* @returns {Promise<OtherVariant[]>}
|
||||
@@ -211,11 +173,10 @@
|
||||
export async function variantList (x) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {LoadStoreAllSizes} a
|
||||
* @returns {Promise<LoadStoreAllSizes>}
|
||||
*/
|
||||
export async function loadStoreEverything (a) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,34 +1,30 @@
|
||||
|
||||
/**
|
||||
* @returns {Promise<[]>}
|
||||
*/
|
||||
export async function mra () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<[]>}
|
||||
*/
|
||||
export async function mrb () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<number>}
|
||||
*/
|
||||
export async function mrc () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<number>}
|
||||
* @returns {Promise<[number]>}
|
||||
*/
|
||||
export async function mrd () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<[number, number]>}
|
||||
*/
|
||||
export async function mre () {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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<Empty>}
|
||||
*/
|
||||
export async function emptyResult () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {Scalars} x
|
||||
* @returns {Promise<[]>}
|
||||
*/
|
||||
export async function scalarArg (x) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<Scalars>}
|
||||
*/
|
||||
export async function scalarResult () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {ReallyFlags} x
|
||||
* @returns {Promise<[]>}
|
||||
*/
|
||||
export async function flagsArg (x) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<ReallyFlags>}
|
||||
*/
|
||||
export async function flagsResult () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {Aggregates} x
|
||||
* @returns {Promise<[]>}
|
||||
*/
|
||||
export async function aggregateArg (x) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<Aggregates>}
|
||||
*/
|
||||
export async function aggregateResult () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {TupleTypedef2} e
|
||||
* @returns {Promise<number>}
|
||||
*/
|
||||
export async function typedefInout (e) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
62
crates/gen-guest-js/tests/resources.js
Normal file
62
crates/gen-guest-js/tests/resources.js
Normal file
@@ -0,0 +1,62 @@
|
||||
|
||||
/**
|
||||
* @returns {Promise<A>}
|
||||
*/
|
||||
export async function constructorA () {
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {Promise<B>}
|
||||
*/
|
||||
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<A>}
|
||||
*/
|
||||
async f1 () {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {A} x
|
||||
* @returns {Promise<Result<number, _>>}
|
||||
*/
|
||||
async f2 (x) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {A[] | null} x
|
||||
* @returns {Promise<Result<A, _>>}
|
||||
*/
|
||||
async f3 (x) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<number>}
|
||||
*/
|
||||
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) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,19 +1,16 @@
|
||||
|
||||
/**
|
||||
* @param {Uint32Array[]} l
|
||||
* @returns {Promise<[]>}
|
||||
*/
|
||||
export async function simpleList1 (l) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<Uint32Array[]>}
|
||||
*/
|
||||
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<Uint32Array[][]>}
|
||||
*/
|
||||
export async function simpleList4 (l) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -4,4 +4,4 @@
|
||||
*/
|
||||
export async function optionTest () {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,19 +1,16 @@
|
||||
|
||||
/**
|
||||
* @param {string} x
|
||||
* @returns {Promise<[]>}
|
||||
*/
|
||||
export async function a (x) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<string>}
|
||||
*/
|
||||
export async function b () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {string} a
|
||||
* @param {string} b
|
||||
@@ -21,4 +18,4 @@
|
||||
*/
|
||||
export async function c (a, b) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
export async function addOneInteger (num) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {AllFloats} num
|
||||
* @returns {Promise<AllFloats>}
|
||||
@@ -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<number>}
|
||||
@@ -31,7 +28,6 @@
|
||||
export async function identifyInteger (num) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {AllFloats} num
|
||||
* @returns {Promise<number>}
|
||||
@@ -39,7 +35,6 @@
|
||||
export async function identifyFloat (num) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {AllText} text
|
||||
* @returns {Promise<number>}
|
||||
@@ -47,7 +42,6 @@
|
||||
export async function identifyText (text) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {DuplicatedS32} num
|
||||
* @returns {Promise<DuplicatedS32>}
|
||||
@@ -55,7 +49,6 @@
|
||||
export async function addOneDuplicated (num) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {DuplicatedS32} num
|
||||
* @returns {Promise<number>}
|
||||
@@ -63,7 +56,6 @@
|
||||
export async function identifyDuplicated (num) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {DistinguishableNum} num
|
||||
* @returns {Promise<DistinguishableNum>}
|
||||
@@ -71,11 +63,10 @@
|
||||
export async function addOneDistinguishableNum (num) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {DistinguishableNum} num
|
||||
* @returns {Promise<number>}
|
||||
*/
|
||||
export async function identifyDistinguishableNum (num) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,64 +1,52 @@
|
||||
|
||||
/**
|
||||
* @param {E1} x
|
||||
* @returns {Promise<[]>}
|
||||
*/
|
||||
export async function e1Arg (x) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<E1>}
|
||||
*/
|
||||
export async function e1Result () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {U1} x
|
||||
* @returns {Promise<[]>}
|
||||
*/
|
||||
export async function u1Arg (x) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<U1>}
|
||||
*/
|
||||
export async function u1Result () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {V1} x
|
||||
* @returns {Promise<[]>}
|
||||
*/
|
||||
export async function v1Arg (x) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<V1>}
|
||||
*/
|
||||
export async function v1Result () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {boolean} x
|
||||
* @returns {Promise<[]>}
|
||||
*/
|
||||
export async function boolArg (x) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<boolean>}
|
||||
*/
|
||||
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<number, V1>} e
|
||||
* @param {Result<string, Uint8Array[]>} f
|
||||
* @returns {Promise<[]>}
|
||||
*/
|
||||
export async function resultArg (a, b, c, d, e, f) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<[Result<_, _>, Result<_, E1>, Result<E1, _>, Result<[], []>, Result<number, V1>, Result<string, Uint8Array[]>]>}
|
||||
*/
|
||||
export async function resultResult () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<Result<number, MyErrno>>}
|
||||
*/
|
||||
export async function returnResultSugar () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<Result<_, MyErrno>>}
|
||||
*/
|
||||
export async function returnResultSugar2 () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<Result<MyErrno, MyErrno>>}
|
||||
*/
|
||||
export async function returnResultSugar3 () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<Result<[number, number], MyErrno>>}
|
||||
*/
|
||||
export async function returnResultSugar4 () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<number | null>}
|
||||
*/
|
||||
export async function returnOptionSugar () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<MyErrno | null>}
|
||||
*/
|
||||
export async function returnOptionSugar2 () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<Result<number, number>>}
|
||||
*/
|
||||
export async function resultSimple () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {IsClone} a
|
||||
* @returns {Promise<[]>}
|
||||
*/
|
||||
export async function isCloneArg (a) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<IsClone>}
|
||||
*/
|
||||
export async function isCloneReturn () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<number | null>}
|
||||
* @returns {Promise<[number | null]>}
|
||||
*/
|
||||
export async function returnNamedOption () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns {Promise<Result<number, MyErrno>>}
|
||||
* @returns {Promise<[Result<number, MyErrno>]>}
|
||||
*/
|
||||
export async function returnNamedResult () {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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!()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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<u8> {
|
||||
@@ -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<String> {
|
||||
|
||||
@@ -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!()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
42
crates/gen-guest-rust/tests/resources.rs
Normal file
42
crates/gen-guest-rust/tests/resources.rs
Normal file
@@ -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<u32, ()> {
|
||||
todo!()
|
||||
}
|
||||
pub async fn f3(&self, x: Option<&'_ [A]>) -> Result<A, ()> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
pub async fn constructor_a() -> A {
|
||||
todo!()
|
||||
}
|
||||
pub async fn constructor_b() -> B {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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<u32> {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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<f32>,
|
||||
f: Option<U1>,
|
||||
g: Option<Option<bool>>,
|
||||
) -> () {
|
||||
) {
|
||||
todo!()
|
||||
}
|
||||
pub async fn option_result() -> (
|
||||
@@ -141,7 +141,7 @@ pub mod variants {
|
||||
d: Result<(), ()>,
|
||||
e: Result<u32, V1Param<'_>>,
|
||||
f: Result<&'_ str, &'_ [u8]>,
|
||||
) -> () {
|
||||
) {
|
||||
todo!()
|
||||
}
|
||||
pub async fn result_result() -> (
|
||||
@@ -175,7 +175,7 @@ pub mod variants {
|
||||
pub async fn result_simple() -> Result<u32, i32> {
|
||||
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 {
|
||||
|
||||
@@ -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}
|
||||
}}"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
/**
|
||||
* A function that accepts a character
|
||||
*/
|
||||
export async function takeChar (x: string) : Promise<[]> {
|
||||
export async function takeChar (x: string) {
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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 () {
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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[]) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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) {
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
|
||||
|
||||
export async function mra () : Promise<[]> {
|
||||
export async function mra () {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
45
crates/gen-guest-ts/tests/resources.ts
Normal file
45
crates/gen-guest-ts/tests/resources.ts
Normal file
@@ -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<A> {
|
||||
}
|
||||
|
||||
|
||||
async f2 (x: A) : Promise<Result<number, _>> {
|
||||
}
|
||||
|
||||
|
||||
async f3 (x: A[] | null) : Promise<Result<A, _>> {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
export async function constructorA () : Promise<A> {
|
||||
}
|
||||
|
||||
|
||||
export async function constructorB () : Promise<B> {
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
|
||||
|
||||
export async function simpleList1 (l: Uint32Array[]) : Promise<[]> {
|
||||
export async function simpleList1 (l: Uint32Array[]) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
|
||||
|
||||
export async function a (x: string) : Promise<[]> {
|
||||
export async function a (x: string) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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<E1, _>, d: Result<[], []>, e: Result<number, V1>, f: Result<string, Uint8Array[]>) : Promise<[]> {
|
||||
export async function resultArg (a: Result<_, _>, b: Result<_, E1>, c: Result<E1, _>, d: Result<[], []>, e: Result<number, V1>, f: Result<string, Uint8Array[]>) {
|
||||
}
|
||||
|
||||
|
||||
@@ -183,7 +183,7 @@ v1: V1,
|
||||
}
|
||||
|
||||
|
||||
export async function isCloneArg (a: IsClone) : Promise<[]> {
|
||||
export async function isCloneArg (a: IsClone) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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<Item = &'a Function>,
|
||||
additional_items: impl Iterator<Item = TokenStream>,
|
||||
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,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<T, U>(
|
||||
router: &mut ::tauri_bindgen_host::ipc_router_wip::Router<T>,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<u8>) -> ();
|
||||
fn list_u16_param(&mut self, x: Vec<u16>) -> ();
|
||||
fn list_u32_param(&mut self, x: Vec<u32>) -> ();
|
||||
fn list_u64_param(&mut self, x: Vec<u64>) -> ();
|
||||
fn list_s8_param(&mut self, x: Vec<i8>) -> ();
|
||||
fn list_s16_param(&mut self, x: Vec<i16>) -> ();
|
||||
fn list_s32_param(&mut self, x: Vec<i32>) -> ();
|
||||
fn list_s64_param(&mut self, x: Vec<i64>) -> ();
|
||||
fn list_float32_param(&mut self, x: Vec<f32>) -> ();
|
||||
fn list_float64_param(&mut self, x: Vec<f64>) -> ();
|
||||
fn list_u8_param(&mut self, x: Vec<u8>);
|
||||
fn list_u16_param(&mut self, x: Vec<u16>);
|
||||
fn list_u32_param(&mut self, x: Vec<u32>);
|
||||
fn list_u64_param(&mut self, x: Vec<u64>);
|
||||
fn list_s8_param(&mut self, x: Vec<i8>);
|
||||
fn list_s16_param(&mut self, x: Vec<i16>);
|
||||
fn list_s32_param(&mut self, x: Vec<i32>);
|
||||
fn list_s64_param(&mut self, x: Vec<i64>);
|
||||
fn list_float32_param(&mut self, x: Vec<f32>);
|
||||
fn list_float64_param(&mut self, x: Vec<f64>);
|
||||
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<String>) -> ();
|
||||
fn string_list_arg(&mut self, a: Vec<String>);
|
||||
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<String>) -> &'_ [&'_ str];
|
||||
|
||||
@@ -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<T, U>(
|
||||
router: &mut ::tauri_bindgen_host::ipc_router_wip::Router<T>,
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
37
crates/gen-host/tests/resources.rs
Normal file
37
crates/gen-host/tests/resources.rs
Normal file
@@ -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<u32, ()>;
|
||||
fn f3(
|
||||
&mut self,
|
||||
x: Option<Vec<::tauri_bindgen_host::ResourceId>>,
|
||||
) -> 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<T, U>(
|
||||
router: &mut ::tauri_bindgen_host::ipc_router_wip::Router<T>,
|
||||
get_cx: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
|
||||
) -> Result<(), ::tauri_bindgen_host::ipc_router_wip::Error>
|
||||
where
|
||||
U: Resources,
|
||||
{
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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<u32>) -> ();
|
||||
fn simple_list1(&mut self, l: Vec<u32>);
|
||||
fn simple_list2(&mut self) -> &'_ [u32];
|
||||
fn simple_list3(&mut self, a: Vec<u32>, b: Vec<u32>) -> (&'_ [u32], &'_ [u32]);
|
||||
fn simple_list4(&mut self, l: Vec<Vec<u32>>) -> &'_ [&'_ [u32]];
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<f32>,
|
||||
f: Option<U1>,
|
||||
g: Option<Option<bool>>,
|
||||
) -> ();
|
||||
);
|
||||
fn option_result(
|
||||
&mut self,
|
||||
) -> (
|
||||
@@ -111,7 +111,7 @@ pub mod variants {
|
||||
d: Result<(), ()>,
|
||||
e: Result<u32, V1>,
|
||||
f: Result<String, Vec<u8>>,
|
||||
) -> ();
|
||||
);
|
||||
fn result_result(
|
||||
&mut self,
|
||||
) -> (
|
||||
@@ -129,7 +129,7 @@ pub mod variants {
|
||||
fn return_option_sugar(&mut self) -> Option<i32>;
|
||||
fn return_option_sugar2(&mut self) -> Option<MyErrno>;
|
||||
fn result_simple(&mut self) -> Result<u32, i32>;
|
||||
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<u8>;
|
||||
fn return_named_result(&mut self) -> Result<u8, MyErrno>;
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
|
||||
### Function mrb
|
||||
|
||||
`func mrb ()`
|
||||
`func mrb () -> ()`
|
||||
|
||||
|
||||
### Function mrc
|
||||
|
||||
56
crates/gen-markdown/tests/resources.md
Normal file
56
crates/gen-markdown/tests/resources.md
Normal file
@@ -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<u32, _>`
|
||||
|
||||
### Method f3
|
||||
|
||||
`func f3 (x: option<list<[a](#a)>>) -> result<[a](#a), _>`
|
||||
|
||||
|
||||
|
||||
## Functions
|
||||
|
||||
### Function constructor_a
|
||||
|
||||
`func constructor_a () -> [a](#a)`
|
||||
|
||||
|
||||
### Function constructor_b
|
||||
|
||||
`func constructor_b () -> [b](#b)`
|
||||
|
||||
@@ -16,6 +16,13 @@ pub trait RustGenerator {
|
||||
fn infos(&self) -> &TypeInfos;
|
||||
fn additional_attrs(&self, ident: &str, info: TypeInfo) -> Option<TokenStream>;
|
||||
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(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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<T> {
|
||||
// id: generational_arena::Index,
|
||||
// _m: PhantomData<T>,
|
||||
// }
|
||||
|
||||
// impl<T> Clone for ResourceId<T> {
|
||||
// fn clone(&self) -> Self {
|
||||
// Self {
|
||||
// id: self.id.clone(),
|
||||
// _m: PhantomData,
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// impl<T> Copy for ResourceId<T> {}
|
||||
|
||||
// impl<T> PartialEq for ResourceId<T> {
|
||||
// fn eq(&self, other: &Self) -> bool {
|
||||
// self.id == other.id
|
||||
// }
|
||||
// }
|
||||
|
||||
// impl<T> Eq for ResourceId<T> {}
|
||||
|
||||
@@ -105,6 +105,7 @@ pub enum TypeDefKind {
|
||||
Variant(Vec<VariantCase>),
|
||||
Enum(Vec<EnumCase>),
|
||||
Union(Vec<UnionCase>),
|
||||
Resource(Vec<Function>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
|
||||
@@ -59,6 +59,7 @@ pub enum InterfaceItemInner {
|
||||
Enum(Vec<EnumCase>),
|
||||
Union(Vec<UnionCase>),
|
||||
Func(Func),
|
||||
Resource(Vec<Method>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
@@ -95,7 +96,6 @@ pub struct VariantCase {
|
||||
pub ty: Option<Type>,
|
||||
}
|
||||
|
||||
|
||||
#[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<Span>,
|
||||
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<Self> {
|
||||
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<Self> {
|
||||
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))
|
||||
}
|
||||
|
||||
@@ -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>()?;
|
||||
|
||||
17
wit/resources.wit
Normal file
17
wit/resources.wit
Normal file
@@ -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<u32>
|
||||
func f3(x: option<list<a>>) -> result<a>
|
||||
}
|
||||
|
||||
func constructor_a () -> a
|
||||
func constructor_b () -> b
|
||||
}
|
||||
Reference in New Issue
Block a user