diff --git a/crates/cli/src/build/cache.rs b/crates/cli/src/build/cache.rs index ec20f382..83264454 100644 --- a/crates/cli/src/build/cache.rs +++ b/crates/cli/src/build/cache.rs @@ -15,7 +15,7 @@ pub(crate) struct Cache { impl Cache { /// Create a new Cache instance. - pub(crate) fn new(cache_dir: &Option) -> Result { + pub(crate) fn new(cache_dir: Option<&PathBuf>) -> Result { // Try to use user's cache directory if no cache_dir has been provided let cache_dir = match cache_dir { Some(cache_dir) => Some(cache_dir.clone()), diff --git a/crates/cli/src/build/crunchbase.rs b/crates/cli/src/build/crunchbase.rs index a508c6dc..5978f954 100644 --- a/crates/cli/src/build/crunchbase.rs +++ b/crates/cli/src/build/crunchbase.rs @@ -177,9 +177,9 @@ async fn collect_organization_data(cb: DynCB, cb_url: &str) -> Result Result>, location_type: &str) -> Option { +fn get_location_value(headquarters_address: Option<&Vec>, location_type: &str) -> Option { headquarters_address .as_ref() .and_then(|addresses| addresses.iter().next()) diff --git a/crates/cli/src/build/mod.rs b/crates/cli/src/build/mod.rs index d61c687a..2c6e322a 100644 --- a/crates/cli/src/build/mod.rs +++ b/crates/cli/src/build/mod.rs @@ -148,7 +148,7 @@ pub async fn build(args: &BuildArgs) -> Result<()> { setup_output_dir(&args.output_dir)?; // Setup cache - let cache = Cache::new(&args.cache_dir)?; + let cache = Cache::new(args.cache_dir.as_ref())?; // Get landscape data from the source provided let mut landscape_data = LandscapeData::new(&args.data_source).await?; @@ -219,8 +219,13 @@ pub async fn build(args: &BuildArgs) -> Result<()> { )?; // Render index and embed-item html files and write them to the output dir - render_index_html(&settings.analytics, &datasets, &settings.osano, &args.output_dir)?; - render_embed_item_html(&settings.colors, &args.output_dir)?; + render_index_html( + settings.analytics.as_ref(), + &datasets, + settings.osano.as_ref(), + &args.output_dir, + )?; + render_embed_item_html(settings.colors.as_ref(), &args.output_dir)?; // Copy embed and web application assets files to the output directory copy_embed_assets(&args.output_dir)?; @@ -333,7 +338,7 @@ async fn collect_clomonitor_reports( #[instrument(skip_all, err)] async fn copy_data_sources_files(args: &BuildArgs, output_dir: &Path) -> Result<()> { // Helper function to copy the data source file provided - async fn copy(src_file: &Option, src_url: &Option, dst_file: PathBuf) -> Result<()> { + async fn copy(src_file: Option<&PathBuf>, src_url: Option<&String>, dst_file: PathBuf) -> Result<()> { if let Some(src_file) = src_file { fs::copy(src_file, dst_file)?; } else if let Some(src_url) = src_url { @@ -348,8 +353,8 @@ async fn copy_data_sources_files(args: &BuildArgs, output_dir: &Path) -> Result< // Landscape data let landscape_data_file = output_dir.join(SOURCES_PATH).join("data.yml"); copy( - &args.data_source.data_file, - &args.data_source.data_url, + args.data_source.data_file.as_ref(), + args.data_source.data_url.as_ref(), landscape_data_file, ) .await?; @@ -357,8 +362,8 @@ async fn copy_data_sources_files(args: &BuildArgs, output_dir: &Path) -> Result< // Settings let settings_file = output_dir.join(SOURCES_PATH).join("settings.yml"); copy( - &args.settings_source.settings_file, - &args.settings_source.settings_url, + args.settings_source.settings_file.as_ref(), + args.settings_source.settings_url.as_ref(), settings_file, ) .await?; @@ -366,8 +371,8 @@ async fn copy_data_sources_files(args: &BuildArgs, output_dir: &Path) -> Result< // Guide let guide_file = output_dir.join(SOURCES_PATH).join("guide.yml"); copy( - &args.guide_source.guide_file, - &args.guide_source.guide_url, + args.guide_source.guide_file.as_ref(), + args.guide_source.guide_url.as_ref(), guide_file, ) .await?; @@ -375,8 +380,8 @@ async fn copy_data_sources_files(args: &BuildArgs, output_dir: &Path) -> Result< // Games data let games_file = output_dir.join(SOURCES_PATH).join("games.yml"); copy( - &args.games_source.games_file, - &args.games_source.games_url, + args.games_source.games_file.as_ref(), + args.games_source.games_url.as_ref(), games_file, ) .await?; @@ -773,7 +778,7 @@ async fn prepare_screenshot(width: u32, output_dir: &Path) -> Result<()> { #[instrument(skip_all, err)] async fn prepare_settings_images(settings: &mut LandscapeSettings, output_dir: &Path) -> Result<()> { // Helper function to process the image provided - async fn process_image(url: &Option, output_dir: &Path) -> Result> { + async fn process_image(url: Option<&String>, output_dir: &Path) -> Result> { let Some(url) = url else { return Ok(None); }; @@ -800,17 +805,17 @@ async fn prepare_settings_images(settings: &mut LandscapeSettings, output_dir: & // Header if let Some(header) = &mut settings.header { - header.logo = process_image(&header.logo, output_dir).await?; + header.logo = process_image(header.logo.as_ref(), output_dir).await?; }; // Footer if let Some(footer) = &mut settings.footer { - footer.logo = process_image(&footer.logo, output_dir).await?; + footer.logo = process_image(footer.logo.as_ref(), output_dir).await?; }; // Other images if let Some(images) = &mut settings.images { - images.favicon = process_image(&images.favicon, output_dir).await?; + images.favicon = process_image(images.favicon.as_ref(), output_dir).await?; }; Ok(()) @@ -864,17 +869,17 @@ fn prepare_view_full_dataset(full: &Full, view: &EmbedView) -> Full { #[derive(Debug, Clone, Template)] #[template(path = "index.html", escape = "none")] struct IndexHtml<'a> { - analytics: &'a Option, + analytics: Option<&'a Analytics>, datasets: &'a Datasets, - osano: &'a Option, + osano: Option<&'a Osano>, } /// Render index html file and write it to the output directory. #[instrument(skip_all, err)] fn render_index_html( - analytics: &Option, + analytics: Option<&Analytics>, datasets: &Datasets, - osano: &Option, + osano: Option<&Osano>, output_dir: &Path, ) -> Result<()> { debug!("rendering index.html file"); @@ -894,12 +899,12 @@ fn render_index_html( #[derive(Debug, Clone, Template)] #[template(path = "embed-item.html", escape = "none")] struct EmbedItemHtml<'a> { - colors: &'a Option, + colors: Option<&'a Colors>, } /// Render embed item html file and write it to the output directory. #[instrument(skip_all, err)] -fn render_embed_item_html(colors: &Option, output_dir: &Path) -> Result<()> { +fn render_embed_item_html(colors: Option<&Colors>, output_dir: &Path) -> Result<()> { debug!("rendering embed-item.html file"); let path = output_dir.join(EMBED_PATH).join("embed-item.html"); @@ -950,8 +955,12 @@ mod filters { /// Filter to get the Google Tag Manager container ID from the analytics /// instance provided. - #[allow(clippy::unnecessary_wraps)] - pub(crate) fn get_gtm_container_id(analytics: &Option) -> askama::Result> { + #[allow( + clippy::unnecessary_wraps, + clippy::trivially_copy_pass_by_ref, + clippy::ref_option_ref + )] + pub(crate) fn get_gtm_container_id(analytics: &Option<&Analytics>) -> askama::Result> { Ok(analytics.as_ref().and_then(|a| a.gtm.as_ref()).and_then(|gtm| gtm.container_id.clone())) } diff --git a/crates/core/src/data/legacy.rs b/crates/core/src/data/legacy.rs index f2235b93..e5c43f2d 100644 --- a/crates/core/src/data/legacy.rs +++ b/crates/core/src/data/legacy.rs @@ -209,14 +209,14 @@ fn validate_urls(item: &Item) -> Result<()> { ("twitter", &item.twitter), ]; for (name, url) in urls { - validate_url(name, url)?; + validate_url(name, url.as_ref())?; } // Check additional repositories if let Some(additional_repos) = &item.additional_repos { for r in additional_repos { let repo_url = Some(r.repo_url.clone()); - validate_url("additional_repository", &repo_url)?; + validate_url("additional_repository", repo_url.as_ref())?; } } @@ -243,14 +243,14 @@ fn validate_urls(item: &Item) -> Result<()> { ("youtube", &extra.youtube_url), ]; for (name, url) in urls { - validate_url(name, url)?; + validate_url(name, url.as_ref())?; } // Check audits urls if let Some(audits) = &extra.audits { for a in audits { let audit_url = Some(a.url.clone()); - validate_url("audit", &audit_url)?; + validate_url("audit", audit_url.as_ref())?; } } @@ -258,7 +258,7 @@ fn validate_urls(item: &Item) -> Result<()> { if let Some(other_links) = &extra.other_links { for link in other_links { let link_url = Some(link.url.clone()); - validate_url("other_link", &link_url)?; + validate_url("other_link", link_url.as_ref())?; } } }; diff --git a/crates/core/src/settings.rs b/crates/core/src/settings.rs index 1795714f..471dcbd7 100644 --- a/crates/core/src/settings.rs +++ b/crates/core/src/settings.rs @@ -200,7 +200,7 @@ impl LandscapeSettings { } // Check url is valid - validate_url("landscape", &Some(self.url.clone()))?; + validate_url("landscape", Some(self.url.clone()).as_ref())?; self.validate_base_path()?; self.validate_categories()?; @@ -347,12 +347,12 @@ impl LandscapeSettings { ("youtube", &links.youtube), ]; for (name, url) in urls { - validate_url(name, url)?; + validate_url(name, url.as_ref())?; } } // Logo - validate_url("footer logo", &footer.logo)?; + validate_url("footer logo", footer.logo.as_ref())?; // Text if let Some(text) = &footer.text { @@ -399,12 +399,12 @@ impl LandscapeSettings { if let Some(links) = &header.links { let urls = [("github", &links.github)]; for (name, url) in urls { - validate_url(name, url)?; + validate_url(name, url.as_ref())?; } } // Logo - validate_url("header logo", &header.logo)?; + validate_url("header logo", header.logo.as_ref())?; Ok(()) } @@ -415,7 +415,7 @@ impl LandscapeSettings { let urls = [("favicon", &images.favicon), ("open_graph", &images.open_graph)]; for (name, url) in urls { - validate_url(name, url)?; + validate_url(name, url.as_ref())?; } Ok(()) diff --git a/crates/core/src/util.rs b/crates/core/src/util.rs index 9904507d..dccc008d 100644 --- a/crates/core/src/util.rs +++ b/crates/core/src/util.rs @@ -40,7 +40,7 @@ pub(crate) fn normalize_name(value: &str) -> String { } /// Validate the url provided. -pub(crate) fn validate_url(kind: &str, url: &Option) -> Result<()> { +pub(crate) fn validate_url(kind: &str, url: Option<&String>) -> Result<()> { if let Some(url) = url { let invalid_url = |reason: &str| bail!("invalid {kind} url: {reason}"); @@ -105,7 +105,7 @@ mod tests { fn validate_url_succeeds() { validate_url( "crunchbase", - &Some("https://www.crunchbase.com/organization/test".to_string()), + Some("https://www.crunchbase.com/organization/test".to_string()).as_ref(), ) .unwrap(); @@ -121,26 +121,30 @@ mod tests { ("twitter", "x.com"), ("youtube", "youtube.com"), ] { - validate_url(kind, &Some(format!("https://{domain}/test"))).unwrap(); + validate_url(kind, Some(format!("https://{domain}/test")).as_ref()).unwrap(); } } #[test] #[should_panic(expected = "relative URL without a base")] fn validate_url_error_parsing() { - validate_url("", &Some("invalid url".to_string())).unwrap(); + validate_url("", Some("invalid url".to_string()).as_ref()).unwrap(); } #[test] #[should_panic(expected = "invalid scheme")] fn validate_url_invalid_scheme() { - validate_url("", &Some("oci://hostname/path".to_string())).unwrap(); + validate_url("", Some("oci://hostname/path".to_string()).as_ref()).unwrap(); } #[test] #[should_panic(expected = "invalid crunchbase url")] fn validate_url_invalid_crunchbase_url() { - validate_url("crunchbase", &Some("https://www.crunchbase.com/test".to_string())).unwrap(); + validate_url( + "crunchbase", + Some("https://www.crunchbase.com/test".to_string()).as_ref(), + ) + .unwrap(); } #[test] @@ -157,7 +161,7 @@ mod tests { "twitter", "youtube", ] { - let error = validate_url(kind, &Some(url.clone())).unwrap_err().to_string(); + let error = validate_url(kind, Some(url.clone()).as_ref()).unwrap_err().to_string(); let expected_error = format!("invalid {kind} url"); assert!(error.starts_with(expected_error.as_str())); }