From 8b4e0b3e07f831bc7f0576b4c0e9a3cde304abb1 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Tue, 8 Aug 2023 15:33:48 -0700 Subject: [PATCH 1/4] Use serde default instead of options in the CLI --- packages/cli/src/builder.rs | 31 ++------- packages/cli/src/cli/build.rs | 9 +-- packages/cli/src/cli/clean.rs | 6 +- packages/cli/src/cli/serve.rs | 11 +-- packages/cli/src/config.rs | 107 ++++++++++++++++++++--------- packages/cli/src/server/mod.rs | 21 +----- packages/cli/src/server/output.rs | 20 ++---- packages/cli/src/server/web/mod.rs | 17 +---- 8 files changed, 100 insertions(+), 122 deletions(-) diff --git a/packages/cli/src/builder.rs b/packages/cli/src/builder.rs index 3fa8e7818..6dca9e779 100644 --- a/packages/cli/src/builder.rs +++ b/packages/cli/src/builder.rs @@ -130,7 +130,7 @@ pub fn build(config: &CrateConfig, quiet: bool) -> Result { } // check binaryen:wasm-opt tool - let dioxus_tools = dioxus_config.application.tools.clone().unwrap_or_default(); + let dioxus_tools = dioxus_config.application.tools.clone(); if dioxus_tools.contains_key("binaryen") { let info = dioxus_tools.get("binaryen").unwrap(); let binaryen = crate::tools::Tool::Binaryen; @@ -346,13 +346,7 @@ pub fn build_desktop(config: &CrateConfig, _is_serve: bool) -> Result String { let mut script_list = resouces.script.unwrap_or_default(); if serve { - let mut dev_style = resouces.dev.style.clone().unwrap_or_default(); - let mut dev_script = resouces.dev.script.unwrap_or_default(); + let mut dev_style = resouces.dev.style.clone(); + let mut dev_script = resouces.dev.script; style_list.append(&mut dev_style); script_list.append(&mut dev_script); } @@ -459,13 +453,7 @@ pub fn gen_page(config: &DioxusConfig, serve: bool) -> String { &style.to_str().unwrap(), )) } - if config - .application - .tools - .clone() - .unwrap_or_default() - .contains_key("tailwindcss") - { + if config.application.tools.clone().contains_key("tailwindcss") { style_str.push_str("\n"); } @@ -516,12 +504,7 @@ pub fn gen_page(config: &DioxusConfig, serve: bool) -> String { ); } - let title = config - .web - .app - .title - .clone() - .unwrap_or_else(|| "dioxus | ⛺".into()); + let title = config.web.app.title.clone(); replace_or_insert_before("{app_title}", &title, " Result> { let mut result = vec![]; let dioxus_config = &config.dioxus_config; - let dioxus_tools = dioxus_config.application.tools.clone().unwrap_or_default(); + let dioxus_tools = dioxus_config.application.tools.clone(); // check sass tool state let sass = Tool::Sass; diff --git a/packages/cli/src/cli/build.rs b/packages/cli/src/cli/build.rs index 3053a6e5a..93136aa19 100644 --- a/packages/cli/src/cli/build.rs +++ b/packages/cli/src/cli/build.rs @@ -54,14 +54,7 @@ impl Build { let mut file = std::fs::File::create( crate_config .crate_dir - .join( - crate_config - .dioxus_config - .application - .out_dir - .clone() - .unwrap_or_else(|| PathBuf::from("dist")), - ) + .join(crate_config.dioxus_config.application.out_dir.clone()) .join("index.html"), )?; file.write_all(temp.as_bytes())?; diff --git a/packages/cli/src/cli/clean.rs b/packages/cli/src/cli/clean.rs index 5fed1e1a6..09fb21469 100644 --- a/packages/cli/src/cli/clean.rs +++ b/packages/cli/src/cli/clean.rs @@ -19,11 +19,7 @@ impl Clean { return custom_error!("Cargo clean failed."); } - let out_dir = crate_config - .dioxus_config - .application - .out_dir - .unwrap_or_else(|| PathBuf::from("dist")); + let out_dir = crate_config.dioxus_config.application.out_dir; if crate_config.crate_dir.join(&out_dir).is_dir() { remove_dir_all(crate_config.crate_dir.join(&out_dir))?; } diff --git a/packages/cli/src/cli/serve.rs b/packages/cli/src/cli/serve.rs index 433d611b7..2681c4a5f 100644 --- a/packages/cli/src/cli/serve.rs +++ b/packages/cli/src/cli/serve.rs @@ -58,14 +58,9 @@ impl Serve { pub fn regen_dev_page(crate_config: &CrateConfig) -> Result<()> { let serve_html = gen_page(&crate_config.dioxus_config, true); - let dist_path = crate_config.crate_dir.join( - crate_config - .dioxus_config - .application - .out_dir - .clone() - .unwrap_or_else(|| PathBuf::from("dist")), - ); + let dist_path = crate_config + .crate_dir + .join(crate_config.dioxus_config.application.out_dir.clone()); if !dist_path.is_dir() { create_dir_all(&dist_path)?; } diff --git a/packages/cli/src/config.rs b/packages/cli/src/config.rs index 2e499451f..2fc9f96ce 100644 --- a/packages/cli/src/config.rs +++ b/packages/cli/src/config.rs @@ -86,33 +86,33 @@ fn acquire_dioxus_toml(dir: &Path) -> Option { impl Default for DioxusConfig { fn default() -> Self { - let name = "name"; + let name = default_name(); Self { application: ApplicationConfig { - name: name.into(), - default_platform: Platform::Web, - out_dir: Some(PathBuf::from("dist")), - asset_dir: Some(PathBuf::from("public")), + name: name.clone(), + default_platform: default_platform(), + out_dir: out_dir_default(), + asset_dir: asset_dir_default(), - tools: None, + tools: Default::default(), sub_package: None, }, web: WebConfig { app: WebAppConfig { - title: Some("dioxus | ⛺".into()), + title: default_title(), base_path: None, }, - proxy: Some(vec![]), + proxy: vec![], watcher: WebWatcherConfig { - watch_path: Some(vec![PathBuf::from("src")]), - reload_html: Some(false), - index_on_404: Some(true), + watch_path: watch_path_default(), + reload_html: false, + index_on_404: true, }, resource: WebResourceConfig { dev: WebDevResourceConfig { - style: Some(vec![]), - script: Some(vec![]), + style: vec![], + script: vec![], }, style: Some(vec![]), script: Some(vec![]), @@ -136,20 +136,44 @@ impl Default for DioxusConfig { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ApplicationConfig { + #[serde(default = "default_name")] pub name: String, + #[serde(default = "default_platform")] pub default_platform: Platform, - pub out_dir: Option, - pub asset_dir: Option, + #[serde(default = "out_dir_default")] + pub out_dir: PathBuf, + #[serde(default = "asset_dir_default")] + pub asset_dir: PathBuf, - pub tools: Option>, + #[serde(default)] + pub tools: HashMap, + #[serde(default)] pub sub_package: Option, } +fn default_name() -> String { + "name".into() +} + +fn default_platform() -> Platform { + Platform::Web +} + +fn asset_dir_default() -> PathBuf { + PathBuf::from("public") +} + +fn out_dir_default() -> PathBuf { + PathBuf::from("dist") +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct WebConfig { + #[serde(default)] pub app: WebAppConfig, - pub proxy: Option>, + #[serde(default)] + pub proxy: Vec, pub watcher: WebWatcherConfig, pub resource: WebResourceConfig, #[serde(default)] @@ -158,10 +182,24 @@ pub struct WebConfig { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct WebAppConfig { - pub title: Option, + #[serde(default = "default_title")] + pub title: String, pub base_path: Option, } +impl Default for WebAppConfig { + fn default() -> Self { + Self { + title: default_title(), + base_path: None, + } + } +} + +fn default_title() -> String { + "dioxus | ⛺".into() +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct WebProxyConfig { pub backend: String, @@ -169,9 +207,16 @@ pub struct WebProxyConfig { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct WebWatcherConfig { - pub watch_path: Option>, - pub reload_html: Option, - pub index_on_404: Option, + #[serde(default = "watch_path_default")] + pub watch_path: Vec, + #[serde(default)] + pub reload_html: bool, + #[serde(default = "true_bool")] + pub index_on_404: bool, +} + +fn watch_path_default() -> Vec { + vec![PathBuf::from("src")] } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -183,8 +228,10 @@ pub struct WebResourceConfig { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct WebDevResourceConfig { - pub style: Option>, - pub script: Option>, + #[serde(default)] + pub style: Vec, + #[serde(default)] + pub script: Vec, } #[derive(Debug, Default, Clone, Serialize, Deserialize)] @@ -238,17 +285,11 @@ impl CrateConfig { let workspace_dir = meta.workspace_root; let target_dir = meta.target_directory; - let out_dir = match dioxus_config.application.out_dir { - Some(ref v) => crate_dir.join(v), - None => crate_dir.join("dist"), - }; + let out_dir = crate_dir.join(&dioxus_config.application.out_dir); let cargo_def = &crate_dir.join("Cargo.toml"); - let asset_dir = match dioxus_config.application.asset_dir { - Some(ref v) => crate_dir.join(v), - None => crate_dir.join("public"), - }; + let asset_dir = crate_dir.join(&dioxus_config.application.asset_dir); let manifest = cargo_toml::Manifest::from_path(cargo_def).unwrap(); @@ -577,3 +618,7 @@ impl Default for WebviewInstallMode { Self::OfflineInstaller { silent: false } } } + +fn true_bool() -> bool { + true +} diff --git a/packages/cli/src/server/mod.rs b/packages/cli/src/server/mod.rs index 19dc44b75..b0f3bc56d 100644 --- a/packages/cli/src/server/mod.rs +++ b/packages/cli/src/server/mod.rs @@ -5,10 +5,7 @@ use dioxus_core::Template; use dioxus_html::HtmlCtx; use dioxus_rsx::hot_reload::*; use notify::{RecommendedWatcher, Watcher}; -use std::{ - path::PathBuf, - sync::{Arc, Mutex}, -}; +use std::sync::{Arc, Mutex}; use tokio::sync::broadcast::Sender; mod output; @@ -25,13 +22,7 @@ async fn setup_file_watcher Result + Send + 'static>( let mut last_update_time = chrono::Local::now().timestamp(); // file watcher: check file change - let allow_watch_path = config - .dioxus_config - .web - .watcher - .watch_path - .clone() - .unwrap_or_else(|| vec![PathBuf::from("src")]); + let allow_watch_path = config.dioxus_config.web.watcher.watch_path.clone(); let watcher_config = config.clone(); let mut watcher = notify::recommended_watcher(move |info: notify::Result| { @@ -87,13 +78,7 @@ async fn setup_file_watcher_hot_reload Result + Send + ' web_info: Option, ) -> Result { // file watcher: check file change - let allow_watch_path = config - .dioxus_config - .web - .watcher - .watch_path - .clone() - .unwrap_or_else(|| vec![PathBuf::from("src")]); + let allow_watch_path = config.dioxus_config.web.watcher.watch_path.clone(); let watcher_config = config.clone(); let mut last_update_time = chrono::Local::now().timestamp(); diff --git a/packages/cli/src/server/output.rs b/packages/cli/src/server/output.rs index ce0f55064..68582f742 100644 --- a/packages/cli/src/server/output.rs +++ b/packages/cli/src/server/output.rs @@ -46,19 +46,13 @@ pub fn print_console_info( } else { "Default" }; - let url_rewrite = if config - .dioxus_config - .web - .watcher - .index_on_404 - .unwrap_or(false) - { + let url_rewrite = if config.dioxus_config.web.watcher.index_on_404 { "True" } else { "False" }; - let proxies = config.dioxus_config.web.proxy.as_ref(); + let proxies = &config.dioxus_config.web.proxy; if options.changed.is_empty() { println!( @@ -106,12 +100,10 @@ pub fn print_console_info( println!(); println!("\t> Profile : {}", profile.green()); println!("\t> Hot Reload : {}", hot_reload.cyan()); - if let Some(proxies) = proxies { - if !proxies.is_empty() { - println!("\t> Proxies :"); - for proxy in proxies { - println!("\t\t- {}", proxy.backend.blue()); - } + if !proxies.is_empty() { + println!("\t> Proxies :"); + for proxy in proxies { + println!("\t\t- {}", proxy.backend.blue()); } } println!("\t> Index Template : {}", custom_html_file.green()); diff --git a/packages/cli/src/server/web/mod.rs b/packages/cli/src/server/web/mod.rs index e08d2297a..768236f5d 100644 --- a/packages/cli/src/server/web/mod.rs +++ b/packages/cli/src/server/web/mod.rs @@ -322,12 +322,7 @@ async fn setup_router( .override_response_header(HeaderName::from_static("cross-origin-opener-policy"), coop) .and_then( move |response: Response| async move { - let response = if file_service_config - .dioxus_config - .web - .watcher - .index_on_404 - .unwrap_or(false) + let response = if file_service_config.dioxus_config.web.watcher.index_on_404 && response.status() == StatusCode::NOT_FOUND { let body = Full::from( @@ -359,7 +354,7 @@ async fn setup_router( let mut router = Router::new().route("/_dioxus/ws", get(ws_handler)); // Setup proxy - for proxy_config in config.dioxus_config.web.proxy.unwrap_or_default() { + for proxy_config in config.dioxus_config.web.proxy { router = proxy::add_proxy(router, &proxy_config)?; } @@ -476,13 +471,7 @@ fn build(config: &CrateConfig, reload_tx: &Sender<()>) -> Result { let result = builder::build(config, true)?; // change the websocket reload state to true; // the page will auto-reload. - if config - .dioxus_config - .web - .watcher - .reload_html - .unwrap_or(false) - { + if config.dioxus_config.web.watcher.reload_html { let _ = Serve::regen_dev_page(config); } let _ = reload_tx.send(()); From 8d1c17ba7dcb2da08379cbcfcbfdf2d20b906849 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Wed, 9 Aug 2023 11:10:41 -0700 Subject: [PATCH 2/4] fix clippy --- packages/cli/src/config.rs | 2 +- packages/core/tests/suspense.rs | 2 +- packages/desktop/src/protocol.rs | 9 +++++++-- packages/html/src/eval.rs | 2 +- packages/native-core/src/real_dom.rs | 1 - packages/rsx/src/lib.rs | 10 ++-------- 6 files changed, 12 insertions(+), 14 deletions(-) diff --git a/packages/cli/src/config.rs b/packages/cli/src/config.rs index 2fc9f96ce..267a7f2a2 100644 --- a/packages/cli/src/config.rs +++ b/packages/cli/src/config.rs @@ -126,7 +126,7 @@ impl Default for DioxusConfig { }, bundle: BundleConfig { identifier: Some(format!("io.github.{name}")), - publisher: Some(name.into()), + publisher: Some(name), ..Default::default() }, plugin: toml::Value::Table(toml::map::Map::new()), diff --git a/packages/core/tests/suspense.rs b/packages/core/tests/suspense.rs index 7b2516447..abce7f8a6 100644 --- a/packages/core/tests/suspense.rs +++ b/packages/core/tests/suspense.rs @@ -36,7 +36,7 @@ fn suspended_child(cx: Scope) -> Element { cx.spawn(async move { val += 1; }); - return cx.suspend()?; + cx.suspend()?; } render!("child") diff --git a/packages/desktop/src/protocol.rs b/packages/desktop/src/protocol.rs index 89ce50a48..53652f82f 100644 --- a/packages/desktop/src/protocol.rs +++ b/packages/desktop/src/protocol.rs @@ -158,8 +158,13 @@ fn get_mime_from_path(trimmed: &Path) -> Result<&'static str> { } let res = match infer::get_from_path(trimmed)?.map(|f| f.mime_type()) { - Some(t) if t == "text/plain" => get_mime_by_ext(trimmed), - Some(f) => f, + Some(f) => { + if f == "text/plain" { + get_mime_by_ext(trimmed) + } else { + f + } + } None => get_mime_by_ext(trimmed), }; diff --git a/packages/html/src/eval.rs b/packages/html/src/eval.rs index b39eef805..c90f5125b 100644 --- a/packages/html/src/eval.rs +++ b/packages/html/src/eval.rs @@ -42,7 +42,7 @@ pub fn use_eval(cx: &ScopeState) -> &EvalCreator { Rc::new(move |script: &str| { eval_provider .new_evaluator(script.to_string()) - .map(|evaluator| UseEval::new(evaluator)) + .map(UseEval::new) }) as Rc Result> }) } diff --git a/packages/native-core/src/real_dom.rs b/packages/native-core/src/real_dom.rs index 44c378ddf..b520b2de1 100644 --- a/packages/native-core/src/real_dom.rs +++ b/packages/native-core/src/real_dom.rs @@ -446,7 +446,6 @@ impl RealDom { drop(tree); children.reverse(); if let Some(node) = self.get_mut(id) { - let node = node; f(node); stack.extend(children.iter()); } diff --git a/packages/rsx/src/lib.rs b/packages/rsx/src/lib.rs index 1f01fcd72..783dd3322 100644 --- a/packages/rsx/src/lib.rs +++ b/packages/rsx/src/lib.rs @@ -288,10 +288,7 @@ impl DynamicMapping { let idx = self.last_attribute_idx; self.last_attribute_idx += 1; - self.attribute_to_idx - .entry(attr) - .or_insert_with(Vec::new) - .push(idx); + self.attribute_to_idx.entry(attr).or_default().push(idx); idx } @@ -300,10 +297,7 @@ impl DynamicMapping { let idx = self.last_element_idx; self.last_element_idx += 1; - self.node_to_idx - .entry(node) - .or_insert_with(Vec::new) - .push(idx); + self.node_to_idx.entry(node).or_default().push(idx); idx } From 4691046e238e850644193ba2ea4232a7a60f9ac9 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Sun, 17 Sep 2023 19:01:20 -0500 Subject: [PATCH 3/4] fix formatting --- packages/fullstack/src/adapters/warp_adapter.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/fullstack/src/adapters/warp_adapter.rs b/packages/fullstack/src/adapters/warp_adapter.rs index 5afcb5077..316d70e56 100644 --- a/packages/fullstack/src/adapters/warp_adapter.rs +++ b/packages/fullstack/src/adapters/warp_adapter.rs @@ -143,7 +143,7 @@ pub fn register_server_fns(server_fn_route: &'static str) -> BoxedFilter<(impl R let req = warp::hyper::Request::from_parts(parts, bytes.into()); service.run(req).await.map_err(|err| { tracing::error!("Server function error: {}", err); - + struct WarpServerFnError(String); impl std::fmt::Debug for WarpServerFnError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -152,7 +152,7 @@ pub fn register_server_fns(server_fn_route: &'static str) -> BoxedFilter<(impl R } impl warp::reject::Reject for WarpServerFnError {} - + warp::reject::custom(WarpServerFnError(err.to_string())) }) } From d4b0451d096c67616be4b27de7a9e482ac5d8fd6 Mon Sep 17 00:00:00 2001 From: tigerros Date: Thu, 9 Nov 2023 19:31:59 +0100 Subject: [PATCH 4/4] Allow warning in necessary functions (#1626) --- packages/core-macro/src/props/mod.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/core-macro/src/props/mod.rs b/packages/core-macro/src/props/mod.rs index aba8137e7..6e6c43d66 100644 --- a/packages/core-macro/src/props/mod.rs +++ b/packages/core-macro/src/props/mod.rs @@ -631,7 +631,7 @@ Finally, call `.build()` to create the instance of `{name}`. Ok(quote! { impl #impl_generics #name #ty_generics #where_clause { #[doc = #builder_method_doc] - #[allow(dead_code)] + #[allow(dead_code, clippy::type_complexity)] #vis fn builder() -> #builder_name #generics_with_empty { #builder_name { fields: #empties_tuple, @@ -823,6 +823,7 @@ Finally, call `.build()` to create the instance of `{name}`. #[allow(dead_code, non_camel_case_types, missing_docs)] impl #impl_generics #builder_name < #( #ty_generics ),* > #where_clause { #doc + #[allow(clippy::type_complexity)] pub fn #field_name (self, #field_name: #arg_type) -> #builder_name < #( #target_generics ),* > { let #field_name = (#arg_expr,); let ( #(#descructuring,)* ) = self.fields; @@ -841,6 +842,7 @@ Finally, call `.build()` to create the instance of `{name}`. #[deprecated( note = #repeated_fields_error_message )] + #[allow(clippy::type_complexity)] pub fn #field_name (self, _: #repeated_fields_error_type_name) -> #builder_name < #( #target_generics ),* > { self }