From 1c2cf3330ee164a9492342ab1132f8d17b15931b Mon Sep 17 00:00:00 2001 From: mrxiaozhuox Date: Tue, 25 Jan 2022 13:52:33 +0800 Subject: [PATCH] fix: use clippy fix --- src/builder.rs | 4 ++-- src/cargo.rs | 6 +++--- src/cli/build/mod.rs | 2 +- src/cli/clean/mod.rs | 2 +- src/cli/serve/mod.rs | 2 +- src/cli/translate/mod.rs | 2 +- src/server/mod.rs | 28 +++++++++++++--------------- 7 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index c281f2b80..7d76f75cc 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -187,7 +187,7 @@ pub fn build_desktop(config: &CrateConfig) -> Result<()> { .application .out_dir .clone() - .unwrap_or(PathBuf::from("dist")) + .unwrap_or_else(|| PathBuf::from("dist")) .display() ); } @@ -238,7 +238,7 @@ pub fn gen_page(config: &DioxusConfig, serve: bool) -> String { html = html.replace("{app_name}", &config.application.name); - let title = config.web.app.title.clone().unwrap_or("dioxus | ⛺".into()); + let title = config.web.app.title.clone().unwrap_or_else(|| "dioxus | ⛺".into()); html.replace("{app_title}", &title) } diff --git a/src/cargo.rs b/src/cargo.rs index 3a911a81e..c268c5a3a 100644 --- a/src/cargo.rs +++ b/src/cargo.rs @@ -1,6 +1,6 @@ //! Utilities for working with cargo and rust files use crate::error::{Error, Result}; -use std::{env, fs, path::PathBuf, process::Command, str}; +use std::{env, fs, path::{PathBuf, Path}, process::Command, str}; /// How many parent folders are searched for a `Cargo.toml` const MAX_ANCESTORS: u32 = 10; @@ -44,7 +44,7 @@ pub fn workspace_root() -> Result { } let stdout = str::from_utf8(&output.stdout).unwrap(); - for line in stdout.lines() { + if let Some(line) = stdout.lines().next() { let meta: serde_json::Value = serde_json::from_str(line) .map_err(|_| Error::CargoError("InvalidOutput".to_string()))?; @@ -58,7 +58,7 @@ pub fn workspace_root() -> Result { } /// Checks if the directory contains `Cargo.toml` -fn contains_manifest(path: &PathBuf) -> bool { +fn contains_manifest(path: &Path) -> bool { fs::read_dir(path) .map(|entries| { entries diff --git a/src/cli/build/mod.rs b/src/cli/build/mod.rs index 318f6b7f5..27015420b 100644 --- a/src/cli/build/mod.rs +++ b/src/cli/build/mod.rs @@ -39,7 +39,7 @@ impl Build { .application .out_dir .clone() - .unwrap_or(PathBuf::from("dist")), + .unwrap_or_else(|| PathBuf::from("dist")), ) .join("index.html"), )?; diff --git a/src/cli/clean/mod.rs b/src/cli/clean/mod.rs index 7ba33b530..62593b1ee 100644 --- a/src/cli/clean/mod.rs +++ b/src/cli/clean/mod.rs @@ -30,7 +30,7 @@ impl Clean { .dioxus_config .application .out_dir - .unwrap_or(PathBuf::from("dist")); + .unwrap_or_else(|| PathBuf::from("dist")); if crate_config.crate_dir.join(&out_dir).is_dir() { remove_dir_all(crate_config.crate_dir.join(&out_dir))?; } diff --git a/src/cli/serve/mod.rs b/src/cli/serve/mod.rs index 04795ab13..a84d8136d 100644 --- a/src/cli/serve/mod.rs +++ b/src/cli/serve/mod.rs @@ -62,7 +62,7 @@ impl Serve { .application .out_dir .clone() - .unwrap_or(PathBuf::from("dist")), + .unwrap_or_else(|| PathBuf::from("dist")), ) .join("index.html"), )?; diff --git a/src/cli/translate/mod.rs b/src/cli/translate/mod.rs index a62a1b4b3..81b5092ed 100644 --- a/src/cli/translate/mod.rs +++ b/src/cli/translate/mod.rs @@ -48,7 +48,7 @@ impl Translate { exit(0); }) }); - if temp.is_none() { + if let Some(..) = temp { if let Some(s) = source { contents = s; } else { diff --git a/src/server/mod.rs b/src/server/mod.rs index 3151975d5..3092a01ec 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -49,7 +49,7 @@ pub async fn startup(config: CrateConfig) -> anyhow::Result<()> { .watcher .watch_path .clone() - .unwrap_or(vec![PathBuf::from("src")]); + .unwrap_or_else(|| vec![PathBuf::from("src")]); let crate_dir = watcher_conf.crate_dir.clone(); loop { if let Ok(v) = rx.recv() { @@ -67,21 +67,19 @@ pub async fn startup(config: CrateConfig) -> anyhow::Result<()> { } } - if reload { - if let Ok(_) = builder::build(&watcher_conf) { - // change the websocket reload state to true; - // the page will auto-reload. - if watcher_conf - .dioxus_config - .web - .watcher - .reload_html - .unwrap_or(false) - { - let _ = Serve::regen_dev_page(&watcher_conf); - } - watcher_ws_state.lock().unwrap().change(); + if reload && builder::build(&watcher_conf).is_ok() { + // change the websocket reload state to true; + // the page will auto-reload. + if watcher_conf + .dioxus_config + .web + .watcher + .reload_html + .unwrap_or(false) + { + let _ = Serve::regen_dev_page(&watcher_conf); } + watcher_ws_state.lock().unwrap().change(); } } _ => {}