Skip to content

Commit

Permalink
Merge pull request #112 from oscartbeaumont/v2.5
Browse files Browse the repository at this point in the history
v2.5
  • Loading branch information
oscartbeaumont authored Aug 1, 2024
2 parents 0f2ab22 + f3c8273 commit 9e91bf7
Show file tree
Hide file tree
Showing 27 changed files with 1,800 additions and 1,743 deletions.
1,042 changes: 549 additions & 493 deletions Cargo.lock

Large diffs are not rendered by default.

39 changes: 29 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,29 @@ rustdoc-args = ["--cfg", "docsrs"]

[features]
default = []
javascript = ["dep:specta-typescript"]
derive = ["dep:tauri-specta-macros"]
javascript = ["dep:specta-jsdoc"]
typescript = ["dep:specta-typescript"]

# TODO: https://github.com/tauri-apps/tauri/pull/10435
UNSTABLE_channels = []

[lints]
workspace = true

[dependencies]
# Public
specta = { workspace = true, features = ["function"] }
specta-typescript = { workspace = true, optional = true }
specta-util = { workspace = true }
tauri-specta-macros = { version = "=2.0.0-rc.5", path = "./macros" }
specta-jsdoc = { workspace = true, optional = true }
tauri-specta-macros = { version = "=2.0.0-rc.5", optional = true, path = "./macros" }
serde = "1"
serde_json = "1"
thiserror = "1"
tauri = { workspace = true, default-features = false, features = ["specta"] }

# Private
heck = "0.5.0"
indoc = "2.0.5"

