Skip to content

Commit fa9341b

Browse files
authored
feat(core): implement Debug on public API structs/enums, closes #2292 (#2387)
1 parent 50bf87a commit fa9341b

24 files changed

Lines changed: 133 additions & 32 deletions

File tree

.changes/implement-debug.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"tauri": patch
3+
"tauri-build": patch
4+
"tauri-utils": patch
5+
"tauri-runtime": patch
6+
"tauri-runtime-wry": patch
7+
---
8+
9+
Implement `Debug` on public API structs and enums.

core/tauri-build/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub use codegen::context::CodegenContext;
1616

1717
/// Attributes used on Windows.
1818
#[allow(dead_code)]
19+
#[derive(Debug)]
1920
pub struct WindowsAttributes {
2021
window_icon_path: PathBuf,
2122
}
@@ -43,7 +44,7 @@ impl WindowsAttributes {
4344
}
4445

4546
/// The attributes used on the build.
46-
#[derive(Default)]
47+
#[derive(Debug, Default)]
4748
pub struct Attributes {
4849
#[allow(dead_code)]
4950
windows_attributes: WindowsAttributes,

core/tauri-runtime-wry/src/lib.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ use std::{
6969
HashMap,
7070
},
7171
convert::TryFrom,
72+
fmt,
7273
fs::read,
7374
path::PathBuf,
7475
sync::{
@@ -126,7 +127,7 @@ macro_rules! getter {
126127
}};
127128
}
128129

