-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.rs
229 lines (176 loc) · 5.97 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use std::{sync::Arc, time::Duration};
use tauri::{AppHandle, Manager, Runtime};
use taurpc::{Router, Windows};
use tokio::{
sync::{oneshot, Mutex},
time::sleep,
};
#[doc = "Doc comments are also generated"]
#[taurpc::ipc_type]
// #[derive(serde::Serialize, serde::Deserialize, specta::Type, Clone)]
struct User {
/// The user's id
uid: i32,
/// The user's first name
first_name: String,
/// The user's last name
last_name: String,
}
// create the error type that represents all errors possible in our program
#[derive(Debug, thiserror::Error)]
enum Error {
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("Other: `{0}`")]
Other(String),
}
// we must manually implement serde::Serialize
impl serde::Serialize for Error {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
serializer.serialize_str(self.to_string().as_ref())
}
}
// #[taurpc::procedures(event_trigger = ApiEventTrigger)]
#[taurpc::procedures(event_trigger = ApiEventTrigger, export_to = "../src/lib/bindings.ts")]
trait Api {
async fn update_state(app_handle: AppHandle<tauri::Wry>, new_value: String);
async fn get_window<R: Runtime>(window: tauri::Window<R>);
async fn get_app_handle<R: Runtime>(app_handle: tauri::AppHandle<R>);
async fn test_io(user: User) -> User;
async fn test_option() -> Option<()>;
async fn test_result(user: User) -> Result<User, Error>;
// #[taurpc(skip)]
async fn with_sleep();
#[taurpc(alias = "method_with_alias")]
async fn with_alias();
#[taurpc(event)]
async fn ev(updated_value: String);
async fn vec_test(arg: Vec<String>);
async fn multiple_args(arg: Vec<String>, arg2: String);
}
#[derive(Clone)]
struct ApiImpl {
state: GlobalState,
}
#[taurpc::resolvers]
impl Api for ApiImpl {
async fn update_state(self, app_handle: AppHandle<tauri::Wry>, new_value: String) {
let mut data = self.state.lock().await;
println!("Before {:?}", data);
*data = new_value;
println!("After {:?}", data);
let uppercase = data.to_uppercase();
TauRpcEventsEventTrigger::new(app_handle)
.state_changed(uppercase)
.unwrap();
}
async fn get_window<R: Runtime>(self, window: tauri::Window<R>) {
println!("{}", window.label());
}
async fn get_app_handle<R: Runtime>(self, app_handle: tauri::AppHandle<R>) {
let app_dir = app_handle.path_resolver().app_config_dir();
println!("{:?}, {:?}", app_dir, app_handle.package_info());
}
async fn test_io(self, user: User) -> User {
user
}
async fn test_option(self) -> Option<()> {
Some(())
}
async fn test_result(self, user: User) -> Result<User, Error> {
Err(Error::Other("Some error message".to_string()))
// Ok(user)
}
async fn with_sleep(self) {
sleep(Duration::from_millis(2000)).await;
}
async fn with_alias(self) {
println!("method with alias called");
}
async fn vec_test(self, arg: Vec<String>) {}
async fn multiple_args(self, arg: Vec<String>, arg2: String) {}
}
#[taurpc::procedures(path = "events", export_to = "../src/lib/bindings.ts")]
trait Events {
#[taurpc(event)]
async fn test_ev();
#[taurpc(event)]
async fn state_changed(new_state: String);
#[taurpc(event)]
async fn vec_test(args: Vec<String>);
#[taurpc(event)]
async fn multiple_args(arg1: u16, arg2: Vec<String>);
}
#[derive(Clone)]
struct EventsImpl;
#[taurpc::resolvers]
impl Events for EventsImpl {}
#[taurpc::procedures(path = "api.ui", export_to = "../src/lib/bindings.ts")]
trait UiApi {
async fn trigger();
#[taurpc(event)]
async fn test_ev();
}
#[derive(Clone)]
struct UiApiImpl;
#[taurpc::resolvers]
impl UiApi for UiApiImpl {
async fn trigger(self) {
println!("Trigger ui event")
}
}
type GlobalState = Arc<Mutex<String>>;
#[tokio::main]
async fn main() {
let (tx, rx) = oneshot::channel::<AppHandle>();
tokio::spawn(async move {
let app_handle = rx.await.unwrap();
let api_trigger = ApiEventTrigger::new(app_handle.clone());
let events_trigger = TauRpcEventsEventTrigger::new(app_handle.clone());
let ui_trigger = TauRpcUiApiEventTrigger::new(app_handle);
let mut interval = tokio::time::interval(Duration::from_secs(1));
loop {
interval.tick().await;
api_trigger
.send_to(Windows::One("main".to_string()))
.update_state("message scoped".to_string())?;
api_trigger.update_state("message".to_string())?;
events_trigger.vec_test(vec![String::from("test"), String::from("test2")])?;
events_trigger.multiple_args(0, vec![String::from("test"), String::from("test2")])?;
events_trigger.test_ev()?;
ui_trigger.test_ev()?;
}
#[allow(unreachable_code)]
Ok::<(), tauri::Error>(())
});
let router = Router::new()
.merge(
ApiImpl {
state: Arc::new(Mutex::new("state".to_string())),
}
.into_handler(),
)
.merge(EventsImpl.into_handler())
.merge(UiApiImpl.into_handler());
tauri::Builder::default()
.invoke_handler(router.into_handler())
// .invoke_handler(taurpc::create_ipc_handler(
// ApiImpl {
// state: Arc::new(Mutex::new("state".to_string())),
// }
// .into_handler(),
// ))
// .invoke_handler(taurpc::create_ipc_handler(EventsImpl.into_handler()))
.setup(|app| {
#[cfg(debug_assertions)]
app.get_window("main").unwrap().open_devtools();
tx.send(app.handle()).unwrap();
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}