Skip to content

Commit 16651e7

Browse files
committed
fix(web): improve make_bridge! macro hygiene
1 parent baafb20 commit 16651e7

File tree

2 files changed

+163
-144
lines changed

2 files changed

+163
-144
lines changed

crates/iron-remote-desktop/src/lib.rs

Lines changed: 148 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -37,125 +37,93 @@ pub trait RemoteDesktopApi {
3737
#[macro_export]
3838
macro_rules! make_bridge {
3939
($api:ty) => {
40-
use wasm_bindgen::prelude::*;
41-
use web_sys::{js_sys, HtmlCanvasElement};
4240
use $crate::{
4341
ClipboardData as _, ClipboardItem as _, DeviceEvent as _, InputTransaction as _, IronError as _,
44-
RemoteDesktopApi, Session as _, SessionBuilder as _, SessionTerminationInfo as _,
42+
Session as _, SessionBuilder as _, SessionTerminationInfo as _,
4543
};
4644

47-
#[wasm_bindgen]
48-
pub fn setup(log_level: &str) {
49-
<$api as RemoteDesktopApi>::pre_setup();
50-
$crate::internal::setup(log_level);
51-
<$api as RemoteDesktopApi>::post_setup();
52-
}
53-
54-
#[wasm_bindgen]
55-
pub struct DeviceEvent(<$api as RemoteDesktopApi>::DeviceEvent);
45+
#[$crate::internal::wasm_bindgen::prelude::wasm_bindgen]
46+
pub struct Session(<$api as $crate::RemoteDesktopApi>::Session);
5647

57-
impl From<<$api as RemoteDesktopApi>::DeviceEvent> for DeviceEvent {
58-
fn from(value: <$api as RemoteDesktopApi>::DeviceEvent) -> Self {
59-
Self(value)
60-
}
61-
}
48+
#[$crate::internal::wasm_bindgen::prelude::wasm_bindgen]
49+
pub struct SessionBuilder(<$api as $crate::RemoteDesktopApi>::SessionBuilder);
6250

63-
#[wasm_bindgen]
64-
impl DeviceEvent {
65-
pub fn mouse_button_pressed(button: u8) -> Self {
66-
Self(<<$api as RemoteDesktopApi>::DeviceEvent>::mouse_button_pressed(
67-
button,
68-
))
69-
}
70-
71-
pub fn mouse_button_released(button: u8) -> Self {
72-
Self(<<$api as RemoteDesktopApi>::DeviceEvent>::mouse_button_released(
73-
button,
74-
))
75-
}
51+
#[$crate::internal::wasm_bindgen::prelude::wasm_bindgen]
52+
pub struct SessionTerminationInfo(<$api as $crate::RemoteDesktopApi>::SessionTerminationInfo);
7653

77-
pub fn mouse_move(x: u16, y: u16) -> Self {
78-
Self(<<$api as RemoteDesktopApi>::DeviceEvent>::mouse_move(x, y))
79-
}
54+
#[$crate::internal::wasm_bindgen::prelude::wasm_bindgen]
55+
pub struct DeviceEvent(<$api as $crate::RemoteDesktopApi>::DeviceEvent);
8056

81-
pub fn wheel_rotations(vertical: bool, rotation_units: i16) -> Self {
82-
Self(<<$api as RemoteDesktopApi>::DeviceEvent>::wheel_rotations(
83-
vertical,
84-
rotation_units,
85-
))
86-
}
57+
#[$crate::internal::wasm_bindgen::prelude::wasm_bindgen]
58+
pub struct InputTransaction(<$api as $crate::RemoteDesktopApi>::InputTransaction);
8759

88-
pub fn key_pressed(scancode: u16) -> Self {
89-
Self(<<$api as RemoteDesktopApi>::DeviceEvent>::key_pressed(scancode))
90-
}
60+
#[$crate::internal::wasm_bindgen::prelude::wasm_bindgen]
61+
pub struct ClipboardData(<$api as $crate::RemoteDesktopApi>::ClipboardData);
9162

92-
pub fn key_released(scancode: u16) -> Self {
93-
Self(<<$api as RemoteDesktopApi>::DeviceEvent>::key_released(scancode))
94-
}
63+
#[$crate::internal::wasm_bindgen::prelude::wasm_bindgen]
64+
pub struct ClipboardItem(<$api as $crate::RemoteDesktopApi>::ClipboardItem);
9565

96-
pub fn unicode_pressed(unicode: char) -> Self {
97-
Self(<<$api as RemoteDesktopApi>::DeviceEvent>::unicode_pressed(
98-
unicode,
99-
))
100-
}
66+
#[$crate::internal::wasm_bindgen::prelude::wasm_bindgen]
67+
pub struct IronError(<$api as $crate::RemoteDesktopApi>::Error);
10168

102-
pub fn unicode_released(unicode: char) -> Self {
103-
Self(<<$api as RemoteDesktopApi>::DeviceEvent>::unicode_released(
104-
unicode,
105-
))
69+
impl From<<$api as $crate::RemoteDesktopApi>::Session> for Session {
70+
fn from(value: <$api as $crate::RemoteDesktopApi>::Session) -> Self {
71+
Self(value)
10672
}
10773
}
10874

109-
#[wasm_bindgen]
110-
pub struct InputTransaction(<$api as RemoteDesktopApi>::InputTransaction);
111-
112-
impl From<<$api as RemoteDesktopApi>::InputTransaction> for InputTransaction {
113-
fn from(value: <$api as RemoteDesktopApi>::InputTransaction) -> Self {
75+
impl From<<$api as $crate::RemoteDesktopApi>::SessionBuilder> for SessionBuilder {
76+
fn from(value: <$api as $crate::RemoteDesktopApi>::SessionBuilder) -> Self {
11477
Self(value)
11578
}
11679
}
11780

118-
#[wasm_bindgen]
119-
impl InputTransaction {
120-
pub fn init() -> Self {
121-
Self(<<$api as RemoteDesktopApi>::InputTransaction>::init())
81+
impl From<<$api as $crate::RemoteDesktopApi>::SessionTerminationInfo> for SessionTerminationInfo {
82+
fn from(value: <$api as $crate::RemoteDesktopApi>::SessionTerminationInfo) -> Self {
83+
Self(value)
12284
}
85+
}
12386

124-
pub fn add_event(&mut self, event: DeviceEvent) {
125-
self.0.add_event(event.0);
87+
impl From<<$api as $crate::RemoteDesktopApi>::DeviceEvent> for DeviceEvent {
88+
fn from(value: <$api as $crate::RemoteDesktopApi>::DeviceEvent) -> Self {
89+
Self(value)
12690
}
12791
}
12892

129-
#[wasm_bindgen]
130-
pub struct IronError(<$api as RemoteDesktopApi>::Error);
131-
132-
impl From<<$api as RemoteDesktopApi>::Error> for IronError {
133-
fn from(value: <$api as RemoteDesktopApi>::Error) -> Self {
93+
impl From<<$api as $crate::RemoteDesktopApi>::InputTransaction> for InputTransaction {
94+
fn from(value: <$api as $crate::RemoteDesktopApi>::InputTransaction) -> Self {
13495
Self(value)
13596
}
13697
}
13798

138-
#[wasm_bindgen]
139-
impl IronError {
140-
pub fn backtrace(&self) -> String {
141-
self.0.backtrace()
99+
impl From<<$api as $crate::RemoteDesktopApi>::ClipboardData> for ClipboardData {
100+
fn from(value: <$api as $crate::RemoteDesktopApi>::ClipboardData) -> Self {
101+
Self(value)
142102
}
103+
}
143104

144-
pub fn kind(&self) -> $crate::IronErrorKind {
145-
self.0.kind()
105+
impl From<<$api as $crate::RemoteDesktopApi>::ClipboardItem> for ClipboardItem {
106+
fn from(value: <$api as $crate::RemoteDesktopApi>::ClipboardItem) -> Self {
107+
Self(value)
146108
}
147109
}
148110

149-
#[wasm_bindgen]
150-
pub struct Session(<$api as RemoteDesktopApi>::Session);
151-
152-
impl From<<$api as RemoteDesktopApi>::Session> for Session {
153-
fn from(value: <$api as RemoteDesktopApi>::Session) -> Self {
111+
impl From<<$api as $crate::RemoteDesktopApi>::Error> for IronError {
112+
fn from(value: <$api as $crate::RemoteDesktopApi>::Error) -> Self {
154113
Self(value)
155114
}
156115
}
157116

158-
#[wasm_bindgen]
117+
#[$crate::internal::wasm_bindgen::prelude::wasm_bindgen]
118+
#[doc(hidden)]
119+
pub fn setup(log_level: &str) {
120+
<$api as $crate::RemoteDesktopApi>::pre_setup();
121+
$crate::internal::setup(log_level);
122+
<$api as $crate::RemoteDesktopApi>::post_setup();
123+
}
124+
125+
#[$crate::internal::wasm_bindgen::prelude::wasm_bindgen]
126+
#[doc(hidden)]
159127
impl Session {
160128
pub async fn run(&self) -> Result<SessionTerminationInfo, IronError> {
161129
self.0.run().await.map(SessionTerminationInfo).map_err(IronError)
@@ -209,24 +177,18 @@ macro_rules! make_bridge {
209177
self.0.supports_unicode_keyboard_shortcuts()
210178
}
211179

212-
pub fn extension_call(ext: $crate::Extension) -> Result<JsValue, IronError> {
213-
<<$api as RemoteDesktopApi>::Session>::extension_call(ext).map_err(IronError)
180+
pub fn extension_call(
181+
ext: $crate::Extension,
182+
) -> Result<$crate::internal::wasm_bindgen::JsValue, IronError> {
183+
<<$api as $crate::RemoteDesktopApi>::Session>::extension_call(ext).map_err(IronError)
214184
}
215185
}
216186

217-
#[wasm_bindgen]
218-
pub struct SessionBuilder(<$api as RemoteDesktopApi>::SessionBuilder);
219-
220-
impl From<<$api as RemoteDesktopApi>::SessionBuilder> for SessionBuilder {
221-
fn from(value: <$api as RemoteDesktopApi>::SessionBuilder) -> Self {
222-
Self(value)
223-
}
224-
}
225-
226-
#[wasm_bindgen]
187+
#[$crate::internal::wasm_bindgen::prelude::wasm_bindgen]
188+
#[doc(hidden)]
227189
impl SessionBuilder {
228190
pub fn init() -> Self {
229-
Self(<<$api as RemoteDesktopApi>::SessionBuilder>::init())
191+
Self(<<$api as $crate::RemoteDesktopApi>::SessionBuilder>::init())
230192
}
231193

232194
pub fn username(&self, username: String) -> Self {
@@ -257,27 +219,36 @@ macro_rules! make_bridge {
257219
Self(self.0.desktop_size(desktop_size))
258220
}
259221

260-
pub fn render_canvas(&self, canvas: HtmlCanvasElement) -> Self {
222+
pub fn render_canvas(&self, canvas: $crate::internal::web_sys::HtmlCanvasElement) -> Self {
261223
Self(self.0.render_canvas(canvas))
262224
}
263225

264-
pub fn set_cursor_style_callback(&self, callback: js_sys::Function) -> Self {
226+
pub fn set_cursor_style_callback(&self, callback: $crate::internal::web_sys::js_sys::Function) -> Self {
265227
Self(self.0.set_cursor_style_callback(callback))
266228
}
267229

268-
pub fn set_cursor_style_callback_context(&self, context: JsValue) -> Self {
230+
pub fn set_cursor_style_callback_context(&self, context: $crate::internal::wasm_bindgen::JsValue) -> Self {
269231
Self(self.0.set_cursor_style_callback_context(context))
270232
}
271233

272-
pub fn remote_clipboard_changed_callback(&self, callback: js_sys::Function) -> Self {
234+
pub fn remote_clipboard_changed_callback(
235+
&self,
236+
callback: $crate::internal::web_sys::js_sys::Function,
237+
) -> Self {
273238
Self(self.0.remote_clipboard_changed_callback(callback))
274239
}
275240

276-
pub fn remote_received_format_list_callback(&self, callback: js_sys::Function) -> Self {
241+
pub fn remote_received_format_list_callback(
242+
&self,
243+
callback: $crate::internal::web_sys::js_sys::Function,
244+
) -> Self {
277245
Self(self.0.remote_received_format_list_callback(callback))
278246
}
279247

280-
pub fn force_clipboard_update_callback(&self, callback: js_sys::Function) -> Self {
248+
pub fn force_clipboard_update_callback(
249+
&self,
250+
callback: $crate::internal::web_sys::js_sys::Function,
251+
) -> Self {
281252
Self(self.0.force_clipboard_update_callback(callback))
282253
}
283254

@@ -290,35 +261,73 @@ macro_rules! make_bridge {
290261
}
291262
}
292263

293-
#[wasm_bindgen]
294-
pub struct SessionTerminationInfo(<$api as RemoteDesktopApi>::SessionTerminationInfo);
295-
296-
impl From<<$api as RemoteDesktopApi>::SessionTerminationInfo> for SessionTerminationInfo {
297-
fn from(value: <$api as RemoteDesktopApi>::SessionTerminationInfo) -> Self {
298-
Self(value)
299-
}
300-
}
301-
302-
#[wasm_bindgen]
264+
#[$crate::internal::wasm_bindgen::prelude::wasm_bindgen]
265+
#[doc(hidden)]
303266
impl SessionTerminationInfo {
304267
pub fn reason(&self) -> String {
305268
self.0.reason()
306269
}
307270
}
308271

309-
#[wasm_bindgen]
310-
pub struct ClipboardData(<$api as RemoteDesktopApi>::ClipboardData);
272+
#[$crate::internal::wasm_bindgen::prelude::wasm_bindgen]
273+
#[doc(hidden)]
274+
impl DeviceEvent {
275+
pub fn mouse_button_pressed(button: u8) -> Self {
276+
Self(<<$api as $crate::RemoteDesktopApi>::DeviceEvent>::mouse_button_pressed(button))
277+
}
311278

312-
impl From<<$api as RemoteDesktopApi>::ClipboardData> for ClipboardData {
313-
fn from(value: <$api as RemoteDesktopApi>::ClipboardData) -> Self {
314-
Self(value)
279+
pub fn mouse_button_released(button: u8) -> Self {
280+
Self(<<$api as $crate::RemoteDesktopApi>::DeviceEvent>::mouse_button_released(button))
281+
}
282+
283+
pub fn mouse_move(x: u16, y: u16) -> Self {
284+
Self(<<$api as $crate::RemoteDesktopApi>::DeviceEvent>::mouse_move(
285+
x, y,
286+
))
287+
}
288+
289+
pub fn wheel_rotations(vertical: bool, rotation_units: i16) -> Self {
290+
Self(<<$api as $crate::RemoteDesktopApi>::DeviceEvent>::wheel_rotations(vertical, rotation_units))
291+
}
292+
293+
pub fn key_pressed(scancode: u16) -> Self {
294+
Self(<<$api as $crate::RemoteDesktopApi>::DeviceEvent>::key_pressed(
295+
scancode,
296+
))
297+
}
298+
299+
pub fn key_released(scancode: u16) -> Self {
300+
Self(<<$api as $crate::RemoteDesktopApi>::DeviceEvent>::key_released(
301+
scancode,
302+
))
303+
}
304+
305+
pub fn unicode_pressed(unicode: char) -> Self {
306+
Self(<<$api as $crate::RemoteDesktopApi>::DeviceEvent>::unicode_pressed(unicode))
307+
}
308+
309+
pub fn unicode_released(unicode: char) -> Self {
310+
Self(<<$api as $crate::RemoteDesktopApi>::DeviceEvent>::unicode_released(unicode))
315311
}
316312
}
317313

318-
#[wasm_bindgen]
314+
#[$crate::internal::wasm_bindgen::prelude::wasm_bindgen]
315+
#[doc(hidden)]
316+
impl InputTransaction {
317+
pub fn init() -> Self {
318+
Self(<<$api as $crate::RemoteDesktopApi>::InputTransaction>::init())
319+
}
320+
321+
pub fn add_event(&mut self, event: DeviceEvent) {
322+
self.0.add_event(event.0);
323+
}
324+
}
325+
326+
#[$crate::internal::wasm_bindgen::prelude::wasm_bindgen]
327+
#[doc(hidden)]
319328
impl ClipboardData {
320329
pub fn init() -> Self {
321-
Self(<<$api as RemoteDesktopApi>::ClipboardData>::init())
330+
Self(<<$api as $crate::RemoteDesktopApi>::ClipboardData>::init())
322331
}
323332

324333
pub fn add_text(&mut self, mime_type: &str, text: &str) {
@@ -338,30 +347,40 @@ macro_rules! make_bridge {
338347
}
339348
}
340349

341-
#[wasm_bindgen]
342-
pub struct ClipboardItem(<$api as RemoteDesktopApi>::ClipboardItem);
343-
344-
impl From<<$api as RemoteDesktopApi>::ClipboardItem> for ClipboardItem {
345-
fn from(value: <$api as RemoteDesktopApi>::ClipboardItem) -> Self {
346-
Self(value)
347-
}
348-
}
349-
350-
#[wasm_bindgen]
350+
#[$crate::internal::wasm_bindgen::prelude::wasm_bindgen]
351+
#[doc(hidden)]
351352
impl ClipboardItem {
352353
pub fn mime_type(&self) -> String {
353354
self.0.mime_type().to_owned()
354355
}
355356

356-
pub fn value(&self) -> JsValue {
357+
pub fn value(&self) -> $crate::internal::wasm_bindgen::JsValue {
357358
self.0.value().into()
358359
}
359360
}
361+
362+
#[$crate::internal::wasm_bindgen::prelude::wasm_bindgen]
363+
#[doc(hidden)]
364+
impl IronError {
365+
pub fn backtrace(&self) -> String {
366+
self.0.backtrace()
367+
}
368+
369+
pub fn kind(&self) -> $crate::IronErrorKind {
370+
self.0.kind()
371+
}
372+
}
360373
};
361374
}
362375

363376
#[doc(hidden)]
364377
pub mod internal {
378+
#[doc(hidden)]
379+
pub use web_sys;
380+
381+
#[doc(hidden)]
382+
pub use wasm_bindgen;
383+
365384
#[doc(hidden)]
366385
pub fn setup(log_level: &str) {
367386
// When the `console_error_panic_hook` feature is enabled, we can call the

0 commit comments

Comments
 (0)