Skip to content

Commit

Permalink
feat(core): implement Debug on public API structs/enums, closes #2292
Browse files Browse the repository at this point in the history
… (#2387)
  • Loading branch information
lucasfernog committed Aug 11, 2021
1 parent 50bf87a commit fa9341b
Show file tree
Hide file tree
Showing 24 changed files with 133 additions and 32 deletions.
9 changes: 9 additions & 0 deletions .changes/implement-debug.md
@@ -0,0 +1,9 @@
---
"tauri": patch
"tauri-build": patch
"tauri-utils": patch
"tauri-runtime": patch
"tauri-runtime-wry": patch
---

Implement `Debug` on public API structs and enums.
3 changes: 2 additions & 1 deletion core/tauri-build/src/lib.rs
Expand Up @@ -16,6 +16,7 @@ pub use codegen::context::CodegenContext;

/// Attributes used on Windows.
#[allow(dead_code)]
#[derive(Debug)]
pub struct WindowsAttributes {
window_icon_path: PathBuf,
}
Expand Down Expand Up @@ -43,7 +44,7 @@ impl WindowsAttributes {
}

/// The attributes used on the build.
#[derive(Default)]
#[derive(Debug, Default)]
pub struct Attributes {
#[allow(dead_code)]
windows_attributes: WindowsAttributes,
Expand Down
34 changes: 27 additions & 7 deletions core/tauri-runtime-wry/src/lib.rs
Expand Up @@ -69,6 +69,7 @@ use std::{
HashMap,
},
convert::TryFrom,
fmt,
fs::read,
path::PathBuf,
sync::{
Expand Down Expand Up @@ -126,7 +127,7 @@ macro_rules! getter {
}};
}

#[derive(Clone)]
#[derive(Debug, Clone)]
struct EventLoopContext {
main_thread_id: ThreadId,
is_event_loop_running: Arc<AtomicBool>,
Expand All @@ -146,6 +147,15 @@ pub struct GlobalShortcutManagerHandle {
listeners: GlobalShortcutListeners,
}

impl fmt::Debug for GlobalShortcutManagerHandle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("GlobalShortcutManagerHandle")
.field("context", &self.context)
.field("shortcuts", &self.shortcuts)
.finish()
}
}

impl GlobalShortcutManager for GlobalShortcutManagerHandle {
fn is_registered(&self, accelerator: &str) -> Result<bool> {
let (tx, rx) = channel();
Expand Down Expand Up @@ -205,7 +215,7 @@ impl GlobalShortcutManager for GlobalShortcutManagerHandle {
}
}

#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct ClipboardManagerWrapper {
context: EventLoopContext,
}
Expand Down Expand Up @@ -728,23 +738,23 @@ pub enum WebviewEvent {
}

#[cfg(feature = "system-tray")]
#[derive(Clone)]
#[derive(Debug, Clone)]
pub enum TrayMessage {
UpdateItem(u16, menu::MenuUpdate),
UpdateIcon(Icon),
#[cfg(target_os = "macos")]
UpdateIconAsTemplate(bool),
}

#[derive(Clone)]
#[derive(Debug, Clone)]
pub enum GlobalShortcutMessage {
IsRegistered(Accelerator, Sender<bool>),
Register(Accelerator, Sender<Result<GlobalShortcutWrapper>>),
Unregister(GlobalShortcutWrapper, Sender<Result<()>>),
UnregisterAll(Sender<Result<()>>),
}

#[derive(Clone)]
#[derive(Debug, Clone)]
pub enum ClipboardMessage {
WriteText(String, Sender<()>),
ReadText(Sender<Option<String>>),
Expand Down Expand Up @@ -780,8 +790,18 @@ struct DispatcherContext {
menu_event_listeners: MenuEventListeners,
}

impl fmt::Debug for DispatcherContext {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DispatcherContext")
.field("main_thread_id", &self.main_thread_id)
.field("is_event_loop_running", &self.is_event_loop_running)
.field("proxy", &self.proxy)
.finish()
}
}

