fix(core): clippy warnings, closes #1490 (#1595)

* fix(core): clippy warnings, closes #1490

* fix: cli.rs build
This commit is contained in:
Lucas Fernandes Nogueira 2021-04-23 01:40:43 -03:00 committed by GitHub
parent 07f5cd21be
commit 82ef8f6a92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 143 additions and 158 deletions

View File

@ -54,7 +54,7 @@ cfg_aliases = "0.1.1"
[dev-dependencies]
proptest = "1.0.0"
serde_json = "1.0"
tauri = { path = ".", features = [ "api-all" ] }
tauri = { path = "." }
serde = { version = "1.0", features = [ "derive" ] }
quickcheck = "1.0.3"
quickcheck_macros = "1.0.0"

View File

@ -10,6 +10,7 @@ use serde::Deserialize;
use std::path::PathBuf;
#[allow(dead_code)]
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DialogFilter {
@ -69,18 +70,16 @@ pub enum Cmd {
impl Cmd {
pub fn run(self) -> crate::Result<InvokeResponse> {
match self {
Self::OpenDialog { options } => {
#[cfg(dialog_open)]
return open(options);
#[cfg(not(dialog_open))]
return Err(crate::Error::ApiNotAllowlisted("dialog > open".to_string()));
}
Self::SaveDialog { options } => {
#[cfg(dialog_save)]
return save(options);
#[cfg(not(dialog_save))]
return Err(crate::Error::ApiNotAllowlisted("dialog > save".to_string()));
}
#[cfg(dialog_open)]
Self::OpenDialog { options } => open(options),
#[cfg(not(dialog_open))]
Self::OpenDialog { .. } => Err(crate::Error::ApiNotAllowlisted("dialog > open".to_string())),
#[cfg(dialog_save)]
Self::SaveDialog { options } => save(options),
#[cfg(not(dialog_save))]
Self::SaveDialog { .. } => Err(crate::Error::ApiNotAllowlisted("dialog > save".to_string())),
Self::MessageDialog { message } => {
let exe = std::env::current_exe()?;
let app_name = exe

View File

@ -98,104 +98,94 @@ pub enum Cmd {
impl Cmd {
pub fn run(self) -> crate::Result<InvokeResponse> {
match self {
Self::ReadTextFile { path, options } => {
#[cfg(fs_read_text_file)]
return read_text_file(path, options).map(Into::into);
#[cfg(not(fs_read_text_file))]
Err(crate::Error::ApiNotAllowlisted(
"fs > readTextFile".to_string(),
))
}
Self::ReadBinaryFile { path, options } => {
#[cfg(fs_read_binary_file)]
return read_binary_file(path, options).map(Into::into);
#[cfg(not(fs_read_binary_file))]
Err(crate::Error::ApiNotAllowlisted(
"readBinaryFile".to_string(),
))
}
#[cfg(fs_read_text_file)]
Self::ReadTextFile { path, options } => read_text_file(path, options).map(Into::into),
#[cfg(not(fs_read_text_file))]
Self::ReadTextFile { .. } => Err(crate::Error::ApiNotAllowlisted(
"fs > readTextFile".to_string(),
)),
#[cfg(fs_read_binary_file)]
Self::ReadBinaryFile { path, options } => read_binary_file(path, options).map(Into::into),
#[cfg(not(fs_read_binary_file))]
Self::ReadBinaryFile { .. } => Err(crate::Error::ApiNotAllowlisted(
"readBinaryFile".to_string(),
)),
#[cfg(fs_write_file)]
Self::WriteFile {
path,
contents,
options,
} => {
#[cfg(fs_write_file)]
return write_file(path, contents, options).map(Into::into);
#[cfg(not(fs_write_file))]
Err(crate::Error::ApiNotAllowlisted(
"fs > writeFile".to_string(),
))
}
} => write_file(path, contents, options).map(Into::into),
#[cfg(not(fs_write_file))]
Self::WriteFile { .. } => Err(crate::Error::ApiNotAllowlisted(
"fs > writeFile".to_string(),
)),
#[cfg(fs_write_binary_file)]
Self::WriteBinaryFile {
path,
contents,
options,
} => {
#[cfg(fs_write_binary_file)]
return write_binary_file(path, contents, options).map(Into::into);
#[cfg(not(fs_write_binary_file))]
Err(crate::Error::ApiNotAllowlisted(
"writeBinaryFile".to_string(),
))
}
Self::ReadDir { path, options } => {
#[cfg(fs_read_dir)]
return read_dir(path, options).map(Into::into);
#[cfg(not(fs_read_dir))]
Err(crate::Error::ApiNotAllowlisted("fs > readDir".to_string()))
}
} => write_binary_file(path, contents, options).map(Into::into),
#[cfg(not(fs_write_binary_file))]
Self::WriteBinaryFile { .. } => Err(crate::Error::ApiNotAllowlisted(
"writeBinaryFile".to_string(),
)),
#[cfg(fs_read_dir)]
Self::ReadDir { path, options } => read_dir(path, options).map(Into::into),
#[cfg(not(fs_read_dir))]
Self::ReadDir { .. } => Err(crate::Error::ApiNotAllowlisted("fs > readDir".to_string())),
#[cfg(fs_copy_file)]
Self::CopyFile {
source,
destination,
options,
} => {
#[cfg(fs_copy_file)]
return copy_file(source, destination, options).map(Into::into);
#[cfg(not(fs_copy_file))]
Err(crate::Error::ApiNotAllowlisted("fs > copyFile".to_string()))
}
Self::CreateDir { path, options } => {
#[cfg(fs_create_dir)]
return create_dir(path, options).map(Into::into);
#[cfg(not(fs_create_dir))]
Err(crate::Error::ApiNotAllowlisted(
"fs > createDir".to_string(),
))
}
Self::RemoveDir { path, options } => {
#[cfg(fs_remove_dir)]
return remove_dir(path, options).map(Into::into);
#[cfg(not(fs_remove_dir))]
Err(crate::Error::ApiNotAllowlisted(
"fs > removeDir".to_string(),
))
}
Self::RemoveFile { path, options } => {
#[cfg(fs_remove_file)]
return remove_file(path, options).map(Into::into);
#[cfg(not(fs_remove_file))]
Err(crate::Error::ApiNotAllowlisted(
"fs > removeFile".to_string(),
))
}
} => copy_file(source, destination, options).map(Into::into),
#[cfg(not(fs_copy_file))]
Self::CopyFile { .. } => Err(crate::Error::ApiNotAllowlisted("fs > copyFile".to_string())),
#[cfg(fs_create_dir)]
Self::CreateDir { path, options } => create_dir(path, options).map(Into::into),
#[cfg(not(fs_create_dir))]
Self::CreateDir { .. } => Err(crate::Error::ApiNotAllowlisted(
"fs > createDir".to_string(),
)),
#[cfg(fs_remove_dir)]
Self::RemoveDir { path, options } => remove_dir(path, options).map(Into::into),
#[cfg(not(fs_remove_dir))]
Self::RemoveDir { .. } => Err(crate::Error::ApiNotAllowlisted(
"fs > removeDir".to_string(),
)),
#[cfg(fs_remove_file)]
Self::RemoveFile { path, options } => remove_file(path, options).map(Into::into),
#[cfg(not(fs_remove_file))]
Self::RemoveFile { .. } => Err(crate::Error::ApiNotAllowlisted(
"fs > removeFile".to_string(),
)),
#[cfg(fs_rename_file)]
Self::RenameFile {
old_path,
new_path,
options,
} => {
#[cfg(fs_rename_file)]
return rename_file(old_path, new_path, options).map(Into::into);
#[cfg(not(fs_rename_file))]
Err(crate::Error::ApiNotAllowlisted(
"fs > renameFile".to_string(),
))
}
} => rename_file(old_path, new_path, options).map(Into::into),
#[cfg(not(fs_rename_file))]
Self::RenameFile { .. } => Err(crate::Error::ApiNotAllowlisted(
"fs > renameFile".to_string(),
)),
#[cfg(fs_path)]
Self::ResolvePath { path, directory } => {
#[cfg(fs_path)]
return resolve_path_handler(path, directory).map(Into::into);
#[cfg(not(fs_path))]
Err(crate::Error::ApiNotAllowlisted("fs > pathApi".to_string()))
resolve_path_handler(path, directory).map(Into::into)
}
#[cfg(not(fs_path))]
Self::ResolvePath { .. } => Err(crate::Error::ApiNotAllowlisted("fs > pathApi".to_string())),
}
}
}

View File

@ -3,19 +3,18 @@
// SPDX-License-Identifier: MIT
use super::InvokeResponse;
use crate::{runtime::Dispatch, Params, Window};
use once_cell::sync::Lazy;
use crate::{Params, Window};
use serde::Deserialize;
use std::sync::{Arc, Mutex};
#[cfg(global_shortcut_all)]
use crate::api::shortcuts::ShortcutManager;
use crate::{api::shortcuts::ShortcutManager, runtime::Dispatch};
#[cfg(global_shortcut_all)]
type ShortcutManagerHandle = Arc<Mutex<ShortcutManager>>;
type ShortcutManagerHandle = std::sync::Arc<std::sync::Mutex<ShortcutManager>>;
#[cfg(global_shortcut_all)]
pub fn manager_handle() -> &'static ShortcutManagerHandle {
use once_cell::sync::Lazy;
static MANAGER: Lazy<ShortcutManagerHandle> = Lazy::new(Default::default);
&MANAGER
}

View File

@ -4,7 +4,7 @@
use super::InvokeResponse;
use crate::api::http::{Client, ClientBuilder, HttpRequestBuilder, ResponseData};
use crate::api::http::{Client, ClientBuilder, HttpRequestBuilder};
use once_cell::sync::Lazy;
use serde::Deserialize;
@ -51,14 +51,14 @@ impl Cmd {
store.remove(&client);
Ok(().into())
}
#[cfg(http_request)]
Self::HttpRequest { client, options } => {
#[cfg(http_request)]
return make_request(client, *options).await.map(Into::into);
#[cfg(not(http_request))]
Err(crate::Error::ApiNotAllowlisted(
"http > request".to_string(),
))
}
#[cfg(not(http_request))]
Self::HttpRequest { .. } => Err(crate::Error::ApiNotAllowlisted(
"http > request".to_string(),
)),
}
}
}
@ -68,7 +68,7 @@ impl Cmd {
pub async fn make_request(
client_id: ClientId,
options: HttpRequestBuilder,
) -> crate::Result<ResponseData> {
) -> crate::Result<crate::api::http::ResponseData> {
let client = clients()
.lock()
.unwrap()

View File

@ -32,14 +32,13 @@ pub enum Cmd {
}
impl Cmd {
#[allow(unused_variables)]
pub fn run(self, identifier: String) -> crate::Result<InvokeResponse> {
match self {
Self::Notification { options } => {
#[cfg(notification_all)]
return send(options, identifier).map(Into::into);
#[cfg(not(notification_all))]
Err(crate::Error::ApiNotAllowlisted("notification".to_string()))
}
#[cfg(notification_all)]
Self::Notification { options } => send(options, identifier).map(Into::into),
#[cfg(not(notification_all))]
Self::Notification { .. } => Err(crate::Error::ApiNotAllowlisted("notification".to_string())),
Self::IsNotificationPermissionGranted => {
#[cfg(notification_all)]
return is_permission_granted().map(Into::into);

View File

@ -2,25 +2,22 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use crate::{
api::{
command::{Command, CommandChild, CommandEvent},
rpc::format_callback,
},
endpoints::InvokeResponse,
Params, Window,
};
use once_cell::sync::Lazy;
use crate::{endpoints::InvokeResponse, Params, Window};
use serde::Deserialize;
#[cfg(shell_execute)]
use std::{
collections::HashMap,
sync::{Arc, Mutex},
};
type ChildId = u32;
type ChildStore = Arc<Mutex<HashMap<ChildId, CommandChild>>>;
#[cfg(shell_execute)]
type ChildStore = Arc<Mutex<HashMap<ChildId, crate::api::command::CommandChild>>>;
#[cfg(shell_execute)]
fn command_childs() -> &'static ChildStore {
use once_cell::sync::Lazy;
static STORE: Lazy<ChildStore> = Lazy::new(Default::default);
&STORE
}
@ -59,6 +56,7 @@ pub enum Cmd {
}
impl Cmd {
#[allow(unused_variables)]
pub fn run<M: Params>(self, window: Window<M>) -> crate::Result<InvokeResponse> {
match self {
Self::Execute {
@ -70,9 +68,9 @@ impl Cmd {
#[cfg(shell_execute)]
{
let mut command = if sidecar {
Command::new_sidecar(program)?
crate::api::command::Command::new_sidecar(program)?
} else {
Command::new(program)
crate::api::command::Command::new(program)
};
command = command.args(args);
let (mut rx, child) = command.spawn()?;
@ -82,10 +80,10 @@ impl Cmd {
crate::async_runtime::spawn(async move {
while let Some(event) = rx.recv().await {
if matches!(event, CommandEvent::Terminated(_)) {
if matches!(event, crate::api::command::CommandEvent::Terminated(_)) {
command_childs().lock().unwrap().remove(&pid);
}
let js = format_callback(on_event_fn.clone(), &event)
let js = crate::api::rpc::format_callback(on_event_fn.clone(), &event)
.expect("unable to serialize CommandEvent");
let _ = window.eval(js.as_str());

View File

@ -2,10 +2,9 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use crate::{
api::config::WindowConfig, endpoints::InvokeResponse, runtime::window::PendingWindow, Manager,
Params, Window,
};
#[cfg(window_create)]
use crate::Manager;
use crate::{api::config::WindowConfig, endpoints::InvokeResponse, Params, Window};
use serde::Deserialize;
use crate::Icon;
@ -99,35 +98,38 @@ struct WindowCreatedEvent {
}
impl Cmd {
pub async fn run<M: Params>(self, mut window: Window<M>) -> crate::Result<InvokeResponse> {
#[allow(dead_code)]
pub async fn run<M: Params>(self, window: Window<M>) -> crate::Result<InvokeResponse> {
if cfg!(not(window_all)) {
Err(crate::Error::ApiNotAllowlisted("window > all".to_string()))
} else {
match self {
Self::CreateWebview { options } => {
#[cfg(not(window_create))]
#[cfg(not(window_create))]
Self::CreateWebview { .. } => {
return Err(crate::Error::ApiNotAllowlisted(
"window > create".to_string(),
));
#[cfg(window_create)]
{
// Panic if the user's `Tag` type decided to return an error while parsing.
let label: M::Label = options.label.parse().unwrap_or_else(|_| {
panic!(
"Window module received unknown window label: {}",
options.label
)
});
}
#[cfg(window_create)]
Self::CreateWebview { options } => {
let mut window = window;
// Panic if the user's `Tag` type decided to return an error while parsing.
let label: M::Label = options.label.parse().unwrap_or_else(|_| {
panic!(
"Window module received unknown window label: {}",
options.label
)
});
let url = options.url.clone();
let pending = PendingWindow::with_config(options, label.clone(), url);
window.create_window(pending)?.emit_others_internal(
"tauri://window-created".to_string(),
Some(WindowCreatedEvent {
label: label.to_string(),
}),
)?;
}
let url = options.url.clone();
let pending =
crate::runtime::window::PendingWindow::with_config(options, label.clone(), url);
window.create_window(pending)?.emit_others_internal(
"tauri://window-created".to_string(),
Some(WindowCreatedEvent {
label: label.to_string(),
}),
)?;
}
Self::SetResizable { resizable } => window.set_resizable(resizable)?,
Self::SetTitle { title } => window.set_title(&title)?,

View File

@ -221,6 +221,7 @@ pub(crate) mod export {
self.emit_internal(event.clone(), payload)
}
#[allow(dead_code)]
pub(crate) fn emit_others_internal<S: Serialize + Clone>(
&self,
event: String,

View File

@ -27,6 +27,7 @@ fn get_settings_path() -> crate::api::Result<PathBuf> {
}
/// Write the settings to the file system.
#[allow(dead_code)]
pub(crate) fn write_settings(settings: Settings) -> crate::Result<()> {
let settings_path = get_settings_path()?;
let settings_folder = Path::new(&settings_path).parent().unwrap();

View File

@ -510,13 +510,9 @@ pub(crate) fn listener<M: Params>(
// Linux we replace the AppImage by launching a new install, it start a new AppImage instance, so we're closing the previous. (the process stop here)
let update_result = updater.clone().download_and_install(pubkey.clone()).await;
if update_result.is_err() {
if let Err(err) = update_result {
// emit {"status": "ERROR", "error": "The error message"}
send_status_update(
window.clone(),
EVENT_STATUS_ERROR,
Some(update_result.err().unwrap().to_string()),
);
send_status_update(window.clone(), EVENT_STATUS_ERROR, Some(err.to_string()));
} else {
// emit {"status": "DONE"}
send_status_update(window.clone(), EVENT_STATUS_SUCCESS, None);

View File

@ -830,11 +830,11 @@ fn generate_resource_data(settings: &Settings) -> crate::Result<ResourceMap> {
.directories
.iter()
.position(|f| f.name == directory_name);
if index.is_some() {
if let Some(index) = index {
// the directory entry is already a part of the chain
let dir = directory_entry
.directories
.get_mut(index.expect("Unable to get index"))
.get_mut(index)
.expect("Unable to get directory");
dir.add_file(resource_entry.clone());
} else {