Skip to content

Commit

Permalink
Make clippy happy
Browse files Browse the repository at this point in the history
Signed-off-by: Sergio Castaño Arteaga <[email protected]>
  • Loading branch information
tegioz committed Dec 5, 2024
1 parent 896bbf6 commit 27eb0e1
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 47 deletions.
2 changes: 1 addition & 1 deletion crates/cli/src/build/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub(crate) struct Cache {

impl Cache {
/// Create a new Cache instance.
pub(crate) fn new(cache_dir: &Option<PathBuf>) -> Result<Self> {
pub(crate) fn new(cache_dir: Option<&PathBuf>) -> Result<Self> {
// 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()),
Expand Down
8 changes: 4 additions & 4 deletions crates/cli/src/build/crunchbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ async fn collect_organization_data(cb: DynCB, cb_url: &str) -> Result<Organizati
Ok(Organization {
generated_at: Utc::now(),
acquisitions,
city: get_location_value(&cb_org.cards.headquarters_address, "city"),
city: get_location_value(cb_org.cards.headquarters_address.as_ref(), "city"),
company_type: cb_org.properties.company_type,
country: get_location_value(&cb_org.cards.headquarters_address, "country"),
country: get_location_value(cb_org.cards.headquarters_address.as_ref(), "country"),
description: cb_org.properties.short_description,
funding: cb_org.properties.funding_total.as_ref().and_then(|f| f.value_usd),
funding_rounds,
Expand All @@ -190,7 +190,7 @@ async fn collect_organization_data(cb: DynCB, cb_url: &str) -> Result<Organizati
name: cb_org.properties.name,
num_employees_max,
num_employees_min,
region: get_location_value(&cb_org.cards.headquarters_address, "region"),
region: get_location_value(cb_org.cards.headquarters_address.as_ref(), "region"),
stock_exchange: cb_org.properties.stock_exchange_symbol,
ticker: cb_org.properties.stock_symbol.and_then(|v| v.value),
twitter_url: cb_org.properties.twitter.and_then(|v| v.value),
Expand Down Expand Up @@ -357,7 +357,7 @@ struct CBMoneyRaised {
}

/// Return the location value for the location type provided if available.
fn get_location_value(headquarters_address: &Option<Vec<CBAddress>>, location_type: &str) -> Option<String> {
fn get_location_value(headquarters_address: Option<&Vec<CBAddress>>, location_type: &str) -> Option<String> {
headquarters_address
.as_ref()
.and_then(|addresses| addresses.iter().next())
Expand Down
57 changes: 33 additions & 24 deletions crates/cli/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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<PathBuf>, src_url: &Option<String>, 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 {
Expand All @@ -348,35 +353,35 @@ 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?;

// 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?;

// 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?;

// 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?;
Expand Down Expand Up @@ -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<String>, output_dir: &Path) -> Result<Option<String>> {
async fn process_image(url: Option<&String>, output_dir: &Path) -> Result<Option<String>> {
let Some(url) = url else {
return Ok(None);
};
Expand All @@ -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(())
Expand Down Expand Up @@ -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>,
analytics: Option<&'a Analytics>,
datasets: &'a Datasets,
osano: &'a Option<Osano>,
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>,
analytics: Option<&Analytics>,
datasets: &Datasets,
osano: &Option<Osano>,
osano: Option<&Osano>,
output_dir: &Path,
) -> Result<()> {
debug!("rendering index.html file");
Expand All @@ -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>,
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<Colors>, 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");
Expand Down Expand Up @@ -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<Analytics>) -> askama::Result<Option<String>> {
#[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<Option<String>> {
Ok(analytics.as_ref().and_then(|a| a.gtm.as_ref()).and_then(|gtm| gtm.container_id.clone()))
}

Expand Down
10 changes: 5 additions & 5 deletions crates/core/src/data/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())?;
}
}

Expand All @@ -243,22 +243,22 @@ 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())?;
}
}

// Other links
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())?;
}
}
};
Expand Down
12 changes: 6 additions & 6 deletions crates/core/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(())
}
Expand All @@ -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(())
Expand Down
18 changes: 11 additions & 7 deletions crates/core/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) -> 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}");

Expand Down Expand Up @@ -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();

Expand All @@ -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]
Expand All @@ -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()));
}
Expand Down

0 comments on commit 27eb0e1

Please sign in to comment.