Skip to content

Commit

Permalink
feat: add #[non_exhaustive] attribute (#1725)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored May 6, 2021
1 parent 6bfac86 commit e087f0f
Show file tree
Hide file tree
Showing 30 changed files with 58 additions and 62 deletions.
8 changes: 8 additions & 0 deletions .changes/non-exhaustive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"tauri": patch
"tauri-bundler": patch
"tauri-codegen": patch
"tauri-utils": patch
---

Added the `#[non_exhaustive] attribute where appropriate.
5 changes: 5 additions & 0 deletions .changes/remove-api-modules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Removed the `tcp` module from `tauri::api`.
5 changes: 5 additions & 0 deletions .changes/setup-error-send.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch
---

The `setup` Error type must be `Send`.
1 change: 1 addition & 0 deletions core/tauri-codegen/src/embedded_assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Asset = (AssetKey, (PathBuf, PathBuf));

/// All possible errors while reading and compressing an [`EmbeddedAssets`] directory
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum EmbeddedAssetsError {
#[error("failed to read asset at {path} because {error}")]
AssetRead {
Expand Down
1 change: 1 addition & 0 deletions core/tauri-codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub mod embedded_assets;

/// Represents all the errors that can happen while reading the config.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ConfigError {
#[error("unable to access current working directory: {0}")]
CurrentDir(std::io::Error),
Expand Down
1 change: 0 additions & 1 deletion core/tauri-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ edition = "2018"
[dependencies]
serde = { version = "1.0", features = [ "derive" ] }
serde_json = "1.0"
sysinfo = "0.17"
thiserror = "1.0.24"
phf = { version = "0.8", features = [ "macros" ] }
zstd = "0.8"
Expand Down
1 change: 1 addition & 0 deletions core/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use url::Url;
/// The window webview URL options.
#[derive(PartialEq, Debug, Clone, Deserialize)]
#[serde(untagged)]
#[non_exhaustive]
pub enum WindowUrl {
/// An external URL.
External(Url),
Expand Down
3 changes: 1 addition & 2 deletions core/tauri-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ pub mod config;
pub mod html;
/// Platform helpers
pub mod platform;
/// Process helpers
pub mod process;

/// Result type alias using the crate's error type.
pub type Result<T> = std::result::Result<T, Error>;

/// The error types.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
/// Target triple architecture error
#[error("Unable to determine target-architecture")]
Expand Down
18 changes: 0 additions & 18 deletions core/tauri-utils/src/process.rs

This file was deleted.

3 changes: 3 additions & 0 deletions core/tauri/src/api/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod macros;

/// The resolution of a arg match.
#[derive(Default, Debug, Serialize)]
#[non_exhaustive]
pub struct ArgData {
/// The value of the arg.
/// - Value::Bool if it's a flag,
Expand All @@ -30,6 +31,7 @@ pub struct ArgData {

/// The matched subcommand.
#[derive(Default, Debug, Serialize)]
#[non_exhaustive]
pub struct SubcommandMatches {
/// The subcommand name.
pub name: String,
Expand All @@ -39,6 +41,7 @@ pub struct SubcommandMatches {

/// The arg matches of a command.
#[derive(Default, Debug, Serialize)]
#[non_exhaustive]
pub struct Matches {
/// Data structure mapping each found arg with its resolution.
pub args: HashMap<String, ArgData>,
Expand Down
1 change: 1 addition & 0 deletions core/tauri/src/api/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use tempfile::{self, tempdir};
/// A DiskEntry is either a file or a directory.
/// The `children` Vec is always `Some` if the entry is a directory.
#[derive(Debug, Serialize)]
#[non_exhaustive]
pub struct DiskEntry {
/// The path to this entry.
pub path: PathBuf,
Expand Down
1 change: 1 addition & 0 deletions core/tauri/src/api/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

/// The error types.
#[derive(thiserror::Error, Debug)]
#[non_exhaustive]
pub enum Error {
/// Command error.
#[error("Command Error: {0}")]
Expand Down
2 changes: 2 additions & 0 deletions core/tauri/src/api/file/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{fs, io, path};

/// The supported archive formats.
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub enum ArchiveFormat {
/// Tar archive.
Tar(Option<Compression>),
Expand All @@ -19,6 +20,7 @@ pub enum ArchiveFormat {

/// The supported compression types.
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub enum Compression {
/// Gz compression (e.g. `.tar.gz` archives)
Gz,
Expand Down
3 changes: 3 additions & 0 deletions core/tauri/src/api/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ impl Client {

#[derive(Serialize_repr, Deserialize_repr, Clone, Debug)]
#[repr(u16)]
#[non_exhaustive]
/// The request's response type
pub enum ResponseType {
/// Read the response as JSON
Expand All @@ -127,6 +128,7 @@ pub enum ResponseType {
/// FormBody data types.
#[derive(Deserialize)]
#[serde(untagged)]
#[non_exhaustive]
pub enum FormPart {
/// A file path value.
File(PathBuf),
Expand All @@ -150,6 +152,7 @@ impl FormBody {
/// A body for the request.
#[derive(Deserialize)]
#[serde(tag = "type", content = "payload")]
#[non_exhaustive]
pub enum Body {
/// A multipart formdata body.
Form(FormBody),
Expand Down
2 changes: 0 additions & 2 deletions core/tauri/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ pub mod process;
pub mod rpc;
/// The shell api.
pub mod shell;
/// TCP ports access API.
pub mod tcp;
/// The semver API.
pub mod version;

Expand Down
1 change: 1 addition & 0 deletions core/tauri/src/api/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use serde_repr::{Deserialize_repr, Serialize_repr};
/// For more information, check the [dirs_next documentation](https://docs.rs/dirs_next/).
#[derive(Serialize_repr, Deserialize_repr, Clone, Debug)]
#[repr(u16)]
#[non_exhaustive]
pub enum BaseDirectory {
/// The Audio directory.
Audio = 1,
Expand Down
1 change: 1 addition & 0 deletions core/tauri/src/api/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct TerminatedPayload {
/// A event sent to the command callback.
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "event", content = "payload")]
#[non_exhaustive]
pub enum CommandEvent {
/// Stderr line.
Stderr(String),
Expand Down
26 changes: 0 additions & 26 deletions core/tauri/src/api/tcp.rs

This file was deleted.

7 changes: 4 additions & 3 deletions core/tauri/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use std::path::PathBuf;

/// Runtime errors that can happen inside a Tauri application.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
/// Failed to create webview.
#[error("failed to create webview: {0}")]
CreateWebview(String),
CreateWebview(Box<dyn std::error::Error + Send>),
/// Failed to create window.
#[error("failed to create window")]
CreateWindow,
Expand Down Expand Up @@ -39,7 +40,7 @@ pub enum Error {
Base64Decode(#[from] base64::DecodeError),
/// Failed to load window icon.
#[error("invalid icon: {0}")]
InvalidIcon(String),
InvalidIcon(Box<dyn std::error::Error + Send>),
/// Client with specified ID not found.
#[error("http client dropped or not initialized")]
HttpClientNotInitialized,
Expand All @@ -54,7 +55,7 @@ pub enum Error {
InvalidArgs(&'static str, serde_json::Error),
/// Encountered an error in the setup hook,
#[error("error encountered during setup hook: {0}")]
Setup(String),
Setup(Box<dyn std::error::Error + Send>),
/// Tauri updater error.
#[cfg(feature = "updater")]
#[error("Updater: {0}")]
Expand Down
3 changes: 2 additions & 1 deletion core/tauri/src/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use serde_json::Value as JsonValue;
use std::{future::Future, sync::Arc};

/// A closure that is run when the Tauri application is setting up.
pub type SetupHook<P> = Box<dyn Fn(&mut App<P>) -> Result<(), Box<dyn std::error::Error>> + Send>;
pub type SetupHook<P> =
Box<dyn Fn(&mut App<P>) -> Result<(), Box<dyn std::error::Error + Send>> + Send>;

/// A closure that is run everytime Tauri receives a message it doesn't explicitly handle.
pub type InvokeHandler<P> = dyn Fn(Invoke<P>) + Send + Sync + 'static;
Expand Down
1 change: 1 addition & 0 deletions core/tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ macro_rules! tauri_build_context {

/// A icon definition.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum Icon {
/// Icon from file path.
File(PathBuf),
Expand Down
6 changes: 4 additions & 2 deletions core/tauri/src/runtime/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ where
/// Defines the setup hook.
pub fn setup<F>(mut self, setup: F) -> Self
where
F: Fn(&mut App<Args<E, L, A, R>>) -> Result<(), Box<dyn std::error::Error>> + Send + 'static,
F: Fn(&mut App<Args<E, L, A, R>>) -> Result<(), Box<dyn std::error::Error + Send>>
+ Send
+ 'static,
{
self.setup = Box::new(setup);
self
Expand Down Expand Up @@ -340,7 +342,7 @@ where
#[cfg(feature = "updater")]
app.run_updater(main_window);

(self.setup)(&mut app).map_err(|e| crate::Error::Setup(e.to_string()))?;
(self.setup)(&mut app).map_err(|e| crate::Error::Setup(e))?;

app.runtime.run();
Ok(())
Expand Down
12 changes: 5 additions & 7 deletions core/tauri/src/runtime/flavors/wry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,9 @@ impl TryFrom<Icon> for WryIcon {
type Error = crate::Error;
fn try_from(icon: Icon) -> Result<Self, Self::Error> {
let image = match icon {
Icon::File(path) => {
image::open(path).map_err(|e| crate::Error::InvalidIcon(e.to_string()))?
}
Icon::File(path) => image::open(path).map_err(|e| crate::Error::InvalidIcon(Box::new(e)))?,
Icon::Raw(raw) => {
image::load_from_memory(&raw).map_err(|e| crate::Error::InvalidIcon(e.to_string()))?
image::load_from_memory(&raw).map_err(|e| crate::Error::InvalidIcon(Box::new(e)))?
}
};
let (width, height) = image.dimensions();
Expand All @@ -83,7 +81,7 @@ impl TryFrom<Icon> for WryIcon {
rgba.extend_from_slice(&pixel.to_rgba().0);
}
let icon = WindowIcon::from_rgba(rgba, width, height)
.map_err(|e| crate::Error::InvalidIcon(e.to_string()))?;
.map_err(|e| crate::Error::InvalidIcon(Box::new(e)))?;
Ok(Self(icon))
}
}
Expand Down Expand Up @@ -813,7 +811,7 @@ fn create_webview<M: Params<Runtime = Wry>>(

let window = window_attributes.build(event_loop).unwrap();
let mut webview_builder = WebViewBuilder::new(window)
.map_err(|e| crate::Error::CreateWebview(e.to_string()))?
.map_err(|e| crate::Error::CreateWebview(Box::new(e)))?
.with_url(&url)
.unwrap(); // safe to unwrap because we validate the URL beforehand
if let Some(handler) = rpc_handler {
Expand Down Expand Up @@ -842,7 +840,7 @@ fn create_webview<M: Params<Runtime = Wry>>(

webview_builder
.build()
.map_err(|e| crate::Error::CreateWebview(e.to_string()))
.map_err(|e| crate::Error::CreateWebview(Box::new(e)))
}

/// Create a wry rpc handler from a tauri rpc handler.
Expand Down
1 change: 1 addition & 0 deletions core/tauri/src/runtime/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ impl<P: Params> WindowManager<P> {
)
}
WindowUrl::External(url) => (url.as_str().starts_with("tauri://"), url.to_string()),
_ => unimplemented!(),
};

if is_local {
Expand Down
2 changes: 2 additions & 0 deletions core/tauri/src/runtime/webview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ pub trait WindowBuilder: WindowBuilderBase {
}

/// Rpc request.
#[non_exhaustive]
pub struct RpcRequest {
/// RPC command.
pub command: String,
Expand All @@ -152,6 +153,7 @@ pub struct CustomProtocol {

/// The file drop event payload.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum FileDropEvent {
/// The file(s) have been dragged onto the window, but have not been dropped yet.
Hovered(Vec<PathBuf>),
Expand Down
1 change: 1 addition & 0 deletions core/tauri/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::{

/// Tauri Settings.
#[derive(Default, Deserialize, Serialize)]
#[non_exhaustive]
pub struct Settings {
/// Whether the user allows notifications or not.
#[cfg(notification_all)]
Expand Down
1 change: 1 addition & 0 deletions core/tauri/src/updater/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use thiserror::Error;

/// All errors that can occur while running the updater.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum Error {
/// IO Errors.
#[error("`{0}`")]
Expand Down
1 change: 1 addition & 0 deletions tooling/bundler/src/bundle/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const MACOS_APP_CATEGORY_PREFIX: &str = "public.app-category.";
/// Corresponds to `LSApplicationCategoryType` on macOS and the GNOME desktop categories on Debian.
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum AppCategory {
Business,
DeveloperTool,
Expand Down
1 change: 1 addition & 0 deletions tooling/bundler/src/bundle/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::{

/// The type of the package we're bundling.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum PackageType {
/// The macOS application bundle (.app).
MacOsBundle,
Expand Down
1 change: 1 addition & 0 deletions tooling/bundler/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use thiserror::Error as DeriveError;

/// Errors returned by the bundler.
#[derive(Debug, DeriveError)]
#[non_exhaustive]
pub enum Error {
/// Bundler error.
#[error("{0}")]
Expand Down

0 comments on commit e087f0f

Please sign in to comment.