129-
#[derive(Clone)]
130+
#[derive(Debug, Clone)]
130131
struct EventLoopContext {
131132
main_thread_id: ThreadId,
132133
is_event_loop_running: Arc<AtomicBool>,
@@ -146,6 +147,15 @@ pub struct GlobalShortcutManagerHandle {
146147
listeners: GlobalShortcutListeners,
147148
}
148149

150+
impl fmt::Debug for GlobalShortcutManagerHandle {
151+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152+
f.debug_struct("GlobalShortcutManagerHandle")
153+
.field("context", &self.context)
154+
.field("shortcuts", &self.shortcuts)
155+
.finish()
156+
}
157+
}
158+
149159
impl GlobalShortcutManager for GlobalShortcutManagerHandle {
150160
fn is_registered(&self, accelerator: &str) -> Result<bool> {
151161
let (tx, rx) = channel();
@@ -205,7 +215,7 @@ impl GlobalShortcutManager for GlobalShortcutManagerHandle {
205215
}
206216
}
207217

208-
#[derive(Clone)]
218+
#[derive(Debug, Clone)]
209219
pub struct ClipboardManagerWrapper {
210220
context: EventLoopContext,
211221
}
@@ -728,23 +738,23 @@ pub enum WebviewEvent {
728738
}
729739

730740
#[cfg(feature = "system-tray")]
731-
#[derive(Clone)]
741+
#[derive(Debug, Clone)]
732742
pub enum TrayMessage {
733743
UpdateItem(u16, menu::MenuUpdate),
734744
UpdateIcon(Icon),
735745
#[cfg(target_os = "macos")]
736746
UpdateIconAsTemplate(bool),
737747
}
738748

739-
#[derive(Clone)]
749+
#[derive(Debug, Clone)]
740750
pub enum GlobalShortcutMessage {
741751
IsRegistered(Accelerator, Sender<bool>),
742752
Register(Accelerator, Sender<Result<GlobalShortcutWrapper>>),
743753
Unregister(GlobalShortcutWrapper, Sender<Result<()>>),
744754
UnregisterAll(Sender<Result<()>>),
745755
}
746756

747-
#[derive(Clone)]
757+
#[derive(Debug, Clone)]
748758
pub enum ClipboardMessage {
749759
WriteText(String, Sender<()>),
750760
ReadText(Sender<Option<String>>),
@@ -780,8 +790,18 @@ struct DispatcherContext {
780790
menu_event_listeners: MenuEventListeners,
781791
}
782792

793+
impl fmt::Debug for DispatcherContext {
794+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
795+
f.debug_struct("DispatcherContext")
796+
.field("main_thread_id", &self.main_thread_id)
797+
.field("is_event_loop_running", &self.is_event_loop_running)
798+
.field("proxy", &self.proxy)
799+
.finish()
800+
}
801+
}
802+
783803
/// The Tauri [`Dispatch`] for [`Wry`].
784-
#[derive(Clone)]
804+
#[derive(Debug, Clone)]
785805
pub struct WryDispatcher {
786806
window_id: WindowId,
787807
context: DispatcherContext,
@@ -1261,7 +1281,7 @@ pub struct Wry {
12611281
}
12621282

12631283
/// A handle to the Wry runtime.
1264-
#[derive(Clone)]
1284+
#[derive(Debug, Clone)]
12651285
pub struct WryHandle {
12661286
dispatcher_context: DispatcherContext,
12671287
}

core/tauri-runtime-wry/src/menu.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub type SystemTrayEventListeners = Arc<Mutex<HashMap<Uuid, SystemTrayEventHandl
5656
pub type SystemTrayItems = Arc<Mutex<HashMap<u16, WryCustomMenuItem>>>;
5757

5858
#[cfg(feature = "system-tray")]
59-
#[derive(Clone)]
59+
#[derive(Debug, Clone)]
6060
pub struct SystemTrayHandle {
6161
pub(crate) proxy: EventLoopProxy<super::Message>,
6262
}
@@ -87,6 +87,7 @@ impl TrayHandle for SystemTrayHandle {
8787
}
8888

8989
#[cfg(target_os = "macos")]
90+
#[derive(Debug)]
9091
pub struct NativeImageWrapper(pub WryNativeImage);
9192

9293
#[cfg(target_os = "macos")]

core/tauri-runtime/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use window::{
3131

3232
#[cfg(feature = "system-tray")]
3333
#[non_exhaustive]
34+
#[derive(Debug)]
3435
pub struct SystemTray {
3536
pub icon: Option<Icon>,
3637
pub menu: Option<menu::SystemTrayMenu>,
@@ -193,6 +194,7 @@ pub enum ExitRequestedEventAction {
193194
}
194195

195196
/// A system tray event.
197+
#[derive(Debug)]
196198
pub enum SystemTrayEvent {
197199
MenuItemClick(u16),
198200
LeftClick {
@@ -216,7 +218,7 @@ pub struct RunIteration {
216218
}
217219

218220
/// A [`Send`] handle to the runtime.
219-
pub trait RuntimeHandle: Send + Sized + Clone + 'static {
221+
pub trait RuntimeHandle: Debug + Send + Sized + Clone + 'static {
220222
type Runtime: Runtime<Handle = Self>;
221223
/// Create a new webview window.
222224
fn create_window(
@@ -230,7 +232,7 @@ pub trait RuntimeHandle: Send + Sized + Clone + 'static {
230232
}
231233

232234
/// A global shortcut manager.
233-
pub trait GlobalShortcutManager {
235+
pub trait GlobalShortcutManager: Debug {
234236
/// Whether the application has registered the given `accelerator`.
235237
///
236238
/// # Panics
@@ -269,7 +271,7 @@ pub trait GlobalShortcutManager {
269271
}
270272

271273
/// Clipboard manager.
272-
pub trait ClipboardManager {
274+
pub trait ClipboardManager: Debug {
273275
/// Writes the text into the clipboard as plain text.
274276
///
275277
/// # Panics
@@ -335,7 +337,7 @@ pub trait Runtime: Sized + 'static {
335337
}
336338

337339
/// Webview dispatcher. A thread-safe handle to the webview API.
338-
pub trait Dispatch: Clone + Send + Sized + 'static {
340+
pub trait Dispatch: Debug + Clone + Send + Sized + 'static {
339341
/// The runtime this [`Dispatch`] runs under.
340342
type Runtime: Runtime;
341343

core/tauri-runtime/src/menu.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use std::{
66
collections::hash_map::DefaultHasher,
7+
fmt,
78
hash::{Hash, Hasher},
89
};
910

@@ -145,7 +146,7 @@ pub enum MenuUpdate {
145146
SetNativeImage(NativeImage),
146147
}
147148

148-
pub trait TrayHandle {
149+
pub trait TrayHandle: fmt::Debug {
149150
fn set_icon(&self, icon: crate::Icon) -> crate::Result<()>;
150151
fn update_item(&self, id: u16, update: MenuUpdate) -> crate::Result<()>;
151152
#[cfg(target_os = "macos")]

core/tauri-runtime/src/webview.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use tauri_utils::config::{WindowConfig, WindowUrl};
1616
#[cfg(windows)]
1717
use winapi::shared::windef::HWND;
1818

19-
use std::{collections::HashMap, path::PathBuf};
19+
use std::{collections::HashMap, fmt, path::PathBuf};
2020

2121
type UriSchemeProtocol =
2222
dyn Fn(&str) -> Result<Vec<u8>, Box<dyn std::error::Error>> + Send + Sync + 'static;
@@ -30,6 +30,17 @@ pub struct WebviewAttributes {
3030
pub file_drop_handler_enabled: bool,
3131
}
3232

33+
impl fmt::Debug for WebviewAttributes {
34+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35+
f.debug_struct("WebviewAttributes")
36+
.field("url", &self.url)
37+
.field("initialization_scripts", &self.initialization_scripts)
38+
.field("data_directory", &self.data_directory)
39+
.field("file_drop_handler_enabled", &self.file_drop_handler_enabled)
40+
.finish()
41+
}
42+
}
43+
3344
impl WebviewAttributes {
3445
/// Initializes the default attributes for a webview.
3546
pub fn new(url: WindowUrl) -> Self {
@@ -93,7 +104,7 @@ impl WebviewAttributes {
93104
/// Do **NOT** implement this trait except for use in a custom [`Runtime`](crate::Runtime).
94105
///
95106
/// This trait is separate from [`WindowBuilder`] to prevent "accidental" implementation.
96-
pub trait WindowBuilderBase: Sized {}
107+
pub trait WindowBuilderBase: fmt::Debug + Sized {}
97108

98109
/// A builder for all attributes related to a single webview.
99110
///
@@ -189,6 +200,7 @@ pub trait WindowBuilder: WindowBuilderBase {
189200
}
190201

191202
/// Rpc request.
203+
#[derive(Debug)]
192204
pub struct RpcRequest {
193205
/// RPC command.
194206
pub command: String,
@@ -222,7 +234,7 @@ pub type WebviewRpcHandler<R> = Box<dyn Fn(DetachedWindow<R>, RpcRequest) + Send
222234
/// Return `true` in the callback to block the OS' default behavior of handling a file drop.
223235
pub type FileDropHandler<R> = Box<dyn Fn(FileDropEvent, DetachedWindow<R>) -> bool + Send>;
224236

225-
#[derive(Deserialize)]
237+
#[derive(Debug, Deserialize)]
226238
pub struct InvokePayload {
227239
#[serde(rename = "__tauriModule")]
228240
pub tauri_module: Option<String>,

core/tauri-runtime/src/window.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub enum WindowEvent {
4848
}
4949

5050
/// A menu event.
51-
#[derive(Serialize)]
51+
#[derive(Debug, Serialize)]
5252
#[serde(rename_all = "camelCase")]
5353
pub struct MenuEvent {
5454
pub menu_item_id: u16,
@@ -110,6 +110,7 @@ impl<R: Runtime> PendingWindow<R> {
110110
}
111111

112112
/// A webview window that is not yet managed by Tauri.
113+
#[derive(Debug)]
113114
pub struct DetachedWindow<R: Runtime> {
114115
/// Name of the window
115116
pub label: String,

core/tauri-utils/src/assets.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ pub trait Assets: Send + Sync + 'static {
8080
}
8181

8282
/// [`Assets`] implementation that only contains compile-time compressed and embedded assets.
83+
#[derive(Debug)]
8384
pub struct EmbeddedAssets(phf::Map<&'static str, &'static [u8]>);
8485

8586
impl EmbeddedAssets {

core/tauri/src/api/dialog.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::path::{Path, PathBuf};
88
/// The file dialog builder.
99
/// Constructs file picker dialogs that can select single/multiple files or directories.
1010
#[cfg(any(dialog_open, dialog_save))]
11-
#[derive(Default)]
11+
#[derive(Debug, Default)]
1212
pub struct FileDialogBuilder(rfd::FileDialog);
1313

1414
#[cfg(any(dialog_open, dialog_save))]

0 commit comments

Comments
 (0)