Skip to content

Commit

Permalink
feat: set documentation_url to None when url starts with https://docs.rs
Browse files Browse the repository at this point in the history


Documentation url is mainly used for the external documentation
websites. When the url starts with https://docs.rs we don't need to
include it on the page.
  • Loading branch information
zahidkizmaz committed Oct 28, 2024
1 parent 8a866f3 commit d69c136
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/test/fakes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ impl<'a> FakeRelease<'a> {
self
}

pub(crate) fn documentation_url(mut self, documentation_url: Option<String>) -> Self {
self.package.documentation = documentation_url;
self
}

pub(crate) fn create(self) -> Result<i32> {
let runtime = self.runtime.clone();
runtime.block_on(self.create_async())
Expand Down
53 changes: 52 additions & 1 deletion src/web/crate_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,14 @@ impl CrateDetails {
.transpose()?,
};

// When documentation_url points to docs.rs itself, then we don't need to
// show it on the page because user is already on docs.rs website
let documentation_url = match krate.documentation_url {
Some(url) if url.starts_with("https://docs.rs/") => None,
Some(url) => Some(url),
None => None,
};

let mut crate_details = CrateDetails {
name: krate.name,
version: version.clone(),
Expand All @@ -241,9 +249,9 @@ impl CrateDetails {
releases: prefetched_releases,
repository_metadata,
metadata,
documentation_url,
is_library: krate.is_library,
license: krate.license,
documentation_url: krate.documentation_url,
documented_items: krate.documented_items,
total_items: krate.total_items,
total_items_needing_examples: krate.total_items_needing_examples,
Expand Down Expand Up @@ -892,6 +900,49 @@ mod tests {
Ok(())
}

#[test]
fn test_crate_details_documentation_url_is_none_when_url_is_docs_rs() {
async_wrapper(|env| async move {
let db = env.async_db().await;
let mut conn = db.async_conn().await;

env.async_fake_release()
.await
.name("foo")
.version("0.1.0")
.documentation_url(Some("https://foo.com".into()))
.create_async()
.await?;
env.async_fake_release()
.await
.name("foo")
.version("0.2.0")
.documentation_url(Some("https://docs.rs/foo/".into()))
.create_async()
.await?;
env.async_fake_release()
.await
.name("foo")
.version("0.3.0")
.documentation_url(None)
.create_async()
.await?;

let details_0_1 = crate_details(&mut conn, "foo", "0.1.0", None).await;
let details_0_2 = crate_details(&mut conn, "foo", "0.2.0", None).await;
let details_0_3 = crate_details(&mut conn, "foo", "0.3.0", None).await;

assert_eq!(
details_0_1.documentation_url,
Some("https://foo.com".into())
);
assert_eq!(details_0_2.documentation_url, None);
assert_eq!(details_0_3.documentation_url, None);

Ok(())
});
}

#[test]
fn test_last_successful_build_when_last_releases_failed_or_yanked() {
async_wrapper(|env| async move {
Expand Down

0 comments on commit d69c136

Please sign in to comment.