[workspace]
members = [
Expand All @@ -48,16 +51,32 @@ members = [
"macros",
]

[workspace.lints.rust]
unsafe_code = { level = "forbid", priority = -1 }
missing_docs = { level = "warn", priority = -1 }

[workspace.lints.clippy]
all = { level = "warn", priority = -1 }
cargo = { level = "warn", priority = -1 }
unwrap_used = { level = "warn", priority = -1 }
panic = { level = "warn", priority = -1 }
todo = { level = "warn", priority = -1 }
panic_in_result_fn = { level = "warn", priority = -1 }

[workspace.dependencies]
tauri = { version = "=2.0.0-beta.25" }
specta = { version = "=2.0.0-rc.16" }
specta-util = { version = "0.0.3" }
specta-typescript = { version = "0.0.3" }
specta-jsdoc = { version = "0.0.3" }

[patch.crates-io]
# tauri = { git = "https://github.com/oscartbeaumont/tauri.git", branch = "fix-channel-specta-remote-impl" }
# tauri-build = { git = "https://github.com/oscartbeaumont/tauri.git", branch = "fix-channel-specta-remote-impl" }
# tauri = { git = "https://github.com/oscartbeaumont/tauri.git", rev = "ddc64b706a7f2db271d40e9b216187b1aa153efa" }
# tauri-build = { git = "https://github.com/oscartbeaumont/tauri.git", rev = "ddc64b706a7f2db271d40e9b216187b1aa153efa" }

# specta = { git = "https://github.com/oscartbeaumont/specta.git", rev = "cfa98fa10484e55923497f3cc21edf7418667c76" }
# specta-util = { git = "https://github.com/oscartbeaumont/specta.git", rev = "cfa98fa10484e55923497f3cc21edf7418667c76" }
# specta-typescript = { git = "https://github.com/oscartbeaumont/specta.git", rev = "cfa98fa10484e55923497f3cc21edf7418667c76" }

specta = { git = "https://github.com/oscartbeaumont/specta.git", rev = "cfa98fa10484e55923497f3cc21edf7418667c76" }
specta-util = { git = "https://github.com/oscartbeaumont/specta.git", rev = "cfa98fa10484e55923497f3cc21edf7418667c76" }
specta-typescript = { git = "https://github.com/oscartbeaumont/specta.git", rev = "cfa98fa10484e55923497f3cc21edf7418667c76" }
specta = { path = "../specta/specta" }
specta-typescript = { path = "../specta/specta-typescript" }
specta-jsdoc = { path = "../specta/specta-jsdoc" }
4 changes: 2 additions & 2 deletions examples/app/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ serde_json = "1.0"
specta = { workspace = true }
serde = { version = "1.0", features = ["derive"] }
tauri = { workspace = true, features = [] }
tauri-specta = { path = "../../../", features = ["typescript", "javascript"] }
specta-util = { workspace = true }
tauri-specta = { path = "../../../", features = ["derive", "typescript", "javascript"] }
specta-typescript = { workspace = true }
specta-jsdoc = { workspace = true }
tauri-plugin-os = "^2.0.0-beta.3"
thiserror = "1"

Expand Down
69 changes: 38 additions & 31 deletions examples/app/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use serde::{Deserialize, Serialize};
use specta::Type;
use specta_util::TypeCollection;
use specta_typescript::Typescript;
use tauri_specta::*;
use thiserror::Error;

Expand Down Expand Up @@ -116,38 +116,45 @@ pub struct Testing {
}

fn main() {
let (invoke_handler, register_events) = {
let builder = ts::builder()
.commands(tauri_specta::collect_commands![
hello_world,
goodbye_world,
has_error,
nested::some_struct,
generic::<tauri::Wry>,
deprecated,
typesafe_errors_using_thiserror,
typesafe_errors_using_thiserror_with_value
])
.events(tauri_specta::collect_events![crate::DemoEvent, EmptyEvent])
.types(TypeCollection::default().register::<Custom>())
.config(
specta_typescript::Typescript::default()
.formatter(specta_typescript::formatter::prettier),
)
.types(TypeCollection::default().register::<Testing>())
.statics(StaticCollection::default().register("universalConstant", 42))
.header("/* These are my Tauri Specta Bindings! */");

#[cfg(debug_assertions)]
let builder = builder.path("../src/bindings.ts");

builder.build().unwrap()
};
let mut builder = Builder::<tauri::Wry>::new()
.commands(tauri_specta::collect_commands![
hello_world,
goodbye_world,
has_error,
// nested::some_struct,
// generic::<tauri::Wry>,
deprecated,
typesafe_errors_using_thiserror,
typesafe_errors_using_thiserror_with_value
])
.events(tauri_specta::collect_events![crate::DemoEvent, EmptyEvent])
.ty::<Custom>()
.constant("universalConstant", 42);

#[cfg(debug_assertions)]
builder
.export(
Typescript::default()
.formatter(specta_typescript::formatter::prettier)
.header("/* eslint-disable */"),
"../src/bindings.ts",
)
.expect("Failed to export typescript bindings");

#[cfg(debug_assertions)]
builder
.export(
specta_jsdoc::JSDoc::default()
.formatter(specta_typescript::formatter::prettier)
.header("/* eslint-disable */"),
"../src/bindings-jsdoc.js",
)
.expect("Failed to export typescript bindings");

tauri::Builder::default()
.invoke_handler(invoke_handler)
.setup(|app| {
register_events(app);
.invoke_handler(builder.invoke_handler())
.setup(move |app| {
builder.mount_events(app);

let handle = app.handle();

Expand Down
175 changes: 175 additions & 0 deletions examples/app/src/bindings-jsdoc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/* eslint-disable */
// This file was generated by [tauri-specta](https://github.com/oscartbeaumont/tauri-specta). Do not edit this file manually.

/** user-defined commands **/

export const commands = {
/**
* HELLO
* WORLD
* !!!!
* @param { string } myName
* @returns { Promise<string> }
*/
async helloWorld(myName) {
return await TAURI_INVOKE("hello_world", { myName });
},
/**
* @returns { Promise<string> }
*/
async goodbyeWorld() {
return await TAURI_INVOKE("goodbye_world");
},
/**
* @returns { Promise<Result<string, number>> }
*/
async hasError() {
try {
return { status: "ok", data: await TAURI_INVOKE("has_error") };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e };
}
},
/**
* @deprecated This is a deprecated function
* @returns { Promise<void> }
*/
async deprecated() {
await TAURI_INVOKE("deprecated");
},
/**
* @returns { Promise<Result<null, MyError>> }
*/
async typesafeErrorsUsingThiserror() {
try {
return { status: "ok", data: await TAURI_INVOKE("typesafe_errors_using_thiserror") };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e };
}
},
/**
* @returns { Promise<Result<null, MyError2>> }
*/
async typesafeErrorsUsingThiserrorWithValue() {
try {
return { status: "ok", data: await TAURI_INVOKE("typesafe_errors_using_thiserror_with_value") };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e };
}
}
}

/** user-defined events **/


/**
* @type {typeof __makeEvents__<{
* demoEvent: DemoEvent
* emptyEvent: EmptyEvent
* }>}
*/

const __typedMakeEvents__ = __makeEvents__;

export const events = __typedMakeEvents__({
demoEvent: "demo-event",
emptyEvent: "empty-event"
})

/** user-defined constants **/

export const universalConstant = 42;

/** user-defined types **/

/**
* @typedef { string } Custom
*/

/**
* @typedef { string } DemoEvent
*/

/**
* @typedef { null } EmptyEvent
*/

/**
* @typedef { { type: "IoError" } | { type: "AnotherError"; data: string } } MyError
*/

/**
* @typedef { { type: "IoError"; data: string } } MyError2
*/


/** tauri-specta globals **/

import {
invoke as TAURI_INVOKE,
Channel as TAURI_CHANNEL,
} from "@tauri-apps/api/core";
import * as TAURI_API_EVENT from "@tauri-apps/api/event";

/** @typedef {typeof import("@tauri-apps/api/window").WebviewWindowHandle} __WebviewWindowHandle__ */

/**
* @template T
* @typedef {{
* listen: (
* cb: TAURI_API_EVENT.EventCallback<T>
* ) => ReturnType<typeof TAURI_API_EVENT.listen<T>>;
* once: (
* cb: TAURI_API_EVENT.EventCallback<T>
* ) => ReturnType<typeof TAURI_API_EVENT.once<T>>;
* emit: T extends null
* ? (payload?: T) => ReturnType<typeof TAURI_API_EVENT.emit>
* : (payload: T) => ReturnType<typeof TAURI_API_EVENT.emit>;
* }} __EventObj__<T>
*/

/**
* @template T,E
* @typedef { { status: "ok", data: T } | { status: "error", error: E } } Result
*/

/**
* @template {Record<string, any>} T
* @param {Record<keyof T, string>} mappings
* @returns {{
* [K in keyof T]: __EventObj__<T[K]> & {
* (handle: __WebviewWindowHandle__): __EventObj__<T[K]>;
* };
* }}
*/
function __makeEvents__(mappings) {
return new Proxy(
{},
{
get: (_, event) => {
const name = mappings[event];

new Proxy(() => {}, {
apply: (_, __, [window]) => ({
listen: (arg) => window.listen(name, arg),
once: (arg) => window.once(name, arg),
emit: (arg) => window.emit(name, arg),
}),
get: (_, command) => {
switch (command) {
case "listen":
return (arg) => TAURI_API_EVENT.listen(name, arg);
case "once":
return (arg) => TAURI_API_EVENT.once(name, arg);
case "emit":
return (arg) => TAURI_API_EVENT.emit(name, arg);
}
},
});
},
},
);
}
Loading

0 comments on commit 9e91bf7

Please sign in to comment.