/// The Tauri [`Dispatch`] for [`Wry`].
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct WryDispatcher {
window_id: WindowId,
context: DispatcherContext,
Expand Down Expand Up @@ -1261,7 +1281,7 @@ pub struct Wry {
}

/// A handle to the Wry runtime.
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct WryHandle {
dispatcher_context: DispatcherContext,
}
Expand Down
3 changes: 2 additions & 1 deletion core/tauri-runtime-wry/src/menu.rs
Expand Up @@ -56,7 +56,7 @@ pub type SystemTrayEventListeners = Arc<Mutex<HashMap<Uuid, SystemTrayEventHandl
pub type SystemTrayItems = Arc<Mutex<HashMap<u16, WryCustomMenuItem>>>;

#[cfg(feature = "system-tray")]
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct SystemTrayHandle {
pub(crate) proxy: EventLoopProxy<super::Message>,
}
Expand Down Expand Up @@ -87,6 +87,7 @@ impl TrayHandle for SystemTrayHandle {
}

#[cfg(target_os = "macos")]
#[derive(Debug)]
pub struct NativeImageWrapper(pub WryNativeImage);

#[cfg(target_os = "macos")]
Expand Down
10 changes: 6 additions & 4 deletions core/tauri-runtime/src/lib.rs
Expand Up @@ -31,6 +31,7 @@ use window::{

#[cfg(feature = "system-tray")]
#[non_exhaustive]
#[derive(Debug)]
pub struct SystemTray {
pub icon: Option<Icon>,
pub menu: Option<menu::SystemTrayMenu>,
Expand Down Expand Up @@ -193,6 +194,7 @@ pub enum ExitRequestedEventAction {
}

/// A system tray event.
#[derive(Debug)]
pub enum SystemTrayEvent {
MenuItemClick(u16),
LeftClick {
Expand All @@ -216,7 +218,7 @@ pub struct RunIteration {
}

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

/// A global shortcut manager.
pub trait GlobalShortcutManager {
pub trait GlobalShortcutManager: Debug {
/// Whether the application has registered the given `accelerator`.
///
/// # Panics
Expand Down Expand Up @@ -269,7 +271,7 @@ pub trait GlobalShortcutManager {
}

/// Clipboard manager.
pub trait ClipboardManager {
pub trait ClipboardManager: Debug {
/// Writes the text into the clipboard as plain text.
///
/// # Panics
Expand Down Expand Up @@ -335,7 +337,7 @@ pub trait Runtime: Sized + 'static {
}

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

Expand Down
3 changes: 2 additions & 1 deletion core/tauri-runtime/src/menu.rs
Expand Up @@ -4,6 +4,7 @@

use std::{
collections::hash_map::DefaultHasher,
fmt,
hash::{Hash, Hasher},
};

Expand Down Expand Up @@ -145,7 +146,7 @@ pub enum MenuUpdate {
SetNativeImage(NativeImage),
}

pub trait TrayHandle {
pub trait TrayHandle: fmt::Debug {
fn set_icon(&self, icon: crate::Icon) -> crate::Result<()>;
fn update_item(&self, id: u16, update: MenuUpdate) -> crate::Result<()>;
#[cfg(target_os = "macos")]
Expand Down
18 changes: 15 additions & 3 deletions core/tauri-runtime/src/webview.rs
Expand Up @@ -16,7 +16,7 @@ use tauri_utils::config::{WindowConfig, WindowUrl};
#[cfg(windows)]
use winapi::shared::windef::HWND;

use std::{collections::HashMap, path::PathBuf};
use std::{collections::HashMap, fmt, path::PathBuf};

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

impl fmt::Debug for WebviewAttributes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("WebviewAttributes")
.field("url", &self.url)
.field("initialization_scripts", &self.initialization_scripts)
.field("data_directory", &self.data_directory)
.field("file_drop_handler_enabled", &self.file_drop_handler_enabled)
.finish()
}
}

impl WebviewAttributes {
/// Initializes the default attributes for a webview.
pub fn new(url: WindowUrl) -> Self {
Expand Down Expand Up @@ -93,7 +104,7 @@ impl WebviewAttributes {
/// Do **NOT** implement this trait except for use in a custom [`Runtime`](crate::Runtime).
///
/// This trait is separate from [`WindowBuilder`] to prevent "accidental" implementation.
pub trait WindowBuilderBase: Sized {}
pub trait WindowBuilderBase: fmt::Debug + Sized {}

/// A builder for all attributes related to a single webview.
///
Expand Down Expand Up @@ -189,6 +200,7 @@ pub trait WindowBuilder: WindowBuilderBase {
}

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

#[derive(Deserialize)]
#[derive(Debug, Deserialize)]
pub struct InvokePayload {
#[serde(rename = "__tauriModule")]
pub tauri_module: Option<String>,
Expand Down
3 changes: 2 additions & 1 deletion core/tauri-runtime/src/window.rs
Expand Up @@ -48,7 +48,7 @@ pub enum WindowEvent {
}

/// A menu event.
#[derive(Serialize)]
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct MenuEvent {
pub menu_item_id: u16,
Expand Down Expand Up @@ -110,6 +110,7 @@ impl<R: Runtime> PendingWindow<R> {
}

/// A webview window that is not yet managed by Tauri.
#[derive(Debug)]
pub struct DetachedWindow<R: Runtime> {
/// Name of the window
pub label: String,
Expand Down
1 change: 1 addition & 0 deletions core/tauri-utils/src/assets.rs
Expand Up @@ -80,6 +80,7 @@ pub trait Assets: Send + Sync + 'static {
}

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

impl EmbeddedAssets {
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/api/dialog.rs
Expand Up @@ -8,7 +8,7 @@ use std::path::{Path, PathBuf};
/// The file dialog builder.
/// Constructs file picker dialogs that can select single/multiple files or directories.
#[cfg(any(dialog_open, dialog_save))]
#[derive(Default)]
#[derive(Debug, Default)]
pub struct FileDialogBuilder(rfd::FileDialog);

#[cfg(any(dialog_open, dialog_save))]
Expand Down
19 changes: 11 additions & 8 deletions core/tauri/src/api/http.rs
Expand Up @@ -10,7 +10,7 @@ use serde_repr::{Deserialize_repr, Serialize_repr};
use std::{collections::HashMap, path::PathBuf, time::Duration};

/// Client builder.
#[derive(Clone, Default, Deserialize)]
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ClientBuilder {
/// Max number of redirections to follow
Expand Down Expand Up @@ -63,12 +63,12 @@ impl ClientBuilder {

/// The HTTP client.
#[cfg(feature = "reqwest-client")]
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct Client(reqwest::Client);

/// The HTTP client.
#[cfg(not(feature = "reqwest-client"))]
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct Client(ClientBuilder);

#[cfg(not(feature = "reqwest-client"))]
Expand Down Expand Up @@ -197,7 +197,7 @@ pub enum ResponseType {
}

/// FormBody data types.
#[derive(Deserialize)]
#[derive(Debug, Deserialize)]
#[serde(untagged)]
#[non_exhaustive]
pub enum FormPart {
Expand All @@ -210,7 +210,7 @@ pub enum FormPart {
}

/// Form body definition.
#[derive(Deserialize)]
#[derive(Debug, Deserialize)]
pub struct FormBody(HashMap<String, FormPart>);

impl FormBody {
Expand All @@ -221,7 +221,7 @@ impl FormBody {
}

/// A body for the request.
#[derive(Deserialize)]
#[derive(Debug, Deserialize)]
#[serde(tag = "type", content = "payload")]
#[non_exhaustive]
pub enum Body {
Expand Down Expand Up @@ -255,7 +255,7 @@ pub enum Body {
/// }
/// }
/// ```
#[derive(Deserialize)]
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HttpRequestBuilder {
/// The request method (GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS, CONNECT or TRACE)
Expand Down Expand Up @@ -321,9 +321,11 @@ impl HttpRequestBuilder {

/// The HTTP response.
#[cfg(feature = "reqwest-client")]
#[derive(Debug)]
pub struct Response(ResponseType, reqwest::Response);
/// The HTTP response.
#[cfg(not(feature = "reqwest-client"))]
#[derive(Debug)]
pub struct Response(ResponseType, attohttpc::Response, String);

impl Response {
Expand Down Expand Up @@ -375,6 +377,7 @@ impl Response {

/// A response with raw bytes.
#[non_exhaustive]
#[derive(Debug)]
pub struct RawResponse {
/// Response status code.
pub status: u16,
Expand All @@ -383,7 +386,7 @@ pub struct RawResponse {
}

/// The response type.
#[derive(Serialize)]
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ResponseData {
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/api/notification.rs
Expand Up @@ -18,7 +18,7 @@ use std::path::MAIN_SEPARATOR;
/// .show();
/// ```
#[allow(dead_code)]
#[derive(Default)]
#[derive(Debug, Default)]
pub struct Notification {
/// The notification body.
body: Option<String>,
Expand Down

0 comments on commit fa9341b

Please sign in to comment.