diff --git a/packages/cli/src/builder.rs b/packages/cli/src/builder.rs index c38514513..4dd1de970 100644 --- a/packages/cli/src/builder.rs +++ b/packages/cli/src/builder.rs @@ -134,7 +134,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; @@ -351,13 +351,7 @@ pub fn build_desktop(config: &CrateConfig, _is_serve: bool) -> Result String { let mut script_list = resources.script.unwrap_or_default(); if serve { - let mut dev_style = resources.dev.style.clone().unwrap_or_default(); - let mut dev_script = resources.dev.script.unwrap_or_default(); + let mut dev_style = resouces.dev.style.clone(); + let mut dev_script = resouces.dev.script.clone(); style_list.append(&mut dev_style); script_list.append(&mut dev_script); } @@ -463,13 +457,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"); } @@ -520,12 +508,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 7ecc286ba..675880111 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 af22a17ca..6d5c063f7 100644 --- a/packages/cli/src/cli/serve.rs +++ b/packages/cli/src/cli/serve.rs @@ -60,14 +60,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 842d3bad6..14e345c7a 100644 --- a/packages/cli/src/config.rs +++ b/packages/cli/src/config.rs @@ -87,33 +87,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"), PathBuf::from("examples")]), - 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![]), @@ -127,7 +127,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()), @@ -137,20 +137,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)] @@ -159,10 +183,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, @@ -170,9 +208,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"), PathBuf::from("examples")] } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -184,8 +229,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)] @@ -239,17 +286,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(); @@ -581,3 +622,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 79a5531b6..7b60c8178 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::{self}; mod output; @@ -26,13 +23,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"), PathBuf::from("examples")]); + 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| { diff --git a/packages/cli/src/server/output.rs b/packages/cli/src/server/output.rs index a221e2f9b..151e29099 100644 --- a/packages/cli/src/server/output.rs +++ b/packages/cli/src/server/output.rs @@ -47,19 +47,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!( @@ -107,12 +101,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 0e1413597..f192f0f52 100644 --- a/packages/cli/src/server/web/mod.rs +++ b/packages/cli/src/server/web/mod.rs @@ -262,12 +262,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( @@ -299,7 +294,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)?; } @@ -416,13 +411,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(()); 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 }